Add tests for Sanitizer::escapeId
[lhc/web/wiklou.git] / tests / phpunit / includes / SanitizerTest.php
index 12db1a1..6d093b0 100644 (file)
@@ -178,6 +178,10 @@ class SanitizerTest extends MediaWikiTestCase {
        public static function provideTagAttributesToDecode() {
                return [
                        [ [ 'foo' => 'bar' ], 'foo=bar', 'Unquoted attribute' ],
+                       [ [ 'עברית' => 'bar' ], 'עברית=bar', 'Non-Latin attribute' ],
+                       [ [ '६' => 'bar' ], '६=bar', 'Devanagari number' ],
+                       [ [ '搭𨋢' => 'bar' ], '搭𨋢=bar', 'Non-BMP character' ],
+                       [ [], 'ńgh=bar', 'Combining accent is not allowed' ],
                        [ [ 'foo' => 'bar' ], '    foo   =   bar    ', 'Spaced attribute' ],
                        [ [ 'foo' => 'bar' ], 'foo="bar"', 'Double-quoted attribute' ],
                        [ [ 'foo' => 'bar' ], 'foo=\'bar\'', 'Single-quoted attribute' ],
@@ -211,7 +215,7 @@ class SanitizerTest extends MediaWikiTestCase {
                        [ [ 'foo6' => 'baz' ], 'foo6=baz', 'Numbers are allowed' ],
 
                        # This bit is more relaxed than XML rules, but some extensions use
-                       # it, like ProofreadPage (see bug 27539)
+                       # it, like ProofreadPage (see T29539)
                        [ [ '1foo' => 'baz' ], '1foo=baz', 'Leading numbers are allowed' ],
                        [ [], 'foo$=baz', 'Symbols are not allowed' ],
                        [ [], 'foo@=baz', 'Symbols are not allowed' ],
@@ -286,7 +290,7 @@ class SanitizerTest extends MediaWikiTestCase {
                        [ ' ', '/* /* */' ],
                        [ 'display: block;', "display:/* foo */block;" ],
                        [ 'display: block;', "display:\\2f\\2a foo \\2a\\2f block;",
-                               'Backslash-escaped comments must be stripped (bug 28450)' ],
+                               'Backslash-escaped comments must be stripped (T30450)' ],
                        [ '', '/* unfinished comment structure',
                                'Remove anything after a comment-start token' ],
                        [ '', "\\2f\\2a unifinished comment'",
@@ -339,6 +343,41 @@ class SanitizerTest extends MediaWikiTestCase {
                ];
        }
 
+       /**
+        * Test Sanitizer::escapeId
+        *
+        * @dataProvider provideEscapeId
+        * @covers Sanitizer::escapeId
+        */
+       public function testEscapeId( $input, $output ) {
+               $this->assertEquals(
+                       $output,
+                       Sanitizer::escapeId( $input, [ 'noninitial', 'legacy' ] )
+               );
+       }
+
+       public static function provideEscapeId() {
+               return [
+                       [ '+', '.2B' ],
+                       [ '&', '.26' ],
+                       [ '=', '.3D' ],
+                       [ ':', ':' ],
+                       [ ';', '.3B' ],
+                       [ '@', '.40' ],
+                       [ '$', '.24' ],
+                       [ '-_.', '-_.' ],
+                       [ '!', '.21' ],
+                       [ '*', '.2A' ],
+                       [ '/', '.2F' ],
+                       [ '[]', '.5B.5D' ],
+                       [ '<>', '.3C.3E' ],
+                       [ '\'', '.27' ],
+                       [ '§', '.C2.A7' ],
+                       [ 'Test:A & B/Here', 'Test:A_.26_B.2FHere' ],
+                       [ 'A&B&amp;C&amp;amp;D&amp;amp;amp;E', 'A.26B.26C.26amp.3BD.26amp.3Bamp.3BE' ],
+               ];
+       }
+
        /**
         * Test escapeIdReferenceList for consistency with escapeId
         *
@@ -362,4 +401,25 @@ class SanitizerTest extends MediaWikiTestCase {
                        [ '+1 +2', '+1', '+2' ],
                ];
        }
+
+       /**
+        * @dataProvider provideIsReservedDataAttribute
+        */
+       public function testIsReservedDataAttribute( $attr, $expected ) {
+               $this->assertSame( $expected, Sanitizer::isReservedDataAttribute( $attr ) );
+       }
+
+       public static function provideIsReservedDataAttribute() {
+               return [
+                       [ 'foo', false ],
+                       [ 'data', false ],
+                       [ 'data-foo', false ],
+                       [ 'data-mw', true ],
+                       [ 'data-ooui', true ],
+                       [ 'data-parsoid', true ],
+                       [ 'data-mw-foo', true ],
+                       [ 'data-ooui-foo', true ],
+                       [ 'data-mwfoo', true ], // could be false but this is how it's implemented currently
+               ];
+       }
 }