Merge "filecache: Use current action instead of "view" only in outage mode"
[lhc/web/wiklou.git] / tests / phpunit / includes / resourceloader / ResourceLoaderTest.php
1 <?php
2
3 use Wikimedia\TestingAccessWrapper;
4
5 class ResourceLoaderTest extends ResourceLoaderTestCase {
6
7 protected function setUp() {
8 parent::setUp();
9
10 $this->setMwGlobals( [
11 'wgResourceLoaderLESSImportPaths' => [
12 dirname( dirname( __DIR__ ) ) . '/data/less/common',
13 ],
14 'wgResourceLoaderLESSVars' => [
15 'foo' => '2px',
16 'Foo' => '#eeeeee',
17 'bar' => 5,
18 ],
19 // Clear ResourceLoaderGetConfigVars hooks (called by StartupModule)
20 // to avoid notices during testMakeModuleResponse for missing
21 // wgResourceLoaderLESSVars keys in extension hooks.
22 'wgHooks' => [],
23 'wgShowExceptionDetails' => true,
24 ] );
25 }
26
27 /**
28 * Ensure the ResourceLoaderRegisterModules hook is called.
29 *
30 * @covers ResourceLoader::__construct
31 */
32 public function testConstructRegistrationHook() {
33 $resourceLoaderRegisterModulesHook = false;
34
35 $this->setMwGlobals( 'wgHooks', [
36 'ResourceLoaderRegisterModules' => [
37 function ( &$resourceLoader ) use ( &$resourceLoaderRegisterModulesHook ) {
38 $resourceLoaderRegisterModulesHook = true;
39 }
40 ]
41 ] );
42
43 $unused = new ResourceLoader();
44 $this->assertTrue(
45 $resourceLoaderRegisterModulesHook,
46 'Hook ResourceLoaderRegisterModules called'
47 );
48 }
49
50 /**
51 * @covers ResourceLoader::register
52 * @covers ResourceLoader::getModule
53 */
54 public function testRegisterValidObject() {
55 $module = new ResourceLoaderTestModule();
56 $resourceLoader = new EmptyResourceLoader();
57 $resourceLoader->register( 'test', $module );
58 $this->assertEquals( $module, $resourceLoader->getModule( 'test' ) );
59 }
60
61 /**
62 * @covers ResourceLoader::register
63 * @covers ResourceLoader::getModule
64 */
65 public function testRegisterValidArray() {
66 $module = new ResourceLoaderTestModule();
67 $resourceLoader = new EmptyResourceLoader();
68 // Covers case of register() setting $rl->moduleInfos,
69 // but $rl->modules lazy-populated by getModule()
70 $resourceLoader->register( 'test', [ 'object' => $module ] );
71 $this->assertEquals( $module, $resourceLoader->getModule( 'test' ) );
72 }
73
74 /**
75 * @covers ResourceLoader::register
76 */
77 public function testRegisterEmptyString() {
78 $module = new ResourceLoaderTestModule();
79 $resourceLoader = new EmptyResourceLoader();
80 $resourceLoader->register( '', $module );
81 $this->assertEquals( $module, $resourceLoader->getModule( '' ) );
82 }
83
84 /**
85 * @covers ResourceLoader::register
86 */
87 public function testRegisterInvalidName() {
88 $resourceLoader = new EmptyResourceLoader();
89 $this->setExpectedException( 'MWException', "name 'test!invalid' is invalid" );
90 $resourceLoader->register( 'test!invalid', new ResourceLoaderTestModule() );
91 }
92
93 /**
94 * @covers ResourceLoader::register
95 */
96 public function testRegisterInvalidType() {
97 $resourceLoader = new EmptyResourceLoader();
98 $this->setExpectedException( 'MWException', 'ResourceLoader module info type error' );
99 $resourceLoader->register( 'test', new stdClass() );
100 }
101
102 /**
103 * @covers ResourceLoader::getModuleNames
104 */
105 public function testGetModuleNames() {
106 // Use an empty one so that core and extension modules don't get in.
107 $resourceLoader = new EmptyResourceLoader();
108 $resourceLoader->register( 'test.foo', new ResourceLoaderTestModule() );
109 $resourceLoader->register( 'test.bar', new ResourceLoaderTestModule() );
110 $this->assertEquals(
111 [ 'test.foo', 'test.bar' ],
112 $resourceLoader->getModuleNames()
113 );
114 }
115
116 public function provideTestIsFileModule() {
117 $fileModuleObj = $this->getMockBuilder( ResourceLoaderFileModule::class )
118 ->disableOriginalConstructor()
119 ->getMock();
120 return [
121 'object' => [ false,
122 new ResourceLoaderTestModule()
123 ],
124 'FileModule object' => [ false,
125 $fileModuleObj
126 ],
127 'simple empty' => [ true,
128 []
129 ],
130 'simple scripts' => [ true,
131 [ 'scripts' => 'example.js' ]
132 ],
133 'simple scripts, raw and targets' => [ true, [
134 'scripts' => [ 'a.js', 'b.js' ],
135 'raw' => true,
136 'targets' => [ 'desktop', 'mobile' ],
137 ] ],
138 'FileModule' => [ true,
139 [ 'class' => ResourceLoaderFileModule::class, 'scripts' => 'example.js' ]
140 ],
141 'TestModule' => [ false,
142 [ 'class' => ResourceLoaderTestModule::class, 'scripts' => 'example.js' ]
143 ],
144 'SkinModule (FileModule subclass)' => [ true,
145 [ 'class' => ResourceLoaderSkinModule::class, 'scripts' => 'example.js' ]
146 ],
147 'JqueryMsgModule (FileModule subclass)' => [ true, [
148 'class' => ResourceLoaderJqueryMsgModule::class,
149 'scripts' => 'example.js',
150 ] ],
151 'WikiModule' => [ false, [
152 'class' => ResourceLoaderWikiModule::class,
153 'scripts' => [ 'MediaWiki:Example.js' ],
154 ] ],
155 ];
156 }
157
158 /**
159 * @dataProvider provideTestIsFileModule
160 * @covers ResourceLoader::isFileModule
161 */
162 public function testIsFileModule( $expected, $module ) {
163 $rl = TestingAccessWrapper::newFromObject( new EmptyResourceLoader() );
164 $rl->register( 'test', $module );
165 $this->assertSame( $expected, $rl->isFileModule( 'test' ) );
166 }
167
168 /**
169 * @covers ResourceLoader::isFileModule
170 */
171 public function testIsFileModuleUnknown() {
172 $rl = TestingAccessWrapper::newFromObject( new EmptyResourceLoader() );
173 $this->assertSame( false, $rl->isFileModule( 'unknown' ) );
174 }
175
176 /**
177 * @covers ResourceLoader::isModuleRegistered
178 */
179 public function testIsModuleRegistered() {
180 $rl = new EmptyResourceLoader();
181 $rl->register( 'test', new ResourceLoaderTestModule() );
182 $this->assertTrue( $rl->isModuleRegistered( 'test' ) );
183 $this->assertFalse( $rl->isModuleRegistered( 'test.unknown' ) );
184 }
185
186 /**
187 * @covers ResourceLoader::getModule
188 */
189 public function testGetModuleUnknown() {
190 $rl = new EmptyResourceLoader();
191 $this->assertSame( null, $rl->getModule( 'test' ) );
192 }
193
194 /**
195 * @covers ResourceLoader::getModule
196 */
197 public function testGetModuleClass() {
198 $rl = new EmptyResourceLoader();
199 $rl->register( 'test', [ 'class' => ResourceLoaderTestModule::class ] );
200 $this->assertInstanceOf(
201 ResourceLoaderTestModule::class,
202 $rl->getModule( 'test' )
203 );
204 }
205
206 /**
207 * @covers ResourceLoader::getModule
208 */
209 public function testGetModuleFactory() {
210 $factory = function ( array $info ) {
211 $this->assertArrayHasKey( 'kitten', $info );
212 return new ResourceLoaderTestModule( $info );
213 };
214
215 $rl = new EmptyResourceLoader();
216 $rl->register( 'test', [ 'factory' => $factory, 'kitten' => 'little ball of fur' ] );
217 $this->assertInstanceOf(
218 ResourceLoaderTestModule::class,
219 $rl->getModule( 'test' )
220 );
221 }
222
223 /**
224 * @covers ResourceLoader::getModule
225 */
226 public function testGetModuleClassDefault() {
227 $rl = new EmptyResourceLoader();
228 $rl->register( 'test', [] );
229 $this->assertInstanceOf(
230 ResourceLoaderFileModule::class,
231 $rl->getModule( 'test' ),
232 'Array-style module registrations default to FileModule'
233 );
234 }
235
236 /**
237 * @covers ResourceLoaderFileModule::compileLessFile
238 */
239 public function testLessFileCompilation() {
240 $context = $this->getResourceLoaderContext();
241 $basePath = __DIR__ . '/../../data/less/module';
242 $module = new ResourceLoaderFileModule( [
243 'localBasePath' => $basePath,
244 'styles' => [ 'styles.less' ],
245 ] );
246 $module->setName( 'test.less' );
247 $styles = $module->getStyles( $context );
248 $this->assertStringEqualsFile( $basePath . '/styles.css', $styles['all'] );
249 }
250
251 public static function providePackedModules() {
252 return [
253 [
254 'Example from makePackedModulesString doc comment',
255 [ 'foo.bar', 'foo.baz', 'bar.baz', 'bar.quux' ],
256 'foo.bar,baz|bar.baz,quux',
257 ],
258 [
259 'Example from expandModuleNames doc comment',
260 [ 'jquery.foo', 'jquery.bar', 'jquery.ui.baz', 'jquery.ui.quux' ],
261 'jquery.foo,bar|jquery.ui.baz,quux',
262 ],
263 [
264 'Regression fixed in r88706 with dotless names',
265 [ 'foo', 'bar', 'baz' ],
266 'foo,bar,baz',
267 ],
268 [
269 'Prefixless modules after a prefixed module',
270 [ 'single.module', 'foobar', 'foobaz' ],
271 'single.module|foobar,foobaz',
272 ],
273 [
274 'Ordering',
275 [ 'foo', 'foo.baz', 'baz.quux', 'foo.bar' ],
276 'foo|foo.baz,bar|baz.quux',
277 [ 'foo', 'foo.baz', 'foo.bar', 'baz.quux' ],
278 ]
279 ];
280 }
281
282 /**
283 * @dataProvider providePackedModules
284 * @covers ResourceLoader::makePackedModulesString
285 */
286 public function testMakePackedModulesString( $desc, $modules, $packed ) {
287 $this->assertEquals( $packed, ResourceLoader::makePackedModulesString( $modules ), $desc );
288 }
289
290 /**
291 * @dataProvider providePackedModules
292 * @covers ResourceLoaderContext::expandModuleNames
293 */
294 public function testExpandModuleNames( $desc, $modules, $packed, $unpacked = null ) {
295 $this->assertEquals(
296 $unpacked ?: $modules,
297 ResourceLoaderContext::expandModuleNames( $packed ),
298 $desc
299 );
300 }
301
302 public static function provideAddSource() {
303 return [
304 [ 'foowiki', 'https://example.org/w/load.php', 'foowiki' ],
305 [ 'foowiki', [ 'loadScript' => 'https://example.org/w/load.php' ], 'foowiki' ],
306 [
307 [
308 'foowiki' => 'https://example.org/w/load.php',
309 'bazwiki' => 'https://example.com/w/load.php',
310 ],
311 null,
312 [ 'foowiki', 'bazwiki' ]
313 ]
314 ];
315 }
316
317 /**
318 * @dataProvider provideAddSource
319 * @covers ResourceLoader::addSource
320 * @covers ResourceLoader::getSources
321 */
322 public function testAddSource( $name, $info, $expected ) {
323 $rl = new ResourceLoader;
324 $rl->addSource( $name, $info );
325 if ( is_array( $expected ) ) {
326 foreach ( $expected as $source ) {
327 $this->assertArrayHasKey( $source, $rl->getSources() );
328 }
329 } else {
330 $this->assertArrayHasKey( $expected, $rl->getSources() );
331 }
332 }
333
334 /**
335 * @covers ResourceLoader::addSource
336 */
337 public function testAddSourceDupe() {
338 $rl = new ResourceLoader;
339 $this->setExpectedException( 'MWException', 'ResourceLoader duplicate source addition error' );
340 $rl->addSource( 'foo', 'https://example.org/w/load.php' );
341 $rl->addSource( 'foo', 'https://example.com/w/load.php' );
342 }
343
344 /**
345 * @covers ResourceLoader::addSource
346 */
347 public function testAddSourceInvalid() {
348 $rl = new ResourceLoader;
349 $this->setExpectedException( 'MWException', 'with no "loadScript" key' );
350 $rl->addSource( 'foo', [ 'x' => 'https://example.org/w/load.php' ] );
351 }
352
353 public static function provideLoaderImplement() {
354 return [
355 [ [
356 'title' => 'Implement scripts, styles and messages',
357
358 'name' => 'test.example',
359 'scripts' => 'mw.example();',
360 'styles' => [ 'css' => [ '.mw-example {}' ] ],
361 'messages' => [ 'example' => '' ],
362 'templates' => [],
363
364 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
365 mw.example();
366 }, {
367 "css": [
368 ".mw-example {}"
369 ]
370 }, {
371 "example": ""
372 } );',
373 ] ],
374 [ [
375 'title' => 'Implement scripts',
376
377 'name' => 'test.example',
378 'scripts' => 'mw.example();',
379 'styles' => [],
380
381 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
382 mw.example();
383 } );',
384 ] ],
385 [ [
386 'title' => 'Implement styles',
387
388 'name' => 'test.example',
389 'scripts' => [],
390 'styles' => [ 'css' => [ '.mw-example {}' ] ],
391
392 'expected' => 'mw.loader.implement( "test.example", [], {
393 "css": [
394 ".mw-example {}"
395 ]
396 } );',
397 ] ],
398 [ [
399 'title' => 'Implement scripts and messages',
400
401 'name' => 'test.example',
402 'scripts' => 'mw.example();',
403 'messages' => [ 'example' => '' ],
404
405 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
406 mw.example();
407 }, {}, {
408 "example": ""
409 } );',
410 ] ],
411 [ [
412 'title' => 'Implement scripts and templates',
413
414 'name' => 'test.example',
415 'scripts' => 'mw.example();',
416 'templates' => [ 'example.html' => '' ],
417
418 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
419 mw.example();
420 }, {}, {}, {
421 "example.html": ""
422 } );',
423 ] ],
424 [ [
425 'title' => 'Implement unwrapped user script',
426
427 'name' => 'user',
428 'scripts' => 'mw.example( 1 );',
429 'wrap' => false,
430
431 'expected' => 'mw.loader.implement( "user", "mw.example( 1 );" );',
432 ] ],
433 ];
434 }
435
436 /**
437 * @dataProvider provideLoaderImplement
438 * @covers ResourceLoader::makeLoaderImplementScript
439 * @covers ResourceLoader::trimArray
440 */
441 public function testMakeLoaderImplementScript( $case ) {
442 $case += [
443 'wrap' => true,
444 'styles' => [], 'templates' => [], 'messages' => new XmlJsCode( '{}' )
445 ];
446 ResourceLoader::clearCache();
447 $this->setMwGlobals( 'wgResourceLoaderDebug', true );
448
449 $rl = TestingAccessWrapper::newFromClass( 'ResourceLoader' );
450 $this->assertEquals(
451 $case['expected'],
452 $rl->makeLoaderImplementScript(
453 $case['name'],
454 ( $case['wrap'] && is_string( $case['scripts'] ) )
455 ? new XmlJsCode( $case['scripts'] )
456 : $case['scripts'],
457 $case['styles'],
458 $case['messages'],
459 $case['templates']
460 )
461 );
462 }
463
464 /**
465 * @covers ResourceLoader::makeLoaderImplementScript
466 */
467 public function testMakeLoaderImplementScriptInvalid() {
468 $this->setExpectedException( 'MWException', 'Invalid scripts error' );
469 $rl = TestingAccessWrapper::newFromClass( 'ResourceLoader' );
470 $rl->makeLoaderImplementScript(
471 'test', // name
472 123, // scripts
473 null, // styles
474 null, // messages
475 null // templates
476 );
477 }
478
479 /**
480 * @covers ResourceLoader::makeLoaderRegisterScript
481 */
482 public function testMakeLoaderRegisterScript() {
483 $this->assertEquals(
484 'mw.loader.register( [
485 [
486 "test.name",
487 "1234567"
488 ]
489 ] );',
490 ResourceLoader::makeLoaderRegisterScript( [
491 [ 'test.name', '1234567' ],
492 ] ),
493 'Nested array parameter'
494 );
495
496 $this->assertEquals(
497 'mw.loader.register( "test.name", "1234567" );',
498 ResourceLoader::makeLoaderRegisterScript(
499 'test.name',
500 '1234567'
501 ),
502 'Variadic parameters'
503 );
504 }
505
506 /**
507 * @covers ResourceLoader::makeLoaderSourcesScript
508 */
509 public function testMakeLoaderSourcesScript() {
510 $this->assertEquals(
511 'mw.loader.addSource( "local", "/w/load.php" );',
512 ResourceLoader::makeLoaderSourcesScript( 'local', '/w/load.php' )
513 );
514 $this->assertEquals(
515 'mw.loader.addSource( {
516 "local": "/w/load.php"
517 } );',
518 ResourceLoader::makeLoaderSourcesScript( [ 'local' => '/w/load.php' ] )
519 );
520 $this->assertEquals(
521 'mw.loader.addSource( {
522 "local": "/w/load.php",
523 "example": "https://example.org/w/load.php"
524 } );',
525 ResourceLoader::makeLoaderSourcesScript( [
526 'local' => '/w/load.php',
527 'example' => 'https://example.org/w/load.php'
528 ] )
529 );
530 $this->assertEquals(
531 'mw.loader.addSource( [] );',
532 ResourceLoader::makeLoaderSourcesScript( [] )
533 );
534 }
535
536 private static function fakeSources() {
537 return [
538 'examplewiki' => [
539 'loadScript' => '//example.org/w/load.php',
540 'apiScript' => '//example.org/w/api.php',
541 ],
542 'example2wiki' => [
543 'loadScript' => '//example.com/w/load.php',
544 'apiScript' => '//example.com/w/api.php',
545 ],
546 ];
547 }
548
549 /**
550 * @covers ResourceLoader::getLoadScript
551 */
552 public function testGetLoadScript() {
553 $this->setMwGlobals( 'wgResourceLoaderSources', [] );
554 $rl = new ResourceLoader();
555 $sources = self::fakeSources();
556 $rl->addSource( $sources );
557 foreach ( [ 'examplewiki', 'example2wiki' ] as $name ) {
558 $this->assertEquals( $rl->getLoadScript( $name ), $sources[$name]['loadScript'] );
559 }
560
561 try {
562 $rl->getLoadScript( 'thiswasneverreigstered' );
563 $this->assertTrue( false, 'ResourceLoader::getLoadScript should have thrown an exception' );
564 } catch ( MWException $e ) {
565 $this->assertTrue( true );
566 }
567 }
568
569 protected function getFailFerryMock() {
570 $mock = $this->getMockBuilder( ResourceLoaderTestModule::class )
571 ->setMethods( [ 'getScript' ] )
572 ->getMock();
573 $mock->method( 'getScript' )->will( $this->throwException(
574 new Exception( 'Ferry not found' )
575 ) );
576 return $mock;
577 }
578
579 protected function getSimpleModuleMock( $script = '' ) {
580 $mock = $this->getMockBuilder( ResourceLoaderTestModule::class )
581 ->setMethods( [ 'getScript' ] )
582 ->getMock();
583 $mock->method( 'getScript' )->willReturn( $script );
584 return $mock;
585 }
586
587 /**
588 * @covers ResourceLoader::getCombinedVersion
589 */
590 public function testGetCombinedVersion() {
591 $rl = new EmptyResourceLoader();
592 $rl->register( [
593 'foo' => self::getSimpleModuleMock(),
594 'ferry' => self::getFailFerryMock(),
595 'bar' => self::getSimpleModuleMock(),
596 ] );
597 $context = $this->getResourceLoaderContext( [], $rl );
598
599 $this->assertEquals(
600 '',
601 $rl->getCombinedVersion( $context, [] ),
602 'empty list'
603 );
604
605 $this->assertEquals(
606 ResourceLoader::makeHash( self::BLANK_VERSION ),
607 $rl->getCombinedVersion( $context, [ 'foo' ] ),
608 'compute foo'
609 );
610
611 // Verify that getCombinedVersion() does not throw when ferry fails.
612 // Instead it gracefully continues to combine the remaining modules.
613 $this->assertEquals(
614 ResourceLoader::makeHash( self::BLANK_VERSION . self::BLANK_VERSION ),
615 $rl->getCombinedVersion( $context, [ 'foo', 'ferry', 'bar' ] ),
616 'compute foo+ferry+bar (T152266)'
617 );
618 }
619
620 public static function provideMakeModuleResponseConcat() {
621 $testcases = [
622 [
623 'modules' => [
624 'foo' => 'foo()',
625 ],
626 'expected' => "foo()\n" . 'mw.loader.state( {
627 "foo": "ready"
628 } );',
629 'minified' => "foo()\n" . 'mw.loader.state({"foo":"ready"});',
630 'message' => 'Script without semi-colon',
631 ],
632 [
633 'modules' => [
634 'foo' => 'foo()',
635 'bar' => 'bar()',
636 ],
637 'expected' => "foo()\nbar()\n" . 'mw.loader.state( {
638 "foo": "ready",
639 "bar": "ready"
640 } );',
641 'minified' => "foo()\nbar()\n" . 'mw.loader.state({"foo":"ready","bar":"ready"});',
642 'message' => 'Two scripts without semi-colon',
643 ],
644 [
645 'modules' => [
646 'foo' => "foo()\n// bar();"
647 ],
648 'expected' => "foo()\n// bar();\n" . 'mw.loader.state( {
649 "foo": "ready"
650 } );',
651 'minified' => "foo()\n" . 'mw.loader.state({"foo":"ready"});',
652 'message' => 'Script with semi-colon in comment (T162719)',
653 ],
654 ];
655 $ret = [];
656 foreach ( $testcases as $i => $case ) {
657 $ret["#$i"] = [
658 $case['modules'],
659 $case['expected'],
660 true, // debug
661 $case['message'],
662 ];
663 $ret["#$i (minified)"] = [
664 $case['modules'],
665 $case['minified'],
666 false, // debug
667 $case['message'],
668 ];
669 }
670 return $ret;
671 }
672
673 /**
674 * Verify how multiple scripts and mw.loader.state() calls are concatenated.
675 *
676 * @dataProvider provideMakeModuleResponseConcat
677 * @covers ResourceLoader::makeModuleResponse
678 */
679 public function testMakeModuleResponseConcat( $scripts, $expected, $debug, $message = null ) {
680 $rl = new EmptyResourceLoader();
681 $modules = array_map( function ( $script ) {
682 return self::getSimpleModuleMock( $script );
683 }, $scripts );
684 $rl->register( $modules );
685
686 $this->setMwGlobals( 'wgResourceLoaderDebug', $debug );
687 $context = $this->getResourceLoaderContext(
688 [
689 'modules' => implode( '|', array_keys( $modules ) ),
690 'only' => 'scripts',
691 ],
692 $rl
693 );
694
695 $response = $rl->makeModuleResponse( $context, $modules );
696 $this->assertSame( [], $rl->getErrors(), 'Errors' );
697 $this->assertEquals( $expected, $response, $message ?: 'Response' );
698 }
699
700 /**
701 * Verify that when building module content in a load.php response,
702 * an exception from one module will not break script output from
703 * other modules.
704 *
705 * @covers ResourceLoader::makeModuleResponse
706 */
707 public function testMakeModuleResponseError() {
708 $modules = [
709 'foo' => self::getSimpleModuleMock( 'foo();' ),
710 'ferry' => self::getFailFerryMock(),
711 'bar' => self::getSimpleModuleMock( 'bar();' ),
712 ];
713 $rl = new EmptyResourceLoader();
714 $rl->register( $modules );
715 $context = $this->getResourceLoaderContext(
716 [
717 'modules' => 'foo|ferry|bar',
718 'only' => 'scripts',
719 ],
720 $rl
721 );
722
723 $response = $rl->makeModuleResponse( $context, $modules );
724 $errors = $rl->getErrors();
725
726 $this->assertCount( 1, $errors );
727 $this->assertRegExp( '/Ferry not found/', $errors[0] );
728 $this->assertEquals(
729 "foo();\nbar();\n" . 'mw.loader.state( {
730 "ferry": "error",
731 "foo": "ready",
732 "bar": "ready"
733 } );',
734 $response
735 );
736 }
737
738 /**
739 * Verify that when building the startup module response,
740 * an exception from one module class will not break the entire
741 * startup module response. See T152266.
742 *
743 * @covers ResourceLoader::makeModuleResponse
744 */
745 public function testMakeModuleResponseStartupError() {
746 $rl = new EmptyResourceLoader();
747 $rl->register( [
748 'foo' => self::getSimpleModuleMock( 'foo();' ),
749 'ferry' => self::getFailFerryMock(),
750 'bar' => self::getSimpleModuleMock( 'bar();' ),
751 'startup' => [ 'class' => 'ResourceLoaderStartUpModule' ],
752 ] );
753 $context = $this->getResourceLoaderContext(
754 [
755 'modules' => 'startup',
756 'only' => 'scripts',
757 ],
758 $rl
759 );
760
761 $this->assertEquals(
762 [ 'foo', 'ferry', 'bar', 'startup' ],
763 $rl->getModuleNames(),
764 'getModuleNames'
765 );
766
767 $modules = [ 'startup' => $rl->getModule( 'startup' ) ];
768 $response = $rl->makeModuleResponse( $context, $modules );
769 $errors = $rl->getErrors();
770
771 $this->assertRegExp( '/Ferry not found/', $errors[0] );
772 $this->assertCount( 1, $errors );
773 $this->assertRegExp(
774 '/isCompatible.*function startUp/s',
775 $response,
776 'startup response undisrupted (T152266)'
777 );
778 $this->assertRegExp(
779 '/register\([^)]+"ferry",\s*""/s',
780 $response,
781 'startup response registers broken module'
782 );
783 $this->assertRegExp(
784 '/state\([^)]+"ferry":\s*"error"/s',
785 $response,
786 'startup response sets state to error'
787 );
788 }
789 }