3use 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' => [
37 function ( &
$resourceLoader ) use ( &$resourceLoaderRegisterModulesHook ) {
38 $resourceLoaderRegisterModulesHook =
true;
45 $resourceLoaderRegisterModulesHook,
46 'Hook ResourceLoaderRegisterModules called'
89 $this->setExpectedException( MWException::class,
"name 'test!invalid' is invalid" );
98 $this->setExpectedException( MWException::class,
'ResourceLoader module info type error' );
111 [
'test.foo',
'test.bar' ],
117 $fileModuleObj = $this->getMockBuilder( ResourceLoaderFileModule::class )
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,
139 [
'class' => ResourceLoaderFileModule::class,
'scripts' =>
'example.js' ]
141 'TestModule' => [
false,
142 [
'class' => ResourceLoaderTestModule::class,
'scripts' =>
'example.js' ]
144 'SkinModule (FileModule subclass)' => [
true,
145 [
'class' => ResourceLoaderSkinModule::class,
'scripts' =>
'example.js' ]
147 'JqueryMsgModule (FileModule subclass)' => [
true, [
148 'class' => ResourceLoaderJqueryMsgModule::class,
149 'scripts' =>
'example.js',
151 'WikiModule' => [
false, [
152 'class' => ResourceLoaderWikiModule::class,
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' ) );
199 $rl->register(
'test', [
'class' => ResourceLoaderTestModule::class ] );
200 $this->assertInstanceOf(
201 ResourceLoaderTestModule::class,
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(
218 ResourceLoaderTestModule::class,
219 $rl->getModule(
'test' )
228 $rl->register(
'test', [] );
229 $this->assertInstanceOf(
230 ResourceLoaderFileModule::class,
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 r87497 (7fee86c38e) 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' ],
297 ResourceLoaderContext::expandModuleNames( $packed ),
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' ]
325 if ( is_array( $expected ) ) {
326 foreach ( $expected as
$source ) {
327 $this->assertArrayHasKey(
$source, $rl->getSources() );
330 $this->assertArrayHasKey( $expected, $rl->getSources() );
339 $this->setExpectedException(
340 MWException::class,
'ResourceLoader duplicate source addition error'
342 $rl->addSource(
'foo',
'https://example.org/w/load.php' );
343 $rl->addSource(
'foo',
'https://example.com/w/load.php' );
351 $this->setExpectedException( MWException::class,
'with no "loadScript" key' );
352 $rl->addSource(
'foo', [
'x' =>
'https://example.org/w/load.php' ] );
358 'title' =>
'Implement scripts, styles and messages',
360 'name' =>
'test.example',
361 'scripts' =>
'mw.example();',
362 'styles' => [
'css' => [
'.mw-example {}' ] ],
363 'messages' => [
'example' =>
'' ],
366 'expected' =>
'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
377 'title' =>
'Implement scripts',
379 'name' =>
'test.example',
380 'scripts' =>
'mw.example();',
383 'expected' =>
'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
388 'title' =>
'Implement styles',
390 'name' =>
'test.example',
392 'styles' => [
'css' => [
'.mw-example {}' ] ],
394 'expected' =>
'mw.loader.implement( "test.example", [], {
401 'title' =>
'Implement scripts and messages',
403 'name' =>
'test.example',
404 'scripts' =>
'mw.example();',
405 'messages' => [
'example' =>
'' ],
407 'expected' =>
'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
414 'title' =>
'Implement scripts and templates',
416 'name' =>
'test.example',
417 'scripts' =>
'mw.example();',
418 'templates' => [
'example.html' =>
'' ],
420 'expected' =>
'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
427 'title' =>
'Implement unwrapped user script',
430 'scripts' =>
'mw.example( 1 );',
433 'expected' =>
'mw.loader.implement( "user", "mw.example( 1 );" );',
446 'styles' => [],
'templates' => [],
'messages' =>
new XmlJsCode(
'{}' )
451 $rl = TestingAccessWrapper::newFromClass( ResourceLoader::class );
454 $rl->makeLoaderImplementScript(
456 ( $case[
'wrap'] && is_string( $case[
'scripts'] ) )
470 $this->setExpectedException( MWException::class,
'Invalid scripts error' );
471 $rl = TestingAccessWrapper::newFromClass( ResourceLoader::class );
472 $rl->makeLoaderImplementScript(
486 'mw.loader.register( [
493 [
'test.name',
'1234567' ],
495 'Nested array parameter'
499 'mw.loader.register( "test.name", "1234567" );',
504 'Variadic parameters'
513 'mw.loader.addSource( "local", "/w/load.php" );',
517 'mw.loader.addSource( {
518 "local": "/w/load.php"
523 'mw.loader.addSource( {
524 "local": "/w/load.php",
525 "example": "https://example.org/w/load.php"
528 'local' =>
'/w/load.php',
529 'example' =>
'https://example.org/w/load.php'
533 'mw.loader.addSource( [] );',
541 'loadScript' =>
'//example.org/w/load.php',
542 'apiScript' =>
'//example.org/w/api.php',
545 'loadScript' =>
'//example.com/w/load.php',
546 'apiScript' =>
'//example.com/w/api.php',
558 $rl->addSource( $sources );
559 foreach ( [
'examplewiki',
'example2wiki' ] as $name ) {
560 $this->assertEquals( $rl->getLoadScript( $name ), $sources[$name][
'loadScript'] );
564 $rl->getLoadScript(
'thiswasneverreigstered' );
565 $this->assertTrue(
false,
'ResourceLoader::getLoadScript should have thrown an exception' );
567 $this->assertTrue(
true );
572 $mock = $this->getMockBuilder( ResourceLoaderTestModule::class )
573 ->setMethods( [
'getScript' ] )
575 $mock->method(
'getScript' )->will( $this->throwException(
576 new Exception(
'Ferry not found' )
582 $mock = $this->getMockBuilder( ResourceLoaderTestModule::class )
583 ->setMethods( [
'getScript' ] )
585 $mock->method(
'getScript' )->willReturn( $script );
593 $rl = $this->getMockBuilder( EmptyResourceLoader::class )
595 ->setMethods( [
'outputErrorAndLog' ] )->getMock();
597 'foo' => self::getSimpleModuleMock(),
598 'ferry' => self::getFailFerryMock(),
599 'bar' => self::getSimpleModuleMock(),
605 $rl->getCombinedVersion(
$context, [] ),
611 $rl->getCombinedVersion(
$context, [
'foo' ] ),
619 $rl->getCombinedVersion(
$context, [
'foo',
'ferry',
'bar' ] ),
620 'compute foo+ferry+bar (T152266)'
630 'expected' =>
"foo()\n" .
'mw.loader.state( {
633 'minified' =>
"foo()\n" .
'mw.loader.state({"foo":"ready"});',
634 'message' =>
'Script without semi-colon',
641 'expected' =>
"foo()\nbar()\n" .
'mw.loader.state( {
645 'minified' =>
"foo()\nbar()\n" .
'mw.loader.state({"foo":"ready","bar":"ready"});',
646 'message' =>
'Two scripts without semi-colon',
650 'foo' =>
"foo()\n// bar();"
652 'expected' =>
"foo()\n// bar();\n" .
'mw.loader.state( {
655 'minified' =>
"foo()\n" .
'mw.loader.state({"foo":"ready"});',
656 'message' =>
'Script with semi-colon in comment (T162719)',
660 foreach ( $testcases as $i => $case ) {
667 $ret[
"#$i (minified)"] = [
685 $modules = array_map(
function ( $script ) {
693 'modules' => implode(
'|', array_keys(
$modules ) ),
700 $this->assertSame( [], $rl->getErrors(),
'Errors' );
701 $this->assertEquals( $expected,
$response, $message ?:
'Response' );
721 'modules' =>
'foo|ferry|bar',
728 $this->
setLogger(
'exception',
new Psr\Log\NullLogger() );
731 $errors = $rl->getErrors();
733 $this->assertCount( 1, $errors );
734 $this->assertRegExp(
'/Ferry not found/', $errors[0] );
736 "foo();\nbar();\n" .
'mw.loader.state( {
755 'foo' => self::getSimpleModuleMock(
'foo();' ),
756 'ferry' => self::getFailFerryMock(),
757 'bar' => self::getSimpleModuleMock(
'bar();' ),
758 'startup' => [
'class' => ResourceLoaderStartUpModule::class ],
762 'modules' =>
'startup',
769 [
'foo',
'ferry',
'bar',
'startup' ],
770 $rl->getModuleNames(),
775 $this->
setLogger(
'exception',
new Psr\Log\NullLogger() );
777 $modules = [
'startup' => $rl->getModule(
'startup' ) ];
779 $errors = $rl->getErrors();
781 $this->assertRegExp(
'/Ferry not found/', $errors[0] );
782 $this->assertCount( 1, $errors );
784 '/isCompatible.*function startUp/s',
786 'startup response undisrupted (T152266)'
789 '/register\([^)]+"ferry",\s*""/s',
791 'startup response registers broken module'
794 '/state\([^)]+"ferry":\s*"error"/s',
796 'startup response sets state to error'
808 $module = $this->getMockBuilder( ResourceLoaderTestModule::class )
809 ->setMethods( [
'getPreloadLinks' ] )->getMock();
810 $module->method(
'getPreloadLinks' )->willReturn( [
811 'https://example.org/script.js' => [
'as' =>
'script' ],
819 [
'modules' =>
'foo',
'only' =>
'scripts' ],
823 $modules = [
'foo' => $rl->getModule(
'foo' ) ];
825 $extraHeaders = TestingAccessWrapper::newFromObject( $rl )->extraHeaders;
829 'Link: <https://example.org/script.js>;rel=preload;as=script'
842 $foo = $this->getMockBuilder( ResourceLoaderTestModule::class )
843 ->setMethods( [
'getPreloadLinks' ] )->getMock();
844 $foo->method(
'getPreloadLinks' )->willReturn( [
845 'https://example.org/script.js' => [
'as' =>
'script' ],
848 $bar = $this->getMockBuilder( ResourceLoaderTestModule::class )
849 ->setMethods( [
'getPreloadLinks' ] )->getMock();
850 $bar->method(
'getPreloadLinks' )->willReturn( [
851 '/example.png' => [
'as' =>
'image' ],
852 '/example.jpg' => [
'as' =>
'image' ],
856 $rl->register( [
'foo' => $foo,
'bar' => $bar ] );
858 [
'modules' =>
'foo|bar',
'only' =>
'scripts' ],
862 $modules = [
'foo' => $rl->getModule(
'foo' ),
'bar' => $rl->getModule(
'bar' ) ];
864 $extraHeaders = TestingAccessWrapper::newFromObject( $rl )->extraHeaders;
867 'Link: <https://example.org/script.js>;rel=preload;as=script',
868 'Link: </example.png>;rel=preload;as=image,</example.jpg>;rel=preload;as=image'
879 $rl = $this->getMockBuilder( EmptyResourceLoader::class )
881 'tryRespondNotModified',
882 'sendResponseHeaders',
883 'measureResponseTime',
888 $rl->expects( $this->once() )->method(
'measureResponseTime' );
889 $this->expectOutputRegex(
'/no modules were requested/' );
898 $stats = $this->getMockBuilder( NullStatsdDataFactory::class )
899 ->setMethods( [
'timing' ] )->getMock();
900 $this->
setService(
'StatsdDataFactory', $stats );
902 $stats->expects( $this->once() )->method(
'timing' )
903 ->with(
'resourceloader.responseTime', $this->
anything() );
906 $timing->mark(
'requestShutdown' );
908 $rl->measureResponseTime( $timing );
909 DeferredUpdates::doUpdates();
ResourceLoader module based on local JavaScript/CSS files.
getResourceLoaderContext( $options=[], ResourceLoader $rl=null)
testMakeLoaderSourcesScript()
ResourceLoader::makeLoaderSourcesScript.
testMakeModuleResponseExtraHeadersMulti()
ResourceLoaderModule::getHeaders ResourceLoaderModule::buildContent ResourceLoader::makeModuleRespons...
testExpandModuleNames( $desc, $modules, $packed, $unpacked=null)
providePackedModules ResourceLoaderContext::expandModuleNames
static providePackedModules()
testAddSourceInvalid()
ResourceLoader::addSource.
testMakeLoaderRegisterScript()
ResourceLoader::makeLoaderRegisterScript.
getSimpleModuleMock( $script='')
testMakeModuleResponseExtraHeaders()
Integration test for modules sending extra HTTP response headers.
testIsModuleRegistered()
ResourceLoader::isModuleRegistered.
testLessFileCompilation()
ResourceLoaderFileModule::compileLessFile.
testGetModuleNames()
ResourceLoader::getModuleNames.
testMakeLoaderImplementScriptInvalid()
ResourceLoader::makeLoaderImplementScript.
provideTestIsFileModule()
testRespond()
ResourceLoader::respond.
testGetModuleClass()
ResourceLoader::getModule.
testMakeModuleResponseConcat( $scripts, $expected, $debug, $message=null)
Verify how multiple scripts and mw.loader.state() calls are concatenated.
testRegisterValidArray()
ResourceLoader::register ResourceLoader::getModule.
static provideAddSource()
testIsFileModule( $expected, $module)
provideTestIsFileModule ResourceLoader::isFileModule
static provideLoaderImplement()
testMakeModuleResponseStartupError()
Verify that when building the startup module response, an exception from one module class will not br...
testAddSourceDupe()
ResourceLoader::addSource.
testGetModuleUnknown()
ResourceLoader::getModule.
testGetLoadScript()
ResourceLoader::getLoadScript.
static provideMakeModuleResponseConcat()
testRegisterValidObject()
ResourceLoader::register ResourceLoader::getModule.
testGetCombinedVersion()
ResourceLoader::getCombinedVersion.
testAddSource( $name, $info, $expected)
provideAddSource ResourceLoader::addSource ResourceLoader::getSources
testMeasureResponseTime()
ResourceLoader::measureResponseTime.
testMakeModuleResponseError()
Verify that when building module content in a load.php response, an exception from one module will no...
testMakeLoaderImplementScript( $case)
provideLoaderImplement ResourceLoader::makeLoaderImplementScript ResourceLoader::trimArray
testMakePackedModulesString( $desc, $modules, $packed)
providePackedModules ResourceLoader::makePackedModulesString
testRegisterInvalidType()
ResourceLoader::register.
testGetModuleClassDefault()
ResourceLoader::getModule.
testRegisterEmptyString()
ResourceLoader::register.
testRegisterInvalidName()
ResourceLoader::register.
testIsFileModuleUnknown()
ResourceLoader::isFileModule.
testGetModuleFactory()
ResourceLoader::getModule.
testConstructRegistrationHook()
Ensure the ResourceLoaderRegisterModules hook is called.
Dynamic JavaScript and CSS resource loading system.
static makeLoaderSourcesScript( $id, $loadUrl=null)
Returns JS code which calls mw.loader.addSource() with the given parameters.
addSource( $id, $loadUrl=null)
Add a foreign source of modules.
static clearCache()
Reset static members used for caching.
static makePackedModulesString( $modules)
Convert an array of module names to a packed query string.
static makeLoaderRegisterScript( $name, $version=null, $dependencies=null, $group=null, $source=null, $skip=null)
Returns JS code which calls mw.loader.register with the given parameters.
An interface to help developers measure the performance of their applications.
A wrapper class which causes Xml::encodeJsVar() and Xml::encodeJsCall() to interpret a given string a...
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 and we might be restricted by PHP settings such as safe mode or open_basedir We cannot assume that the software even has read access anywhere useful Many shared hosts run all users web applications under the same so they can t rely on Unix and must forbid reads to even standard directories like tmp lest users read each others files We cannot assume that the user has the ability to install or run any programs not written as web accessible PHP scripts Since anything that works on cheap shared hosting will work if you have shell or root access MediaWiki s design is based around catering to the lowest common denominator Although we support higher end setups as the way many things work by default is tailored toward shared hosting These defaults are unconventional from the point of view of and they certainly aren t ideal for someone who s installing MediaWiki as MediaWiki does not conform to normal Unix filesystem layout Hopefully we ll offer direct support for standard layouts in the but for now *any change to the location of files is unsupported *Moving things and leaving symlinks will *probably *not break anything
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
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
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
this hook is for auditing only $response
returning false will NOT prevent logging $e