CommentStore: Hard-deprecate newKey()
[lhc/web/wiklou.git] / tests / phpunit / includes / HtmlTest.php
index 974373b..62094b6 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 
 class HtmlTest extends MediaWikiTestCase {
+       private $restoreWarnings;
 
        protected function setUp() {
                parent::setUp();
@@ -36,6 +37,15 @@ class HtmlTest extends MediaWikiTestCase {
                ] );
                $this->setUserLang( $langObj );
                $this->setContentLang( $langObj );
+               $this->restoreWarnings = false;
+       }
+
+       protected function tearDown() {
+               if ( $this->restoreWarnings ) {
+                       $this->restoreWarnings = false;
+                       Wikimedia\restoreWarnings();
+               }
+               parent::tearDown();
        }
 
        /**
@@ -331,7 +341,7 @@ class HtmlTest extends MediaWikiTestCase {
                );
 
                $this->assertEquals(
-                       '<label for="mw-test-namespace">Select a namespace:</label>&#160;' .
+                       '<label for="mw-test-namespace">Select a namespace:</label>' . "\u{00A0}" .
                                '<select id="mw-test-namespace" name="wpNamespace">' . "\n" .
                                '<option value="all">all</option>' . "\n" .
                                '<option value="0">(Main)</option>' . "\n" .
@@ -359,7 +369,7 @@ class HtmlTest extends MediaWikiTestCase {
                );
 
                $this->assertEquals(
-                       '<label for="namespace">Select a namespace:</label>&#160;' .
+                       '<label for="namespace">Select a namespace:</label>' . "\u{00A0}" .
                                '<select id="namespace" name="namespace">' . "\n" .
                                '<option value="0">(Main)</option>' . "\n" .
                                '<option value="1">Talk</option>' . "\n" .
@@ -476,6 +486,10 @@ class HtmlTest extends MediaWikiTestCase {
                        Html::errorBox( 'err', 'heading' ),
                        '<div class="errorbox"><h2>heading</h2>err</div>'
                );
+               $this->assertEquals(
+                       Html::errorBox( 'err', '0' ),
+                       '<div class="errorbox"><h2>0</h2>err</div>'
+               );
        }
 
        /**
@@ -785,6 +799,58 @@ class HtmlTest extends MediaWikiTestCase {
        public function testSrcSet( $images, $expected, $message ) {
                $this->assertEquals( Html::srcSet( $images ), $expected, $message );
        }
+
+       public static function provideInlineScript() {
+               return [
+                       'Empty' => [
+                               '',
+                               '<script></script>'
+                       ],
+                       'Simple' => [
+                               'EXAMPLE.label("foo");',
+                               '<script>EXAMPLE.label("foo");</script>'
+                       ],
+                       'Ampersand' => [
+                               'EXAMPLE.is(a && b);',
+                               '<script>EXAMPLE.is(a && b);</script>'
+                       ],
+                       'HTML' => [
+                               'EXAMPLE.label("<a>");',
+                               '<script>EXAMPLE.label("<a>");</script>'
+                       ],
+                       'Script closing string (lower)' => [
+                               'EXAMPLE.label("</script>");',
+                               '<script>/* ERROR: Invalid script */</script>',
+                               true,
+                       ],
+                       'Script closing with non-standard attributes (mixed)' => [
+                               'EXAMPLE.label("</SCriPT and STyLE>");',
+                               '<script>/* ERROR: Invalid script */</script>',
+                               true,
+                       ],
+                       'HTML-comment-open and script-open' => [
+                               // In HTML, <script> contents aren't just plain CDATA until </script>,
+                               // there are levels of escaping modes, and the below sequence puts an
+                               // HTML parser in a state where </script> would *not* close the script.
+                               // https://html.spec.whatwg.org/multipage/parsing.html#script-data-double-escape-end-state
+                               'var a = "<!--<script>";',
+                               '<script>/* ERROR: Invalid script */</script>',
+                               true,
+                       ],
+               ];
+       }
+
+       /**
+        * @dataProvider provideInlineScript
+        * @covers Html::inlineScript
+        */
+       public function testInlineScript( $code, $expected, $error = false ) {
+               if ( $error ) {
+                       Wikimedia\suppressWarnings();
+                       $this->restoreWarnings = true;
+               }
+               $this->assertSame( Html::inlineScript( $code ), $expected );
+       }
 }
 
 class HtmlTestValue {