Skip to content

Commit 58c08b4

Browse files
committed
Fix risky tests
1 parent a8715d6 commit 58c08b4

3 files changed

Lines changed: 28 additions & 0 deletions

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,18 @@ public function test_verify_aioseo_active_or_fail_not_active() {
6969
// Verify aioseo function doesn't exist.
7070
$this->assertFalse( \function_exists( 'aioseo' ) );
7171

72+
// WordPress Core's _handleAjax() starts output buffering before calling AJAX actions.
73+
// We need to do the same since we're calling methods directly.
74+
// WordPress Core's dieHandler() will call ob_get_clean() to clean this buffer.
75+
\ini_set( 'implicit_flush', false );
76+
\ob_start();
77+
7278
// WP_Ajax_UnitTestCase allows us to test AJAX methods that call wp_send_json_error().
7379
try {
7480
$this->mock_class->public_verify_aioseo_active_or_fail();
7581
$this->fail( 'Expected WPAjaxDieContinueException was not thrown' );
7682
} catch ( \WPAjaxDieContinueException $e ) {
83+
// WordPress Core's dieHandler() calls ob_get_clean() which gets output and cleans buffer.
7784
// Get the response.
7885
$response = json_decode( $this->_last_response, true );
7986
$this->assertFalse( $response['success'] );

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,18 @@ public function test_verify_nonce_or_fail_valid() {
102102
public function test_verify_nonce_or_fail_invalid() {
103103
$_REQUEST['nonce'] = 'invalid_nonce';
104104

105+
// WordPress Core's _handleAjax() starts output buffering before calling AJAX actions.
106+
// We need to do the same since we're calling methods directly.
107+
// WordPress Core's dieHandler() will call ob_get_clean() to clean this buffer.
108+
\ini_set( 'implicit_flush', false );
109+
\ob_start();
110+
105111
// WP_Ajax_UnitTestCase allows us to test AJAX methods that call wp_send_json_error().
106112
try {
107113
$this->mock_class->public_verify_nonce_or_fail();
108114
$this->fail( 'Expected WPAjaxDieContinueException was not thrown' );
109115
} catch ( \WPAjaxDieContinueException $e ) {
116+
// WordPress Core's dieHandler() calls ob_get_clean() which gets output and cleans buffer.
110117
// Get the response.
111118
$response = json_decode( $this->_last_response, true );
112119
$this->assertFalse( $response['success'] );
@@ -148,11 +155,18 @@ public function test_verify_capability_or_fail_non_admin() {
148155
$user_id = $this->factory->user->create( [ 'role' => 'subscriber' ] );
149156
\wp_set_current_user( $user_id );
150157

158+
// WordPress Core's _handleAjax() starts output buffering before calling AJAX actions.
159+
// We need to do the same since we're calling methods directly.
160+
// WordPress Core's dieHandler() will call ob_get_clean() to clean this buffer.
161+
\ini_set( 'implicit_flush', false );
162+
\ob_start();
163+
151164
// WP_Ajax_UnitTestCase allows us to test AJAX methods that call wp_send_json_error().
152165
try {
153166
$this->mock_class->public_verify_capability_or_fail();
154167
$this->fail( 'Expected WPAjaxDieContinueException was not thrown' );
155168
} catch ( \WPAjaxDieContinueException $e ) {
169+
// WordPress Core's dieHandler() calls ob_get_clean() which gets output and cleans buffer.
156170
// Get the response.
157171
$response = json_decode( $this->_last_response, true );
158172
$this->assertFalse( $response['success'] );

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,18 @@ public function test_verify_yoast_active_or_fail_not_active() {
6969
// Verify WPSEO_Options class doesn't exist.
7070
$this->assertFalse( \class_exists( 'WPSEO_Options' ) );
7171

72+
// WordPress Core's _handleAjax() starts output buffering before calling AJAX actions.
73+
// We need to do the same since we're calling methods directly.
74+
// WordPress Core's dieHandler() will call ob_get_clean() to clean this buffer.
75+
\ini_set( 'implicit_flush', false );
76+
\ob_start();
77+
7278
// WP_Ajax_UnitTestCase allows us to test AJAX methods that call wp_send_json_error().
7379
try {
7480
$this->mock_class->public_verify_yoast_active_or_fail();
7581
$this->fail( 'Expected WPAjaxDieContinueException was not thrown' );
7682
} catch ( \WPAjaxDieContinueException $e ) {
83+
// WordPress Core's dieHandler() calls ob_get_clean() which gets output and cleans buffer.
7784
// Get the response.
7885
$response = json_decode( $this->_last_response, true );
7986
$this->assertFalse( $response['success'] );

0 commit comments

Comments
 (0)