From 3e871f201544570833b2e258834768104d260ef5 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Fri, 5 Jan 2024 21:48:58 +0000 Subject: [PATCH] tests: Improve validateScriptFile coverage and tolerate valid ES6 Remove the test that expects ES6 to fail by default, instead assert that invalid syntax fails (as it should). Also add various other test cases, including a commented-out test for valid ES6 by default, to be enabled after Ie309e761f8b20640f7c0 in MediaWiki core. Change-Id: If45856c563518255189687e3b1f620973349fb27 --- includes/GadgetResourceLoaderModule.php | 2 +- .../GadgetResourceLoaderModuleTest.php | 55 +++++++++++-------- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/includes/GadgetResourceLoaderModule.php b/includes/GadgetResourceLoaderModule.php index 79c53402..f7f38fd0 100644 --- a/includes/GadgetResourceLoaderModule.php +++ b/includes/GadgetResourceLoaderModule.php @@ -101,7 +101,7 @@ class GadgetResourceLoaderModule extends RL\WikiModule { * @return bool */ public function isPackaged(): bool { - return $this->gadget->isPackaged(); + return $this->getGadget()->isPackaged(); } /** diff --git a/tests/phpunit/integration/GadgetResourceLoaderModuleTest.php b/tests/phpunit/integration/GadgetResourceLoaderModuleTest.php index 4c6cd2ec..dc31ad9d 100644 --- a/tests/phpunit/integration/GadgetResourceLoaderModuleTest.php +++ b/tests/phpunit/integration/GadgetResourceLoaderModuleTest.php @@ -15,19 +15,16 @@ use Wikimedia\TestingAccessWrapper; class GadgetResourceLoaderModuleTest extends MediaWikiIntegrationTestCase { use GadgetTestTrait; - /** - * @var Gadget - */ + /** @var Gadget */ private $gadget; - /** - * @var TestingAccessWrapper - */ + /** @var TestingAccessWrapper */ private $gadgetModule; protected function setUp(): void { parent::setUp(); $this->gadget = $this->makeGadget( '*foo [ResourceLoader|package]|foo.js|foo.css|foo.json' ); $this->gadgetModule = $this->makeGadgetModule( $this->gadget ); + $this->overrideConfigValue( MainConfigNames::ResourceLoaderValidateJS, true ); } /** @@ -51,25 +48,35 @@ class GadgetResourceLoaderModuleTest extends MediaWikiIntegrationTestCase { $nonPackageGadgetModule->getPages( $context ) ); } - public function testEs6Gadget() { - $this->editPage( 'MediaWiki:Gadget-foo.js', '(() => {})();' ); - $repo = new StaticGadgetRepo( [ - 'g1' => $this->makeGadget( '*g1 [ResourceLoader]|foo.js' ), - 'g2' => $this->makeGadget( '*g1 [ResourceLoader|requiresES6]|foo.js' ) - ] ); - $this->setService( 'GadgetsRepo', $repo ); - $this->overrideConfigValue( MainConfigNames::ResourceLoaderValidateJS, true ); - $rlContext = RL\Context::newDummyContext(); + public static function provideValidateScript() { + yield 'valid ES5' => [ true, '[ResourceLoader]', 'var quux = function() {};' ]; + // TODO: T75714 + // yield 'valid ES6' => [ true, '[ResourceLoader]', 'let quux = (() => {})();' ]; + yield 'invalid' => [ false, '[ResourceLoader]', 'boom quux = <3;' ]; - $m1 = new GadgetResourceLoaderModule( [ 'id' => 'g1' ] ); - $this->assertFalse( $m1->requiresES6() ); - $m1->setConfig( $this->getServiceContainer()->getMainConfig() ); - $this->assertStringContainsString( 'mw.log.error', $m1->getScript( $rlContext ) ); - - $m2 = new GadgetResourceLoaderModule( [ 'id' => 'g2' ] ); - $this->assertTrue( $m2->requiresES6() ); - $m2->setConfig( $this->getServiceContainer()->getMainConfig() ); - $this->assertStringNotContainsString( 'mw.log.error', $m2->getScript( $rlContext ) ); + yield 'requiresES6 allows ES5' => [ true, '[ResourceLoader|requiresES6]', 'var quux = function() {};' ]; + yield 'requiresES6 allows ES6' => [ true, '[ResourceLoader|requiresES6]', 'let quux = (() => {})();' ]; + yield 'requiresES6 allows invalid' => [ true, '[ResourceLoader|requiresES6]', 'boom quux = <3;' ]; } + /** + * @dataProvider provideValidateScript + */ + public function testValidateScriptFile( $valid, $options, $content ) { + $this->editPage( 'MediaWiki:Gadget-foo.js', $content ); + $repo = new StaticGadgetRepo( [ + 'example' => $this->makeGadget( "* example $options | foo.js" ), + ] ); + $this->setService( 'GadgetsRepo', $repo ); + $rlContext = RL\Context::newDummyContext(); + + $module = new GadgetResourceLoaderModule( [ 'id' => 'example' ] ); + $module->setConfig( $this->getServiceContainer()->getMainConfig() ); + $actual = $module->getScript( $rlContext ); + + $expect = $valid ? 'quux' : 'mw.log.error'; + $expectNot = $valid ? 'mw.log.error' : 'quux'; + $this->assertStringContainsString( $expect, $actual ); + $this->assertStringNotContainsString( $expectNot, $actual ); + } }