Go to the documentation of this file.
3 use Wikimedia\TestingAccessWrapper;
7 protected function setUp() {
11 'wgResourceLoaderLESSImportPaths' => [
12 dirname( dirname( __DIR__ ) ) .
'/data/less/common',
14 'wgResourceLoaderLESSVars' => [
23 'wgShowExceptionDetails' =>
true,
33 $resourceLoaderRegisterModulesHook =
false;
36 'ResourceLoaderRegisterModules' => [
38 $resourceLoaderRegisterModulesHook =
true;
43 $unused =
new ResourceLoader();
45 $resourceLoaderRegisterModulesHook,
46 'Hook ResourceLoaderRegisterModules called'
89 $this->setExpectedException(
'MWException',
"name 'test!invalid' is invalid" );
98 $this->setExpectedException(
'MWException',
'ResourceLoader module info type error' );
111 [
'test.foo',
'test.bar' ],
118 ->disableOriginalConstructor()
124 'FileModule object' => [
false,
127 'simple empty' => [
true,
130 'simple scripts' => [
true,
131 [
'scripts' =>
'example.js' ]
133 'simple scripts, raw and targets' => [
true, [
134 'scripts' => [
'a.js',
'b.js' ],
136 'targets' => [
'desktop',
'mobile' ],
138 'FileModule' => [
true,
141 'TestModule' => [
false,
144 'SkinModule (FileModule subclass)' => [
true,
147 'JqueryMsgModule (FileModule subclass)' => [
true, [
149 'scripts' =>
'example.js',
151 'WikiModule' => [
false, [
153 'scripts' => [
'MediaWiki:Example.js' ],
164 $rl->register(
'test', $module );
165 $this->assertSame( $expected, $rl->isFileModule(
'test' ) );
173 $this->assertSame(
false, $rl->isFileModule(
'unknown' ) );
182 $this->assertTrue( $rl->isModuleRegistered(
'test' ) );
183 $this->assertFalse( $rl->isModuleRegistered(
'test.unknown' ) );
191 $this->assertSame(
null, $rl->getModule(
'test' ) );
200 $this->assertInstanceOf(
202 $rl->getModule(
'test' )
210 $factory =
function (
array $info ) {
211 $this->assertArrayHasKey(
'kitten', $info );
216 $rl->register(
'test', [
'factory' => $factory,
'kitten' =>
'little ball of fur' ] );
217 $this->assertInstanceOf(
219 $rl->getModule(
'test' )
228 $rl->register(
'test', [] );
229 $this->assertInstanceOf(
231 $rl->getModule(
'test' ),
232 'Array-style module registrations default to FileModule'
241 $basePath = __DIR__ .
'/../../data/less/module';
244 'styles' => [
'styles.less' ],
246 $module->setName(
'test.less' );
247 $styles = $module->getStyles(
$context );
248 $this->assertStringEqualsFile(
$basePath .
'/styles.css', $styles[
'all'] );
254 'Example from makePackedModulesString doc comment',
255 [
'foo.bar',
'foo.baz',
'bar.baz',
'bar.quux' ],
256 'foo.bar,baz|bar.baz,quux',
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',
264 'Regression fixed in r88706 with dotless names',
265 [
'foo',
'bar',
'baz' ],
269 'Prefixless modules after a prefixed module',
270 [
'single.module',
'foobar',
'foobaz' ],
271 'single.module|foobar,foobaz',
275 [
'foo',
'foo.baz',
'baz.quux',
'foo.bar' ],
276 'foo|foo.baz,bar|baz.quux',
277 [
'foo',
'foo.baz',
'foo.bar',
'baz.quux' ],
287 $this->assertEquals( $packed, ResourceLoader::makePackedModulesString(
$modules ), $desc );
304 [
'foowiki',
'https://example.org/w/load.php',
'foowiki' ],
305 [
'foowiki', [
'loadScript' =>
'https://example.org/w/load.php' ],
'foowiki' ],
308 'foowiki' =>
'https://example.org/w/load.php',
309 'bazwiki' =>
'https://example.com/w/load.php',
312 [
'foowiki',
'bazwiki' ]
323 $rl =
new ResourceLoader;
324 $rl->addSource(
$name, $info );
325 if ( is_array( $expected ) ) {
327 $this->assertArrayHasKey(
$source, $rl->getSources() );
330 $this->assertArrayHasKey( $expected, $rl->getSources() );
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' );
348 $rl =
new ResourceLoader;
349 $this->setExpectedException(
'MWException',
'with no "loadScript" key' );
350 $rl->addSource(
'foo', [
'x' =>
'https://example.org/w/load.php' ] );
356 'title' =>
'Implement scripts, styles and messages',
358 'name' =>
'test.example',
359 'scripts' =>
'mw.example();',
360 'styles' => [
'css' => [
'.mw-example {}' ] ],
361 'messages' => [
'example' =>
'' ],
364 'expected' =>
'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
375 'title' =>
'Implement scripts',
377 'name' =>
'test.example',
378 'scripts' =>
'mw.example();',
381 'expected' =>
'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
386 'title' =>
'Implement styles',
388 'name' =>
'test.example',
390 'styles' => [
'css' => [
'.mw-example {}' ] ],
392 'expected' =>
'mw.loader.implement( "test.example", [], {
399 'title' =>
'Implement scripts and messages',
401 'name' =>
'test.example',
402 'scripts' =>
'mw.example();',
403 'messages' => [
'example' =>
'' ],
405 'expected' =>
'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
412 'title' =>
'Implement scripts and templates',
414 'name' =>
'test.example',
415 'scripts' =>
'mw.example();',
416 'templates' => [
'example.html' =>
'' ],
418 'expected' =>
'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
425 'title' =>
'Implement unwrapped user script',
428 'scripts' =>
'mw.example( 1 );',
431 'expected' =>
'mw.loader.implement( "user", "mw.example( 1 );" );',
444 'styles' => [],
'templates' => [],
'messages' =>
new XmlJsCode(
'{}' )
446 ResourceLoader::clearCache();
449 $rl = TestingAccessWrapper::newFromClass(
'ResourceLoader' );
452 $rl->makeLoaderImplementScript(
454 ( $case[
'wrap'] && is_string( $case[
'scripts'] ) )
468 $this->setExpectedException(
'MWException',
'Invalid scripts error' );
469 $rl = TestingAccessWrapper::newFromClass(
'ResourceLoader' );
470 $rl->makeLoaderImplementScript(
484 'mw.loader.register( [
490 ResourceLoader::makeLoaderRegisterScript( [
491 [
'test.name',
'1234567' ],
493 'Nested array parameter'
497 'mw.loader.register( "test.name", "1234567" );',
498 ResourceLoader::makeLoaderRegisterScript(
502 'Variadic parameters'
511 'mw.loader.addSource( "local", "/w/load.php" );',
512 ResourceLoader::makeLoaderSourcesScript(
'local',
'/w/load.php' )
515 'mw.loader.addSource( {
516 "local": "/w/load.php"
518 ResourceLoader::makeLoaderSourcesScript( [
'local' =>
'/w/load.php' ] )
521 'mw.loader.addSource( {
522 "local": "/w/load.php",
523 "example": "https://example.org/w/load.php"
525 ResourceLoader::makeLoaderSourcesScript( [
526 'local' =>
'/w/load.php',
527 'example' =>
'https://example.org/w/load.php'
531 'mw.loader.addSource( [] );',
532 ResourceLoader::makeLoaderSourcesScript( [] )
539 'loadScript' =>
'//example.org/w/load.php',
540 'apiScript' =>
'//example.org/w/api.php',
543 'loadScript' =>
'//example.com/w/load.php',
544 'apiScript' =>
'//example.com/w/api.php',
554 $rl =
new ResourceLoader();
556 $rl->addSource( $sources );
557 foreach ( [
'examplewiki',
'example2wiki' ]
as $name ) {
558 $this->assertEquals( $rl->getLoadScript(
$name ), $sources[
$name][
'loadScript'] );
562 $rl->getLoadScript(
'thiswasneverreigstered' );
563 $this->assertTrue(
false,
'ResourceLoader::getLoadScript should have thrown an exception' );
565 $this->assertTrue(
true );
571 ->setMethods( [
'getScript' ] )
573 $mock->method(
'getScript' )->will( $this->throwException(
574 new Exception(
'Ferry not found' )
581 ->setMethods( [
'getScript' ] )
583 $mock->method(
'getScript' )->willReturn( $script );
593 ->setMethods( [
'outputErrorAndLog' ] )->getMock();
595 'foo' => self::getSimpleModuleMock(),
596 'ferry' => self::getFailFerryMock(),
597 'bar' => self::getSimpleModuleMock(),
603 $rl->getCombinedVersion(
$context, [] ),
608 ResourceLoader::makeHash( self::BLANK_VERSION ),
609 $rl->getCombinedVersion(
$context, [
'foo' ] ),
616 ResourceLoader::makeHash( self::BLANK_VERSION . self::BLANK_VERSION ),
617 $rl->getCombinedVersion(
$context, [
'foo',
'ferry',
'bar' ] ),
618 'compute foo+ferry+bar (T152266)'
628 'expected' =>
"foo()\n" .
'mw.loader.state( {
631 'minified' =>
"foo()\n" .
'mw.loader.state({"foo":"ready"});',
632 'message' =>
'Script without semi-colon',
639 'expected' =>
"foo()\nbar()\n" .
'mw.loader.state( {
643 'minified' =>
"foo()\nbar()\n" .
'mw.loader.state({"foo":"ready","bar":"ready"});',
644 'message' =>
'Two scripts without semi-colon',
648 'foo' =>
"foo()\n// bar();"
650 'expected' =>
"foo()\n// bar();\n" .
'mw.loader.state( {
653 'minified' =>
"foo()\n" .
'mw.loader.state({"foo":"ready"});',
654 'message' =>
'Script with semi-colon in comment (T162719)',
658 foreach ( $testcases
as $i => $case ) {
665 $ret[
"#$i (minified)"] = [
683 $modules = array_map(
function ( $script ) {
691 'modules' => implode(
'|', array_keys(
$modules ) ),
698 $this->assertSame( [], $rl->getErrors(),
'Errors' );
699 $this->assertEquals( $expected,
$response, $message ?:
'Response' );
719 'modules' =>
'foo|ferry|bar',
726 $this->
setLogger(
'exception',
new Psr\Log\NullLogger() );
729 $errors = $rl->getErrors();
731 $this->assertCount( 1, $errors );
732 $this->assertRegExp(
'/Ferry not found/', $errors[0] );
734 "foo();\nbar();\n" .
'mw.loader.state( {
753 'foo' => self::getSimpleModuleMock(
'foo();' ),
754 'ferry' => self::getFailFerryMock(),
755 'bar' => self::getSimpleModuleMock(
'bar();' ),
756 'startup' => [
'class' =>
'ResourceLoaderStartUpModule' ],
760 'modules' =>
'startup',
767 [
'foo',
'ferry',
'bar',
'startup' ],
768 $rl->getModuleNames(),
773 $this->
setLogger(
'exception',
new Psr\Log\NullLogger() );
775 $modules = [
'startup' => $rl->getModule(
'startup' ) ];
777 $errors = $rl->getErrors();
779 $this->assertRegExp(
'/Ferry not found/', $errors[0] );
780 $this->assertCount( 1, $errors );
782 '/isCompatible.*function startUp/s',
784 'startup response undisrupted (T152266)'
787 '/register\([^)]+"ferry",\s*""/s',
789 'startup response registers broken module'
792 '/state\([^)]+"ferry":\s*"error"/s',
794 'startup response sets state to error'
807 ->setMethods( [
'getPreloadLinks' ] )->getMock();
808 $module->method(
'getPreloadLinks' )->willReturn( [
809 'https://example.org/script.js' => [
'as' =>
'script' ],
817 [
'modules' =>
'foo',
'only' =>
'scripts' ],
821 $modules = [
'foo' => $rl->getModule(
'foo' ) ];
823 $extraHeaders = TestingAccessWrapper::newFromObject( $rl )->extraHeaders;
827 'Link: <https://example.org/script.js>;rel=preload;as=script'
841 ->setMethods( [
'getPreloadLinks' ] )->getMock();
842 $foo->method(
'getPreloadLinks' )->willReturn( [
843 'https://example.org/script.js' => [
'as' =>
'script' ],
847 ->setMethods( [
'getPreloadLinks' ] )->getMock();
848 $bar->method(
'getPreloadLinks' )->willReturn( [
849 '/example.png' => [
'as' =>
'image' ],
850 '/example.jpg' => [
'as' =>
'image' ],
854 $rl->register( [
'foo' => $foo,
'bar' => $bar ] );
856 [
'modules' =>
'foo|bar',
'only' =>
'scripts' ],
860 $modules = [
'foo' => $rl->getModule(
'foo' ),
'bar' => $rl->getModule(
'bar' ) ];
862 $extraHeaders = TestingAccessWrapper::newFromObject( $rl )->extraHeaders;
865 'Link: <https://example.org/script.js>;rel=preload;as=script',
866 'Link: </example.png>;rel=preload;as=image,</example.jpg>;rel=preload;as=image'
testIsFileModule( $expected, $module)
provideTestIsFileModule ResourceLoader::isFileModule
testMakePackedModulesString( $desc, $modules, $packed)
providePackedModules ResourceLoader::makePackedModulesString
testConstructRegistrationHook()
Ensure the ResourceLoaderRegisterModules hook is called.
testRegisterInvalidType()
ResourceLoader::register.
testGetModuleClassDefault()
ResourceLoader::getModule.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
testIsFileModuleUnknown()
ResourceLoader::isFileModule.
testMakeModuleResponseError()
Verify that when building module content in a load.php response, an exception from one module will no...
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
testRegisterInvalidName()
ResourceLoader::register.
static expandModuleNames( $modules)
Expand a string of the form jquery.foo,bar|jquery.ui.baz,quux to an array of module names like [ 'jqu...
testMakeLoaderImplementScriptInvalid()
ResourceLoader::makeLoaderImplementScript.
Allows to change the fields on the form that will be generated $name
testGetModuleNames()
ResourceLoader::getModuleNames.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext such as when responding to a resource loader request or generating HTML output & $resourceLoader
provideTestIsFileModule()
testGetModuleClass()
ResourceLoader::getModule.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
ResourceLoader module based on local JavaScript/CSS files.
A wrapper class which causes Xml::encodeJsVar() and Xml::encodeJsCall() to interpret a given string a...
static provideAddSource()
testMakeLoaderSourcesScript()
ResourceLoader::makeLoaderSourcesScript.
testAddSourceDupe()
ResourceLoader::addSource.
testMakeLoaderRegisterScript()
ResourceLoader::makeLoaderRegisterScript.
testGetLoadScript()
ResourceLoader::getLoadScript.
testRegisterValidArray()
ResourceLoader::register ResourceLoader::getModule.
static provideMakeModuleResponseConcat()
testRegisterEmptyString()
ResourceLoader::register.
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
testMakeModuleResponseExtraHeaders()
Integration test for modules sending extra HTTP response headers.
testMakeLoaderImplementScript( $case)
provideLoaderImplement ResourceLoader::makeLoaderImplementScript ResourceLoader::trimArray
testMakeModuleResponseConcat( $scripts, $expected, $debug, $message=null)
Verify how multiple scripts and mw.loader.state() calls are concatenated.
testRegisterValidObject()
ResourceLoader::register ResourceLoader::getModule.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
getSimpleModuleMock( $script='')
this hook is for auditing only $response
testExpandModuleNames( $desc, $modules, $packed, $unpacked=null)
providePackedModules ResourceLoaderContext::expandModuleNames
testGetCombinedVersion()
ResourceLoader::getCombinedVersion.
testAddSourceInvalid()
ResourceLoader::addSource.
testMakeModuleResponseExtraHeadersMulti()
ResourceLoaderModule::getHeaders ResourceLoaderModule::buildContent ResourceLoader::makeModuleRespons...
testGetModuleFactory()
ResourceLoader::getModule.
getResourceLoaderContext( $options=[], ResourceLoader $rl=null)
testIsModuleRegistered()
ResourceLoader::isModuleRegistered.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
testLessFileCompilation()
ResourceLoaderFileModule::compileLessFile.
static providePackedModules()
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
testGetModuleUnknown()
ResourceLoader::getModule.
testMakeModuleResponseStartupError()
Verify that when building the startup module response, an exception from one module class will not br...
static provideLoaderImplement()
the array() calling protocol came about after MediaWiki 1.4rc1.
testAddSource( $name, $info, $expected)
provideAddSource ResourceLoader::addSource ResourceLoader::getSources