From ad5031e51d0da83ea427a8060423c772a083ab15 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 16 Jul 2026 16:42:05 -0400 Subject: [PATCH 1/4] 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/4] 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 3879919830c93b4019745b30004872f4a12b0eb9 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 16 Jul 2026 17:46:17 -0400 Subject: [PATCH 3/4] Add unit tests for `get_temp_dir()` function --- .../phpunit/tests/functions/wpJsonEncode.php | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 tests/phpunit/tests/functions/wpJsonEncode.php diff --git a/tests/phpunit/tests/functions/wpJsonEncode.php b/tests/phpunit/tests/functions/wpJsonEncode.php new file mode 100644 index 0000000000000..c9ce05edb3537 --- /dev/null +++ b/tests/phpunit/tests/functions/wpJsonEncode.php @@ -0,0 +1,96 @@ +assertSame( $expected, wp_json_encode( $data ) ); + } + + /** + * Data provider for test_wp_json_encode(). + * + * @return array + */ + public function data_wp_json_encode(): array { + return array( + 'simple array' => array( + array( 'a' => 1, 'b' => 2 ), + '{"a":1,"b":2}', + ), + 'nested object' => array( + (object) array( 'a' => array( 1, 2 ) ), + '{"a":[1,2]}', + ), + 'string' => array( + 'hello world', + '"hello world"', + ), + 'integer' => array( + 123, + '123', + ), + ); + } + + /** + * Tests encoding with options. + * + * @ticket 65652 + */ + public function test_wp_json_encode_options() { + $data = array( 'a' => 1 ); + $this->assertSame( "{\n \"a\": 1\n}", wp_json_encode( $data, JSON_PRETTY_PRINT ) ); + } + + /** + * Tests encoding with depth. + * + * @ticket 65652 + */ + public function test_wp_json_encode_depth() { + $data = array( 'a' => array( 'b' => 1 ) ); + // Depth 1 should fail to encode the nested array. + $this->assertFalse( wp_json_encode( $data, 0, 1 ) ); + // Depth 2 should succeed. + $this->assertSame( '{"a":{"b":1}}', wp_json_encode( $data, 0, 2 ) ); + } + + /** + * Tests handling of invalid UTF-8. + * + * wp_json_encode() attempts to fix invalid UTF-8 by using _wp_json_prepare_data(). + * + * @ticket 65652 + */ + public function test_wp_json_encode_invalid_utf8() { + // Invalid UTF-8 sequence. + $invalid_utf8 = "Invalid\x80UTF8"; + $encoded = wp_json_encode( $invalid_utf8 ); + + // It should not be false, it should have attempted to fix it. + $this->assertNotFalse( $encoded ); + // In newer PHP versions, json_encode might handle it or it's sanitized to 'Invalid' or similar. + // WordPress's _wp_json_sanity_check will attempt to clean it. + $this->assertStringContainsString( 'Invalid', $encoded ); + } +} From 629d558f6d296b8744411d8349c129b7a48828f4 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 16 Jul 2026 18:22:07 -0400 Subject: [PATCH 4/4] Update wpJsonEncode.php --- tests/phpunit/tests/functions/wpJsonEncode.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/functions/wpJsonEncode.php b/tests/phpunit/tests/functions/wpJsonEncode.php index c9ce05edb3537..04716cf200e70 100644 --- a/tests/phpunit/tests/functions/wpJsonEncode.php +++ b/tests/phpunit/tests/functions/wpJsonEncode.php @@ -34,7 +34,10 @@ public function test_wp_json_encode( $data, $expected ) { public function data_wp_json_encode(): array { return array( 'simple array' => array( - array( 'a' => 1, 'b' => 2 ), + array( + 'a' => 1, + 'b' => 2 + ), '{"a":1,"b":2}', ), 'nested object' => array(