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.
21 'wgShowExceptionDetails' => true,
26 * Ensure the ResourceLoaderRegisterModules hook is called.
28 * @covers ResourceLoader::__construct
30 public function testConstructRegistrationHook() {
31 $resourceLoaderRegisterModulesHook = false;
33 $this->setMwGlobals( 'wgHooks', [
34 'ResourceLoaderRegisterModules' => [
35 function ( &$resourceLoader ) use ( &$resourceLoaderRegisterModulesHook ) {
36 $resourceLoaderRegisterModulesHook = true;
41 $unused = new ResourceLoader();
43 $resourceLoaderRegisterModulesHook,
44 'Hook ResourceLoaderRegisterModules called'
49 * @covers ResourceLoader::register
50 * @covers ResourceLoader::getModule
52 public function testRegisterValid() {
53 $module = new ResourceLoaderTestModule();
54 $resourceLoader = new EmptyResourceLoader();
55 $resourceLoader->register( 'test', $module );
56 $this->assertEquals( $module, $resourceLoader->getModule( 'test' ) );
60 * @covers ResourceLoader::register
62 public function testRegisterEmptyString() {
63 $module = new ResourceLoaderTestModule();
64 $resourceLoader = new EmptyResourceLoader();
65 $resourceLoader->register( '', $module );
66 $this->assertEquals( $module, $resourceLoader->getModule( '' ) );
70 * @covers ResourceLoader::register
72 public function testRegisterInvalidName() {
73 $resourceLoader = new EmptyResourceLoader();
74 $this->setExpectedException( 'MWException', "name 'test!invalid' is invalid" );
75 $resourceLoader->register( 'test!invalid', new ResourceLoaderTestModule() );
79 * @covers ResourceLoader::register
81 public function testRegisterInvalidType() {
82 $resourceLoader = new EmptyResourceLoader();
83 $this->setExpectedException( 'MWException', 'ResourceLoader module info type error' );
84 $resourceLoader->register( 'test', new stdClass() );
88 * @covers ResourceLoader::getModuleNames
90 public function testGetModuleNames() {
91 // Use an empty one so that core and extension modules don't get in.
92 $resourceLoader = new EmptyResourceLoader();
93 $resourceLoader->register( 'test.foo', new ResourceLoaderTestModule() );
94 $resourceLoader->register( 'test.bar', new ResourceLoaderTestModule() );
96 [ 'test.foo', 'test.bar' ],
97 $resourceLoader->getModuleNames()
102 * @covers ResourceLoader::isModuleRegistered
104 public function testIsModuleRegistered() {
105 $rl = new EmptyResourceLoader();
106 $rl->register( 'test', new ResourceLoaderTestModule() );
107 $this->assertTrue( $rl->isModuleRegistered( 'test' ) );
108 $this->assertFalse( $rl->isModuleRegistered( 'test.unknown' ) );
112 * @covers ResourceLoader::getModule
114 public function testGetModuleUnknown() {
115 $rl = new EmptyResourceLoader();
116 $this->assertSame( null, $rl->getModule( 'test' ) );
120 * @covers ResourceLoader::getModule
122 public function testGetModuleClass() {
123 $rl = new EmptyResourceLoader();
124 $rl->register( 'test', [ 'class' => ResourceLoaderTestModule
::class ] );
125 $this->assertInstanceOf(
126 ResourceLoaderTestModule
::class,
127 $rl->getModule( 'test' )
132 * @covers ResourceLoader::getModule
134 public function testGetModuleClassDefault() {
135 $rl = new EmptyResourceLoader();
136 $rl->register( 'test', [] );
137 $this->assertInstanceOf(
138 ResourceLoaderFileModule
::class,
139 $rl->getModule( 'test' ),
140 'Array-style module registrations default to FileModule'
145 * @covers ResourceLoaderFileModule::compileLessFile
147 public function testLessFileCompilation() {
148 $context = $this->getResourceLoaderContext();
149 $basePath = __DIR__
. '/../../data/less/module';
150 $module = new ResourceLoaderFileModule( [
151 'localBasePath' => $basePath,
152 'styles' => [ 'styles.less' ],
154 $module->setName( 'test.less' );
155 $styles = $module->getStyles( $context );
156 $this->assertStringEqualsFile( $basePath . '/styles.css', $styles['all'] );
159 public static function providePackedModules() {
162 'Example from makePackedModulesString doc comment',
163 [ 'foo.bar', 'foo.baz', 'bar.baz', 'bar.quux' ],
164 'foo.bar,baz|bar.baz,quux',
167 'Example from expandModuleNames doc comment',
168 [ 'jquery.foo', 'jquery.bar', 'jquery.ui.baz', 'jquery.ui.quux' ],
169 'jquery.foo,bar|jquery.ui.baz,quux',
172 'Regression fixed in r88706 with dotless names',
173 [ 'foo', 'bar', 'baz' ],
177 'Prefixless modules after a prefixed module',
178 [ 'single.module', 'foobar', 'foobaz' ],
179 'single.module|foobar,foobaz',
183 [ 'foo', 'foo.baz', 'baz.quux', 'foo.bar' ],
184 'foo|foo.baz,bar|baz.quux',
185 [ 'foo', 'foo.baz', 'foo.bar', 'baz.quux' ],
191 * @dataProvider providePackedModules
192 * @covers ResourceLoader::makePackedModulesString
194 public function testMakePackedModulesString( $desc, $modules, $packed ) {
195 $this->assertEquals( $packed, ResourceLoader
::makePackedModulesString( $modules ), $desc );
199 * @dataProvider providePackedModules
200 * @covers ResourceLoaderContext::expandModuleNames
202 public function testExpandModuleNames( $desc, $modules, $packed, $unpacked = null ) {
204 $unpacked ?
: $modules,
205 ResourceLoaderContext
::expandModuleNames( $packed ),
210 public static function provideAddSource() {
212 [ 'foowiki', 'https://example.org/w/load.php', 'foowiki' ],
213 [ 'foowiki', [ 'loadScript' => 'https://example.org/w/load.php' ], 'foowiki' ],
216 'foowiki' => 'https://example.org/w/load.php',
217 'bazwiki' => 'https://example.com/w/load.php',
220 [ 'foowiki', 'bazwiki' ]
226 * @dataProvider provideAddSource
227 * @covers ResourceLoader::addSource
228 * @covers ResourceLoader::getSources
230 public function testAddSource( $name, $info, $expected ) {
231 $rl = new ResourceLoader
;
232 $rl->addSource( $name, $info );
233 if ( is_array( $expected ) ) {
234 foreach ( $expected as $source ) {
235 $this->assertArrayHasKey( $source, $rl->getSources() );
238 $this->assertArrayHasKey( $expected, $rl->getSources() );
243 * @covers ResourceLoader::addSource
245 public function testAddSourceDupe() {
246 $rl = new ResourceLoader
;
247 $this->setExpectedException( 'MWException', 'ResourceLoader duplicate source addition error' );
248 $rl->addSource( 'foo', 'https://example.org/w/load.php' );
249 $rl->addSource( 'foo', 'https://example.com/w/load.php' );
253 * @covers ResourceLoader::addSource
255 public function testAddSourceInvalid() {
256 $rl = new ResourceLoader
;
257 $this->setExpectedException( 'MWException', 'with no "loadScript" key' );
258 $rl->addSource( 'foo', [ 'x' => 'https://example.org/w/load.php' ] );
261 public static function provideLoaderImplement() {
264 'title' => 'Implement scripts, styles and messages',
266 'name' => 'test.example',
267 'scripts' => 'mw.example();',
268 'styles' => [ 'css' => [ '.mw-example {}' ] ],
269 'messages' => [ 'example' => '' ],
272 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
283 'title' => 'Implement scripts',
285 'name' => 'test.example',
286 'scripts' => 'mw.example();',
289 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
294 'title' => 'Implement styles',
296 'name' => 'test.example',
298 'styles' => [ 'css' => [ '.mw-example {}' ] ],
300 'expected' => 'mw.loader.implement( "test.example", [], {
307 'title' => 'Implement scripts and messages',
309 'name' => 'test.example',
310 'scripts' => 'mw.example();',
311 'messages' => [ 'example' => '' ],
313 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
320 'title' => 'Implement scripts and templates',
322 'name' => 'test.example',
323 'scripts' => 'mw.example();',
324 'templates' => [ 'example.html' => '' ],
326 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
333 'title' => 'Implement unwrapped user script',
336 'scripts' => 'mw.example( 1 );',
339 'expected' => 'mw.loader.implement( "user", "mw.example( 1 );" );',
345 * @dataProvider provideLoaderImplement
346 * @covers ResourceLoader::makeLoaderImplementScript
347 * @covers ResourceLoader::trimArray
349 public function testMakeLoaderImplementScript( $case ) {
352 'styles' => [], 'templates' => [], 'messages' => new XmlJsCode( '{}' )
354 ResourceLoader
::clearCache();
355 $this->setMwGlobals( 'wgResourceLoaderDebug', true );
357 $rl = TestingAccessWrapper
::newFromClass( 'ResourceLoader' );
360 $rl->makeLoaderImplementScript(
362 ( $case['wrap'] && is_string( $case['scripts'] ) )
363 ?
new XmlJsCode( $case['scripts'] )
373 * @covers ResourceLoader::makeLoaderImplementScript
375 public function testMakeLoaderImplementScriptInvalid() {
376 $this->setExpectedException( 'MWException', 'Invalid scripts error' );
377 $rl = TestingAccessWrapper
::newFromClass( 'ResourceLoader' );
378 $rl->makeLoaderImplementScript(
388 * @covers ResourceLoader::makeLoaderSourcesScript
390 public function testMakeLoaderSourcesScript() {
392 'mw.loader.addSource( "local", "/w/load.php" );',
393 ResourceLoader
::makeLoaderSourcesScript( 'local', '/w/load.php' )
396 'mw.loader.addSource( {
397 "local": "/w/load.php"
399 ResourceLoader
::makeLoaderSourcesScript( [ 'local' => '/w/load.php' ] )
402 'mw.loader.addSource( {
403 "local": "/w/load.php",
404 "example": "https://example.org/w/load.php"
406 ResourceLoader
::makeLoaderSourcesScript( [
407 'local' => '/w/load.php',
408 'example' => 'https://example.org/w/load.php'
412 'mw.loader.addSource( [] );',
413 ResourceLoader
::makeLoaderSourcesScript( [] )
417 private static function fakeSources() {
420 'loadScript' => '//example.org/w/load.php',
421 'apiScript' => '//example.org/w/api.php',
424 'loadScript' => '//example.com/w/load.php',
425 'apiScript' => '//example.com/w/api.php',
431 * @covers ResourceLoader::getLoadScript
433 public function testGetLoadScript() {
434 $this->setMwGlobals( 'wgResourceLoaderSources', [] );
435 $rl = new ResourceLoader();
436 $sources = self
::fakeSources();
437 $rl->addSource( $sources );
438 foreach ( [ 'examplewiki', 'example2wiki' ] as $name ) {
439 $this->assertEquals( $rl->getLoadScript( $name ), $sources[$name]['loadScript'] );
443 $rl->getLoadScript( 'thiswasneverreigstered' );
444 $this->assertTrue( false, 'ResourceLoader::getLoadScript should have thrown an exception' );
445 } catch ( MWException
$e ) {
446 $this->assertTrue( true );
450 protected function getFailFerryMock() {
451 $mock = $this->getMockBuilder( ResourceLoaderTestModule
::class )
452 ->setMethods( [ 'getScript' ] )
454 $mock->method( 'getScript' )->will( $this->throwException(
455 new Exception( 'Ferry not found' )
460 protected function getSimpleModuleMock( $script = '' ) {
461 $mock = $this->getMockBuilder( ResourceLoaderTestModule
::class )
462 ->setMethods( [ 'getScript' ] )
464 $mock->method( 'getScript' )->willReturn( $script );
469 * @covers ResourceLoader::getCombinedVersion
471 public function testGetCombinedVersion() {
472 $rl = new EmptyResourceLoader();
474 'foo' => self
::getSimpleModuleMock(),
475 'ferry' => self
::getFailFerryMock(),
476 'bar' => self
::getSimpleModuleMock(),
478 $context = $this->getResourceLoaderContext( [], $rl );
481 ResourceLoader
::makeHash( self
::BLANK_VERSION
),
482 $rl->getCombinedVersion( $context, [ 'foo' ] ),
486 // Verify that getCombinedVersion() does not throw when ferry fails.
487 // Instead it gracefully continues to combine the remaining modules.
489 ResourceLoader
::makeHash( self
::BLANK_VERSION
. self
::BLANK_VERSION
),
490 $rl->getCombinedVersion( $context, [ 'foo', 'ferry', 'bar' ] ),
491 'compute foo+ferry+bar (T152266)'
496 * Verify that when building module content in a load.php response,
497 * an exception from one module will not break script output from
500 public function testMakeModuleResponseError() {
502 'foo' => self
::getSimpleModuleMock( 'foo();' ),
503 'ferry' => self
::getFailFerryMock(),
504 'bar' => self
::getSimpleModuleMock( 'bar();' ),
506 $rl = new EmptyResourceLoader();
507 $rl->register( $modules );
508 $context = $this->getResourceLoaderContext(
510 'modules' => 'foo|ferry|bar',
516 $response = $rl->makeModuleResponse( $context, $modules );
517 $errors = $rl->getErrors();
519 $this->assertCount( 1, $errors );
520 $this->assertRegExp( '/Ferry not found/', $errors[0] );
522 'foo();bar();mw.loader.state( {
532 * Verify that when building the startup module response,
533 * an exception from one module class will not break the entire
534 * startup module response. See T152266.
536 public function testMakeModuleResponseStartupError() {
537 $rl = new EmptyResourceLoader();
539 'foo' => self
::getSimpleModuleMock( 'foo();' ),
540 'ferry' => self
::getFailFerryMock(),
541 'bar' => self
::getSimpleModuleMock( 'bar();' ),
542 'startup' => [ 'class' => 'ResourceLoaderStartUpModule' ],
544 $context = $this->getResourceLoaderContext(
546 'modules' => 'startup',
553 [ 'foo', 'ferry', 'bar', 'startup' ],
554 $rl->getModuleNames(),
558 $modules = [ 'startup' => $rl->getModule( 'startup' ) ];
559 $response = $rl->makeModuleResponse( $context, $modules );
560 $errors = $rl->getErrors();
562 $this->assertRegExp( '/Ferry not found/', $errors[0] );
563 $this->assertCount( 1, $errors );
565 '/isCompatible.*function startUp/s',
567 'startup response undisrupted (T152266)'
570 '/register\([^)]+"ferry",\s*""/s',
572 'startup response registers broken module'
575 '/state\([^)]+"ferry":\s*"error"/s',
577 'startup response sets state to error'