mediawiki-extensions-Gadgets/tests/phpunit/integration/GadgetResourceLoaderModuleTest.php
Timo Tijhof 3e871f2015 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
2024-01-05 21:48:58 +00:00

83 lines
3 KiB
PHP

<?php
use MediaWiki\Extension\Gadgets\Gadget;
use MediaWiki\Extension\Gadgets\GadgetResourceLoaderModule;
use MediaWiki\Extension\Gadgets\StaticGadgetRepo;
use MediaWiki\MainConfigNames;
use MediaWiki\ResourceLoader as RL;
use Wikimedia\TestingAccessWrapper;
/**
* @covers \MediaWiki\Extension\Gadgets\GadgetResourceLoaderModule
* @group Gadgets
* @group Database
*/
class GadgetResourceLoaderModuleTest extends MediaWikiIntegrationTestCase {
use GadgetTestTrait;
/** @var Gadget */
private $gadget;
/** @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 );
}
/**
* @covers \MediaWiki\Extension\Gadgets\GadgetResourceLoaderModule::getPages
*/
public function testGetPages() {
$context = $this->createMock( RL\Context::class );
$pages = $this->gadgetModule->getPages( $context );
$this->assertArrayHasKey( 'MediaWiki:Gadget-foo.css', $pages );
$this->assertArrayHasKey( 'MediaWiki:Gadget-foo.js', $pages );
$this->assertArrayHasKey( 'MediaWiki:Gadget-foo.json', $pages );
$this->assertArrayEquals( $pages, [
[ 'type' => 'style' ],
[ 'type' => 'script' ],
[ 'type' => 'data' ]
] );
$nonPackageGadget = $this->makeGadget( '*foo [ResourceLoader]|foo.js|foo.css|foo.json' );
$nonPackageGadgetModule = $this->makeGadgetModule( $nonPackageGadget );
$this->assertArrayNotHasKey( 'MediaWiki:Gadget-foo.json',
$nonPackageGadgetModule->getPages( $context ) );
}
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;' ];
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 );
}
}