3 use Wikimedia\TestingAccessWrapper
;
4 use MediaWiki\MediaWikiServices
;
6 class ResourceLoaderTest
extends ResourceLoaderTestCase
{
8 protected function setUp() {
11 $this->setMwGlobals( [
12 'wgShowExceptionDetails' => true,
17 * Ensure the ResourceLoaderRegisterModules hook is called.
20 public function testServiceWiring() {
21 $this->overrideMwServices();
24 $this->setMwGlobals( 'wgHooks', [
25 'ResourceLoaderRegisterModules' => [
26 function ( &$resourceLoader ) use ( &$ranHook ) {
32 MediaWikiServices
::getInstance()->getResourceLoader();
34 $this->assertSame( 1, $ranHook, 'Hook was called' );
37 public static function provideInvalidModuleName() {
39 'name with 300 chars' => [ str_repeat( 'x', 300 ) ],
40 'name with bang' => [ 'this!that' ],
41 'name with comma' => [ 'this,that' ],
42 'name with pipe' => [ 'this|that' ],
46 public static function provideValidModuleName() {
48 'empty string' => [ '' ],
49 'simple name' => [ 'this.and-that2' ],
50 'name with 100 chars' => [ str_repeat( 'x', 100 ) ],
51 'name with hash' => [ 'this#that' ],
52 'name with slash' => [ 'this/that' ],
53 'name with at' => [ 'this@that' ],
58 * @dataProvider provideInvalidModuleName
59 * @covers ResourceLoader
61 public function testIsValidModuleName_invalid( $name ) {
62 $this->assertFalse( ResourceLoader
::isValidModuleName( $name ) );
66 * @dataProvider provideValidModuleName
67 * @covers ResourceLoader
69 public function testIsValidModuleName_valid( $name ) {
70 $this->assertTrue( ResourceLoader
::isValidModuleName( $name ) );
74 * @covers ResourceLoader::register
75 * @covers ResourceLoader::getModule
77 public function testRegisterValidObject() {
78 $module = new ResourceLoaderTestModule();
79 $resourceLoader = new EmptyResourceLoader();
80 $resourceLoader->register( 'test', $module );
81 $this->assertEquals( $module, $resourceLoader->getModule( 'test' ) );
85 * @covers ResourceLoader::register
86 * @covers ResourceLoader::getModule
88 public function testRegisterValidArray() {
89 $module = new ResourceLoaderTestModule();
90 $resourceLoader = new EmptyResourceLoader();
91 // Covers case of register() setting $rl->moduleInfos,
92 // but $rl->modules lazy-populated by getModule()
93 $resourceLoader->register( 'test', [ 'object' => $module ] );
94 $this->assertEquals( $module, $resourceLoader->getModule( 'test' ) );
98 * @covers ResourceLoader::register
101 public function testRegisterEmptyString() {
102 $module = new ResourceLoaderTestModule();
103 $resourceLoader = new EmptyResourceLoader();
104 $resourceLoader->register( '', $module );
105 $this->assertEquals( $module, $resourceLoader->getModule( '' ) );
109 * @covers ResourceLoader::register
112 public function testRegisterInvalidName() {
113 $resourceLoader = new EmptyResourceLoader();
114 $this->setExpectedException( MWException
::class, "name 'test!invalid' is invalid" );
115 $resourceLoader->register( 'test!invalid', new ResourceLoaderTestModule() );
119 * @covers ResourceLoader::register
121 public function testRegisterInvalidType() {
122 $resourceLoader = new EmptyResourceLoader();
123 $this->setExpectedException( MWException
::class, 'ResourceLoader module info type error' );
124 $resourceLoader->register( 'test', new stdClass() );
128 * @covers ResourceLoader::register
130 public function testRegisterDuplicate() {
131 $logger = $this->getMockBuilder( Psr\Log\LoggerInterface
::class )->getMock();
132 $logger->expects( $this->once() )
133 ->method( 'warning' );
134 $resourceLoader = new EmptyResourceLoader( null, $logger );
136 $module1 = new ResourceLoaderTestModule();
137 $module2 = new ResourceLoaderTestModule();
138 $resourceLoader->register( 'test', $module1 );
139 $resourceLoader->register( 'test', $module2 );
140 $this->assertSame( $module2, $resourceLoader->getModule( 'test' ) );
144 * @covers ResourceLoader::getModuleNames
146 public function testGetModuleNames() {
147 // Use an empty one so that core and extension modules don't get in.
148 $resourceLoader = new EmptyResourceLoader();
149 $resourceLoader->register( 'test.foo', new ResourceLoaderTestModule() );
150 $resourceLoader->register( 'test.bar', new ResourceLoaderTestModule() );
152 [ 'startup', 'test.foo', 'test.bar' ],
153 $resourceLoader->getModuleNames()
157 public function provideTestIsFileModule() {
158 $fileModuleObj = $this->getMockBuilder( ResourceLoaderFileModule
::class )
159 ->disableOriginalConstructor()
163 new ResourceLoaderTestModule()
165 'FileModule object' => [ false,
168 'simple empty' => [ true,
171 'simple scripts' => [ true,
172 [ 'scripts' => 'example.js' ]
174 'simple scripts with targets' => [ true, [
175 'scripts' => [ 'a.js', 'b.js' ],
176 'targets' => [ 'desktop', 'mobile' ],
178 'FileModule' => [ true,
179 [ 'class' => ResourceLoaderFileModule
::class, 'scripts' => 'example.js' ]
181 'TestModule' => [ false,
182 [ 'class' => ResourceLoaderTestModule
::class, 'scripts' => 'example.js' ]
184 'SkinModule (FileModule subclass)' => [ true,
185 [ 'class' => ResourceLoaderSkinModule
::class, 'scripts' => 'example.js' ]
187 'WikiModule' => [ false, [
188 'class' => ResourceLoaderWikiModule
::class,
189 'scripts' => [ 'MediaWiki:Example.js' ],
195 * @dataProvider provideTestIsFileModule
196 * @covers ResourceLoader::isFileModule
198 public function testIsFileModule( $expected, $module ) {
199 $rl = TestingAccessWrapper
::newFromObject( new EmptyResourceLoader() );
200 $rl->register( 'test', $module );
201 $this->assertSame( $expected, $rl->isFileModule( 'test' ) );
205 * @covers ResourceLoader::isFileModule
207 public function testIsFileModuleUnknown() {
208 $rl = TestingAccessWrapper
::newFromObject( new EmptyResourceLoader() );
209 $this->assertSame( false, $rl->isFileModule( 'unknown' ) );
213 * @covers ResourceLoader::isModuleRegistered
215 public function testIsModuleRegistered() {
216 $rl = new EmptyResourceLoader();
217 $rl->register( 'test', new ResourceLoaderTestModule() );
218 $this->assertTrue( $rl->isModuleRegistered( 'test' ) );
219 $this->assertFalse( $rl->isModuleRegistered( 'test.unknown' ) );
223 * @covers ResourceLoader::getModule
225 public function testGetModuleUnknown() {
226 $rl = new EmptyResourceLoader();
227 $this->assertSame( null, $rl->getModule( 'test' ) );
231 * @covers ResourceLoader::getModule
233 public function testGetModuleClass() {
234 $rl = new EmptyResourceLoader();
235 $rl->register( 'test', [ 'class' => ResourceLoaderTestModule
::class ] );
236 $this->assertInstanceOf(
237 ResourceLoaderTestModule
::class,
238 $rl->getModule( 'test' )
243 * @covers ResourceLoader::getModule
245 public function testGetModuleFactory() {
246 $factory = function ( array $info ) {
247 $this->assertArrayHasKey( 'kitten', $info );
248 return new ResourceLoaderTestModule( $info );
251 $rl = new EmptyResourceLoader();
252 $rl->register( 'test', [ 'factory' => $factory, 'kitten' => 'little ball of fur' ] );
253 $this->assertInstanceOf(
254 ResourceLoaderTestModule
::class,
255 $rl->getModule( 'test' )
260 * @covers ResourceLoader::getModule
262 public function testGetModuleClassDefault() {
263 $rl = new EmptyResourceLoader();
264 $rl->register( 'test', [] );
265 $this->assertInstanceOf(
266 ResourceLoaderFileModule
::class,
267 $rl->getModule( 'test' ),
268 'Array-style module registrations default to FileModule'
273 * @covers ResourceLoader::getLessCompiler
275 public function testLessImportDirs() {
276 $rl = new EmptyResourceLoader();
277 $lc = $rl->getLessCompiler( [ 'foo' => '2px', 'Foo' => '#eeeeee' ] );
278 $basePath = dirname( dirname( __DIR__
) ) . '/data/less';
279 $lc->SetImportDirs( [
280 "$basePath/common" => '',
282 $css = $lc->parseFile( "$basePath/module/use-import-dir.less" )->getCss();
283 $this->assertStringEqualsFile( "$basePath/module/styles.css", $css );
286 public static function providePackedModules() {
289 'Example from makePackedModulesString doc comment',
290 [ 'foo.bar', 'foo.baz', 'bar.baz', 'bar.quux' ],
291 'foo.bar,baz|bar.baz,quux',
294 'Example from expandModuleNames doc comment',
295 [ 'jquery.foo', 'jquery.bar', 'jquery.ui.baz', 'jquery.ui.quux' ],
296 'jquery.foo,bar|jquery.ui.baz,quux',
299 'Regression fixed in r87497 (7fee86c38e) with dotless names',
300 [ 'foo', 'bar', 'baz' ],
304 'Prefixless modules after a prefixed module',
305 [ 'single.module', 'foobar', 'foobaz' ],
306 'single.module|foobar,foobaz',
310 [ 'foo', 'foo.baz', 'baz.quux', 'foo.bar' ],
311 'foo|foo.baz,bar|baz.quux',
312 [ 'foo', 'foo.baz', 'foo.bar', 'baz.quux' ],
318 * @dataProvider providePackedModules
319 * @covers ResourceLoader::makePackedModulesString
321 public function testMakePackedModulesString( $desc, $modules, $packed ) {
322 $this->assertEquals( $packed, ResourceLoader
::makePackedModulesString( $modules ), $desc );
326 * @dataProvider providePackedModules
327 * @covers ResourceLoader::expandModuleNames
329 public function testExpandModuleNames( $desc, $modules, $packed, $unpacked = null ) {
331 $unpacked ?
: $modules,
332 ResourceLoader
::expandModuleNames( $packed ),
337 public static function provideAddSource() {
339 [ 'foowiki', 'https://example.org/w/load.php', 'foowiki' ],
340 [ 'foowiki', [ 'loadScript' => 'https://example.org/w/load.php' ], 'foowiki' ],
343 'foowiki' => 'https://example.org/w/load.php',
344 'bazwiki' => 'https://example.com/w/load.php',
347 [ 'foowiki', 'bazwiki' ]
353 * @dataProvider provideAddSource
354 * @covers ResourceLoader::addSource
355 * @covers ResourceLoader::getSources
357 public function testAddSource( $name, $info, $expected ) {
358 $rl = new EmptyResourceLoader
;
359 $rl->addSource( $name, $info );
360 if ( is_array( $expected ) ) {
361 foreach ( $expected as $source ) {
362 $this->assertArrayHasKey( $source, $rl->getSources() );
365 $this->assertArrayHasKey( $expected, $rl->getSources() );
370 * @covers ResourceLoader::addSource
372 public function testAddSourceDupe() {
373 $rl = new EmptyResourceLoader
;
374 $this->setExpectedException(
375 MWException
::class, 'ResourceLoader duplicate source addition error'
377 $rl->addSource( 'foo', 'https://example.org/w/load.php' );
378 $rl->addSource( 'foo', 'https://example.com/w/load.php' );
382 * @covers ResourceLoader::addSource
384 public function testAddSourceInvalid() {
385 $rl = new EmptyResourceLoader
;
386 $this->setExpectedException( MWException
::class, 'with no "loadScript" key' );
387 $rl->addSource( 'foo', [ 'x' => 'https://example.org/w/load.php' ] );
390 public static function provideLoaderImplement() {
393 'title' => 'Implement scripts, styles and messages',
395 'name' => 'test.example',
396 'scripts' => 'mw.example();',
397 'styles' => [ 'css' => [ '.mw-example {}' ] ],
398 'messages' => [ 'example' => '' ],
401 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
412 'title' => 'Implement scripts',
414 'name' => 'test.example',
415 'scripts' => 'mw.example();',
418 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
423 'title' => 'Implement styles',
425 'name' => 'test.example',
427 'styles' => [ 'css' => [ '.mw-example {}' ] ],
429 'expected' => 'mw.loader.implement( "test.example", [], {
436 'title' => 'Implement scripts and messages',
438 'name' => 'test.example',
439 'scripts' => 'mw.example();',
440 'messages' => [ 'example' => '' ],
442 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
449 'title' => 'Implement scripts and templates',
451 'name' => 'test.example',
452 'scripts' => 'mw.example();',
453 'templates' => [ 'example.html' => '' ],
455 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
462 'title' => 'Implement unwrapped user script',
465 'scripts' => 'mw.example( 1 );',
468 'expected' => 'mw.loader.implement( "user", "mw.example( 1 );" );',
471 'title' => 'Implement multi-file script',
473 'name' => 'test.multifile',
478 'content' => 'mw.example( 1 );',
482 'content' => [ 'n' => 2 ],
486 'content' => 'mw.example( 3 );'
489 'main' => 'three.js',
493 mw.loader.implement( "test.multifile", {
496 "one.js": function ( require, module ) {
502 "three.js": function ( require, module ) {
513 * @dataProvider provideLoaderImplement
514 * @covers ResourceLoader::makeLoaderImplementScript
515 * @covers ResourceLoader::trimArray
517 public function testMakeLoaderImplementScript( $case ) {
520 'styles' => [], 'templates' => [], 'messages' => new XmlJsCode( '{}' ), 'packageFiles' => [],
522 ResourceLoader
::clearCache();
523 $this->setMwGlobals( 'wgResourceLoaderDebug', true );
525 $rl = TestingAccessWrapper
::newFromClass( ResourceLoader
::class );
528 $rl->makeLoaderImplementScript(
530 ( $case['wrap'] && is_string( $case['scripts'] ) )
531 ?
new XmlJsCode( $case['scripts'] )
536 $case['packageFiles']
542 * @covers ResourceLoader::makeLoaderImplementScript
544 public function testMakeLoaderImplementScriptInvalid() {
545 $this->setExpectedException( MWException
::class, 'Invalid scripts error' );
546 $rl = TestingAccessWrapper
::newFromClass( ResourceLoader
::class );
547 $rl->makeLoaderImplementScript(
553 null // package files
558 * @covers ResourceLoader::makeLoaderRegisterScript
560 public function testMakeLoaderRegisterScript() {
562 'mw.loader.register( [
568 ResourceLoader
::makeLoaderRegisterScript( [
569 [ 'test.name', '1234567' ],
571 'Nested array parameter'
575 'mw.loader.register( [
604 ResourceLoader
::makeLoaderRegisterScript( [
605 [ 'test.foo', '100' , [], null, null ],
606 [ 'test.bar', '200', [ 'test.unknown' ], null ],
607 [ 'test.baz', '300', [ 'test.quux', 'test.foo' ], null ],
608 [ 'test.quux', '400', [], null, null, 'return true;' ],
610 'Compact dependency indexes'
615 * @covers ResourceLoader::makeLoaderSourcesScript
617 public function testMakeLoaderSourcesScript() {
619 'mw.loader.addSource( {
620 "local": "/w/load.php"
622 ResourceLoader
::makeLoaderSourcesScript( 'local', '/w/load.php' )
625 'mw.loader.addSource( {
626 "local": "/w/load.php"
628 ResourceLoader
::makeLoaderSourcesScript( [ 'local' => '/w/load.php' ] )
631 'mw.loader.addSource( {
632 "local": "/w/load.php",
633 "example": "https://example.org/w/load.php"
635 ResourceLoader
::makeLoaderSourcesScript( [
636 'local' => '/w/load.php',
637 'example' => 'https://example.org/w/load.php'
641 'mw.loader.addSource( [] );',
642 ResourceLoader
::makeLoaderSourcesScript( [] )
646 private static function fakeSources() {
649 'loadScript' => '//example.org/w/load.php',
650 'apiScript' => '//example.org/w/api.php',
653 'loadScript' => '//example.com/w/load.php',
654 'apiScript' => '//example.com/w/api.php',
660 * @covers ResourceLoader::getLoadScript
662 public function testGetLoadScript() {
663 $rl = new EmptyResourceLoader();
664 $sources = self
::fakeSources();
665 $rl->addSource( $sources );
666 foreach ( [ 'examplewiki', 'example2wiki' ] as $name ) {
667 $this->assertEquals( $rl->getLoadScript( $name ), $sources[$name]['loadScript'] );
671 $rl->getLoadScript( 'thiswasneverreigstered' );
672 $this->assertTrue( false, 'ResourceLoader::getLoadScript should have thrown an exception' );
673 } catch ( MWException
$e ) {
674 $this->assertTrue( true );
678 protected function getFailFerryMock( $getter = 'getScript' ) {
679 $mock = $this->getMockBuilder( ResourceLoaderTestModule
::class )
680 ->setMethods( [ $getter ] )
682 $mock->method( $getter )->will( $this->throwException(
683 new Exception( 'Ferry not found' )
688 protected function getSimpleModuleMock( $script = '' ) {
689 $mock = $this->getMockBuilder( ResourceLoaderTestModule
::class )
690 ->setMethods( [ 'getScript' ] )
692 $mock->method( 'getScript' )->willReturn( $script );
696 protected function getSimpleStyleModuleMock( $styles = '' ) {
697 $mock = $this->getMockBuilder( ResourceLoaderTestModule
::class )
698 ->setMethods( [ 'getStyles' ] )
700 $mock->method( 'getStyles' )->willReturn( [ '' => $styles ] );
705 * @covers ResourceLoader::getCombinedVersion
707 public function testGetCombinedVersion() {
708 $rl = $this->getMockBuilder( EmptyResourceLoader
::class )
709 // Disable log from outputErrorAndLog
710 ->setMethods( [ 'outputErrorAndLog' ] )->getMock();
712 'foo' => self
::getSimpleModuleMock(),
713 'ferry' => self
::getFailFerryMock(),
714 'bar' => self
::getSimpleModuleMock(),
716 $context = $this->getResourceLoaderContext( [], $rl );
720 $rl->getCombinedVersion( $context, [] ),
725 ResourceLoader
::makeHash( self
::BLANK_VERSION
),
726 $rl->getCombinedVersion( $context, [ 'foo' ] ),
730 // Verify that getCombinedVersion() does not throw when ferry fails.
731 // Instead it gracefully continues to combine the remaining modules.
733 ResourceLoader
::makeHash( self
::BLANK_VERSION
. self
::BLANK_VERSION
),
734 $rl->getCombinedVersion( $context, [ 'foo', 'ferry', 'bar' ] ),
735 'compute foo+ferry+bar (T152266)'
739 public static function provideMakeModuleResponseConcat() {
745 'expected' => "foo()\n" . 'mw.loader.state( {
748 'minified' => "foo()\n" . 'mw.loader.state({"foo":"ready"});',
749 'message' => 'Script without semi-colon',
756 'expected' => "foo()\nbar()\n" . 'mw.loader.state( {
760 'minified' => "foo()\nbar()\n" . 'mw.loader.state({"foo":"ready","bar":"ready"});',
761 'message' => 'Two scripts without semi-colon',
765 'foo' => "foo()\n// bar();"
767 'expected' => "foo()\n// bar();\n" . 'mw.loader.state( {
770 'minified' => "foo()\n" . 'mw.loader.state({"foo":"ready"});',
771 'message' => 'Script with semi-colon in comment (T162719)',
775 foreach ( $testcases as $i => $case ) {
782 $ret["#$i (minified)"] = [
793 * Verify how multiple scripts and mw.loader.state() calls are concatenated.
795 * @dataProvider provideMakeModuleResponseConcat
796 * @covers ResourceLoader::makeModuleResponse
798 public function testMakeModuleResponseConcat( $scripts, $expected, $debug, $message = null ) {
799 $rl = new EmptyResourceLoader();
800 $modules = array_map( function ( $script ) {
801 return self
::getSimpleModuleMock( $script );
803 $rl->register( $modules );
805 $context = $this->getResourceLoaderContext(
807 'modules' => implode( '|', array_keys( $modules ) ),
809 'debug' => $debug ?
'true' : 'false',
814 $response = $rl->makeModuleResponse( $context, $modules );
815 $this->assertSame( [], $rl->getErrors(), 'Errors' );
816 $this->assertEquals( $expected, $response, $message ?
: 'Response' );
820 * @covers ResourceLoader::makeModuleResponse
822 public function testMakeModuleResponseEmpty() {
823 $rl = new EmptyResourceLoader();
824 $context = $this->getResourceLoaderContext(
825 [ 'modules' => '', 'only' => 'scripts' ],
829 $response = $rl->makeModuleResponse( $context, [] );
830 $this->assertSame( [], $rl->getErrors(), 'Errors' );
831 $this->assertRegExp( '/^\/\*.+no modules were requested.+\*\/$/ms', $response );
835 * Verify that when building module content in a load.php response,
836 * an exception from one module will not break script output from
839 * @covers ResourceLoader::makeModuleResponse
841 public function testMakeModuleResponseError() {
843 'foo' => self
::getSimpleModuleMock( 'foo();' ),
844 'ferry' => self
::getFailFerryMock(),
845 'bar' => self
::getSimpleModuleMock( 'bar();' ),
847 $rl = new EmptyResourceLoader();
848 $rl->register( $modules );
849 $context = $this->getResourceLoaderContext(
851 'modules' => 'foo|ferry|bar',
857 // Disable log from makeModuleResponse via outputErrorAndLog
858 $this->setLogger( 'exception', new Psr\Log\
NullLogger() );
860 $response = $rl->makeModuleResponse( $context, $modules );
861 $errors = $rl->getErrors();
863 $this->assertCount( 1, $errors );
864 $this->assertRegExp( '/Ferry not found/', $errors[0] );
866 "foo();\nbar();\n" . 'mw.loader.state( {
876 * Verify that exceptions in PHP for one module will not break others
877 * (stylesheet response).
879 * @covers ResourceLoader::makeModuleResponse
881 public function testMakeModuleResponseErrorCSS() {
883 'foo' => self
::getSimpleStyleModuleMock( '.foo{}' ),
884 'ferry' => self
::getFailFerryMock( 'getStyles' ),
885 'bar' => self
::getSimpleStyleModuleMock( '.bar{}' ),
887 $rl = new EmptyResourceLoader();
888 $rl->register( $modules );
889 $context = $this->getResourceLoaderContext(
891 'modules' => 'foo|ferry|bar',
898 // Disable log from makeModuleResponse via outputErrorAndLog
899 $this->setLogger( 'exception', new Psr\Log\
NullLogger() );
901 $response = $rl->makeModuleResponse( $context, $modules );
902 $errors = $rl->getErrors();
904 $this->assertCount( 2, $errors );
905 $this->assertRegExp( '/Ferry not found/', $errors[0] );
906 $this->assertRegExp( '/Problem.+"ferry":\s*"error"/ms', $errors[1] );
914 * Verify that when building the startup module response,
915 * an exception from one module class will not break the entire
916 * startup module response. See T152266.
918 * @covers ResourceLoader::makeModuleResponse
920 public function testMakeModuleResponseStartupError() {
921 // This is an integration test that uses a lot of MediaWiki state,
922 // provide the full Config object here.
923 $rl = new EmptyResourceLoader( MediaWikiServices
::getInstance()->getMainConfig() );
925 'foo' => self
::getSimpleModuleMock( 'foo();' ),
926 'ferry' => self
::getFailFerryMock(),
927 'bar' => self
::getSimpleModuleMock( 'bar();' ),
929 $context = $this->getResourceLoaderContext(
931 'modules' => 'startup',
938 [ 'startup', 'foo', 'ferry', 'bar' ],
939 $rl->getModuleNames(),
943 // Disable log from makeModuleResponse via outputErrorAndLog
944 $this->setLogger( 'exception', new Psr\Log\
NullLogger() );
946 $modules = [ 'startup' => $rl->getModule( 'startup' ) ];
947 $response = $rl->makeModuleResponse( $context, $modules );
948 $errors = $rl->getErrors();
950 $this->assertRegExp( '/Ferry not found/', $errors[0] );
951 $this->assertCount( 1, $errors );
953 '/isCompatible.*window\.RLQ/s',
955 'startup response undisrupted (T152266)'
958 '/register\([^)]+"ferry",\s*""/s',
960 'startup response registers broken module'
963 '/state\([^)]+"ferry":\s*"error"/s',
965 'startup response sets state to error'
970 * Integration test for modules sending extra HTTP response headers.
972 * @covers ResourceLoaderModule::getHeaders
973 * @covers ResourceLoaderModule::buildContent
974 * @covers ResourceLoader::makeModuleResponse
976 public function testMakeModuleResponseExtraHeaders() {
977 $module = $this->getMockBuilder( ResourceLoaderTestModule
::class )
978 ->setMethods( [ 'getPreloadLinks' ] )->getMock();
979 $module->method( 'getPreloadLinks' )->willReturn( [
980 'https://example.org/script.js' => [ 'as' => 'script' ],
983 $rl = new EmptyResourceLoader();
987 $context = $this->getResourceLoaderContext(
988 [ 'modules' => 'foo', 'only' => 'scripts' ],
992 $modules = [ 'foo' => $rl->getModule( 'foo' ) ];
993 $response = $rl->makeModuleResponse( $context, $modules );
994 $extraHeaders = TestingAccessWrapper
::newFromObject( $rl )->extraHeaders
;
998 'Link: <https://example.org/script.js>;rel=preload;as=script'
1006 * @covers ResourceLoaderModule::getHeaders
1007 * @covers ResourceLoaderModule::buildContent
1008 * @covers ResourceLoader::makeModuleResponse
1010 public function testMakeModuleResponseExtraHeadersMulti() {
1011 $foo = $this->getMockBuilder( ResourceLoaderTestModule
::class )
1012 ->setMethods( [ 'getPreloadLinks' ] )->getMock();
1013 $foo->method( 'getPreloadLinks' )->willReturn( [
1014 'https://example.org/script.js' => [ 'as' => 'script' ],
1017 $bar = $this->getMockBuilder( ResourceLoaderTestModule
::class )
1018 ->setMethods( [ 'getPreloadLinks' ] )->getMock();
1019 $bar->method( 'getPreloadLinks' )->willReturn( [
1020 '/example.png' => [ 'as' => 'image' ],
1021 '/example.jpg' => [ 'as' => 'image' ],
1024 $rl = new EmptyResourceLoader();
1025 $rl->register( [ 'foo' => $foo, 'bar' => $bar ] );
1026 $context = $this->getResourceLoaderContext(
1027 [ 'modules' => 'foo|bar', 'only' => 'scripts' ],
1031 $modules = [ 'foo' => $rl->getModule( 'foo' ), 'bar' => $rl->getModule( 'bar' ) ];
1032 $response = $rl->makeModuleResponse( $context, $modules );
1033 $extraHeaders = TestingAccessWrapper
::newFromObject( $rl )->extraHeaders
;
1034 $this->assertEquals(
1036 'Link: <https://example.org/script.js>;rel=preload;as=script',
1037 'Link: </example.png>;rel=preload;as=image,</example.jpg>;rel=preload;as=image'
1045 * @covers ResourceLoader::respond
1047 public function testRespondEmpty() {
1048 $rl = $this->getMockBuilder( EmptyResourceLoader
::class )
1050 'tryRespondNotModified',
1051 'sendResponseHeaders',
1052 'measureResponseTime',
1055 $context = $this->getResourceLoaderContext( [ 'modules' => '' ], $rl );
1057 $rl->expects( $this->once() )->method( 'measureResponseTime' );
1058 $this->expectOutputRegex( '/no modules were requested/' );
1060 $rl->respond( $context );
1064 * @covers ResourceLoader::respond
1066 public function testRespondSimple() {
1067 $module = new ResourceLoaderTestModule( [ 'script' => 'foo();' ] );
1068 $rl = $this->getMockBuilder( EmptyResourceLoader
::class )
1070 'measureResponseTime',
1071 'tryRespondNotModified',
1072 'sendResponseHeaders',
1073 'makeModuleResponse',
1076 $rl->register( 'test', $module );
1077 $context = $this->getResourceLoaderContext(
1078 [ 'modules' => 'test', 'only' => null ],
1082 $rl->expects( $this->once() )->method( 'makeModuleResponse' )
1083 ->with( $context, [ 'test' => $module ] )
1084 ->willReturn( 'implement_foo;' );
1085 $this->expectOutputRegex( '/^implement_foo;/' );
1087 $rl->respond( $context );
1091 * @covers ResourceLoader::respond
1093 public function testRespondInternalFailures() {
1094 $module = new ResourceLoaderTestModule( [ 'script' => 'foo();' ] );
1095 $rl = $this->getMockBuilder( EmptyResourceLoader
::class )
1097 'measureResponseTime',
1098 'preloadModuleInfo',
1099 'getCombinedVersion',
1100 'tryRespondNotModified',
1101 'makeModuleResponse',
1102 'sendResponseHeaders',
1105 $rl->register( 'test', $module );
1106 $context = $this->getResourceLoaderContext( [ 'modules' => 'test' ], $rl );
1107 // Disable logging from outputErrorAndLog
1108 $this->setLogger( 'exception', new Psr\Log\
NullLogger() );
1110 $rl->expects( $this->once() )->method( 'preloadModuleInfo' )
1111 ->willThrowException( new Exception( 'Preload error' ) );
1112 $rl->expects( $this->once() )->method( 'getCombinedVersion' )
1113 ->willThrowException( new Exception( 'Version error' ) );
1114 $rl->expects( $this->once() )->method( 'makeModuleResponse' )
1115 ->with( $context, [ 'test' => $module ] )
1116 ->willReturn( 'foo;' );
1117 // Internal errors should be caught and logged without affecting module output
1118 $this->expectOutputRegex( '/^\/\*.+Preload error.+Version error.+\*\/.*foo;/ms' );
1120 $rl->respond( $context );
1124 * @covers ResourceLoader::measureResponseTime
1126 public function testMeasureResponseTime() {
1127 $stats = $this->getMockBuilder( NullStatsdDataFactory
::class )
1128 ->setMethods( [ 'timing' ] )->getMock();
1129 $this->setService( 'StatsdDataFactory', $stats );
1131 $stats->expects( $this->once() )->method( 'timing' )
1132 ->with( 'resourceloader.responseTime', $this->anything() );
1134 $timing = new Timing();
1135 $timing->mark( 'requestShutdown' );
1136 $rl = TestingAccessWrapper
::newFromObject( new EmptyResourceLoader
);
1137 $rl->measureResponseTime( $timing );
1138 DeferredUpdates
::doUpdates();