@@ -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 ' ] );
0 commit comments