2021-10-17 13:05:15 +00:00
|
|
|
<?php
|
|
|
|
|
2022-02-06 18:54:47 +00:00
|
|
|
use MediaWiki\Extension\Gadgets\Gadget;
|
2023-12-06 07:06:06 +00:00
|
|
|
use MediaWiki\Extension\Gadgets\GadgetResourceLoaderModule;
|
|
|
|
use MediaWiki\Extension\Gadgets\StaticGadgetRepo;
|
|
|
|
use MediaWiki\MainConfigNames;
|
2023-12-05 18:04:27 +00:00
|
|
|
use MediaWiki\ResourceLoader as RL;
|
2021-10-17 13:05:15 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
|
|
|
|
/**
|
2023-12-06 07:06:06 +00:00
|
|
|
* @covers \MediaWiki\Extension\Gadgets\GadgetResourceLoaderModule
|
2021-10-17 13:05:15 +00:00
|
|
|
* @group Gadgets
|
2023-12-06 07:06:06 +00:00
|
|
|
* @group Database
|
2021-10-17 13:05:15 +00:00
|
|
|
*/
|
2023-12-06 07:06:06 +00:00
|
|
|
class GadgetResourceLoaderModuleTest extends MediaWikiIntegrationTestCase {
|
2022-08-16 15:24:01 +00:00
|
|
|
use GadgetTestTrait;
|
2021-10-17 13:05:15 +00:00
|
|
|
|
2024-01-05 21:48:58 +00:00
|
|
|
/** @var Gadget */
|
2021-10-17 13:05:15 +00:00
|
|
|
private $gadget;
|
2024-01-05 21:48:58 +00:00
|
|
|
/** @var TestingAccessWrapper */
|
2021-10-17 13:05:15 +00:00
|
|
|
private $gadgetModule;
|
|
|
|
|
|
|
|
protected function setUp(): void {
|
|
|
|
parent::setUp();
|
2022-08-16 15:24:01 +00:00
|
|
|
$this->gadget = $this->makeGadget( '*foo [ResourceLoader|package]|foo.js|foo.css|foo.json' );
|
|
|
|
$this->gadgetModule = $this->makeGadgetModule( $this->gadget );
|
2024-01-05 21:48:58 +00:00
|
|
|
$this->overrideConfigValue( MainConfigNames::ResourceLoaderValidateJS, true );
|
2021-10-17 13:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-02-06 18:54:47 +00:00
|
|
|
* @covers \MediaWiki\Extension\Gadgets\GadgetResourceLoaderModule::getPages
|
2021-10-17 13:05:15 +00:00
|
|
|
*/
|
|
|
|
public function testGetPages() {
|
2023-12-05 18:04:27 +00:00
|
|
|
$context = $this->createMock( RL\Context::class );
|
|
|
|
$pages = $this->gadgetModule->getPages( $context );
|
2021-10-17 13:05:15 +00:00
|
|
|
$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' ]
|
|
|
|
] );
|
|
|
|
|
2022-08-16 15:24:01 +00:00
|
|
|
$nonPackageGadget = $this->makeGadget( '*foo [ResourceLoader]|foo.js|foo.css|foo.json' );
|
|
|
|
$nonPackageGadgetModule = $this->makeGadgetModule( $nonPackageGadget );
|
2021-10-17 13:05:15 +00:00
|
|
|
$this->assertArrayNotHasKey( 'MediaWiki:Gadget-foo.json',
|
2023-12-05 18:04:27 +00:00
|
|
|
$nonPackageGadgetModule->getPages( $context ) );
|
2021-10-17 13:05:15 +00:00
|
|
|
}
|
|
|
|
|
2024-01-05 21:48:58 +00:00
|
|
|
public static function provideValidateScript() {
|
|
|
|
yield 'valid ES5' => [ true, '[ResourceLoader]', 'var quux = function() {};' ];
|
2024-01-05 21:51:33 +00:00
|
|
|
yield 'valid ES6' => [ true, '[ResourceLoader]', 'let quux = (() => {})();' ];
|
2024-01-05 21:48:58 +00:00
|
|
|
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 );
|
2023-12-06 07:06:06 +00:00
|
|
|
$repo = new StaticGadgetRepo( [
|
2024-01-05 21:48:58 +00:00
|
|
|
'example' => $this->makeGadget( "* example $options | foo.js" ),
|
2023-12-06 07:06:06 +00:00
|
|
|
] );
|
|
|
|
$this->setService( 'GadgetsRepo', $repo );
|
|
|
|
$rlContext = RL\Context::newDummyContext();
|
|
|
|
|
2024-01-05 21:48:58 +00:00
|
|
|
$module = new GadgetResourceLoaderModule( [ 'id' => 'example' ] );
|
|
|
|
$module->setConfig( $this->getServiceContainer()->getMainConfig() );
|
|
|
|
$actual = $module->getScript( $rlContext );
|
2023-12-06 07:06:06 +00:00
|
|
|
|
2024-01-05 21:48:58 +00:00
|
|
|
$expect = $valid ? 'quux' : 'mw.log.error';
|
|
|
|
$expectNot = $valid ? 'mw.log.error' : 'quux';
|
|
|
|
$this->assertStringContainsString( $expect, $actual );
|
|
|
|
$this->assertStringNotContainsString( $expectNot, $actual );
|
2023-12-06 07:06:06 +00:00
|
|
|
}
|
2021-10-17 13:05:15 +00:00
|
|
|
}
|