diff --git a/tests/phpunit/tests/functions/_defaultWpDieHandler.php b/tests/phpunit/tests/functions/_defaultWpDieHandler.php new file mode 100644 index 0000000000000..19f36de83da40 --- /dev/null +++ b/tests/phpunit/tests/functions/_defaultWpDieHandler.php @@ -0,0 +1,78 @@ + false ) ); + } catch ( WPDieException $e ) { + } + $output = ob_get_clean(); + + $this->assertStringContainsString( '
Error Message
', $output ); + $this->assertStringContainsString( 'Error Title', $output ); + } + + /** + * Tests handling of additional errors. + * + * @ticket 65655 + */ + public function test_default_wp_die_handler_additional_errors() { + $args = array( + 'exit' => false, + 'additional_errors' => array( + array( 'message' => 'Extra Error 1' ), + array( 'message' => 'Extra Error 2' ), + ), + ); + + ob_start(); + try { + _default_wp_die_handler( 'Main Error', '', $args ); + } catch ( WPDieException $e ) { + } + $output = ob_get_clean(); + + $this->assertStringContainsString( '
  • Main Error
  • ', $output ); + $this->assertStringContainsString( '
  • Extra Error 1
  • ', $output ); + $this->assertStringContainsString( '
  • Extra Error 2
  • ', $output ); + } + + /** + * Tests handling of a link. + * + * @ticket 65655 + */ + public function test_default_wp_die_handler_link() { + $args = array( + 'exit' => false, + 'link_url' => 'https://example.com', + 'link_text' => 'Go to Example', + ); + + ob_start(); + try { + _default_wp_die_handler( 'Error', '', $args ); + } catch ( WPDieException $e ) { + } + $output = ob_get_clean(); + + // The default handler uses single quotes for attributes in some cases. + $this->assertStringContainsString( "href='https://example.com'", $output ); + $this->assertStringContainsString( '>Go to Example', $output ); + } +} diff --git a/tests/phpunit/tests/functions/wpDie.php b/tests/phpunit/tests/functions/wpDie.php new file mode 100644 index 0000000000000..f53abdd1b4c4c --- /dev/null +++ b/tests/phpunit/tests/functions/wpDie.php @@ -0,0 +1,83 @@ + false ) ); + } catch ( WPDieException $e ) { + // The default test handler throws this. + } + + $this->assertSame( 1, $filter->get_call_count(), 'The wp_die_handler filter should have been called once.' ); + } + + /** + * Tests that wp_die() respects the Ajax handler filter. + * + * @ticket 65655 + * + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function test_wp_die_ajax_handler_selection() { + if ( ! defined( 'DOING_AJAX' ) ) { + define( 'DOING_AJAX', true ); + } + + $filter = new MockAction(); + add_filter( 'wp_die_ajax_handler', array( $filter, 'filter' ) ); + + ob_start(); + try { + wp_die( 'Ajax Message', 'Ajax Title', array( 'exit' => false ) ); + } catch ( WPDieException $e ) { + } + ob_end_clean(); + + $this->assertSame( 1, $filter->get_call_count(), 'The wp_die_ajax_handler filter should have been called once.' ); + } + + /** + * Tests that wp_die() handles WP_Error objects. + * + * @ticket 65655 + */ + public function test_wp_die_with_wp_error() { + $error = new WP_Error( 'test_error', 'Test Error Message' ); + + // Use a simple variable to capture the first argument. + $captured_message = null; + add_filter( + 'wp_die_handler', + function ( $callback ) use ( &$captured_message ) { + return function ( $message ) use ( &$captured_message ) { + $captured_message = $message; + throw new WPDieException( 'Intercepted' ); + }; + } + ); + + try { + wp_die( $error, '', array( 'exit' => false ) ); + } catch ( WPDieException $e ) { + } + + $this->assertSame( $error, $captured_message, 'The error object should be passed as the first argument to the handler.' ); + } +}