|
14 | 14 | * |
15 | 15 | * @covers \Progress_Planner\Suggested_Tasks\Providers\Traits\Ajax_Security_Base |
16 | 16 | */ |
17 | | -class Ajax_Security_Base_Test extends \WP_UnitTestCase { |
| 17 | +class Ajax_Security_Base_Test extends \WP_Ajax_UnitTestCase { |
18 | 18 |
|
19 | 19 | /** |
20 | 20 | * Mock class that uses the trait. |
@@ -102,10 +102,17 @@ public function test_verify_nonce_or_fail_valid() { |
102 | 102 | public function test_verify_nonce_or_fail_invalid() { |
103 | 103 | $_REQUEST['nonce'] = 'invalid_nonce'; |
104 | 104 |
|
105 | | - // We can't easily test wp_send_json_error because it exits, |
106 | | - // but we can verify the check_ajax_referer behavior. |
107 | | - $result = \check_ajax_referer( 'progress_planner', 'nonce', false ); |
108 | | - $this->assertFalse( $result ); |
| 105 | + // WP_Ajax_UnitTestCase allows us to test AJAX methods that call wp_send_json_error(). |
| 106 | + try { |
| 107 | + $this->mock_class->public_verify_nonce_or_fail(); |
| 108 | + $this->fail( 'Expected WPAjaxDieStopException was not thrown' ); |
| 109 | + } catch ( \WPAjaxDieStopException $e ) { |
| 110 | + // Get the response. |
| 111 | + $response = json_decode( $this->_last_response, true ); |
| 112 | + $this->assertFalse( $response['success'] ); |
| 113 | + $this->assertArrayHasKey( 'data', $response ); |
| 114 | + $this->assertArrayHasKey( 'message', $response['data'] ); |
| 115 | + } |
109 | 116 |
|
110 | 117 | // Clean up. |
111 | 118 | unset( $_REQUEST['nonce'] ); |
@@ -141,8 +148,18 @@ public function test_verify_capability_or_fail_non_admin() { |
141 | 148 | $user_id = $this->factory->user->create( [ 'role' => 'subscriber' ] ); |
142 | 149 | \wp_set_current_user( $user_id ); |
143 | 150 |
|
144 | | - // Verify user cannot manage options. |
145 | | - $this->assertFalse( \current_user_can( 'manage_options' ) ); |
| 151 | + // WP_Ajax_UnitTestCase allows us to test AJAX methods that call wp_send_json_error(). |
| 152 | + try { |
| 153 | + $this->mock_class->public_verify_capability_or_fail(); |
| 154 | + $this->fail( 'Expected WPAjaxDieStopException was not thrown' ); |
| 155 | + } catch ( \WPAjaxDieStopException $e ) { |
| 156 | + // Get the response. |
| 157 | + $response = json_decode( $this->_last_response, true ); |
| 158 | + $this->assertFalse( $response['success'] ); |
| 159 | + $this->assertArrayHasKey( 'data', $response ); |
| 160 | + $this->assertArrayHasKey( 'message', $response['data'] ); |
| 161 | + $this->assertStringContainsString( 'permission', $response['data']['message'] ); |
| 162 | + } |
146 | 163 | } |
147 | 164 |
|
148 | 165 | /** |
|
0 commit comments