diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php index f2978644faf2a..224929eb377a8 100644 --- a/tests/phpunit/includes/abstract-testcase.php +++ b/tests/phpunit/includes/abstract-testcase.php @@ -1070,6 +1070,40 @@ public function assertNonEmptyMultidimensionalArray( $actual, $message = '' ) { } } + /** + * Assert that two text strings representing file paths are the same, while ignoring + * OS-specific differences in the directory separators. + * + * This allows for tests to be compatible for running on both *nix based as well as Windows OS. + * + * @since 6.7.0 + * + * @param string $path_a File or directory path. + * @param string $path_b File or directory path. + */ + public function assertSamePathIgnoringDirectorySeparators( $path_a, $path_b ) { + $path_a = $this->normalizeDirectorySeparatorsInPath( $path_a ); + $path_b = $this->normalizeDirectorySeparatorsInPath( $path_b ); + + $this->assertSame( $path_a, $path_b ); + } + + /** + * Normalize directory separators in a file path to be a forward slash. + * + * @since 6.7.0 + * + * @param string $path File or directory path. + * @return string The normalized file or directory path. + */ + public function normalizeDirectorySeparatorsInPath( $path ) { + if ( ! is_string( $path ) || PHP_OS_FAMILY !== 'Windows' ) { + return $path; + } + + return strtr( $path, '\\', '/' ); + } + /** * Checks each of the WP_Query is_* functions/properties against expected boolean value. * diff --git a/tests/phpunit/tests/admin/includesFile.php b/tests/phpunit/tests/admin/includesFile.php index af65644a20f1a..4bfe80db6fc5d 100644 --- a/tests/phpunit/tests/admin/includesFile.php +++ b/tests/phpunit/tests/admin/includesFile.php @@ -15,7 +15,7 @@ public function test_get_home_path() { $home = get_option( 'home' ); $siteurl = get_option( 'siteurl' ); $sfn = $_SERVER['SCRIPT_FILENAME']; - $this->assertSame( str_replace( '\\', '/', ABSPATH ), get_home_path() ); + $this->assertSamePathIgnoringDirectorySeparators( ABSPATH, get_home_path() ); update_option( 'home', 'http://localhost' ); update_option( 'siteurl', 'http://localhost/wp' ); diff --git a/tests/phpunit/tests/block-template.php b/tests/phpunit/tests/block-template.php index 708c7be779ef3..7b91b3f2ea185 100644 --- a/tests/phpunit/tests/block-template.php +++ b/tests/phpunit/tests/block-template.php @@ -395,7 +395,7 @@ public function data_get_block_theme_folders() { * @covers ::_get_block_templates_paths */ public function test_get_block_templates_paths_dir_exists() { - $theme_dir = get_template_directory(); + $theme_dir = $this->normalizeDirectorySeparatorsInPath( get_template_directory() ); // Templates in the current theme. $templates = array( 'parts/small-header.html', @@ -415,6 +415,8 @@ static function ( $template ) use ( $theme_dir ) { ); $template_paths = _get_block_templates_paths( $theme_dir ); + $template_paths = array_map( array( $this, 'normalizeDirectorySeparatorsInPath' ), _get_block_templates_paths( $theme_dir ) ); + $this->assertSameSets( $expected_template_paths, $template_paths ); } diff --git a/tests/phpunit/tests/filesystem/wpFilesystemDirect/chdir.php b/tests/phpunit/tests/filesystem/wpFilesystemDirect/chdir.php index cdea47928b2f2..8d70acc3b76e1 100644 --- a/tests/phpunit/tests/filesystem/wpFilesystemDirect/chdir.php +++ b/tests/phpunit/tests/filesystem/wpFilesystemDirect/chdir.php @@ -86,7 +86,7 @@ public function test_should_change_directory() { 'Changing working directory failed.' ); - $this->assertSame( + $this->assertSamePathIgnoringDirectorySeparators( $path, $cwd_result, 'The current working directory was incorrect.' diff --git a/tests/phpunit/tests/theme.php b/tests/phpunit/tests/theme.php index e12bf159bf5f0..5f9ef3959ead1 100644 --- a/tests/phpunit/tests/theme.php +++ b/tests/phpunit/tests/theme.php @@ -925,7 +925,7 @@ public function test_is_child_theme_false() { */ public function test_get_stylesheet_directory() { switch_theme( 'block-theme-child' ); - $this->assertSame( realpath( DIR_TESTDATA ) . '/themedir1/block-theme-child', get_stylesheet_directory() ); + $this->assertSamePathIgnoringDirectorySeparators( realpath( DIR_TESTDATA ) . '/themedir1/block-theme-child', get_stylesheet_directory() ); } /** @@ -937,7 +937,7 @@ public function test_get_stylesheet_directory() { */ public function test_get_template_directory() { switch_theme( 'block-theme-child' ); - $this->assertSame( realpath( DIR_TESTDATA ) . '/themedir1/block-theme', get_template_directory() ); + $this->assertSamePathIgnoringDirectorySeparators( realpath( DIR_TESTDATA ) . '/themedir1/block-theme', get_template_directory() ); } /**