Make WordPress Core

Ticket #22363: test-SanitizeFileName.patch

File test-SanitizeFileName.patch, 3.0 KB (added by p_enrique, 12 years ago)

Updated unit test for sanitize_file_name()

  • tests/phpunit/tests/formatting/SanitizeFileName.php

     
    1616                foreach ( $special_chars as $char )
    1717                        $string .= $char;
    1818                $string .= 'test';
    19                 $this->assertEquals( 'testtest', sanitize_file_name( $string ) );
     19                $this->assertEquals( 'test-test', sanitize_file_name( $string ) );
    2020        }
    2121
    2222        function test_replaces_any_number_of_hyphens_with_one_hyphen() {
     
    2929
    3030        function test_replaces_any_amount_of_whitespace_with_one_hyphen() {
    3131                $this->assertEquals("a-t", sanitize_file_name("a          t"));
    32                 $this->assertEquals("a-t", sanitize_file_name("a    \n\n\nt"));
     32                $this->assertEquals("a-t", sanitize_file_name("a    \t\r\n\n\nt"));
    3333        }
     34       
     35        function pcre_utf8_support() {
     36                static $support = null;
     37                if ( is_null($support) )
     38                        $support = ( 1 === @preg_match( '`[\p{L}]`u', "\xc3\xa0" ) );
     39                return $support;
     40        }
     41       
     42        function test_replaces_utf8_whitespace() {
     43                if ( ! $this->pcre_utf8_support() )
     44                        $this->markTestSkipped();
     45                $this->assertEquals("non-breaking", sanitize_file_name("non\xc2\xa0breaking"));
     46        }
     47       
     48        function test_returns_lowercase() {
     49                if ( ! function_exists( 'mb_strtolower' ) )
     50                        $this->markTestSkipped();
     51                $this->assertEquals( 'abcd', sanitize_file_name('ABCD') );
     52        }
     53       
     54        function test_replaces_accents() {
     55                $in  = 'àáâãäåæçèéêëìíîïñòóôõöøùúûüýÿ';
     56                $out = 'aaaaaaaeceeeeiiiinoooooouuuuyy';
     57                $this->assertEquals( $out, sanitize_file_name( $in ) );
     58        }
     59       
     60        function test_strips_non_alpha_html_entities() {
     61                if ( ! $this->pcre_utf8_support() )
     62                        $this->markTestSkipped();
     63                $this->assertEquals("start-end", sanitize_file_name( "start © & € –   end" ) );
     64                $this->assertEquals("start-invalid-end", sanitize_file_name( "start &invalid; end" ) );
     65        }
     66       
     67        function test_converts_alpha_html_entities() {
     68                if ( ! $this->pcre_utf8_support() )
     69                        $this->markTestSkipped();
     70                $this->assertEquals("start-a-e-n-o-ae-end", sanitize_file_name("start à ê ñ ø æ end" ) );
     71        }
     72       
     73        function test_removes_non_alphanum_characters() {
     74                if ( ! $this->pcre_utf8_support() )
     75                        $this->markTestSkipped();
     76                $test_input = "start ¿”©“? «±€—°» middle “√¼ = ♫” & „☺ + ☻ = ♥‟ end";
     77                $this->assertEquals("start-middle-end", sanitize_file_name( $test_input ) );
     78        }
     79       
     80        function test_returns_empty_on_invalid_utf8() {
     81                if ( ! $this->pcre_utf8_support() )
     82                        $this->markTestSkipped();
     83                $this->assertEquals( '', sanitize_file_name( "Invalid \xff utf8" ) );
     84        }
     85       
     86        function test_returns_lowercase_utf8() {
     87                if ( ! $this->pcre_utf8_support() || ! function_exists( 'mb_strtolower' ) )
     88                        $this->markTestSkipped();
     89                $this->assertEquals( 'βασιλ-правах', sanitize_file_name( 'ΒΑΣΙΛ ПРАВАХ' ) );
     90        }
    3491}