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
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Gadget
|
|
|
|
*/
|
|
|
|
private $gadget;
|
|
|
|
/**
|
|
|
|
* @var TestingAccessWrapper
|
|
|
|
*/
|
|
|
|
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 );
|
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
|
|
|
}
|
|
|
|
|
2023-12-06 07:06:06 +00:00
|
|
|
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();
|
|
|
|
|
|
|
|
$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 ) );
|
|
|
|
}
|
|
|
|
|
2021-10-17 13:05:15 +00:00
|
|
|
}
|