Skip to content
41 changes: 41 additions & 0 deletions tests/phpunit/tests/formatting/wpSpacesRegexp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* Tests for the wp_spaces_regexp() function.
*
* @group formatting
*
* @covers ::wp_spaces_regexp
*/
class Tests_Formatting_WpSpacesRegexp extends WP_UnitTestCase {

/**
* Tests wp_spaces_regexp() filter behavior during cold start (before static cache is populated).
*
* @ticket 60319
*/
public function test_wp_spaces_regexp_cold_start(): void {
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; 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 = $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( '[ ]', $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.' );
}
}
Loading