| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Extension\Gadgets\Gadget; |
| 4 |
use MediaWiki\Extension\Gadgets\GadgetResourceLoaderModule; |
| 5 |
use MediaWiki\Extension\Gadgets\StaticGadgetRepo; |
| 6 |
use MediaWiki\MainConfigNames; |
| 7 |
use MediaWiki\ResourceLoader as RL; |
| 8 |
use Wikimedia\TestingAccessWrapper; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class GadgetResourceLoaderModuleTest extends MediaWikiIntegrationTestCase { |
| 16 |
use GadgetTestTrait; |
| 17 |
|
| 18 |
|
| 19 |
private $gadget; |
| 20 |
|
| 21 |
private $gadgetModule; |
| 22 |
|
| 23 |
protected function setUp(): void { |
| 24 |
parent::setUp(); |
| 25 |
$this->gadget = $this->makeGadget( '*foo [ResourceLoader|package]|foo.js|foo.css|foo.json' ); |
| 26 |
$this->gadgetModule = $this->makeGadgetModule( $this->gadget ); |
| 27 |
$this->overrideConfigValue( MainConfigNames::ResourceLoaderValidateJS, true ); |
| 28 |
} |
| 29 |
|
| 30 |
public function testGetPages() { |
| 31 |
$context = $this->createMock( RL\Context::class ); |
| 32 |
$pages = $this->gadgetModule->getPages( $context ); |
| 33 |
$this->assertArrayHasKey( 'MediaWiki:Gadget-foo.css', $pages ); |
| 34 |
$this->assertArrayHasKey( 'MediaWiki:Gadget-foo.js', $pages ); |
| 35 |
$this->assertArrayHasKey( 'MediaWiki:Gadget-foo.json', $pages ); |
| 36 |
$this->assertArrayEquals( $pages, [ |
| 37 |
[ 'type' => 'style' ], |
| 38 |
[ 'type' => 'script' ], |
| 39 |
[ 'type' => 'data' ] |
| 40 |
] ); |
| 41 |
|
| 42 |
$nonPackageGadget = $this->makeGadget( '*foo [ResourceLoader]|foo.js|foo.css|foo.json' ); |
| 43 |
$nonPackageGadgetModule = $this->makeGadgetModule( $nonPackageGadget ); |
| 44 |
$this->assertArrayNotHasKey( 'MediaWiki:Gadget-foo.json', |
| 45 |
$nonPackageGadgetModule->getPages( $context ) ); |
| 46 |
} |
| 47 |
|
| 48 |
public static function provideValidateScript() { |
| 49 |
yield 'valid ES5' => [ true, '[ResourceLoader]', 'var quux = function() {};' ]; |
| 50 |
yield 'valid ES6' => [ true, '[ResourceLoader]', 'let quux = (() => {})();' ]; |
| 51 |
yield 'invalid' => [ false, '[ResourceLoader]', 'boom quux = <3;' ]; |
| 52 |
|
| 53 |
yield 'requiresES6 allows ES5' => [ true, '[ResourceLoader|requiresES6]', 'var quux = function() {};' ]; |
| 54 |
yield 'requiresES6 allows ES6' => [ true, '[ResourceLoader|requiresES6]', 'let quux = (() => {})();' ]; |
| 55 |
yield 'requiresES6 allows invalid' => [ true, '[ResourceLoader|requiresES6]', 'boom quux = <3;' ]; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function testValidateScriptFile( $valid, $options, $content ) { |
| 62 |
$this->editPage( 'MediaWiki:Gadget-foo.js', $content ); |
| 63 |
$repo = new StaticGadgetRepo( [ |
| 64 |
'example' => $this->makeGadget( "* example $options | foo.js" ), |
| 65 |
] ); |
| 66 |
$this->setService( 'GadgetsRepo', $repo ); |
| 67 |
$rlContext = RL\Context::newDummyContext(); |
| 68 |
|
| 69 |
$module = new GadgetResourceLoaderModule( [ 'id' => 'example' ] ); |
| 70 |
$module->setConfig( $this->getServiceContainer()->getMainConfig() ); |
| 71 |
$actual = $module->getScript( $rlContext ); |
| 72 |
|
| 73 |
if ( !$valid ) { |
| 74 |
$this->assertStringContainsString( 'mw.log.error', $actual ); |
| 75 |
} else { |
| 76 |
$this->assertStringContainsString( $content, $actual ); |
| 77 |
$this->assertStringNotContainsString( 'mw.log.error', $actual ); |
| 78 |
} |
| 79 |
} |
| 80 |
} |