Make WordPress Core


Ignore:
Timestamp:
08/31/2021 11:56:11 PM (3 years ago)
Author:
desrosj
Message:

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.
Merges [51653] to the 5.8 branch.
Fixes #53668.

Location:
branches/5.8
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/5.8

  • branches/5.8/tests/phpunit/tests/functions.php

    r50814 r51706  
    169169
    170170        // Sanity check.
    171         $this->assertSame( 'abcdefg.png', wp_unique_filename( $testdir, 'abcdefg.png' ), 'Sanitiy check failed' );
    172 
    173         // Check number is appended for file already exists.
     171        $this->assertSame( 'abcdefg.png', wp_unique_filename( $testdir, 'abcdefg.png' ), 'Test non-existing file, file name should be unchanged.' );
     172
     173        // Ensure correct images exist.
    174174        $this->assertFileExists( $testdir . 'test-image.png', 'Test image does not exist' );
    175         $this->assertSame( 'test-image-1.png', wp_unique_filename( $testdir, 'test-image.png' ), 'Number not appended correctly' );
    176175        $this->assertFileNotExists( $testdir . 'test-image-1.png' );
     176
     177        // Check number is appended if file already exists.
     178        $this->assertSame( 'test-image-1.png', wp_unique_filename( $testdir, 'test-image.png' ), 'File name not unique, number not appended.' );
     179
     180        // Check file with uppercase extension.
     181        $this->assertSame( 'test-image-1.png', wp_unique_filename( $testdir, 'test-image.PNG' ), 'File name with uppercase extension not unique, number not appended.' );
     182
     183        // Check file name with already added number.
     184        $this->assertSame( 'test-image-2-1.gif', wp_unique_filename( $testdir, 'test-image-2.gif' ), 'File name not unique, number not appended correctly.' );
    177185
    178186        // Check special chars.
     
    220228        $upload_dir['basedir'] = DIR_TESTDATA . '/images/';
    221229        return $upload_dir;
     230    }
     231
     232    /**
     233     * @ticket 53668
     234     */
     235    function test_wp_unique_filename_with_additional_image_extension() {
     236        $testdir = DIR_TESTDATA . '/images/';
     237
     238        add_filter( 'upload_dir', array( $this, 'upload_dir_patch_basedir' ) );
     239
     240        // Set conversions for uploaded images.
     241        add_filter( 'image_editor_output_format', array( $this, 'image_editor_output_format_handler' ) );
     242
     243        // Ensure the test images exist.
     244        $this->assertFileExists( $testdir . 'test-image-1-100x100.jpg', 'test-image-1-100x100.jpg does not exist' );
     245        $this->assertFileExists( $testdir . 'test-image-2.gif', 'test-image-2.gif does not exist' );
     246        $this->assertFileExists( $testdir . 'test-image-3.jpg', 'test-image-3.jpg does not exist' );
     247        $this->assertFileExists( $testdir . 'test-image-4.png', 'test-image-4.png does not exist' );
     248
     249        // Standard test: file does not exist and there are no possible intersections with other files.
     250        $this->assertSame(
     251            'abcdef.png',
     252            wp_unique_filename( $testdir, 'abcdef.png' ),
     253            'The abcdef.png, abcdef.gif, and abcdef.jpg images do not exist. The file name should not be changed.'
     254        );
     255
     256        // Actual clash recognized.
     257        $this->assertSame(
     258            'canola-1.jpg',
     259            wp_unique_filename( $testdir, 'canola.jpg' ),
     260            'The canola.jpg image exists. The file name should be unique.'
     261        );
     262
     263        // Same name with different extension and the image will be converted.
     264        $this->assertSame(
     265            'canola-1.png',
     266            wp_unique_filename( $testdir, 'canola.png' ),
     267            'The canola.jpg image exists. Uploading canola.png that will be converted to canola.jpg should produce unique file name.'
     268        );
     269
     270        // Same name with different uppercase extension and the image will be converted.
     271        $this->assertSame(
     272            'canola-1.png',
     273            wp_unique_filename( $testdir, 'canola.PNG' ),
     274            'The canola.jpg image exists. Uploading canola.PNG that will be converted to canola.jpg should produce unique file name.'
     275        );
     276
     277        // Actual clash with several images with different extensions.
     278        $this->assertSame(
     279            'test-image-5.png',
     280            wp_unique_filename( $testdir, 'test-image.png' ),
     281            'The test-image.png, test-image-1-100x100.jpg, test-image-2.gif, test-image-3.jpg, and test-image-4.png images exist.' .
     282            'All of them may clash when creating sub-sizes or regenerating thumbnails in the future. The filename should be unique.'
     283        );
     284
     285        // Possible clash with regenerated thumbnails in the future.
     286        $this->assertSame(
     287            'codeispoetry-1.jpg',
     288            wp_unique_filename( $testdir, 'codeispoetry.jpg' ),
     289            'The codeispoetry.png image exists. When regenerating thumbnails for it they will be converted to JPG.' .
     290            'The name of the newly uploaded codeispoetry.jpg should be made unique.'
     291        );
     292
     293        remove_filter( 'image_editor_output_format', array( $this, 'image_editor_output_format_handler' ) );
     294        remove_filter( 'upload_dir', array( $this, 'upload_dir_patch_basedir' ) );
     295    }
     296
     297    /**
     298     * Changes the output format when editing images. When uploading a PNG file
     299     * it will be converted to JPEG, GIF to JPEG, and PICT to BMP
     300     * (if the image editor in PHP supports it).
     301     *
     302     * @param array $formats
     303     *
     304     * @return array
     305     */
     306    public function image_editor_output_format_handler( $formats ) {
     307        $formats['image/png'] = 'image/jpeg';
     308        $formats['image/gif'] = 'image/jpeg';
     309        $formats['image/pct'] = 'image/bmp';
     310
     311        return $formats;
    222312    }
    223313
     
    19222012        );
    19232013    }
     2014
     2015    /**
     2016     * @ticket 53668
     2017     */
     2018    public function test_wp_get_default_extension_for_mime_type() {
     2019        $this->assertEquals( 'jpg', wp_get_default_extension_for_mime_type( 'image/jpeg' ), 'jpg not returned as default extension for "image/jpeg"' );
     2020        $this->assertNotEquals( 'jpeg', wp_get_default_extension_for_mime_type( 'image/jpeg' ), 'jpeg should not be returned as default extension for "image/jpeg"' );
     2021        $this->assertEquals( 'png', wp_get_default_extension_for_mime_type( 'image/png' ), 'png not returned as default extension for "image/png"' );
     2022        $this->assertFalse( wp_get_default_extension_for_mime_type( 'wibble/wobble' ), 'false not returned for unrecognized mime type' );
     2023        $this->assertFalse( wp_get_default_extension_for_mime_type( '' ), 'false not returned when empty string as mime type supplied' );
     2024        $this->assertFalse( wp_get_default_extension_for_mime_type( '   ' ), 'false not returned when empty string as mime type supplied' );
     2025        $this->assertFalse( wp_get_default_extension_for_mime_type( 123 ), 'false not returned when int as mime type supplied' );
     2026        $this->assertFalse( wp_get_default_extension_for_mime_type( null ), 'false not returned when null as mime type supplied' );
     2027    }
    19242028}
Note: See TracChangeset for help on using the changeset viewer.