Skip to content

Commit 80ad98d

Browse files
committed
Extend WP_Ajax_UnitTestCase instead of WP_UnitTestCase
1 parent e6d9a69 commit 80ad98d

3 files changed

Lines changed: 54 additions & 15 deletions

File tree

tests/phpunit/traits/test-class-ajax-security-aioseo.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
* @covers \Progress_Planner\Suggested_Tasks\Providers\Traits\Ajax_Security_AIOSEO
1616
*/
17-
class Ajax_Security_AIOSEO_Test extends \WP_UnitTestCase {
17+
class Ajax_Security_AIOSEO_Test extends \WP_Ajax_UnitTestCase {
1818

1919
/**
2020
* Mock class that uses the trait.
@@ -61,15 +61,26 @@ public function public_verify_aioseo_ajax_security( $capability = 'manage_option
6161
/**
6262
* Test verify_aioseo_active_or_fail when AIOSEO is not active.
6363
*
64+
* @covers \Progress_Planner\Suggested_Tasks\Providers\Traits\Ajax_Security_AIOSEO::verify_aioseo_active_or_fail
65+
*
6466
* @return void
6567
*/
6668
public function test_verify_aioseo_active_or_fail_not_active() {
6769
// Verify aioseo function doesn't exist.
6870
$this->assertFalse( \function_exists( 'aioseo' ) );
6971

70-
// We can't easily test wp_send_json_error because it exits,
71-
// but we can verify the function check.
72-
$this->assertTrue( true );
72+
// WP_Ajax_UnitTestCase allows us to test AJAX methods that call wp_send_json_error().
73+
try {
74+
$this->mock_class->public_verify_aioseo_active_or_fail();
75+
$this->fail( 'Expected WPAjaxDieStopException was not thrown' );
76+
} catch ( \WPAjaxDieStopException $e ) {
77+
// Get the response.
78+
$response = json_decode( $this->_last_response, true );
79+
$this->assertFalse( $response['success'] );
80+
$this->assertArrayHasKey( 'data', $response );
81+
$this->assertArrayHasKey( 'message', $response['data'] );
82+
$this->assertStringContainsString( 'AIOSEO', $response['data']['message'] );
83+
}
7384
}
7485

7586
/**

tests/phpunit/traits/test-class-ajax-security-base.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
* @covers \Progress_Planner\Suggested_Tasks\Providers\Traits\Ajax_Security_Base
1616
*/
17-
class Ajax_Security_Base_Test extends \WP_UnitTestCase {
17+
class Ajax_Security_Base_Test extends \WP_Ajax_UnitTestCase {
1818

1919
/**
2020
* Mock class that uses the trait.
@@ -102,10 +102,17 @@ public function test_verify_nonce_or_fail_valid() {
102102
public function test_verify_nonce_or_fail_invalid() {
103103
$_REQUEST['nonce'] = 'invalid_nonce';
104104

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+
}
109116

110117
// Clean up.
111118
unset( $_REQUEST['nonce'] );
@@ -141,8 +148,18 @@ public function test_verify_capability_or_fail_non_admin() {
141148
$user_id = $this->factory->user->create( [ 'role' => 'subscriber' ] );
142149
\wp_set_current_user( $user_id );
143150

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+
}
146163
}
147164

148165
/**

tests/phpunit/traits/test-class-ajax-security-yoast.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
* @covers \Progress_Planner\Suggested_Tasks\Providers\Traits\Ajax_Security_Yoast
1616
*/
17-
class Ajax_Security_Yoast_Test extends \WP_UnitTestCase {
17+
class Ajax_Security_Yoast_Test extends \WP_Ajax_UnitTestCase {
1818

1919
/**
2020
* Mock class that uses the trait.
@@ -61,15 +61,26 @@ public function public_verify_yoast_ajax_security( $capability = 'manage_options
6161
/**
6262
* Test verify_yoast_active_or_fail when Yoast is not active.
6363
*
64+
* @covers \Progress_Planner\Suggested_Tasks\Providers\Traits\Ajax_Security_Yoast::verify_yoast_active_or_fail
65+
*
6466
* @return void
6567
*/
6668
public function test_verify_yoast_active_or_fail_not_active() {
6769
// Verify WPSEO_Options class doesn't exist.
6870
$this->assertFalse( \class_exists( 'WPSEO_Options' ) );
6971

70-
// We can't easily test wp_send_json_error because it exits,
71-
// but we can verify the class check.
72-
$this->assertTrue( true );
72+
// WP_Ajax_UnitTestCase allows us to test AJAX methods that call wp_send_json_error().
73+
try {
74+
$this->mock_class->public_verify_yoast_active_or_fail();
75+
$this->fail( 'Expected WPAjaxDieStopException was not thrown' );
76+
} catch ( \WPAjaxDieStopException $e ) {
77+
// Get the response.
78+
$response = json_decode( $this->_last_response, true );
79+
$this->assertFalse( $response['success'] );
80+
$this->assertArrayHasKey( 'data', $response );
81+
$this->assertArrayHasKey( 'message', $response['data'] );
82+
$this->assertStringContainsString( 'Yoast', $response['data']['message'] );
83+
}
7384
}
7485

7586
/**

0 commit comments

Comments
 (0)