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
|
|
|
|
tests: Avoid matching source in validateScriptFile error message
Follows-up 3e871f2015 (If45856c563), which was meant to make
change Ie309e761f8b20640f7c0 in MediaWiki core pass, but didn't, and
I failed to realize this before the change landed.
The current error message is quite generic, and does not mention
"quux". The new validator (Peast) gives a better error message, and
thus would contain the word quux:
> Parse error: Unexpected: quux on line 1
My previous change was loosely checking this as a proxy for whether
the source code is included (valid) or stripped (invalid), but this
was a poor proxy. I did that because I assumed, incorrectly, that
this code would become minified. Since that's not true, we can simply
assert the code in its entirety for the valid case, and for the
invalid case only check for the presence of a warning, and leave it to
core to be responsible for omitting the invalid code in that case.
Change-Id: If3b2b4a75013baeaa0d9b92cd10dfb06e5534153
2024-01-06 22:22:25 +00:00
|
|
|
if ( !$valid ) {
|
|
|
|
$this->assertStringContainsString( 'mw.log.error', $actual );
|
|
|
|
} else {
|
|
|
|
$this->assertStringContainsString( $content, $actual );
|
|
|
|
$this->assertStringNotContainsString( 'mw.log.error', $actual );
|
|
|
|
}
|
2023-12-06 07:06:06 +00:00
|
|
|
}
|
2021-10-17 13:05:15 +00:00
|
|
|
}
|