Skip to content

Commit a2c7e07

Browse files
committed
chore: improved test
1 parent 9a2cc2f commit a2c7e07

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

tests/test-ajax.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ class Test_Visualizer_Ajax extends WP_Ajax_UnitTestCase {
2020
*/
2121
private $admin_user_id;
2222

23+
/**
24+
* Contributor user ID.
25+
*
26+
* @var int
27+
*/
28+
private $contibutor_user_id;
29+
2330
/**
2431
* Subscriber user ID.
2532
*
@@ -39,6 +46,12 @@ public function setUp() {
3946
);
4047
wp_set_current_user( $this->admin_user_id );
4148

49+
$this->contibutor_user_id = $this->factory->user->create(
50+
array(
51+
'role' => 'contributor',
52+
)
53+
);
54+
4255
$this->subscriber_user_id = $this->factory->user->create(
4356
array(
4457
'role' => 'subscriber',
@@ -126,6 +139,34 @@ public function test_ajax_response_get_query_data_valid_query_with_filtered_colu
126139
$this->assertTrue( $response->success );
127140
}
128141

142+
/**
143+
* Test the AJAX response for fetching the database data with user capability.
144+
*/
145+
public function test_ajax_response_get_query_data_contributor_dissallow() {
146+
wp_set_current_user( $this->contibutor_user_id );
147+
$this->_setRole( 'contributor' );
148+
149+
$_GET['security'] = wp_create_nonce( Visualizer_Plugin::ACTION_FETCH_DB_DATA . Visualizer_Plugin::VERSION );
150+
151+
$_POST['params'] = array(
152+
'query' => "/**/UPDATE wp_options SET option_value='administrator' WHERE option_name='default_role' --",
153+
'chart_id' => 1,
154+
);
155+
try {
156+
// Trigger the AJAX action
157+
$this->_handleAjax( Visualizer_Plugin::ACTION_FETCH_DB_DATA );
158+
} catch ( WPAjaxDieContinueException $e ) {
159+
// We expected this, do nothing.
160+
}
161+
162+
$response = json_decode( $this->_last_response );
163+
$this->assertIsObject( $response );
164+
$this->assertObjectHasAttribute( 'success', $response );
165+
$this->assertObjectHasAttribute( 'data', $response );
166+
$this->assertEquals( 'Action not allowed for this user.', $response->data->msg );
167+
$this->assertFalse( $response->success );
168+
}
169+
129170
/**
130171
* Test the AJAX response for fetching the database data with user capability.
131172
*/

0 commit comments

Comments
 (0)