From ad5031e51d0da83ea427a8060423c772a083ab15 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 16 Jul 2026 16:42:05 -0400 Subject: [PATCH 1/6] Add unit tests for `status_header()` function --- .../phpunit/tests/functions/statusHeader.php | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 tests/phpunit/tests/functions/statusHeader.php diff --git a/tests/phpunit/tests/functions/statusHeader.php b/tests/phpunit/tests/functions/statusHeader.php new file mode 100644 index 0000000000000..51dcc2ad3adea --- /dev/null +++ b/tests/phpunit/tests/functions/statusHeader.php @@ -0,0 +1,134 @@ +get_args(); + $this->assertNotEmpty( $args, 'The status_header filter was not called.' ); + + $actual_header = $args[0][0]; + $this->assertMatchesRegularExpression( $expected_regex, $actual_header ); + $this->assertSame( (int) $code, $args[0][1], 'The status code passed to the filter does not match.' ); + + if ( $description ) { + $this->assertSame( $description, $args[0][2], 'The description passed to the filter does not match.' ); + } else { + $this->assertSame( get_status_header_desc( $code ), $args[0][2], 'The default description passed to the filter does not match.' ); + } + } + + /** + * Data provider for test_status_header_filter(). + * + * @return array + */ + public function data_status_header(): array { + return array( + '200 OK' => array( + 200, + '', + '/^HTTP\/1\.[012] 200 OK$/', + ), + '404 Not Found' => array( + 404, + '', + '/^HTTP\/1\.[012] 404 Not Found$/', + ), + 'Custom description' => array( + 200, + 'Everything is Fine', + '/^HTTP\/1\.[012] 200 Everything is Fine$/', + ), + '301 Moved Permanently' => array( + 301, + '', + '/^HTTP\/1\.[012] 301 Moved Permanently$/', + ), + ); + } + + /** + * Tests that status_header() returns early for unknown status codes without a description. + * + * @ticket 65650 + */ + public function test_status_header_unknown_code_returns_early() { + $filter = new MockAction(); + add_filter( 'status_header', array( $filter, 'filter' ), 10, 4 ); + + status_header( 999 ); + + $this->assertEmpty( $filter->get_args(), 'The status_header filter should not have been called for an unknown status code without description.' ); + } + + /** + * Tests that status_header() works with unknown status codes if a description is provided. + * + * @ticket 65650 + */ + public function test_status_header_unknown_code_with_description() { + $filter = new MockAction(); + add_filter( 'status_header', array( $filter, 'filter' ), 10, 4 ); + + status_header( 999, 'Custom Status' ); + + $args = $filter->get_args(); + $this->assertNotEmpty( $args, 'The status_header filter should have been called when a description is provided.' ); + $this->assertMatchesRegularExpression( '/^HTTP\/1\.[012] 999 Custom Status$/', $args[0][0] ); + } + + /** + * Tests that status_header() respects the server protocol. + * + * @ticket 65650 + */ + public function test_status_header_respects_protocol() { + $old_protocol = $_SERVER['SERVER_PROTOCOL'] ?? null; + $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1'; + + $filter = new MockAction(); + add_filter( 'status_header', array( $filter, 'filter' ), 10, 4 ); + + status_header( 200 ); + + $args = $filter->get_args(); + $this->assertStringStartsWith( 'HTTP/1.1 200 OK', $args[0][0] ); + + // Reset protocol. + if ( null === $old_protocol ) { + unset( $_SERVER['SERVER_PROTOCOL'] ); + } else { + $_SERVER['SERVER_PROTOCOL'] = $old_protocol; + } + } +} From dd6b536f61991062abe8754ce6bbe1b0bdc1a716 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 16 Jul 2026 16:44:17 -0400 Subject: [PATCH 2/6] Revert "Add unit tests for `status_header()` function" This reverts commit ad5031e51d0da83ea427a8060423c772a083ab15. --- .../phpunit/tests/functions/statusHeader.php | 134 ------------------ 1 file changed, 134 deletions(-) delete mode 100644 tests/phpunit/tests/functions/statusHeader.php diff --git a/tests/phpunit/tests/functions/statusHeader.php b/tests/phpunit/tests/functions/statusHeader.php deleted file mode 100644 index 51dcc2ad3adea..0000000000000 --- a/tests/phpunit/tests/functions/statusHeader.php +++ /dev/null @@ -1,134 +0,0 @@ -get_args(); - $this->assertNotEmpty( $args, 'The status_header filter was not called.' ); - - $actual_header = $args[0][0]; - $this->assertMatchesRegularExpression( $expected_regex, $actual_header ); - $this->assertSame( (int) $code, $args[0][1], 'The status code passed to the filter does not match.' ); - - if ( $description ) { - $this->assertSame( $description, $args[0][2], 'The description passed to the filter does not match.' ); - } else { - $this->assertSame( get_status_header_desc( $code ), $args[0][2], 'The default description passed to the filter does not match.' ); - } - } - - /** - * Data provider for test_status_header_filter(). - * - * @return array - */ - public function data_status_header(): array { - return array( - '200 OK' => array( - 200, - '', - '/^HTTP\/1\.[012] 200 OK$/', - ), - '404 Not Found' => array( - 404, - '', - '/^HTTP\/1\.[012] 404 Not Found$/', - ), - 'Custom description' => array( - 200, - 'Everything is Fine', - '/^HTTP\/1\.[012] 200 Everything is Fine$/', - ), - '301 Moved Permanently' => array( - 301, - '', - '/^HTTP\/1\.[012] 301 Moved Permanently$/', - ), - ); - } - - /** - * Tests that status_header() returns early for unknown status codes without a description. - * - * @ticket 65650 - */ - public function test_status_header_unknown_code_returns_early() { - $filter = new MockAction(); - add_filter( 'status_header', array( $filter, 'filter' ), 10, 4 ); - - status_header( 999 ); - - $this->assertEmpty( $filter->get_args(), 'The status_header filter should not have been called for an unknown status code without description.' ); - } - - /** - * Tests that status_header() works with unknown status codes if a description is provided. - * - * @ticket 65650 - */ - public function test_status_header_unknown_code_with_description() { - $filter = new MockAction(); - add_filter( 'status_header', array( $filter, 'filter' ), 10, 4 ); - - status_header( 999, 'Custom Status' ); - - $args = $filter->get_args(); - $this->assertNotEmpty( $args, 'The status_header filter should have been called when a description is provided.' ); - $this->assertMatchesRegularExpression( '/^HTTP\/1\.[012] 999 Custom Status$/', $args[0][0] ); - } - - /** - * Tests that status_header() respects the server protocol. - * - * @ticket 65650 - */ - public function test_status_header_respects_protocol() { - $old_protocol = $_SERVER['SERVER_PROTOCOL'] ?? null; - $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1'; - - $filter = new MockAction(); - add_filter( 'status_header', array( $filter, 'filter' ), 10, 4 ); - - status_header( 200 ); - - $args = $filter->get_args(); - $this->assertStringStartsWith( 'HTTP/1.1 200 OK', $args[0][0] ); - - // Reset protocol. - if ( null === $old_protocol ) { - unset( $_SERVER['SERVER_PROTOCOL'] ); - } else { - $_SERVER['SERVER_PROTOCOL'] = $old_protocol; - } - } -} From 36340c6f503764d938f67758c843df5711b1263b Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 16 Jul 2026 18:47:47 -0400 Subject: [PATCH 3/6] Add unit tests for `_default_wp_die_handler()` and `wp_die()` functions --- .../tests/functions/_defaultWpDieHandler.php | 78 ++++++++++++++++++ tests/phpunit/tests/functions/wpDie.php | 80 +++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 tests/phpunit/tests/functions/_defaultWpDieHandler.php create mode 100644 tests/phpunit/tests/functions/wpDie.php 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..4a9a0525f22d0 --- /dev/null +++ b/tests/phpunit/tests/functions/wpDie.php @@ -0,0 +1,80 @@ + 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.' ); + } +} From 70917affb927f3f863c22e03ceeb700126a3f842 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Fri, 17 Jul 2026 15:32:48 -0400 Subject: [PATCH 4/6] Improve formatting of wp_die_handler filter Refactor wp_die_handler filter for better readability. --- tests/phpunit/tests/functions/wpDie.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/tests/functions/wpDie.php b/tests/phpunit/tests/functions/wpDie.php index 4a9a0525f22d0..f53abdd1b4c4c 100644 --- a/tests/phpunit/tests/functions/wpDie.php +++ b/tests/phpunit/tests/functions/wpDie.php @@ -63,12 +63,15 @@ public function test_wp_die_with_wp_error() { // 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' ); - }; - } ); + 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 ) ); From 8111b5d0b6952c393331ddb2854d85ab19a9747a Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Fri, 18 Sep 2026 14:40:20 -0400 Subject: [PATCH 5/6] Rename and update `wp_die()` test file and class to follow naming conventions --- .../wpDie.php => wp-includes/functions/WpDieTest.php} | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) rename tests/phpunit/tests/{functions/wpDie.php => wp-includes/functions/WpDieTest.php} (90%) diff --git a/tests/phpunit/tests/functions/wpDie.php b/tests/phpunit/tests/wp-includes/functions/WpDieTest.php similarity index 90% rename from tests/phpunit/tests/functions/wpDie.php rename to tests/phpunit/tests/wp-includes/functions/WpDieTest.php index f53abdd1b4c4c..89fe182ae8522 100644 --- a/tests/phpunit/tests/functions/wpDie.php +++ b/tests/phpunit/tests/wp-includes/functions/WpDieTest.php @@ -1,13 +1,20 @@ Date: Wed, 4 Nov 2026 18:23:39 -0500 Subject: [PATCH 6/6] Enhance WpDieTest with additional assertions Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- tests/phpunit/tests/wp-includes/functions/WpDieTest.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/wp-includes/functions/WpDieTest.php b/tests/phpunit/tests/wp-includes/functions/WpDieTest.php index 89fe182ae8522..306d9f9d82eb5 100644 --- a/tests/phpunit/tests/wp-includes/functions/WpDieTest.php +++ b/tests/phpunit/tests/wp-includes/functions/WpDieTest.php @@ -23,7 +23,7 @@ class WpDieTest extends WP_UnitTestCase { */ public function test_wp_die_handler_selection() { $filter = new MockAction(); - add_filter( 'wp_die_handler', array( $filter, 'filter' ) ); + add_filter( 'wp_die_handler', array( $filter, 'filter' ), 9 ); try { wp_die( 'Message', 'Title', array( 'exit' => false ) ); @@ -32,6 +32,7 @@ public function test_wp_die_handler_selection() { } $this->assertSame( 1, $filter->get_call_count(), 'The wp_die_handler filter should have been called once.' ); + $this->assertSame( array( array( '_default_wp_die_handler' ) ), $filter->get_args(), 'The standard handler should be supplied to the filter.' ); } /** @@ -55,9 +56,11 @@ public function test_wp_die_ajax_handler_selection() { wp_die( 'Ajax Message', 'Ajax Title', array( 'exit' => false ) ); } catch ( WPDieException $e ) { } - ob_end_clean(); + $output = ob_get_clean(); $this->assertSame( 1, $filter->get_call_count(), 'The wp_die_ajax_handler filter should have been called once.' ); + $this->assertSame( array( array( '_ajax_wp_die_handler' ) ), $filter->get_args(), 'The Ajax handler should be supplied to the filter.' ); + $this->assertSame( 'Ajax Message', $output, 'The Ajax handler should output only the message.' ); } /**