When running on windows systems, normalise the capitalisation of the drive letter for more reliable string comparisons.

Props tyxla
Fixes #33265


git-svn-id: https://develop.svn.wordpress.org/trunk@34104 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Dion Hulse 2015-09-14 01:58:03 +00:00
parent b52b5893c4
commit 9555882e90
2 changed files with 31 additions and 2 deletions

View File

@ -1575,10 +1575,12 @@ function path_join( $base, $path ) {
/** /**
* Normalize a filesystem path. * Normalize a filesystem path.
* *
* Replaces backslashes with forward slashes for Windows systems, and ensures * On windows systems, replaces backslashes with forward slashes
* no duplicate slashes exist. * and forces upper-case drive letters.
* Ensures that no duplicate slashes exist.
* *
* @since 3.9.0 * @since 3.9.0
* @since 4.4.0 Ensures upper-case drive letters on Windows systems.
* *
* @param string $path Path to normalize. * @param string $path Path to normalize.
* @return string Normalized path. * @return string Normalized path.
@ -1586,6 +1588,9 @@ function path_join( $base, $path ) {
function wp_normalize_path( $path ) { function wp_normalize_path( $path ) {
$path = str_replace( '\\', '/', $path ); $path = str_replace( '\\', '/', $path );
$path = preg_replace( '|/+|','/', $path ); $path = preg_replace( '|/+|','/', $path );
if ( ':' === substr( $path, 1, 1 ) ) {
$path = ucfirst( $path );
}
return $path; return $path;
} }

View File

@ -113,6 +113,30 @@ class Tests_Functions extends WP_UnitTestCase {
$this->assertFalse( path_is_absolute($path), "path_is_absolute('$path') should return false" ); $this->assertFalse( path_is_absolute($path), "path_is_absolute('$path') should return false" );
} }
/**
* @ticket 33265
*/
function test_wp_normalize_path() {
$paths = array(
'/WINDOWS' => '/WINDOWS',
'C:/' => 'C:/',
'C:/WINDOWS' => 'C:/WINDOWS',
'C:/WINDOWS/system32' => 'C:/WINDOWS/system32',
'\\WINDOWS' => '/WINDOWS',
'C:\\' => 'C:/',
'C:\\WINDOWS' => 'C:/WINDOWS',
'C:\\\\WINDOWS' => 'C:/WINDOWS',
'C:\\WINDOWS\\system32' => 'C:/WINDOWS/system32',
'\\\\sambashare\\foo' => '/sambashare/foo',
'c:/windows' => 'C:/windows',
'c:\\windows' => 'C:/windows',
);
foreach ($paths as $original => $expected) {
$this->assertEquals( $expected, wp_normalize_path( $original ) );
}
}
function test_wp_unique_filename() { function test_wp_unique_filename() {
$testdir = DIR_TESTDATA . '/images/'; $testdir = DIR_TESTDATA . '/images/';