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 4b39599db433d245077a107c741b25c7175d2f88 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 16 Jul 2026 18:00:07 -0400 Subject: [PATCH 3/6] Add unit tests for `wp_get_update_php_url()` function --- .../tests/functions/wpGetUpdatePhpUrl.php | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 tests/phpunit/tests/functions/wpGetUpdatePhpUrl.php diff --git a/tests/phpunit/tests/functions/wpGetUpdatePhpUrl.php b/tests/phpunit/tests/functions/wpGetUpdatePhpUrl.php new file mode 100644 index 0000000000000..d75ce16c2cca0 --- /dev/null +++ b/tests/phpunit/tests/functions/wpGetUpdatePhpUrl.php @@ -0,0 +1,85 @@ +assertSame( 'https://wordpress.org/support/update-php/', wp_get_update_php_url() ); + } + + /** + * Tests that wp_get_update_php_url() respects the WP_UPDATE_PHP_URL environment variable. + * + * @ticket 65653 + */ + public function test_wp_get_update_php_url_env_var() { + $custom_url = 'https://example.com/update-php/'; + putenv( 'WP_UPDATE_PHP_URL=' . $custom_url ); + + $url = wp_get_update_php_url(); + + // Clean up. + putenv( 'WP_UPDATE_PHP_URL' ); + + $this->assertSame( $custom_url, $url ); + } + + /** + * Tests that wp_get_update_php_url() respects the wp_update_php_url filter. + * + * @ticket 65653 + */ + public function test_wp_get_update_php_url_filter() { + $custom_url = 'https://example.org/custom-php-update/'; + add_filter( 'wp_update_php_url', function() use ( $custom_url ) { + return $custom_url; + } ); + + $url = wp_get_update_php_url(); + + $this->assertSame( $custom_url, $url ); + } + + /** + * Tests that wp_get_update_php_url() falls back to the default URL if an empty string is provided. + * + * @ticket 65653 + * + * @dataProvider data_wp_get_update_php_url_empty_fallbacks + * + * @param string $empty_value The empty value to test. + */ + public function test_wp_get_update_php_url_empty_fallbacks( $empty_value ) { + add_filter( 'wp_update_php_url', function() use ( $empty_value ) { + return $empty_value; + } ); + + $this->assertSame( 'https://wordpress.org/support/update-php/', wp_get_update_php_url() ); + } + + /** + * Data provider for test_wp_get_update_php_url_empty_fallbacks(). + * + * @return array + */ + public function data_wp_get_update_php_url_empty_fallbacks(): array { + return array( + 'empty string' => array( '' ), + 'null' => array( null ), + 'false' => array( false ), + ); + } +} From a38ea31b6f32b6e2d9ee27de3e376d4acc7cc895 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 16 Jul 2026 18:05:28 -0400 Subject: [PATCH 4/6] Update wpGetUpdatePhpUrl.php --- .../tests/functions/wpGetUpdatePhpUrl.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/tests/functions/wpGetUpdatePhpUrl.php b/tests/phpunit/tests/functions/wpGetUpdatePhpUrl.php index d75ce16c2cca0..c07f239820dee 100644 --- a/tests/phpunit/tests/functions/wpGetUpdatePhpUrl.php +++ b/tests/phpunit/tests/functions/wpGetUpdatePhpUrl.php @@ -42,9 +42,11 @@ public function test_wp_get_update_php_url_env_var() { */ public function test_wp_get_update_php_url_filter() { $custom_url = 'https://example.org/custom-php-update/'; - add_filter( 'wp_update_php_url', function() use ( $custom_url ) { - return $custom_url; - } ); + add_filter( 'wp_update_php_url', + function () use ( $custom_url ) { + return $custom_url; + } + ); $url = wp_get_update_php_url(); @@ -61,9 +63,11 @@ public function test_wp_get_update_php_url_filter() { * @param string $empty_value The empty value to test. */ public function test_wp_get_update_php_url_empty_fallbacks( $empty_value ) { - add_filter( 'wp_update_php_url', function() use ( $empty_value ) { - return $empty_value; - } ); + add_filter( 'wp_update_php_url', + function () use ( $empty_value ) { + return $empty_value; + } + ); $this->assertSame( 'https://wordpress.org/support/update-php/', wp_get_update_php_url() ); } From 9a535611409007b184cd6793de1820e801cab809 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 16 Jul 2026 18:08:17 -0400 Subject: [PATCH 5/6] Refactor add_filter calls for better readability --- .../tests/functions/wpGetUpdatePhpUrl.php | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/tests/phpunit/tests/functions/wpGetUpdatePhpUrl.php b/tests/phpunit/tests/functions/wpGetUpdatePhpUrl.php index c07f239820dee..886bf44f55b87 100644 --- a/tests/phpunit/tests/functions/wpGetUpdatePhpUrl.php +++ b/tests/phpunit/tests/functions/wpGetUpdatePhpUrl.php @@ -42,11 +42,12 @@ public function test_wp_get_update_php_url_env_var() { */ public function test_wp_get_update_php_url_filter() { $custom_url = 'https://example.org/custom-php-update/'; - add_filter( 'wp_update_php_url', - function () use ( $custom_url ) { - return $custom_url; - } - ); + add_filter( + 'wp_update_php_url', + function () use ( $custom_url ) { + return $custom_url; + } + ); $url = wp_get_update_php_url(); @@ -63,11 +64,12 @@ function () use ( $custom_url ) { * @param string $empty_value The empty value to test. */ public function test_wp_get_update_php_url_empty_fallbacks( $empty_value ) { - add_filter( 'wp_update_php_url', - function () use ( $empty_value ) { - return $empty_value; - } - ); + add_filter( + 'wp_update_php_url', + function () use ( $empty_value ) { + return $empty_value; + } + ); $this->assertSame( 'https://wordpress.org/support/update-php/', wp_get_update_php_url() ); } From 1422f415ce9590a63bd4f654e09991aa4ff3bd96 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 16 Jul 2026 18:10:14 -0400 Subject: [PATCH 6/6] Fix syntax error in wpGetUpdatePhpUrl test cases --- tests/phpunit/tests/functions/wpGetUpdatePhpUrl.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/functions/wpGetUpdatePhpUrl.php b/tests/phpunit/tests/functions/wpGetUpdatePhpUrl.php index 886bf44f55b87..67831b35e46a5 100644 --- a/tests/phpunit/tests/functions/wpGetUpdatePhpUrl.php +++ b/tests/phpunit/tests/functions/wpGetUpdatePhpUrl.php @@ -46,7 +46,7 @@ public function test_wp_get_update_php_url_filter() { 'wp_update_php_url', function () use ( $custom_url ) { return $custom_url; - } + } ); $url = wp_get_update_php_url(); @@ -68,7 +68,7 @@ public function test_wp_get_update_php_url_empty_fallbacks( $empty_value ) { 'wp_update_php_url', function () use ( $empty_value ) { return $empty_value; - } + } ); $this->assertSame( 'https://wordpress.org/support/update-php/', wp_get_update_php_url() );