@@ -243,20 +243,47 @@ public function test_extract_zip_valid() {
243243 wp_delete_file ( $ tmp );
244244 }
245245
246+ /**
247+ * Set up AJAX context so wp_send_json() uses wp_die() instead of die().
248+ *
249+ * WordPress dispatches wp_die() to the AJAX handler when wp_doing_ajax() is true.
250+ * The default AJAX handler calls native die(), so we must also override it to
251+ * throw WPDieException (like WP_Ajax_UnitTestCase does).
252+ */
253+ private function enable_ajax_die_handler () {
254+ add_filter ( 'wp_doing_ajax ' , '__return_true ' );
255+ add_filter (
256+ 'wp_die_ajax_handler ' ,
257+ function () {
258+ return array ( $ this , 'wp_die_handler ' );
259+ },
260+ 1
261+ );
262+ }
263+
264+ /**
265+ * Remove AJAX die handler overrides.
266+ */
267+ private function disable_ajax_die_handler () {
268+ remove_filter ( 'wp_doing_ajax ' , '__return_true ' );
269+ remove_all_filters ( 'wp_die_ajax_handler ' );
270+ }
271+
246272 /**
247273 * Test handle_install_request requires nonce.
248274 */
249275 public function test_handle_install_request_requires_nonce () {
250276 $ user_id = $ this ->factory ->user ->create ( array ( 'role ' => 'administrator ' ) );
251277 wp_set_current_user ( $ user_id );
252278
253- // wp_send_json_error prints JSON before die(), capture it to avoid polluting stdout.
279+ $ this -> enable_ajax_die_handler ();
254280 $ this ->expectException ( WPDieException::class );
255281 ob_start ();
256282 try {
257283 $ this ->installer ->handle_install_request ();
258284 } finally {
259285 ob_end_clean ();
286+ $ this ->disable_ajax_die_handler ();
260287 }
261288 }
262289
@@ -269,12 +296,14 @@ public function test_handle_install_request_requires_admin() {
269296
270297 $ _REQUEST ['_nonce ' ] = wp_create_nonce ( ExeLearning_Static_Editor_Installer::AJAX_ACTION );
271298
299+ $ this ->enable_ajax_die_handler ();
272300 $ this ->expectException ( WPDieException::class );
273301 ob_start ();
274302 try {
275303 $ this ->installer ->handle_install_request ();
276304 } finally {
277305 ob_end_clean ();
306+ $ this ->disable_ajax_die_handler ();
278307 }
279308 }
280309
@@ -454,12 +483,14 @@ public function test_concurrent_install_blocked() {
454483
455484 set_transient ( 'exelearning_installing_editor ' , true , 300 );
456485
486+ $ this ->enable_ajax_die_handler ();
457487 $ this ->expectException ( WPDieException::class );
458488 ob_start ();
459489 try {
460490 $ this ->installer ->handle_install_request ();
461491 } finally {
462492 ob_end_clean ();
493+ $ this ->disable_ajax_die_handler ();
463494 delete_transient ( 'exelearning_installing_editor ' );
464495 }
465496 }
0 commit comments