From 6dbeec0b1b9c443ebd9270742ce41d6a863ea3a9 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Mon, 22 Jan 2024 13:48:15 -0500 Subject: [PATCH 1/6] Add tests for wp_spaces_regexp function A new PHPUnit test file is added to check the functionality of the wp_spaces_regexp function in a WordPress application. These tests verify the return value of the function with given input and confirm the filter instantiation calls. --- .../tests/formatting/wpSpacesRegexp.php | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 tests/phpunit/tests/formatting/wpSpacesRegexp.php diff --git a/tests/phpunit/tests/formatting/wpSpacesRegexp.php b/tests/phpunit/tests/formatting/wpSpacesRegexp.php new file mode 100644 index 0000000000000..7629f6f4a2607 --- /dev/null +++ b/tests/phpunit/tests/formatting/wpSpacesRegexp.php @@ -0,0 +1,23 @@ +assertSame( '[\r\n\t ]|\xC2\xA0| ', wp_spaces_regexp() ); + // filter not call as this is set up in the init call for site + $this->assertSame( 0, $filter->get_call_count(), '1st call' ); + } +} From 98bd7de15a209940af0356ac9adb2f9f016367d5 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Mon, 2 Nov 2026 18:52:27 -0500 Subject: [PATCH 2/6] Refactor and expand `wp_spaces_regexp()` tests to cover cold start and warmed cache scenarios. --- .../tests/formatting/wpSpacesRegexp.php | 42 ++++++++++++++++--- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/tests/formatting/wpSpacesRegexp.php b/tests/phpunit/tests/formatting/wpSpacesRegexp.php index 7629f6f4a2607..23360f893694b 100644 --- a/tests/phpunit/tests/formatting/wpSpacesRegexp.php +++ b/tests/phpunit/tests/formatting/wpSpacesRegexp.php @@ -1,23 +1,53 @@ assertSame( $custom_pattern, wp_spaces_regexp(), 'First call should return filtered pattern.' ); + $this->assertSame( 1, $filter_calls, 'Filter should be called exactly once on first invocation.' ); + + // Subsequent invocations should return cached result without calling the filter again. + $this->assertSame( $custom_pattern, wp_spaces_regexp(), 'Second call should return cached filtered pattern.' ); + $this->assertSame( 1, $filter_calls, 'Filter should not be called again on subsequent invocations.' ); + } + + /** + * Tests that wp_spaces_regexp() returns the default pattern and uses the static cache. + * + * @ticket 60319 + */ + public function test_wp_spaces_regexp_warmed_cache(): void { $filter = new MockAction(); add_filter( 'wp_spaces_regexp', array( $filter, 'filter' ) ); - $this->assertSame( '[\r\n\t ]|\xC2\xA0| ', wp_spaces_regexp() ); - // filter not call as this is set up in the init call for site - $this->assertSame( 0, $filter->get_call_count(), '1st call' ); + $this->assertSame( '[\r\n\t ]|\xC2\xA0| ', wp_spaces_regexp(), 'Should return default regex pattern.' ); + $this->assertSame( 0, $filter->get_call_count(), 'Filter should not be called when static cache is already populated.' ); } } From 913ca612642203c7c66e538d0dd6abdb5e34d570 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Mon, 2 Nov 2026 19:02:18 -0500 Subject: [PATCH 3/6] Refactor `wp_spaces_regexp()` cold start test to use isolated subprocess execution. --- .../tests/formatting/wpSpacesRegexp.php | 33 +++++++++---------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/tests/phpunit/tests/formatting/wpSpacesRegexp.php b/tests/phpunit/tests/formatting/wpSpacesRegexp.php index 23360f893694b..894cc1b7d1404 100644 --- a/tests/phpunit/tests/formatting/wpSpacesRegexp.php +++ b/tests/phpunit/tests/formatting/wpSpacesRegexp.php @@ -13,29 +13,26 @@ class Tests_Formatting_WpSpacesRegexp extends WP_UnitTestCase { * Tests wp_spaces_regexp() filter behavior during cold start (before static cache is populated). * * @ticket 60319 - * - * @runInSeparateProcess - * @preserveGlobalState disabled */ public function test_wp_spaces_regexp_cold_start(): void { - $custom_pattern = '[ ]'; - $filter_calls = 0; - - add_filter( - 'wp_spaces_regexp', - static function ( $pattern ) use ( &$filter_calls, $custom_pattern ) { - ++$filter_calls; - return $custom_pattern; - } + $php_binary = defined( 'WP_PHP_BINARY' ) ? WP_PHP_BINARY : PHP_BINARY; + + $php_code = sprintf( + 'require %s; require %s; $filter_calls = 0; add_filter( "wp_spaces_regexp", static function ( $pattern ) use ( &$filter_calls ) { ++$filter_calls; return "[ ]"; } ); $first = wp_spaces_regexp(); $second = wp_spaces_regexp(); echo json_encode( array( "first" => $first, "second" => $second, "calls" => $filter_calls ) );', + var_export( ABSPATH . WPINC . '/plugin.php', true ), + var_export( ABSPATH . WPINC . '/formatting.php', true ) ); - // First invocation should apply the filter and cache the result. - $this->assertSame( $custom_pattern, wp_spaces_regexp(), 'First call should return filtered pattern.' ); - $this->assertSame( 1, $filter_calls, 'Filter should be called exactly once on first invocation.' ); + $command = escapeshellcmd( $php_binary ) . ' -r ' . escapeshellarg( $php_code ); + exec( $command, $output, $return_var ); + + $this->assertSame( 0, $return_var, 'PHP execution should succeed.' ); - // Subsequent invocations should return cached result without calling the filter again. - $this->assertSame( $custom_pattern, wp_spaces_regexp(), 'Second call should return cached filtered pattern.' ); - $this->assertSame( 1, $filter_calls, 'Filter should not be called again on subsequent invocations.' ); + $result = json_decode( implode( "\n", $output ), true ); + $this->assertIsArray( $result, 'Subprocess output should decode to an array.' ); + $this->assertSame( '[ ]', $result['first'], 'First call should return filtered pattern.' ); + $this->assertSame( 1, $result['calls'], 'Filter should be called exactly once on first invocation.' ); + $this->assertSame( '[ ]', $result['second'], 'Second call should return cached filtered pattern.' ); } /** From 34c27cf9607de5d1cac8c1f4b2892daf7852141a Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Wed, 25 Nov 2026 18:02:12 -0500 Subject: [PATCH 4/6] Remove redundant `wp_spaces_regexp()` warmed-cache test --- tests/phpunit/tests/formatting/wpSpacesRegexp.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/tests/phpunit/tests/formatting/wpSpacesRegexp.php b/tests/phpunit/tests/formatting/wpSpacesRegexp.php index 894cc1b7d1404..4183cd937a031 100644 --- a/tests/phpunit/tests/formatting/wpSpacesRegexp.php +++ b/tests/phpunit/tests/formatting/wpSpacesRegexp.php @@ -35,16 +35,4 @@ public function test_wp_spaces_regexp_cold_start(): void { $this->assertSame( '[ ]', $result['second'], 'Second call should return cached filtered pattern.' ); } - /** - * Tests that wp_spaces_regexp() returns the default pattern and uses the static cache. - * - * @ticket 60319 - */ - public function test_wp_spaces_regexp_warmed_cache(): void { - $filter = new MockAction(); - add_filter( 'wp_spaces_regexp', array( $filter, 'filter' ) ); - - $this->assertSame( '[\r\n\t ]|\xC2\xA0| ', wp_spaces_regexp(), 'Should return default regex pattern.' ); - $this->assertSame( 0, $filter->get_call_count(), 'Filter should not be called when static cache is already populated.' ); - } } From b91625025f6cba33e9f4c8f44cf4208f61fb3b4c Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 26 Nov 2026 14:42:47 -0500 Subject: [PATCH 5/6] Refactor wpSpacesRegexp test to use WP_PHP_BINARY Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- tests/phpunit/tests/formatting/wpSpacesRegexp.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/formatting/wpSpacesRegexp.php b/tests/phpunit/tests/formatting/wpSpacesRegexp.php index 4183cd937a031..7a0e8dfc7f6f2 100644 --- a/tests/phpunit/tests/formatting/wpSpacesRegexp.php +++ b/tests/phpunit/tests/formatting/wpSpacesRegexp.php @@ -18,18 +18,19 @@ public function test_wp_spaces_regexp_cold_start(): void { $php_binary = defined( 'WP_PHP_BINARY' ) ? WP_PHP_BINARY : PHP_BINARY; $php_code = sprintf( - 'require %s; require %s; $filter_calls = 0; add_filter( "wp_spaces_regexp", static function ( $pattern ) use ( &$filter_calls ) { ++$filter_calls; return "[ ]"; } ); $first = wp_spaces_regexp(); $second = wp_spaces_regexp(); echo json_encode( array( "first" => $first, "second" => $second, "calls" => $filter_calls ) );', + 'require %s; require %s; $filter_calls = 0; $initial_pattern = null; add_filter( "wp_spaces_regexp", static function ( $pattern ) use ( &$filter_calls, &$initial_pattern ) { ++$filter_calls; $initial_pattern = $pattern; return "[ ]"; } ); $first = wp_spaces_regexp(); $second = wp_spaces_regexp(); echo json_encode( array( "initial" => $initial_pattern, "first" => $first, "second" => $second, "calls" => $filter_calls ) );', var_export( ABSPATH . WPINC . '/plugin.php', true ), var_export( ABSPATH . WPINC . '/formatting.php', true ) ); - $command = escapeshellcmd( $php_binary ) . ' -r ' . escapeshellarg( $php_code ); + $command = ( defined( 'WP_PHP_BINARY' ) ? $php_binary : escapeshellarg( $php_binary ) ) . ' -r ' . escapeshellarg( $php_code ); exec( $command, $output, $return_var ); $this->assertSame( 0, $return_var, 'PHP execution should succeed.' ); $result = json_decode( implode( "\n", $output ), true ); $this->assertIsArray( $result, 'Subprocess output should decode to an array.' ); + $this->assertSame( '[\r\n\t ]|\xC2\xA0| ', $result['initial'], 'Filter should receive the default whitespace pattern.' ); $this->assertSame( '[ ]', $result['first'], 'First call should return filtered pattern.' ); $this->assertSame( 1, $result['calls'], 'Filter should be called exactly once on first invocation.' ); $this->assertSame( '[ ]', $result['second'], 'Second call should return cached filtered pattern.' ); From 9b5bf442a114a38d330311c6603806e39c23d684 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 26 Nov 2026 14:45:04 -0500 Subject: [PATCH 6/6] Fix `wp_spaces_regexp()` cold start test subprocess Skip the test when `exec()` is unavailable and correctly quote the fallback PHP binary. Remove the assertion for the initial pattern passed to the filter. --- tests/phpunit/tests/formatting/wpSpacesRegexp.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/tests/formatting/wpSpacesRegexp.php b/tests/phpunit/tests/formatting/wpSpacesRegexp.php index 7a0e8dfc7f6f2..7c0bf31188768 100644 --- a/tests/phpunit/tests/formatting/wpSpacesRegexp.php +++ b/tests/phpunit/tests/formatting/wpSpacesRegexp.php @@ -15,25 +15,27 @@ class Tests_Formatting_WpSpacesRegexp extends WP_UnitTestCase { * @ticket 60319 */ public function test_wp_spaces_regexp_cold_start(): void { - $php_binary = defined( 'WP_PHP_BINARY' ) ? WP_PHP_BINARY : PHP_BINARY; + if ( ! function_exists( 'exec' ) ) { + $this->markTestSkipped( 'The exec() function is not available.' ); + } + + $php_binary = defined( 'WP_PHP_BINARY' ) ? WP_PHP_BINARY : escapeshellarg( PHP_BINARY ); $php_code = sprintf( - 'require %s; require %s; $filter_calls = 0; $initial_pattern = null; add_filter( "wp_spaces_regexp", static function ( $pattern ) use ( &$filter_calls, &$initial_pattern ) { ++$filter_calls; $initial_pattern = $pattern; return "[ ]"; } ); $first = wp_spaces_regexp(); $second = wp_spaces_regexp(); echo json_encode( array( "initial" => $initial_pattern, "first" => $first, "second" => $second, "calls" => $filter_calls ) );', + 'require %s; require %s; $filter_calls = 0; add_filter( "wp_spaces_regexp", static function ( $pattern ) use ( &$filter_calls ) { ++$filter_calls; return "[ ]"; } ); $first = wp_spaces_regexp(); $second = wp_spaces_regexp(); echo json_encode( array( "first" => $first, "second" => $second, "calls" => $filter_calls ) );', var_export( ABSPATH . WPINC . '/plugin.php', true ), var_export( ABSPATH . WPINC . '/formatting.php', true ) ); - $command = ( defined( 'WP_PHP_BINARY' ) ? $php_binary : escapeshellarg( $php_binary ) ) . ' -r ' . escapeshellarg( $php_code ); + $command = $php_binary . ' -r ' . escapeshellarg( $php_code ); exec( $command, $output, $return_var ); $this->assertSame( 0, $return_var, 'PHP execution should succeed.' ); $result = json_decode( implode( "\n", $output ), true ); $this->assertIsArray( $result, 'Subprocess output should decode to an array.' ); - $this->assertSame( '[\r\n\t ]|\xC2\xA0| ', $result['initial'], 'Filter should receive the default whitespace pattern.' ); $this->assertSame( '[ ]', $result['first'], 'First call should return filtered pattern.' ); $this->assertSame( 1, $result['calls'], 'Filter should be called exactly once on first invocation.' ); $this->assertSame( '[ ]', $result['second'], 'Second call should return cached filtered pattern.' ); } - }