Merge "resourceloader: Add basic tests for getScript() and buildContent()"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Tue, 27 Jun 2017 22:35:52 +0000 (22:35 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Tue, 27 Jun 2017 22:35:52 +0000 (22:35 +0000)
tests/phpunit/data/resourceloader/script-comment.js [new file with mode: 0644]
tests/phpunit/data/resourceloader/script-nosemi.js [new file with mode: 0644]
tests/phpunit/includes/resourceloader/ResourceLoaderFileModuleTest.php
tests/phpunit/includes/resourceloader/ResourceLoaderModuleTest.php

diff --git a/tests/phpunit/data/resourceloader/script-comment.js b/tests/phpunit/data/resourceloader/script-comment.js
new file mode 100644 (file)
index 0000000..46b60f9
--- /dev/null
@@ -0,0 +1,3 @@
+/* eslint-disable */
+mw.foo()
+// mw.bar();
diff --git a/tests/phpunit/data/resourceloader/script-nosemi.js b/tests/phpunit/data/resourceloader/script-nosemi.js
new file mode 100644 (file)
index 0000000..2b1550f
--- /dev/null
@@ -0,0 +1,2 @@
+/* eslint-disable */
+mw.foo()
index 9750ea4..ded56e9 100644 (file)
@@ -132,10 +132,30 @@ class ResourceLoaderFileModuleTest extends ResourceLoaderTestCase {
         */
        public function testDeprecatedModules( $name, $expected ) {
                $modules = self::getModules();
-               $rl = new ResourceLoaderFileModule( $modules[$name] );
-               $rl->setName( $name );
+               $module = new ResourceLoaderFileModule( $modules[$name] );
+               $module->setName( $name );
                $ctx = $this->getResourceLoaderContext();
-               $this->assertEquals( $rl->getScript( $ctx ), $expected );
+               $this->assertEquals( $module->getScript( $ctx ), $expected );
+       }
+
+       /**
+        * @covers ResourceLoaderFileModule::getScript
+        */
+       public function testGetScript() {
+               $module = new ResourceLoaderFileModule( [
+                       'localBasePath' => __DIR__ . '/../../data/resourceloader',
+                       'scripts' => [ 'script-nosemi.js', 'script-comment.js' ],
+               ] );
+               $module->setName( 'testing' );
+               $ctx = $this->getResourceLoaderContext();
+               $this->assertEquals(
+                       "/* eslint-disable */\nmw.foo()\n" .
+                       "\n" .
+                       "/* eslint-disable */\nmw.foo()\n// mw.bar();\n" .
+                       "\n",
+                       $module->getScript( $ctx ),
+                       'scripts are concatenated with a new-line'
+               );
        }
 
        /**
index 17861d8..6057b97 100644 (file)
@@ -94,6 +94,52 @@ class ResourceLoaderModuleTest extends ResourceLoaderTestCase {
                );
        }
 
+       public static function provideBuildContentScripts() {
+               return [
+                       [
+                               "mw.foo()",
+                               "mw.foo();\n",
+                       ],
+                       [
+                               "mw.foo();",
+                               "mw.foo();",
+                       ],
+                       [
+                               "mw.foo();\n",
+                               "mw.foo();\n",
+                       ],
+                       [
+                               "mw.foo()\n",
+                               "mw.foo()\n;\n",
+                       ],
+                       [
+                               "mw.foo()\n// mw.bar();",
+                               "mw.foo()\n// mw.bar();",
+                       ],
+                       [
+                               "mw.foo()// mw.bar();",
+                               "mw.foo()// mw.bar();",
+                       ],
+               ];
+       }
+
+       /**
+        * @dataProvider provideBuildContentScripts
+        * @covers ResourceLoaderModule::buildContent
+        */
+       public function testBuildContentScripts( $raw, $build, $message = null ) {
+               $context = $this->getResourceLoaderContext();
+               $module = new ResourceLoaderTestModule( [
+                       'script' => $raw
+               ] );
+               $this->assertEquals( $raw, $module->getScript( $context ), 'Raw script' );
+               $this->assertEquals(
+                       [ 'scripts' => $build ],
+                       $module->getModuleContent( $context ),
+                       $message
+               );
+       }
+
        /**
         * @covers ResourceLoaderModule::getRelativePaths
         * @covers ResourceLoaderModule::expandRelativePaths