Html: Add test coverage for inlineScript()
authorTimo Tijhof <krinklemail@gmail.com>
Tue, 7 Aug 2018 15:16:21 +0000 (16:16 +0100)
committerKrinkle <krinklemail@gmail.com>
Thu, 9 Aug 2018 14:28:50 +0000 (14:28 +0000)
This asserts current behaviour in preparation for changing it
in a later commit (see T200506).

Bug: T200506
Change-Id: I76fd15c32c763754d0825d02a49de3d9973c4f8f

tests/phpunit/includes/HtmlTest.php

index 3c8fa25..dc32a6f 100644 (file)
@@ -785,6 +785,45 @@ 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>/*<![CDATA[*/EXAMPLE.is(a && b);/*]]>*/</script>'
+                       ],
+                       'HTML' => [
+                               'EXAMPLE.label("<a>");',
+                               '<script>/*<![CDATA[*/EXAMPLE.label("<a>");/*]]>*/</script>'
+                       ],
+                       'Script closing string' => [
+                               'EXAMPLE.label("</script>");',
+                               // Broken: First </script> ends the script in HTML
+                               '<script>/*<![CDATA[*/EXAMPLE.label("</script>");/*]]>*/</script>'
+                       ],
+                       'CDATA string' => [
+                               'EXAMPLE.label("&> CDATA ]]>");',
+                               // Broken: Works in HTML, but is invalid XML.
+                               '<script>/*<![CDATA[*/EXAMPLE.label("&> CDATA ]]>");/*]]>*/</script>'
+                       ],
+               ];
+       }
+
+       /**
+        * @dataProvider provideInlineScript
+        * @covers Html::inlineScript
+        */
+       public function testInlineScript( $code, $expected ) {
+               $this->assertSame( Html::inlineScript( $code ), $expected );
+       }
 }
 
 class HtmlTestValue {