| 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 | } |