3 class ResourceLoaderTest
extends ResourceLoaderTestCase
{
5 protected function setUp() {
9 'wgResourceLoaderLESSImportPaths' => [
10 dirname( dirname( __DIR__
) ) . '/data/less/common',
12 'wgResourceLoaderLESSVars' => [
17 // Clear ResourceLoaderGetConfigVars hooks (called by StartupModule)
18 // to avoid notices during testMakeModuleResponse for missing
19 // wgResourceLoaderLESSVars keys in extension hooks.
25 * Ensure the ResourceLoaderRegisterModules hook is called.
27 * @covers ResourceLoader::__construct
29 public function testConstructRegistrationHook() {
30 $resourceLoaderRegisterModulesHook = false;
32 $this->setMwGlobals( 'wgHooks', [
33 'ResourceLoaderRegisterModules' => [
34 function ( &$resourceLoader ) use ( &$resourceLoaderRegisterModulesHook ) {
35 $resourceLoaderRegisterModulesHook = true;
40 $unused = new ResourceLoader();
42 $resourceLoaderRegisterModulesHook,
43 'Hook ResourceLoaderRegisterModules called'
48 * @covers ResourceLoader::register
49 * @covers ResourceLoader::getModule
51 public function testRegisterValid() {
52 $module = new ResourceLoaderTestModule();
53 $resourceLoader = new EmptyResourceLoader();
54 $resourceLoader->register( 'test', $module );
55 $this->assertEquals( $module, $resourceLoader->getModule( 'test' ) );
59 * @covers ResourceLoader::register
61 public function testRegisterEmptyString() {
62 $module = new ResourceLoaderTestModule();
63 $resourceLoader = new EmptyResourceLoader();
64 $resourceLoader->register( '', $module );
65 $this->assertEquals( $module, $resourceLoader->getModule( '' ) );
69 * @covers ResourceLoader::register
71 public function testRegisterInvalidName() {
72 $resourceLoader = new EmptyResourceLoader();
73 $this->setExpectedException( 'MWException', "name 'test!invalid' is invalid" );
74 $resourceLoader->register( 'test!invalid', new ResourceLoaderTestModule() );
78 * @covers ResourceLoader::register
80 public function testRegisterInvalidType() {
81 $resourceLoader = new EmptyResourceLoader();
82 $this->setExpectedException( 'MWException', 'ResourceLoader module info type error' );
83 $resourceLoader->register( 'test', new stdClass() );
87 * @covers ResourceLoader::getModuleNames
89 public function testGetModuleNames() {
90 // Use an empty one so that core and extension modules don't get in.
91 $resourceLoader = new EmptyResourceLoader();
92 $resourceLoader->register( 'test.foo', new ResourceLoaderTestModule() );
93 $resourceLoader->register( 'test.bar', new ResourceLoaderTestModule() );
95 [ 'test.foo', 'test.bar' ],
96 $resourceLoader->getModuleNames()
101 * @covers ResourceLoader::isModuleRegistered
103 public function testIsModuleRegistered() {
104 $rl = new EmptyResourceLoader();
105 $rl->register( 'test', new ResourceLoaderTestModule() );
106 $this->assertTrue( $rl->isModuleRegistered( 'test' ) );
107 $this->assertFalse( $rl->isModuleRegistered( 'test.unknown' ) );
111 * @covers ResourceLoader::getModule
113 public function testGetModuleUnknown() {
114 $rl = new EmptyResourceLoader();
115 $this->assertSame( null, $rl->getModule( 'test' ) );
119 * @covers ResourceLoader::getModule
121 public function testGetModuleClass() {
122 $rl = new EmptyResourceLoader();
123 $rl->register( 'test', [ 'class' => ResourceLoaderTestModule
::class ] );
124 $this->assertInstanceOf(
125 ResourceLoaderTestModule
::class,
126 $rl->getModule( 'test' )
131 * @covers ResourceLoader::getModule
133 public function testGetModuleClassDefault() {
134 $rl = new EmptyResourceLoader();
135 $rl->register( 'test', [] );
136 $this->assertInstanceOf(
137 ResourceLoaderFileModule
::class,
138 $rl->getModule( 'test' ),
139 'Array-style module registrations default to FileModule'
144 * @covers ResourceLoaderFileModule::compileLessFile
146 public function testLessFileCompilation() {
147 $context = $this->getResourceLoaderContext();
148 $basePath = __DIR__
. '/../../data/less/module';
149 $module = new ResourceLoaderFileModule( [
150 'localBasePath' => $basePath,
151 'styles' => [ 'styles.less' ],
153 $module->setName( 'test.less' );
154 $styles = $module->getStyles( $context );
155 $this->assertStringEqualsFile( $basePath . '/styles.css', $styles['all'] );
158 public static function providePackedModules() {
161 'Example from makePackedModulesString doc comment',
162 [ 'foo.bar', 'foo.baz', 'bar.baz', 'bar.quux' ],
163 'foo.bar,baz|bar.baz,quux',
166 'Example from expandModuleNames doc comment',
167 [ 'jquery.foo', 'jquery.bar', 'jquery.ui.baz', 'jquery.ui.quux' ],
168 'jquery.foo,bar|jquery.ui.baz,quux',
171 'Regression fixed in r88706 with dotless names',
172 [ 'foo', 'bar', 'baz' ],
176 'Prefixless modules after a prefixed module',
177 [ 'single.module', 'foobar', 'foobaz' ],
178 'single.module|foobar,foobaz',
182 [ 'foo', 'foo.baz', 'baz.quux', 'foo.bar' ],
183 'foo|foo.baz,bar|baz.quux',
184 [ 'foo', 'foo.baz', 'foo.bar', 'baz.quux' ],
190 * @dataProvider providePackedModules
191 * @covers ResourceLoader::makePackedModulesString
193 public function testMakePackedModulesString( $desc, $modules, $packed ) {
194 $this->assertEquals( $packed, ResourceLoader
::makePackedModulesString( $modules ), $desc );
198 * @dataProvider providePackedModules
199 * @covers ResourceLoaderContext::expandModuleNames
201 public function testExpandModuleNames( $desc, $modules, $packed, $unpacked = null ) {
203 $unpacked ?
: $modules,
204 ResourceLoaderContext
::expandModuleNames( $packed ),
209 public static function provideAddSource() {
211 [ 'foowiki', 'https://example.org/w/load.php', 'foowiki' ],
212 [ 'foowiki', [ 'loadScript' => 'https://example.org/w/load.php' ], 'foowiki' ],
215 'foowiki' => 'https://example.org/w/load.php',
216 'bazwiki' => 'https://example.com/w/load.php',
219 [ 'foowiki', 'bazwiki' ]
225 * @dataProvider provideAddSource
226 * @covers ResourceLoader::addSource
227 * @covers ResourceLoader::getSources
229 public function testAddSource( $name, $info, $expected ) {
230 $rl = new ResourceLoader
;
231 $rl->addSource( $name, $info );
232 if ( is_array( $expected ) ) {
233 foreach ( $expected as $source ) {
234 $this->assertArrayHasKey( $source, $rl->getSources() );
237 $this->assertArrayHasKey( $expected, $rl->getSources() );
242 * @covers ResourceLoader::addSource
244 public function testAddSourceDupe() {
245 $rl = new ResourceLoader
;
246 $this->setExpectedException( 'MWException', 'ResourceLoader duplicate source addition error' );
247 $rl->addSource( 'foo', 'https://example.org/w/load.php' );
248 $rl->addSource( 'foo', 'https://example.com/w/load.php' );
252 * @covers ResourceLoader::addSource
254 public function testAddSourceInvalid() {
255 $rl = new ResourceLoader
;
256 $this->setExpectedException( 'MWException', 'with no "loadScript" key' );
257 $rl->addSource( 'foo', [ 'x' => 'https://example.org/w/load.php' ] );
260 public static function provideLoaderImplement() {
263 'title' => 'Implement scripts, styles and messages',
265 'name' => 'test.example',
266 'scripts' => 'mw.example();',
267 'styles' => [ 'css' => [ '.mw-example {}' ] ],
268 'messages' => [ 'example' => '' ],
271 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
282 'title' => 'Implement scripts',
284 'name' => 'test.example',
285 'scripts' => 'mw.example();',
288 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
293 'title' => 'Implement styles',
295 'name' => 'test.example',
297 'styles' => [ 'css' => [ '.mw-example {}' ] ],
299 'expected' => 'mw.loader.implement( "test.example", [], {
306 'title' => 'Implement scripts and messages',
308 'name' => 'test.example',
309 'scripts' => 'mw.example();',
310 'messages' => [ 'example' => '' ],
312 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
319 'title' => 'Implement scripts and templates',
321 'name' => 'test.example',
322 'scripts' => 'mw.example();',
323 'templates' => [ 'example.html' => '' ],
325 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
332 'title' => 'Implement unwrapped user script',
335 'scripts' => 'mw.example( 1 );',
338 'expected' => 'mw.loader.implement( "user", "mw.example( 1 );" );',
344 * @dataProvider provideLoaderImplement
345 * @covers ResourceLoader::makeLoaderImplementScript
346 * @covers ResourceLoader::trimArray
348 public function testMakeLoaderImplementScript( $case ) {
351 'styles' => [], 'templates' => [], 'messages' => new XmlJsCode( '{}' )
353 ResourceLoader
::clearCache();
354 $this->setMwGlobals( 'wgResourceLoaderDebug', true );
356 $rl = TestingAccessWrapper
::newFromClass( 'ResourceLoader' );
359 $rl->makeLoaderImplementScript(
361 ( $case['wrap'] && is_string( $case['scripts'] ) )
362 ?
new XmlJsCode( $case['scripts'] )
372 * @covers ResourceLoader::makeLoaderImplementScript
374 public function testMakeLoaderImplementScriptInvalid() {
375 $this->setExpectedException( 'MWException', 'Invalid scripts error' );
376 $rl = TestingAccessWrapper
::newFromClass( 'ResourceLoader' );
377 $rl->makeLoaderImplementScript(
387 * @covers ResourceLoader::makeLoaderSourcesScript
389 public function testMakeLoaderSourcesScript() {
391 'mw.loader.addSource( "local", "/w/load.php" );',
392 ResourceLoader
::makeLoaderSourcesScript( 'local', '/w/load.php' )
395 'mw.loader.addSource( {
396 "local": "/w/load.php"
398 ResourceLoader
::makeLoaderSourcesScript( [ 'local' => '/w/load.php' ] )
401 'mw.loader.addSource( {
402 "local": "/w/load.php",
403 "example": "https://example.org/w/load.php"
405 ResourceLoader
::makeLoaderSourcesScript( [
406 'local' => '/w/load.php',
407 'example' => 'https://example.org/w/load.php'
411 'mw.loader.addSource( [] );',
412 ResourceLoader
::makeLoaderSourcesScript( [] )
416 private static function fakeSources() {
419 'loadScript' => '//example.org/w/load.php',
420 'apiScript' => '//example.org/w/api.php',
423 'loadScript' => '//example.com/w/load.php',
424 'apiScript' => '//example.com/w/api.php',
430 * @covers ResourceLoader::getLoadScript
432 public function testGetLoadScript() {
433 $this->setMwGlobals( 'wgResourceLoaderSources', [] );
434 $rl = new ResourceLoader();
435 $sources = self
::fakeSources();
436 $rl->addSource( $sources );
437 foreach ( [ 'examplewiki', 'example2wiki' ] as $name ) {
438 $this->assertEquals( $rl->getLoadScript( $name ), $sources[$name]['loadScript'] );
442 $rl->getLoadScript( 'thiswasneverreigstered' );
443 $this->assertTrue( false, 'ResourceLoader::getLoadScript should have thrown an exception' );
444 } catch ( MWException
$e ) {
445 $this->assertTrue( true );
449 protected function getFailFerryMock() {
450 $mock = $this->getMockBuilder( ResourceLoaderTestModule
::class )
451 ->setMethods( [ 'getScript' ] )
453 $mock->method( 'getScript' )->will( $this->throwException(
454 new Exception( 'Ferry not found' )
459 protected function getSimpleModuleMock( $script = '' ) {
460 $mock = $this->getMockBuilder( ResourceLoaderTestModule
::class )
461 ->setMethods( [ 'getScript' ] )
463 $mock->method( 'getScript' )->willReturn( $script );
468 * @covers ResourceLoader::getCombinedVersion
470 public function testGetCombinedVersion() {
471 $rl = new EmptyResourceLoader();
473 'foo' => self
::getSimpleModuleMock(),
474 'ferry' => self
::getFailFerryMock(),
475 'bar' => self
::getSimpleModuleMock(),
477 $context = $this->getResourceLoaderContext( [], $rl );
480 ResourceLoader
::makeHash( self
::BLANK_VERSION
),
481 $rl->getCombinedVersion( $context, [ 'foo' ] ),
485 // Verify that getCombinedVersion() does not throw when ferry fails.
486 // Instead it gracefully continues to combine the remaining modules.
488 ResourceLoader
::makeHash( self
::BLANK_VERSION
. self
::BLANK_VERSION
),
489 $rl->getCombinedVersion( $context, [ 'foo', 'ferry', 'bar' ] ),
490 'compute foo+ferry+bar (T152266)'
495 * Verify that when building module content in a load.php response,
496 * an exception from one module will not break script output from
499 public function testMakeModuleResponseError() {
501 'foo' => self
::getSimpleModuleMock( 'foo();' ),
502 'ferry' => self
::getFailFerryMock(),
503 'bar' => self
::getSimpleModuleMock( 'bar();' ),
505 $rl = new EmptyResourceLoader();
506 $rl->register( $modules );
507 $context = $this->getResourceLoaderContext(
509 'modules' => 'foo|ferry|bar',
515 $response = $rl->makeModuleResponse( $context, $modules );
516 $errors = $rl->getErrors();
518 $this->assertCount( 1, $errors );
519 $this->assertRegExp( '/Ferry not found/', $errors[0] );
521 'foo();bar();mw.loader.state( {
531 * Verify that when building the startup module response,
532 * an exception from one module class will not break the entire
533 * startup module response. See T152266.
535 public function testMakeModuleResponseStartupError() {
536 $rl = new EmptyResourceLoader();
538 'foo' => self
::getSimpleModuleMock( 'foo();' ),
539 'ferry' => self
::getFailFerryMock(),
540 'bar' => self
::getSimpleModuleMock( 'bar();' ),
541 'startup' => [ 'class' => 'ResourceLoaderStartUpModule' ],
543 $context = $this->getResourceLoaderContext(
545 'modules' => 'startup',
552 [ 'foo', 'ferry', 'bar', 'startup' ],
553 $rl->getModuleNames(),
557 $modules = [ 'startup' => $rl->getModule( 'startup' ) ];
558 $response = $rl->makeModuleResponse( $context, $modules );
559 $errors = $rl->getErrors();
561 $this->assertRegExp( '/Ferry not found/', $errors[0] );
562 $this->assertCount( 1, $errors );
564 '/isCompatible.*function startUp/s',
566 'startup response undisrupted (T152266)'
569 '/register\([^)]+"ferry",\s*""/s',
571 'startup response registers broken module'
574 '/state\([^)]+"ferry":\s*"error"/s',
576 'startup response sets state to error'