Media: Fix wp_unique_filename() to check for name collisions with all alternate file names when an image may be converted after uploading. This includes possible collinions with pre-existing images whose sub-sizes/thumbnails are regenerated.

Props ianmjones, azaozz.
Fixes #53668.

git-svn-id: https://develop.svn.wordpress.org/trunk@51653 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrew Ozz
2021-08-24 20:50:21 +00:00
parent d4abd5ca4a
commit 67f8702477
7 changed files with 269 additions and 39 deletions
+107 -3
View File
@@ -168,13 +168,21 @@ class Tests_Functions extends WP_UnitTestCase {
$testdir = DIR_TESTDATA . '/images/';
// Sanity check.
$this->assertSame( 'abcdefg.png', wp_unique_filename( $testdir, 'abcdefg.png' ), 'Sanitiy check failed' );
$this->assertSame( 'abcdefg.png', wp_unique_filename( $testdir, 'abcdefg.png' ), 'Test non-existing file, file name should be unchanged.' );
// Check number is appended for file already exists.
// Ensure correct images exist.
$this->assertFileExists( $testdir . 'test-image.png', 'Test image does not exist' );
$this->assertSame( 'test-image-1.png', wp_unique_filename( $testdir, 'test-image.png' ), 'Number not appended correctly' );
$this->assertFileDoesNotExist( $testdir . 'test-image-1.png' );
// Check number is appended if file already exists.
$this->assertSame( 'test-image-1.png', wp_unique_filename( $testdir, 'test-image.png' ), 'File name not unique, number not appended.' );
// Check file with uppercase extension.
$this->assertSame( 'test-image-1.png', wp_unique_filename( $testdir, 'test-image.PNG' ), 'File name with uppercase extension not unique, number not appended.' );
// Check file name with already added number.
$this->assertSame( 'test-image-2-1.gif', wp_unique_filename( $testdir, 'test-image-2.gif' ), 'File name not unique, number not appended correctly.' );
// Check special chars.
$this->assertSame( 'testtest-image.png', wp_unique_filename( $testdir, 'testtést-imagé.png' ), 'Filename with special chars failed' );
@@ -221,6 +229,88 @@ class Tests_Functions extends WP_UnitTestCase {
return $upload_dir;
}
/**
* @ticket 53668
*/
function test_wp_unique_filename_with_additional_image_extension() {
$testdir = DIR_TESTDATA . '/images/';
add_filter( 'upload_dir', array( $this, 'upload_dir_patch_basedir' ) );
// Set conversions for uploaded images.
add_filter( 'image_editor_output_format', array( $this, 'image_editor_output_format_handler' ) );
// Ensure the test images exist.
$this->assertFileExists( $testdir . 'test-image-1-100x100.jpg', 'test-image-1-100x100.jpg does not exist' );
$this->assertFileExists( $testdir . 'test-image-2.gif', 'test-image-2.gif does not exist' );
$this->assertFileExists( $testdir . 'test-image-3.jpg', 'test-image-3.jpg does not exist' );
$this->assertFileExists( $testdir . 'test-image-4.png', 'test-image-4.png does not exist' );
// Standard test: file does not exist and there are no possible intersections with other files.
$this->assertSame(
'abcdef.png',
wp_unique_filename( $testdir, 'abcdef.png' ),
'The abcdef.png, abcdef.gif, and abcdef.jpg images do not exist. The file name should not be changed.'
);
// Actual clash recognized.
$this->assertSame(
'canola-1.jpg',
wp_unique_filename( $testdir, 'canola.jpg' ),
'The canola.jpg image exists. The file name should be unique.'
);
// Same name with different extension and the image will be converted.
$this->assertSame(
'canola-1.png',
wp_unique_filename( $testdir, 'canola.png' ),
'The canola.jpg image exists. Uploading canola.png that will be converted to canola.jpg should produce unique file name.'
);
// Same name with different uppercase extension and the image will be converted.
$this->assertSame(
'canola-1.png',
wp_unique_filename( $testdir, 'canola.PNG' ),
'The canola.jpg image exists. Uploading canola.PNG that will be converted to canola.jpg should produce unique file name.'
);
// Actual clash with several images with different extensions.
$this->assertSame(
'test-image-5.png',
wp_unique_filename( $testdir, 'test-image.png' ),
'The test-image.png, test-image-1-100x100.jpg, test-image-2.gif, test-image-3.jpg, and test-image-4.png images exist.' .
'All of them may clash when creating sub-sizes or regenerating thumbnails in the future. The filename should be unique.'
);
// Possible clash with regenerated thumbnails in the future.
$this->assertSame(
'codeispoetry-1.jpg',
wp_unique_filename( $testdir, 'codeispoetry.jpg' ),
'The codeispoetry.png image exists. When regenerating thumbnails for it they will be converted to JPG.' .
'The name of the newly uploaded codeispoetry.jpg should be made unique.'
);
remove_filter( 'image_editor_output_format', array( $this, 'image_editor_output_format_handler' ) );
remove_filter( 'upload_dir', array( $this, 'upload_dir_patch_basedir' ) );
}
/**
* Changes the output format when editing images. When uploading a PNG file
* it will be converted to JPEG, GIF to JPEG, and PICT to BMP
* (if the image editor in PHP supports it).
*
* @param array $formats
*
* @return array
*/
public function image_editor_output_format_handler( $formats ) {
$formats['image/png'] = 'image/jpeg';
$formats['image/gif'] = 'image/jpeg';
$formats['image/pct'] = 'image/bmp';
return $formats;
}
/**
* @dataProvider data_is_not_serialized
*/
@@ -1946,4 +2036,18 @@ class Tests_Functions extends WP_UnitTestCase {
array( 'application/activity+json, application/nojson', true ),
);
}
/**
* @ticket 53668
*/
public function test_wp_get_default_extension_for_mime_type() {
$this->assertEquals( 'jpg', wp_get_default_extension_for_mime_type( 'image/jpeg' ), 'jpg not returned as default extension for "image/jpeg"' );
$this->assertNotEquals( 'jpeg', wp_get_default_extension_for_mime_type( 'image/jpeg' ), 'jpeg should not be returned as default extension for "image/jpeg"' );
$this->assertEquals( 'png', wp_get_default_extension_for_mime_type( 'image/png' ), 'png not returned as default extension for "image/png"' );
$this->assertFalse( wp_get_default_extension_for_mime_type( 'wibble/wobble' ), 'false not returned for unrecognized mime type' );
$this->assertFalse( wp_get_default_extension_for_mime_type( '' ), 'false not returned when empty string as mime type supplied' );
$this->assertFalse( wp_get_default_extension_for_mime_type( ' ' ), 'false not returned when empty string as mime type supplied' );
$this->assertFalse( wp_get_default_extension_for_mime_type( 123 ), 'false not returned when int as mime type supplied' );
$this->assertFalse( wp_get_default_extension_for_mime_type( null ), 'false not returned when null as mime type supplied' );
}
}