mediawiki-extensions-Gadgets/tests/phpunit/integration/GadgetResourceLoaderModuleTest.php
Siddharth VP ce486be160 Further expand test coverage
* Separate out tests for MediaWikiGadgetsDefinitionRepo and
Hooks::onGetPreferences. Removed hack in fetchStructuredList() for
overriding page content for tests.
* Add tests for ES6 gadgets. For this, ResourceLoaderModuleTest is
converted back to an integration test.
* Removed createGadgetDefinitionPage() test helper method. Now using
MediaWikiIntegrationTestCase::editPage().
* Fixed GDNamespaceRepoTest failure when GDNamespaceRepo is the active
repo, by clearing WAN process cache.

Change-Id: I26b84576a91f6cb0ebae64c5fc1408666d767911
2023-12-09 09:29:15 +05:30

76 lines
2.5 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 );
}
/**
* @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 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 ) );
}
}