From: Timo Tijhof Date: Tue, 27 Jun 2017 05:04:21 +0000 (-0700) Subject: resourceloader: Add basic tests for getScript() and buildContent() X-Git-Tag: 1.31.0-rc.0~2869^2 X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=commitdiff_plain;h=d3e31c7ea472d59adf5bb56ab383bac37d03adb4 resourceloader: Add basic tests for getScript() and buildContent() Bug: T162719 Change-Id: I37d64da77682adfef61e78033d639b623d7c9c2b --- diff --git a/tests/phpunit/data/resourceloader/script-comment.js b/tests/phpunit/data/resourceloader/script-comment.js new file mode 100644 index 0000000000..46b60f9c01 --- /dev/null +++ b/tests/phpunit/data/resourceloader/script-comment.js @@ -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 index 0000000000..2b1550fa9e --- /dev/null +++ b/tests/phpunit/data/resourceloader/script-nosemi.js @@ -0,0 +1,2 @@ +/* eslint-disable */ +mw.foo() diff --git a/tests/phpunit/includes/resourceloader/ResourceLoaderFileModuleTest.php b/tests/phpunit/includes/resourceloader/ResourceLoaderFileModuleTest.php index 9750ea49de..ded56e9201 100644 --- a/tests/phpunit/includes/resourceloader/ResourceLoaderFileModuleTest.php +++ b/tests/phpunit/includes/resourceloader/ResourceLoaderFileModuleTest.php @@ -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' + ); } /** diff --git a/tests/phpunit/includes/resourceloader/ResourceLoaderModuleTest.php b/tests/phpunit/includes/resourceloader/ResourceLoaderModuleTest.php index 17861d8929..6057b9710b 100644 --- a/tests/phpunit/includes/resourceloader/ResourceLoaderModuleTest.php +++ b/tests/phpunit/includes/resourceloader/ResourceLoaderModuleTest.php @@ -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