@@ -66,6 +66,8 @@ public function setUp() {
6666 public function test_ajax_response_get_query_data_valid_query () {
6767 $ this ->_setRole ( 'administrator ' );
6868
69+ $ this ->enable_pro ();
70+
6971 $ _GET ['security ' ] = wp_create_nonce ( Visualizer_Plugin::ACTION_FETCH_DB_DATA . Visualizer_Plugin::VERSION );
7072
7173 global $ wpdb ;
@@ -93,6 +95,8 @@ public function test_ajax_response_get_query_data_valid_query() {
9395 public function test_ajax_response_get_query_data_invalid_query () {
9496 $ this ->_setRole ( 'administrator ' );
9597
98+ $ this ->enable_pro ();
99+
96100 $ _GET ['security ' ] = wp_create_nonce ( Visualizer_Plugin::ACTION_FETCH_DB_DATA . Visualizer_Plugin::VERSION );
97101
98102 $ _POST ['params ' ] = array (
@@ -120,6 +124,8 @@ public function test_ajax_response_get_query_data_invalid_query() {
120124 public function test_ajax_response_get_query_data_valid_query_with_filtered_columns () {
121125 $ this ->_setRole ( 'administrator ' );
122126
127+ $ this ->enable_pro ();
128+
123129 $ _GET ['security ' ] = wp_create_nonce ( Visualizer_Plugin::ACTION_FETCH_DB_DATA . Visualizer_Plugin::VERSION );
124130
125131 $ _POST ['params ' ] = array (
@@ -203,6 +209,8 @@ public function test_ajax_response_get_query_data_subcriber_dissallow() {
203209 public function test_ajax_response_get_query_data_invalid_query_subquery () {
204210 $ this ->_setRole ( 'administrator ' );
205211
212+ $ this ->enable_pro ();
213+
206214 $ _GET ['security ' ] = wp_create_nonce ( Visualizer_Plugin::ACTION_FETCH_DB_DATA . Visualizer_Plugin::VERSION );
207215
208216 $ _POST ['params ' ] = array (
@@ -230,6 +238,8 @@ public function test_ajax_response_get_query_data_invalid_query_subquery() {
230238 public function test_ajax_response_get_query_data_invalid_query_comment () {
231239 $ this ->_setRole ( 'administrator ' );
232240
241+ $ this ->enable_pro ();
242+
233243 $ _GET ['security ' ] = wp_create_nonce ( Visualizer_Plugin::ACTION_FETCH_DB_DATA . Visualizer_Plugin::VERSION );
234244
235245 $ _POST ['params ' ] = array (
@@ -264,4 +274,66 @@ public function test_sql_comment_strip() {
264274 $ source = new Visualizer_Source_Query ( "/* SELECT */ DELETE * FROM test_table /* WHERE post_type = 'post' */ " );
265275 $ this ->assertEquals ( 'DELETE * FROM test_table ' , $ source ->get_query () );
266276 }
277+
278+ /**
279+ * Test Save Query not allowed for subscriber.
280+ */
281+ public function test_sql_save_chart_subscriber () {
282+ $ this ->_setRole ( 'subscriber ' );
283+
284+ $ _GET ['security ' ] = wp_create_nonce ( Visualizer_Plugin::ACTION_SAVE_DB_QUERY . Visualizer_Plugin::VERSION );
285+ $ _GET ['chart ' ] = '1 ' ;
286+
287+ $ _POST ['params ' ] = array (
288+ 'query ' => 'SELECT * FROM wp_posts LIMIT 1 ' ,
289+ );
290+ try {
291+ // Trigger the AJAX action
292+ $ this ->_handleAjax ( Visualizer_Plugin::ACTION_SAVE_DB_QUERY );
293+ } catch ( WPAjaxDieContinueException $ e ) {
294+ // We expected this, do nothing.
295+ }
296+
297+ $ response = json_decode ( $ this ->_last_response );
298+ $ this ->assertIsObject ( $ response );
299+ $ this ->assertObjectHasAttribute ( 'success ' , $ response );
300+ $ this ->assertObjectHasAttribute ( 'data ' , $ response );
301+ $ this ->assertEquals ( 'Action not allowed for this user. ' , $ response ->data ->msg );
302+ $ this ->assertFalse ( $ response ->success );
303+ }
304+
305+ /**
306+ * Test Save Query not allowed if not pro.
307+ */
308+ public function test_sql_save_chart_admin () {
309+ wp_set_current_user ( $ this ->admin_user_id );
310+ $ this ->_setRole ( 'administrator ' );
311+
312+ $ _GET ['security ' ] = wp_create_nonce ( Visualizer_Plugin::ACTION_SAVE_DB_QUERY . Visualizer_Plugin::VERSION );
313+ $ _GET ['chart ' ] = '1 ' ;
314+
315+ $ _POST ['params ' ] = array (
316+ 'query ' => 'SELECT * FROM wp_posts LIMIT 1 ' ,
317+ );
318+ try {
319+ // Trigger the AJAX action
320+ $ this ->_handleAjax ( Visualizer_Plugin::ACTION_SAVE_DB_QUERY );
321+ } catch ( WPAjaxDieContinueException $ e ) {
322+ // We expected this, do nothing.
323+ }
324+
325+ $ response = json_decode ( $ this ->_last_response );
326+ $ this ->assertIsObject ( $ response );
327+ $ this ->assertObjectHasAttribute ( 'success ' , $ response );
328+ $ this ->assertObjectHasAttribute ( 'data ' , $ response );
329+ $ this ->assertEquals ( 'Feature is not available. ' , $ response ->data ->msg );
330+ $ this ->assertFalse ( $ response ->success );
331+ }
332+
333+ /**
334+ * Utility method to mock pro version.
335+ */
336+ private function enable_pro () {
337+ add_filter ( 'visualizer_is_pro ' , '__return_true ' );
338+ }
267339}
0 commit comments