3 class ResourceLoaderStartUpModuleTest
extends ResourceLoaderTestCase
{
5 protected static function expandPlaceholders( $text ) {
7 '{blankVer}' => self
::BLANK_VERSION
11 public function provideGetModuleRegistrations() {
14 'msg' => 'Empty registry',
17 mw.loader.addSource( {
18 "local": "/w/load.php"
20 mw.loader.register( [] );'
23 'msg' => 'Basic registry',
25 'test.blank' => new ResourceLoaderTestModule(),
28 mw.loader.addSource( {
29 "local": "/w/load.php"
39 'msg' => 'Omit raw modules from registry',
41 'test.raw' => new ResourceLoaderTestModule( [ 'isRaw' => true ] ),
42 'test.blank' => new ResourceLoaderTestModule(),
45 mw.loader.addSource( {
46 "local": "/w/load.php"
56 'msg' => 'Version falls back gracefully if getVersionHash throws',
59 ( $mock = $this->getMockBuilder( 'ResourceLoaderTestModule' )
60 ->setMethods( [ 'getVersionHash' ] )->getMock() )
61 && $mock->method( 'getVersionHash' )->will(
62 $this->throwException( new Exception
)
67 mw.loader.addSource( {
68 "local": "/w/load.php"
81 'msg' => 'Use version from getVersionHash',
84 ( $mock = $this->getMockBuilder( 'ResourceLoaderTestModule' )
85 ->setMethods( [ 'getVersionHash' ] )->getMock() )
86 && $mock->method( 'getVersionHash' )->willReturn( '1234567' )
90 mw.loader.addSource( {
91 "local": "/w/load.php"
101 'msg' => 'Re-hash version from getVersionHash if too long',
104 ( $mock = $this->getMockBuilder( 'ResourceLoaderTestModule' )
105 ->setMethods( [ 'getVersionHash' ] )->getMock() )
106 && $mock->method( 'getVersionHash' )->willReturn( '12345678' )
110 mw.loader.addSource( {
111 "local": "/w/load.php"
113 mw.loader.register( [
121 'msg' => 'Group signature',
123 'test.blank' => new ResourceLoaderTestModule(),
124 'test.group.foo' => new ResourceLoaderTestModule( [ 'group' => 'x-foo' ] ),
125 'test.group.bar' => new ResourceLoaderTestModule( [ 'group' => 'x-bar' ] ),
128 mw.loader.addSource( {
129 "local": "/w/load.php"
131 mw.loader.register( [
151 'msg' => 'Different target (non-test should not be registered)',
153 'test.blank' => new ResourceLoaderTestModule(),
154 'test.target.foo' => new ResourceLoaderTestModule( [ 'targets' => [ 'x-foo' ] ] ),
157 mw.loader.addSource( {
158 "local": "/w/load.php"
160 mw.loader.register( [
168 'msg' => 'Foreign source',
171 'loadScript' => 'http://example.org/w/load.php',
172 'apiScript' => 'http://example.org/w/api.php',
176 'test.blank' => new ResourceLoaderTestModule( [ 'source' => 'example' ] ),
179 mw.loader.addSource( {
180 "local": "/w/load.php",
181 "example": "http://example.org/w/load.php"
183 mw.loader.register( [
194 'msg' => 'Conditional dependency function',
196 'test.x.core' => new ResourceLoaderTestModule(),
197 'test.x.polyfill' => new ResourceLoaderTestModule( [
198 'skipFunction' => 'return true;'
200 'test.y.polyfill' => new ResourceLoaderTestModule( [
208 'test.z.foo' => new ResourceLoaderTestModule( [
217 mw.loader.addSource( {
218 "local": "/w/load.php"
220 mw.loader.register( [
239 "return !!( window.JSON \u0026\u0026 JSON.parse \u0026\u0026 JSON.stringify);"
253 // This may seem like an edge case, but a plain MediaWiki core install
254 // with a few extensions installed is likely far more complex than this
255 // even, not to mention an install like Wikipedia.
256 // TODO: Make this even more realistic.
257 'msg' => 'Advanced (everything combined)',
260 'loadScript' => 'http://example.org/w/load.php',
261 'apiScript' => 'http://example.org/w/api.php',
265 'test.blank' => new ResourceLoaderTestModule(),
266 'test.x.core' => new ResourceLoaderTestModule(),
267 'test.x.util' => new ResourceLoaderTestModule( [
272 'test.x.foo' => new ResourceLoaderTestModule( [
277 'test.x.bar' => new ResourceLoaderTestModule( [
283 'test.x.quux' => new ResourceLoaderTestModule( [
291 'test.group.foo.1' => new ResourceLoaderTestModule( [
294 'test.group.foo.2' => new ResourceLoaderTestModule( [
297 'test.group.bar.1' => new ResourceLoaderTestModule( [
300 'test.group.bar.2' => new ResourceLoaderTestModule( [
302 'source' => 'example',
304 'test.target.foo' => new ResourceLoaderTestModule( [
305 'targets' => [ 'x-foo' ],
307 'test.target.bar' => new ResourceLoaderTestModule( [
308 'source' => 'example',
309 'targets' => [ 'x-foo' ],
313 mw.loader.addSource( {
314 "local": "/w/load.php",
315 "example": "http://example.org/w/load.php"
317 mw.loader.register( [
387 * @dataProvider provideGetModuleRegistrations
388 * @covers ResourceLoaderStartUpModule::getModuleRegistrations
389 * @covers ResourceLoaderStartUpModule::compileUnresolvedDependencies
390 * @covers ResourceLoader::makeLoaderRegisterScript
392 public function testGetModuleRegistrations( $case ) {
393 if ( isset( $case['sources'] ) ) {
394 $this->setMwGlobals( 'wgResourceLoaderSources', $case['sources'] );
397 $context = $this->getResourceLoaderContext();
398 $rl = $context->getResourceLoader();
399 $rl->register( $case['modules'] );
400 $module = new ResourceLoaderStartUpModule();
401 $out = ltrim( $case['out'], "\n" );
403 // Disable log from getModuleRegistrations via MWExceptionHandler
404 // for case where getVersionHash() is expected to throw.
405 $this->setLogger( 'exception', new Psr\Log\
NullLogger() );
408 self
::expandPlaceholders( $out ),
409 $module->getModuleRegistrations( $context ),
414 public static function provideRegistrations() {
417 'test.blank' => new ResourceLoaderTestModule(),
418 'test.min' => new ResourceLoaderTestModule( [
433 * @covers ResourceLoaderStartUpModule::getModuleRegistrations
434 * @dataProvider provideRegistrations
436 public function testRegistrationsMinified( $modules ) {
437 $this->setMwGlobals( 'wgResourceLoaderDebug', false );
439 $context = $this->getResourceLoaderContext();
440 $rl = $context->getResourceLoader();
441 $rl->register( $modules );
442 $module = new ResourceLoaderStartUpModule();
443 $out = 'mw.loader.addSource({"local":"/w/load.php"});' . "\n"
444 . 'mw.loader.register(['
445 . '["test.blank","{blankVer}"],'
446 . '["test.min","{blankVer}",[0],null,null,'
447 . '"return!!(window.JSON\u0026\u0026JSON.parse\u0026\u0026JSON.stringify);"'
451 self
::expandPlaceholders( $out ),
452 $module->getModuleRegistrations( $context ),
458 * @covers ResourceLoaderStartUpModule::getModuleRegistrations
459 * @dataProvider provideRegistrations
461 public function testRegistrationsUnminified( $modules ) {
462 $context = $this->getResourceLoaderContext();
463 $rl = $context->getResourceLoader();
464 $rl->register( $modules );
465 $module = new ResourceLoaderStartUpModule();
467 'mw.loader.addSource( {
468 "local": "/w/load.php"
470 mw.loader.register( [
483 "return !!( window.JSON \u0026\u0026 JSON.parse \u0026\u0026 JSON.stringify);"
488 self
::expandPlaceholders( $out ),
489 $module->getModuleRegistrations( $context ),