From 7f341bd504804c1fea2a4b28c41e4592f2434084 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Mon, 27 Apr 2026 13:55:39 -0400 Subject: [PATCH 1/7] Add unit tests for got_mod_rewrite() in src/wp-admin/includes/misc.php Reference: https://core.trac.wordpress.org/ticket/65134 --- .../admin/includes/misc/gotModRewrite.php | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 tests/phpunit/tests/admin/includes/misc/gotModRewrite.php diff --git a/tests/phpunit/tests/admin/includes/misc/gotModRewrite.php b/tests/phpunit/tests/admin/includes/misc/gotModRewrite.php new file mode 100644 index 0000000000000..1b0b57c1a621c --- /dev/null +++ b/tests/phpunit/tests/admin/includes/misc/gotModRewrite.php @@ -0,0 +1,91 @@ +temp_filter_value = $filter_value; + } + + // If we are NOT filtering, we need to be aware of the environment. + // However, the function's internal logic is: + // return apply_filters( 'got_rewrite', apache_mod_loaded( 'mod_rewrite', true ) ); + // Since we want to test the function's behavior across different scenarios, + // we use the filter to simulate the different outcomes of the internal check. + + $this->assertSame( $expected, got_mod_rewrite() ); + + if ( null !== $filter_value ) { + remove_filter( 'got_rewrite', array( $this, 'filter_got_rewrite' ) ); + unset( $this->temp_filter_value ); + } + } + + /** + * Data provider for test_got_mod_rewrite. + * + * @return array[] { + * @type bool $expected The expected result. + * @type bool $apache_loaded Whether mod_rewrite is loaded (simulated via filter). + * @type bool|null $filter_value The value to return from the filter. + * } + */ + public function data_got_mod_rewrite() { + return array( + 'Default behavior (should match filter or internal check)' => array( + 'expected' => true, + 'apache_loaded' => true, + 'filter_value' => true, + ), + 'Filter returns false' => array( + 'expected' => false, + 'apache_loaded' => true, + 'filter_value' => false, + ), + 'Filter returns true even if Apache check might be false' => array( + 'expected' => true, + 'apache_loaded' => false, + 'filter_value' => true, + ), + ); + } + + /** + * Temporary property for filter value. + * @var bool + */ + private $temp_filter_value; + + /** + * Filter callback for 'got_rewrite'. + * + * @return bool + */ + public function filter_got_rewrite() { + return $this->temp_filter_value; + } +} From 53ae4e0ca6e8ffcfc9e657ef61071a17afbb2a27 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Mon, 27 Apr 2026 15:27:55 -0400 Subject: [PATCH 2/7] Ensure empty marker lines are excluded in src/wp-admin/includes/misc.php --- src/wp-admin/includes/misc.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/wp-admin/includes/misc.php b/src/wp-admin/includes/misc.php index 3724684ffd428..942f75b90e8e0 100644 --- a/src/wp-admin/includes/misc.php +++ b/src/wp-admin/includes/misc.php @@ -86,7 +86,9 @@ function extract_from_markers( $filename, $marker ) { continue; } - $result[] = $markerline; + if ( '' !== $markerline ) { + $result[] = $markerline; + } } if ( str_contains( $markerline, '# BEGIN ' . $marker ) ) { From 1f73812b32044947d3b9dadd3ec0edc02e0ed8da Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 28 Apr 2026 15:37:48 -0400 Subject: [PATCH 3/7] reverted blank line check src/wp-admin/includes/misc.php --- src/wp-admin/includes/misc.php | 4 +-- .../admin/includes/misc/gotModRewrite.php | 31 ++++++------------- 2 files changed, 10 insertions(+), 25 deletions(-) diff --git a/src/wp-admin/includes/misc.php b/src/wp-admin/includes/misc.php index 942f75b90e8e0..3724684ffd428 100644 --- a/src/wp-admin/includes/misc.php +++ b/src/wp-admin/includes/misc.php @@ -86,9 +86,7 @@ function extract_from_markers( $filename, $marker ) { continue; } - if ( '' !== $markerline ) { - $result[] = $markerline; - } + $result[] = $markerline; } if ( str_contains( $markerline, '# BEGIN ' . $marker ) ) { diff --git a/tests/phpunit/tests/admin/includes/misc/gotModRewrite.php b/tests/phpunit/tests/admin/includes/misc/gotModRewrite.php index 1b0b57c1a621c..d3198854cc404 100644 --- a/tests/phpunit/tests/admin/includes/misc/gotModRewrite.php +++ b/tests/phpunit/tests/admin/includes/misc/gotModRewrite.php @@ -27,8 +27,9 @@ public function test_got_mod_rewrite( $expected, $apache_loaded, $filter_value = // without a framework, we rely on the filter for full control. if ( null !== $filter_value ) { - add_filter( 'got_rewrite', array( $this, 'filter_got_rewrite' ) ); - $this->temp_filter_value = $filter_value; + add_filter( 'got_rewrite', function() use ( $filter_value ) { + return $filter_value; + } ); } // If we are NOT filtering, we need to be aware of the environment. @@ -38,11 +39,6 @@ public function test_got_mod_rewrite( $expected, $apache_loaded, $filter_value = // we use the filter to simulate the different outcomes of the internal check. $this->assertSame( $expected, got_mod_rewrite() ); - - if ( null !== $filter_value ) { - remove_filter( 'got_rewrite', array( $this, 'filter_got_rewrite' ) ); - unset( $this->temp_filter_value ); - } } /** @@ -53,6 +49,12 @@ public function test_got_mod_rewrite( $expected, $apache_loaded, $filter_value = * @type bool $apache_loaded Whether mod_rewrite is loaded (simulated via filter). * @type bool|null $filter_value The value to return from the filter. * } + * + * @phpstan-return array */ public function data_got_mod_rewrite() { return array( @@ -73,19 +75,4 @@ public function data_got_mod_rewrite() { ), ); } - - /** - * Temporary property for filter value. - * @var bool - */ - private $temp_filter_value; - - /** - * Filter callback for 'got_rewrite'. - * - * @return bool - */ - public function filter_got_rewrite() { - return $this->temp_filter_value; - } } From 84a37f57f69a04d7712208f5816d37526ca1fd57 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 28 Apr 2026 15:49:25 -0400 Subject: [PATCH 4/7] Declare return type for `data_got_mod_rewrite()` in unit test. --- tests/phpunit/tests/admin/includes/misc/gotModRewrite.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/admin/includes/misc/gotModRewrite.php b/tests/phpunit/tests/admin/includes/misc/gotModRewrite.php index d3198854cc404..6f4d3949420da 100644 --- a/tests/phpunit/tests/admin/includes/misc/gotModRewrite.php +++ b/tests/phpunit/tests/admin/includes/misc/gotModRewrite.php @@ -10,6 +10,11 @@ */ class Tests_got_mod_rewrite extends WP_UnitTestCase { + public function tear_down() { + remove_all_filters( 'got_rewrite' ); + parent::tear_down(); + } + /** * Tests that got_mod_rewrite() correctly detects mod_rewrite based on server and filters. * @@ -56,7 +61,7 @@ public function test_got_mod_rewrite( $expected, $apache_loaded, $filter_value = * filter_value: bool|null, * }> */ - public function data_got_mod_rewrite() { + public function data_got_mod_rewrite(): array { return array( 'Default behavior (should match filter or internal check)' => array( 'expected' => true, From 719bd4df85985623998b72a34b46cecaedf491c3 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 28 Apr 2026 15:58:58 -0400 Subject: [PATCH 5/7] Remove unused `tear_down()` method in gotModRewrite unit test. --- tests/phpunit/tests/admin/includes/misc/gotModRewrite.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/phpunit/tests/admin/includes/misc/gotModRewrite.php b/tests/phpunit/tests/admin/includes/misc/gotModRewrite.php index 6f4d3949420da..445a0ca828962 100644 --- a/tests/phpunit/tests/admin/includes/misc/gotModRewrite.php +++ b/tests/phpunit/tests/admin/includes/misc/gotModRewrite.php @@ -10,11 +10,6 @@ */ class Tests_got_mod_rewrite extends WP_UnitTestCase { - public function tear_down() { - remove_all_filters( 'got_rewrite' ); - parent::tear_down(); - } - /** * Tests that got_mod_rewrite() correctly detects mod_rewrite based on server and filters. * From c25af28e4ef1caf6bbf96a134300052eded8b52c Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 28 Apr 2026 16:16:30 -0400 Subject: [PATCH 6/7] Simplify inline closure in `got_rewrite` filter within unit test. --- tests/phpunit/tests/admin/includes/misc/gotModRewrite.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/phpunit/tests/admin/includes/misc/gotModRewrite.php b/tests/phpunit/tests/admin/includes/misc/gotModRewrite.php index 445a0ca828962..5be9488d51ea0 100644 --- a/tests/phpunit/tests/admin/includes/misc/gotModRewrite.php +++ b/tests/phpunit/tests/admin/includes/misc/gotModRewrite.php @@ -27,9 +27,7 @@ public function test_got_mod_rewrite( $expected, $apache_loaded, $filter_value = // without a framework, we rely on the filter for full control. if ( null !== $filter_value ) { - add_filter( 'got_rewrite', function() use ( $filter_value ) { - return $filter_value; - } ); + add_filter( 'got_rewrite', function() use ( $filter_value ) { return $filter_value; } ); } // If we are NOT filtering, we need to be aware of the environment. From 9513cbc3850acdfee6eac67fb3738583f05c0b9a Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Wed, 29 Apr 2026 08:23:54 -0400 Subject: [PATCH 7/7] Apply suggestions from code review Co-authored-by: Weston Ruter --- .../tests/admin/includes/misc/gotModRewrite.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/tests/phpunit/tests/admin/includes/misc/gotModRewrite.php b/tests/phpunit/tests/admin/includes/misc/gotModRewrite.php index 5be9488d51ea0..c2968e1decc92 100644 --- a/tests/phpunit/tests/admin/includes/misc/gotModRewrite.php +++ b/tests/phpunit/tests/admin/includes/misc/gotModRewrite.php @@ -27,7 +27,12 @@ public function test_got_mod_rewrite( $expected, $apache_loaded, $filter_value = // without a framework, we rely on the filter for full control. if ( null !== $filter_value ) { - add_filter( 'got_rewrite', function() use ( $filter_value ) { return $filter_value; } ); + add_filter( + 'got_rewrite', + static function () use ( $filter_value ) { + return $filter_value; + } + ); } // If we are NOT filtering, we need to be aware of the environment. @@ -42,13 +47,7 @@ public function test_got_mod_rewrite( $expected, $apache_loaded, $filter_value = /** * Data provider for test_got_mod_rewrite. * - * @return array[] { - * @type bool $expected The expected result. - * @type bool $apache_loaded Whether mod_rewrite is loaded (simulated via filter). - * @type bool|null $filter_value The value to return from the filter. - * } - * - * @phpstan-return array