Make WordPress Core

Ticket #37111: 37111.3.diff

File 37111.3.diff, 2.0 KB (added by joemcgill, 8 years ago)
  • src/wp-includes/formatting.php

    diff --git src/wp-includes/formatting.php src/wp-includes/formatting.php
    index 46d9a90..153b9db 100644
    function remove_accents( $string ) { 
    17251725 * operating systems and special characters requiring special escaping
    17261726 * to manipulate at the command line. Replaces spaces and consecutive
    17271727 * dashes with a single dash. Trims period, dash and underscore from beginning
    1728  * and end of filename.
     1728 * and end of filename. It is not guaranteed that this function will return a
     1729 * filename that is allowed to be uploaded.
    17291730 *
    17301731 * @since 2.1.0
    17311732 *
    function sanitize_file_name( $filename ) { 
    17501751        $filename = preg_replace( '/[\r\n\t -]+/', '-', $filename );
    17511752        $filename = trim( $filename, '.-_' );
    17521753
     1754        if ( false === strpos( $filename, '.' ) ) {
     1755                $mime_types = wp_get_mime_types();
     1756                $filetype = wp_check_filetype( 'test.' . $filename, $mime_types );
     1757                if ( $filetype['ext'] === $filename ) {
     1758                        $filename = 'unnamed-file.' . $filetype['ext'];
     1759                }
     1760        }
     1761
    17531762        // Split the filename into a base and extension[s]
    17541763        $parts = explode('.', $filename);
    17551764
  • tests/phpunit/tests/formatting/SanitizeFileName.php

    diff --git tests/phpunit/tests/formatting/SanitizeFileName.php tests/phpunit/tests/formatting/SanitizeFileName.php
    index 8927fec..d26b871 100644
    class Tests_Formatting_SanitizeFileName extends WP_UnitTestCase { 
    5656        function test_replaces_percent_sign() {
    5757                $this->assertEquals( 'a22b.jpg', sanitize_file_name( 'a%22b.jpg' ) );
    5858        }
     59
     60        function test_replaces_unnammed_file_extensions() {
     61                // Test filenames with both supported and unsupported extensions.
     62                $this->assertEquals( 'unnamed-file.exe', sanitize_file_name( '_.exe' ) );
     63                $this->assertEquals( 'unnamed-file.jpg', sanitize_file_name( '_.jpg' ) );
     64        }
     65
     66        function test_replaces_unnammed_file_extensionless() {
     67                // Test a filenames that becomes extensionless.
     68                $this->assertEquals( 'no-extension', sanitize_file_name( '_.no-extension' ) );
     69        }
    5970}