From ad5031e51d0da83ea427a8060423c772a083ab15 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 16 Jul 2026 16:42:05 -0400 Subject: [PATCH 1/3] 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/3] 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 bbf62ef90ec08e006dd202e92a2651c80a8ffae6 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 16 Jul 2026 17:09:18 -0400 Subject: [PATCH 3/3] Add unit tests for `get_temp_dir()` function --- tests/phpunit/tests/functions/getTempDir.php | 53 ++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 tests/phpunit/tests/functions/getTempDir.php diff --git a/tests/phpunit/tests/functions/getTempDir.php b/tests/phpunit/tests/functions/getTempDir.php new file mode 100644 index 0000000000000..2e660985bcfb2 --- /dev/null +++ b/tests/phpunit/tests/functions/getTempDir.php @@ -0,0 +1,53 @@ +assertStringEndsWith( '/', $temp_dir ); + } + + /** + * Tests that get_temp_dir() returns a writable directory. + * + * @ticket 65651 + */ + public function test_get_temp_dir_is_writable_directory() { + $temp_dir = get_temp_dir(); + $this->assertDirectoryExists( $temp_dir ); + $this->assertTrue( wp_is_writable( $temp_dir ), 'The temporary directory must be writable.' ); + } + + /** + * Tests that get_temp_dir() respects the WP_TEMP_DIR constant. + * + * Since constants cannot be redefined, we use a separate process to test this. + * + * @ticket 65651 + * + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function test_get_temp_dir_respects_constant() { + $custom_temp = '/tmp/wp-custom-temp/'; + if ( 'Windows' === PHP_OS_FAMILY ) { + $custom_temp = 'C:\\Windows\\Temp\\wp-custom-temp\\'; + } + + define( 'WP_TEMP_DIR', $custom_temp ); + + $this->assertSame( trailingslashit( $custom_temp ), get_temp_dir() ); + } +}