Merge "Make the PHP code snippet in SkinFallbackTemplate explicitly ltr"
[lhc/web/wiklou.git] / tests / phpunit / includes / SanitizerTest.php
index 159ea71..50c1e50 100644 (file)
@@ -1,5 +1,9 @@
 <?php
 
+/**
+ * @todo Tests covering decodeCharReferences can be refactored into a single
+ * method and dataprovider.
+ */
 class SanitizerTest extends MediaWikiTestCase {
 
        protected function setUp() {
@@ -8,7 +12,10 @@ class SanitizerTest extends MediaWikiTestCase {
                AutoLoader::loadClass( 'Sanitizer' );
        }
 
-       function testDecodeNamedEntities() {
+       /**
+        * @covers Sanitizer::decodeCharReferences
+        */
+       public function testDecodeNamedEntities() {
                $this->assertEquals(
                        "\xc3\xa9cole",
                        Sanitizer::decodeCharReferences( '&eacute;cole' ),
@@ -16,7 +23,10 @@ class SanitizerTest extends MediaWikiTestCase {
                );
        }
 
-       function testDecodeNumericEntities() {
+       /**
+        * @covers Sanitizer::decodeCharReferences
+        */
+       public function testDecodeNumericEntities() {
                $this->assertEquals(
                        "\xc4\x88io bonas dans l'\xc3\xa9cole!",
                        Sanitizer::decodeCharReferences( "&#x108;io bonas dans l'&#233;cole!" ),
@@ -24,7 +34,10 @@ class SanitizerTest extends MediaWikiTestCase {
                );
        }
 
-       function testDecodeMixedEntities() {
+       /**
+        * @covers Sanitizer::decodeCharReferences
+        */
+       public function testDecodeMixedEntities() {
                $this->assertEquals(
                        "\xc4\x88io bonas dans l'\xc3\xa9cole!",
                        Sanitizer::decodeCharReferences( "&#x108;io bonas dans l'&eacute;cole!" ),
@@ -32,7 +45,10 @@ class SanitizerTest extends MediaWikiTestCase {
                );
        }
 
-       function testDecodeMixedComplexEntities() {
+       /**
+        * @covers Sanitizer::decodeCharReferences
+        */
+       public function testDecodeMixedComplexEntities() {
                $this->assertEquals(
                        "\xc4\x88io bonas dans l'\xc3\xa9cole! (mais pas &#x108;io dans l'&eacute;cole)",
                        Sanitizer::decodeCharReferences(
@@ -42,7 +58,10 @@ class SanitizerTest extends MediaWikiTestCase {
                );
        }
 
-       function testInvalidAmpersand() {
+       /**
+        * @covers Sanitizer::decodeCharReferences
+        */
+       public function testInvalidAmpersand() {
                $this->assertEquals(
                        'a & b',
                        Sanitizer::decodeCharReferences( 'a & b' ),
@@ -50,7 +69,10 @@ class SanitizerTest extends MediaWikiTestCase {
                );
        }
 
-       function testInvalidEntities() {
+       /**
+        * @covers Sanitizer::decodeCharReferences
+        */
+       public function testInvalidEntities() {
                $this->assertEquals(
                        '&foo;',
                        Sanitizer::decodeCharReferences( '&foo;' ),
@@ -58,21 +80,26 @@ class SanitizerTest extends MediaWikiTestCase {
                );
        }
 
-       function testInvalidNumberedEntities() {
-               $this->assertEquals( UTF8_REPLACEMENT, Sanitizer::decodeCharReferences( "&#88888888888888;" ), 'Invalid numbered entity' );
+       /**
+        * @covers Sanitizer::decodeCharReferences
+        */
+       public function testInvalidNumberedEntities() {
+               $this->assertEquals(
+                       UTF8_REPLACEMENT,
+                       Sanitizer::decodeCharReferences( "&#88888888888888;" ),
+                       'Invalid numbered entity'
+               );
        }
 
        /**
         * @covers Sanitizer::removeHTMLtags
         * @dataProvider provideHtml5Tags
         *
-        * @param String $tag Name of an HTML5 element (ie: 'video')
-        * @param Boolean $escaped Wheter sanitizer let the tag in or escape it (ie: '&lt;video&gt;')
+        * @param string $tag Name of an HTML5 element (ie: 'video')
+        * @param bool $escaped Whether sanitizer let the tag in or escape it (ie: '&lt;video&gt;')
         */
-       function testRemovehtmltagsOnHtml5Tags( $tag, $escaped ) {
+       public function testRemovehtmltagsOnHtml5Tags( $tag, $escaped ) {
                $this->setMwGlobals( array(
-                       # Enable HTML5 mode
-                       'wgHtml5' => true,
                        'wgUseTidy' => false
                ) );
 
@@ -133,8 +160,9 @@ class SanitizerTest extends MediaWikiTestCase {
 
        /**
         * @dataProvider dataRemoveHTMLtags
+        * @covers Sanitizer::removeHTMLtags
         */
-       function testRemoveHTMLtags( $input, $output, $msg = null ) {
+       public function testRemoveHTMLtags( $input, $output, $msg = null ) {
                $GLOBALS['wgUseTidy'] = false;
                $this->assertEquals( $output, Sanitizer::removeHTMLtags( $input ), $msg );
        }
@@ -143,7 +171,7 @@ class SanitizerTest extends MediaWikiTestCase {
         * @dataProvider provideTagAttributesToDecode
         * @covers Sanitizer::decodeTagAttributes
         */
-       function testDecodeTagAttributes( $expected, $attributes, $message = '' ) {
+       public function testDecodeTagAttributes( $expected, $attributes, $message = '' ) {
                $this->assertEquals( $expected,
                        Sanitizer::decodeTagAttributes( $attributes ),
                        $message
@@ -156,9 +184,21 @@ class SanitizerTest extends MediaWikiTestCase {
                        array( array( 'foo' => 'bar' ), '    foo   =   bar    ', 'Spaced attribute' ),
                        array( array( 'foo' => 'bar' ), 'foo="bar"', 'Double-quoted attribute' ),
                        array( array( 'foo' => 'bar' ), 'foo=\'bar\'', 'Single-quoted attribute' ),
-                       array( array( 'foo' => 'bar', 'baz' => 'foo' ), 'foo=\'bar\'   baz="foo"', 'Several attributes' ),
-                       array( array( 'foo' => 'bar', 'baz' => 'foo' ), 'foo=\'bar\'   baz="foo"', 'Several attributes' ),
-                       array( array( 'foo' => 'bar', 'baz' => 'foo' ), 'foo=\'bar\'   baz="foo"', 'Several attributes' ),
+                       array(
+                               array( 'foo' => 'bar', 'baz' => 'foo' ),
+                               'foo=\'bar\'   baz="foo"',
+                               'Several attributes'
+                       ),
+                       array(
+                               array( 'foo' => 'bar', 'baz' => 'foo' ),
+                               'foo=\'bar\'   baz="foo"',
+                               'Several attributes'
+                       ),
+                       array(
+                               array( 'foo' => 'bar', 'baz' => 'foo' ),
+                               'foo=\'bar\'   baz="foo"',
+                               'Several attributes'
+                       ),
                        array( array( ':foo' => 'bar' ), ':foo=\'bar\'', 'Leading :' ),
                        array( array( '_foo' => 'bar' ), '_foo=\'bar\'', 'Leading _' ),
                        array( array( 'foo' => 'bar' ), 'Foo=\'bar\'', 'Leading capital' ),
@@ -179,9 +219,21 @@ class SanitizerTest extends MediaWikiTestCase {
                        array( array(), 'foo$=baz', 'Symbols are not allowed' ),
                        array( array(), 'foo@=baz', 'Symbols are not allowed' ),
                        array( array(), 'foo~=baz', 'Symbols are not allowed' ),
-                       array( array( 'foo' => '1[#^`*%w/(' ), 'foo=1[#^`*%w/(', 'All kind of characters are allowed as values' ),
-                       array( array( 'foo' => '1[#^`*%\'w/(' ), 'foo="1[#^`*%\'w/("', 'Double quotes are allowed if quoted by single quotes' ),
-                       array( array( 'foo' => '1[#^`*%"w/(' ), 'foo=\'1[#^`*%"w/(\'', 'Single quotes are allowed if quoted by double quotes' ),
+                       array(
+                               array( 'foo' => '1[#^`*%w/(' ),
+                               'foo=1[#^`*%w/(',
+                               'All kind of characters are allowed as values'
+                       ),
+                       array(
+                               array( 'foo' => '1[#^`*%\'w/(' ),
+                               'foo="1[#^`*%\'w/("',
+                               'Double quotes are allowed if quoted by single quotes'
+                       ),
+                       array(
+                               array( 'foo' => '1[#^`*%"w/(' ),
+                               'foo=\'1[#^`*%"w/(\'',
+                               'Single quotes are allowed if quoted by double quotes'
+                       ),
                        array( array( 'foo' => '&"' ), 'foo=&amp;&quot;', 'Special chars can be provided as entities' ),
                        array( array( 'foo' => '&foobar;' ), 'foo=&foobar;', 'Entity-like items are accepted' ),
                );
@@ -191,7 +243,7 @@ class SanitizerTest extends MediaWikiTestCase {
         * @dataProvider provideDeprecatedAttributes
         * @covers Sanitizer::fixTagAttributes
         */
-       function testDeprecatedAttributesUnaltered( $inputAttr, $inputEl, $message = '' ) {
+       public function testDeprecatedAttributesUnaltered( $inputAttr, $inputEl, $message = '' ) {
                $this->assertEquals( " $inputAttr",
                        Sanitizer::fixTagAttributes( $inputAttr, $inputEl ),
                        $message
@@ -211,7 +263,7 @@ class SanitizerTest extends MediaWikiTestCase {
                        array( 'align="left"', 'tr' ),
                        array( 'align="center"', 'div' ),
                        array( 'align="left"', 'h1' ),
-                       array( 'align="left"', 'span' ),
+                       array( 'align="left"', 'p' ),
                );
        }
 
@@ -219,7 +271,7 @@ class SanitizerTest extends MediaWikiTestCase {
         * @dataProvider provideCssCommentsFixtures
         * @covers Sanitizer::checkCss
         */
-       function testCssCommentsChecking( $expected, $css, $message = '' ) {
+       public function testCssCommentsChecking( $expected, $css, $message = '' ) {
                $this->assertEquals( $expected,
                        Sanitizer::checkCss( $css ),
                        $message
@@ -229,24 +281,42 @@ class SanitizerTest extends MediaWikiTestCase {
        public static function provideCssCommentsFixtures() {
                /** array( <expected>, <css>, [message] ) */
                return array(
-                       array( ' ', '/**/' ),
+                       // Valid comments spanning entire input
+                       array( '/**/', '/**/' ),
+                       array( '/* comment */', '/* comment */' ),
+                       // Weird stuff
                        array( ' ', '/****/' ),
-                       array( ' ', '/* comment */' ),
-                       array( ' ', "\\2f\\2a foo \\2a\\2f",
+                       array( ' ', '/* /* */' ),
+                       array( 'display: block;', "display:/* foo */block;" ),
+                       array( 'display: block;', "display:\\2f\\2a foo \\2a\\2f block;",
                                'Backslash-escaped comments must be stripped (bug 28450)' ),
                        array( '', '/* unfinished comment structure',
                                'Remove anything after a comment-start token' ),
                        array( '', "\\2f\\2a unifinished comment'",
                                'Remove anything after a backslash-escaped comment-start token' ),
-                       array( '/* insecure input */', 'filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'asdf.png\',sizingMethod=\'scale\');' ),
-                       array( '/* insecure input */', '-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'asdf.png\',sizingMethod=\'scale\')";' ),
+                       array(
+                               '/* insecure input */',
+                               'filter: progid:DXImageTransform.Microsoft.AlphaImageLoader'
+                                       . '(src=\'asdf.png\',sizingMethod=\'scale\');'
+                       ),
+                       array(
+                               '/* insecure input */',
+                               '-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader'
+                                       . '(src=\'asdf.png\',sizingMethod=\'scale\')";'
+                       ),
                        array( '/* insecure input */', 'width: expression(1+1);' ),
                        array( '/* insecure input */', 'background-image: image(asdf.png);' ),
                        array( '/* insecure input */', 'background-image: -webkit-image(asdf.png);' ),
                        array( '/* insecure input */', 'background-image: -moz-image(asdf.png);' ),
                        array( '/* insecure input */', 'background-image: image-set("asdf.png" 1x, "asdf.png" 2x);' ),
-                       array( '/* insecure input */', 'background-image: -webkit-image-set("asdf.png" 1x, "asdf.png" 2x);' ),
-                       array( '/* insecure input */', 'background-image: -moz-image-set("asdf.png" 1x, "asdf.png" 2x);' ),
+                       array(
+                               '/* insecure input */',
+                               'background-image: -webkit-image-set("asdf.png" 1x, "asdf.png" 2x);'
+                       ),
+                       array(
+                               '/* insecure input */',
+                               'background-image: -moz-image-set("asdf.png" 1x, "asdf.png" 2x);'
+                       ),
                );
        }
 
@@ -256,15 +326,21 @@ class SanitizerTest extends MediaWikiTestCase {
        public static function provideAttributeSupport() {
                /** array( <attributes>, <expected>, <message> ) */
                return array(
-                       array( 'div', ' role="presentation"', ' role="presentation"', 'Support for WAI-ARIA\'s role="presentation".' ),
+                       array(
+                               'div',
+                               ' role="presentation"',
+                               ' role="presentation"',
+                               'Support for WAI-ARIA\'s role="presentation".'
+                       ),
                        array( 'div', ' role="main"', '', "Other WAI-ARIA roles are currently not supported." ),
                );
        }
 
        /**
         * @dataProvider provideAttributeSupport
+        * @covers Sanitizer::fixTagAttributes
         */
-       function testAttributeSupport( $tag, $attributes, $expected, $message ) {
+       public function testAttributeSupport( $tag, $attributes, $expected, $message ) {
                $this->assertEquals( $expected,
                        Sanitizer::fixTagAttributes( $attributes, $tag ),
                        $message