wiki.techinc.nl/tests/phpunit/includes/resourceloader/ResourceLoaderTest.php

440 lines
11 KiB
PHP
Raw Normal View History

<?php
class ResourceLoaderTest extends ResourceLoaderTestCase {
Support LESS stylesheets in ResourceLoader This patch adds support for the LESS stylesheet language to ResourceLoader. LESS is a stylesheet language that compiles into CSS. The patch includes lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is explained in a MediaWiki RFC which accompanies this patch, available at <https://www.mediawiki.org/wiki/Requests_for_comment/LESS>. LESS support is provided for ResourceLoader file modules. It is triggered by the presence of the '.less' extension in stylesheet filenames. LESS files are compiled by lessc, and the resultant CSS is subjected to the standard set of transformations (CSSJanus & CSSMin). The immediate result of LESS compilation is encoded as an array, which includes the list of LESS files that were compiled and their mtimes. This array is cached. Cache invalidation is performed by comparing the cached mtimes with the mtimes of the files on disk. If the compiler itself throws an exception, ResourceLoader constructs a compilation result which consists of the error message encoded as a CSS comment. Failed compilation results are cached too, but with an expiration time of five minutes. The expiration time is required because the full list of referenced files is not known. Three configuration variables configure the global environment for LESS modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and $wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names to CSS values, specified as strings. Variables declared in this array are available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except it maps custom function names to PHP callables. These functions can be called from within LESS to transform values. Read more about custom functions at <http://leafo.net/lessphp/docs/#custom_functions>. Finally, $wgResourceLoaderLESSImportPaths specifies file system paths in addition to the current module's path where the LESS compiler should look up files referenced in @import statements. The issue of handling of /* @embed */ and /* @noflip */ annotations is left unresolved. Earlier versions of this patch included an @embed analog implemented as a LESS custom function, but there was enough ambiguity about whether the strategy it took was optimal to merit discussing it in a separate, follow-up patch. Bug: 40964 Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
protected function setUp() {
parent::setUp();
$this->setMwGlobals( [
'wgResourceLoaderLESSImportPaths' => [
dirname( dirname( __DIR__ ) ) . '/data/less/common',
],
'wgResourceLoaderLESSVars' => [
Support LESS stylesheets in ResourceLoader This patch adds support for the LESS stylesheet language to ResourceLoader. LESS is a stylesheet language that compiles into CSS. The patch includes lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is explained in a MediaWiki RFC which accompanies this patch, available at <https://www.mediawiki.org/wiki/Requests_for_comment/LESS>. LESS support is provided for ResourceLoader file modules. It is triggered by the presence of the '.less' extension in stylesheet filenames. LESS files are compiled by lessc, and the resultant CSS is subjected to the standard set of transformations (CSSJanus & CSSMin). The immediate result of LESS compilation is encoded as an array, which includes the list of LESS files that were compiled and their mtimes. This array is cached. Cache invalidation is performed by comparing the cached mtimes with the mtimes of the files on disk. If the compiler itself throws an exception, ResourceLoader constructs a compilation result which consists of the error message encoded as a CSS comment. Failed compilation results are cached too, but with an expiration time of five minutes. The expiration time is required because the full list of referenced files is not known. Three configuration variables configure the global environment for LESS modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and $wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names to CSS values, specified as strings. Variables declared in this array are available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except it maps custom function names to PHP callables. These functions can be called from within LESS to transform values. Read more about custom functions at <http://leafo.net/lessphp/docs/#custom_functions>. Finally, $wgResourceLoaderLESSImportPaths specifies file system paths in addition to the current module's path where the LESS compiler should look up files referenced in @import statements. The issue of handling of /* @embed */ and /* @noflip */ annotations is left unresolved. Earlier versions of this patch included an @embed analog implemented as a LESS custom function, but there was enough ambiguity about whether the strategy it took was optimal to merit discussing it in a separate, follow-up patch. Bug: 40964 Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
'foo' => '2px',
'Foo' => '#eeeeee',
'bar' => 5,
],
] );
Support LESS stylesheets in ResourceLoader This patch adds support for the LESS stylesheet language to ResourceLoader. LESS is a stylesheet language that compiles into CSS. The patch includes lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is explained in a MediaWiki RFC which accompanies this patch, available at <https://www.mediawiki.org/wiki/Requests_for_comment/LESS>. LESS support is provided for ResourceLoader file modules. It is triggered by the presence of the '.less' extension in stylesheet filenames. LESS files are compiled by lessc, and the resultant CSS is subjected to the standard set of transformations (CSSJanus & CSSMin). The immediate result of LESS compilation is encoded as an array, which includes the list of LESS files that were compiled and their mtimes. This array is cached. Cache invalidation is performed by comparing the cached mtimes with the mtimes of the files on disk. If the compiler itself throws an exception, ResourceLoader constructs a compilation result which consists of the error message encoded as a CSS comment. Failed compilation results are cached too, but with an expiration time of five minutes. The expiration time is required because the full list of referenced files is not known. Three configuration variables configure the global environment for LESS modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and $wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names to CSS values, specified as strings. Variables declared in this array are available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except it maps custom function names to PHP callables. These functions can be called from within LESS to transform values. Read more about custom functions at <http://leafo.net/lessphp/docs/#custom_functions>. Finally, $wgResourceLoaderLESSImportPaths specifies file system paths in addition to the current module's path where the LESS compiler should look up files referenced in @import statements. The issue of handling of /* @embed */ and /* @noflip */ annotations is left unresolved. Earlier versions of this patch included an @embed analog implemented as a LESS custom function, but there was enough ambiguity about whether the strategy it took was optimal to merit discussing it in a separate, follow-up patch. Bug: 40964 Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
}
/**
* Ensure the ResourceLoaderRegisterModules hook is called.
*
* @covers ResourceLoader::__construct
*/
public function testConstructRegistrationHook() {
$resourceLoaderRegisterModulesHook = false;
$this->setMwGlobals( 'wgHooks', [
'ResourceLoaderRegisterModules' => [
function ( &$resourceLoader ) use ( &$resourceLoaderRegisterModulesHook ) {
$resourceLoaderRegisterModulesHook = true;
}
]
] );
$unused = new ResourceLoader();
$this->assertTrue(
$resourceLoaderRegisterModulesHook,
'Hook ResourceLoaderRegisterModules called'
);
}
/**
* @covers ResourceLoader::register
* @covers ResourceLoader::getModule
*/
public function testRegisterValid() {
$module = new ResourceLoaderTestModule();
$resourceLoader = new EmptyResourceLoader();
$resourceLoader->register( 'test', $module );
$this->assertEquals( $module, $resourceLoader->getModule( 'test' ) );
}
Support LESS stylesheets in ResourceLoader This patch adds support for the LESS stylesheet language to ResourceLoader. LESS is a stylesheet language that compiles into CSS. The patch includes lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is explained in a MediaWiki RFC which accompanies this patch, available at <https://www.mediawiki.org/wiki/Requests_for_comment/LESS>. LESS support is provided for ResourceLoader file modules. It is triggered by the presence of the '.less' extension in stylesheet filenames. LESS files are compiled by lessc, and the resultant CSS is subjected to the standard set of transformations (CSSJanus & CSSMin). The immediate result of LESS compilation is encoded as an array, which includes the list of LESS files that were compiled and their mtimes. This array is cached. Cache invalidation is performed by comparing the cached mtimes with the mtimes of the files on disk. If the compiler itself throws an exception, ResourceLoader constructs a compilation result which consists of the error message encoded as a CSS comment. Failed compilation results are cached too, but with an expiration time of five minutes. The expiration time is required because the full list of referenced files is not known. Three configuration variables configure the global environment for LESS modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and $wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names to CSS values, specified as strings. Variables declared in this array are available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except it maps custom function names to PHP callables. These functions can be called from within LESS to transform values. Read more about custom functions at <http://leafo.net/lessphp/docs/#custom_functions>. Finally, $wgResourceLoaderLESSImportPaths specifies file system paths in addition to the current module's path where the LESS compiler should look up files referenced in @import statements. The issue of handling of /* @embed */ and /* @noflip */ annotations is left unresolved. Earlier versions of this patch included an @embed analog implemented as a LESS custom function, but there was enough ambiguity about whether the strategy it took was optimal to merit discussing it in a separate, follow-up patch. Bug: 40964 Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
/**
* @covers ResourceLoader::register
Support LESS stylesheets in ResourceLoader This patch adds support for the LESS stylesheet language to ResourceLoader. LESS is a stylesheet language that compiles into CSS. The patch includes lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is explained in a MediaWiki RFC which accompanies this patch, available at <https://www.mediawiki.org/wiki/Requests_for_comment/LESS>. LESS support is provided for ResourceLoader file modules. It is triggered by the presence of the '.less' extension in stylesheet filenames. LESS files are compiled by lessc, and the resultant CSS is subjected to the standard set of transformations (CSSJanus & CSSMin). The immediate result of LESS compilation is encoded as an array, which includes the list of LESS files that were compiled and their mtimes. This array is cached. Cache invalidation is performed by comparing the cached mtimes with the mtimes of the files on disk. If the compiler itself throws an exception, ResourceLoader constructs a compilation result which consists of the error message encoded as a CSS comment. Failed compilation results are cached too, but with an expiration time of five minutes. The expiration time is required because the full list of referenced files is not known. Three configuration variables configure the global environment for LESS modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and $wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names to CSS values, specified as strings. Variables declared in this array are available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except it maps custom function names to PHP callables. These functions can be called from within LESS to transform values. Read more about custom functions at <http://leafo.net/lessphp/docs/#custom_functions>. Finally, $wgResourceLoaderLESSImportPaths specifies file system paths in addition to the current module's path where the LESS compiler should look up files referenced in @import statements. The issue of handling of /* @embed */ and /* @noflip */ annotations is left unresolved. Earlier versions of this patch included an @embed analog implemented as a LESS custom function, but there was enough ambiguity about whether the strategy it took was optimal to merit discussing it in a separate, follow-up patch. Bug: 40964 Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
*/
public function testRegisterInvalidName() {
$resourceLoader = new EmptyResourceLoader();
$this->setExpectedException( 'MWException', "name 'test!invalid' is invalid" );
$resourceLoader->register( 'test!invalid', new ResourceLoaderTestModule() );
Support LESS stylesheets in ResourceLoader This patch adds support for the LESS stylesheet language to ResourceLoader. LESS is a stylesheet language that compiles into CSS. The patch includes lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is explained in a MediaWiki RFC which accompanies this patch, available at <https://www.mediawiki.org/wiki/Requests_for_comment/LESS>. LESS support is provided for ResourceLoader file modules. It is triggered by the presence of the '.less' extension in stylesheet filenames. LESS files are compiled by lessc, and the resultant CSS is subjected to the standard set of transformations (CSSJanus & CSSMin). The immediate result of LESS compilation is encoded as an array, which includes the list of LESS files that were compiled and their mtimes. This array is cached. Cache invalidation is performed by comparing the cached mtimes with the mtimes of the files on disk. If the compiler itself throws an exception, ResourceLoader constructs a compilation result which consists of the error message encoded as a CSS comment. Failed compilation results are cached too, but with an expiration time of five minutes. The expiration time is required because the full list of referenced files is not known. Three configuration variables configure the global environment for LESS modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and $wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names to CSS values, specified as strings. Variables declared in this array are available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except it maps custom function names to PHP callables. These functions can be called from within LESS to transform values. Read more about custom functions at <http://leafo.net/lessphp/docs/#custom_functions>. Finally, $wgResourceLoaderLESSImportPaths specifies file system paths in addition to the current module's path where the LESS compiler should look up files referenced in @import statements. The issue of handling of /* @embed */ and /* @noflip */ annotations is left unresolved. Earlier versions of this patch included an @embed analog implemented as a LESS custom function, but there was enough ambiguity about whether the strategy it took was optimal to merit discussing it in a separate, follow-up patch. Bug: 40964 Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
}
/**
* @covers ResourceLoader::register
*/
public function testRegisterInvalidType() {
$resourceLoader = new EmptyResourceLoader();
$this->setExpectedException( 'MWException', 'ResourceLoader module info type error' );
$resourceLoader->register( 'test', new stdClass() );
}
/**
* @covers ResourceLoader::getModuleNames
*/
public function testGetModuleNames() {
// Use an empty one so that core and extension modules don't get in.
$resourceLoader = new EmptyResourceLoader();
$resourceLoader->register( 'test.foo', new ResourceLoaderTestModule() );
$resourceLoader->register( 'test.bar', new ResourceLoaderTestModule() );
$this->assertEquals(
[ 'test.foo', 'test.bar' ],
$resourceLoader->getModuleNames()
);
}
/**
* @covers ResourceLoader::isModuleRegistered
*/
public function testIsModuleRegistered() {
$rl = new EmptyResourceLoader();
$rl->register( 'test', new ResourceLoaderTestModule() );
$this->assertTrue( $rl->isModuleRegistered( 'test' ) );
$this->assertFalse( $rl->isModuleRegistered( 'test.unknown' ) );
}
/**
* @covers ResourceLoader::getModule
*/
public function testGetModuleUnknown() {
$rl = new EmptyResourceLoader();
$this->assertSame( null, $rl->getModule( 'test' ) );
}
/**
* @covers ResourceLoader::getModule
*/
public function testGetModuleClass() {
$rl = new EmptyResourceLoader();
$rl->register( 'test', [ 'class' => ResourceLoaderTestModule::class ] );
$this->assertInstanceOf(
ResourceLoaderTestModule::class,
$rl->getModule( 'test' )
);
}
/**
* @covers ResourceLoader::getModule
*/
public function testGetModuleClassDefault() {
$rl = new EmptyResourceLoader();
$rl->register( 'test', [] );
$this->assertInstanceOf(
ResourceLoaderFileModule::class,
$rl->getModule( 'test' ),
'Array-style module registrations default to FileModule'
);
}
/**
* @covers ResourceLoaderFileModule::compileLessFile
*/
public function testLessFileCompilation() {
$context = $this->getResourceLoaderContext();
$basePath = __DIR__ . '/../../data/less/module';
$module = new ResourceLoaderFileModule( [
'localBasePath' => $basePath,
'styles' => [ 'styles.less' ],
] );
$module->setName( 'test.less' );
$styles = $module->getStyles( $context );
$this->assertStringEqualsFile( $basePath . '/styles.css', $styles['all'] );
}
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
public static function providePackedModules() {
return [
[
'Example from makePackedModulesString doc comment',
[ 'foo.bar', 'foo.baz', 'bar.baz', 'bar.quux' ],
'foo.bar,baz|bar.baz,quux',
],
[
'Example from expandModuleNames doc comment',
[ 'jquery.foo', 'jquery.bar', 'jquery.ui.baz', 'jquery.ui.quux' ],
'jquery.foo,bar|jquery.ui.baz,quux',
],
[
'Regression fixed in r88706 with dotless names',
[ 'foo', 'bar', 'baz' ],
'foo,bar,baz',
],
[
'Prefixless modules after a prefixed module',
[ 'single.module', 'foobar', 'foobaz' ],
'single.module|foobar,foobaz',
],
[
'Ordering',
[ 'foo', 'foo.baz', 'baz.quux', 'foo.bar' ],
'foo|foo.baz,bar|baz.quux',
[ 'foo', 'foo.baz', 'foo.bar', 'baz.quux' ],
]
];
}
/**
* @dataProvider providePackedModules
* @covers ResourceLoader::makePackedModulesString
*/
public function testMakePackedModulesString( $desc, $modules, $packed ) {
$this->assertEquals( $packed, ResourceLoader::makePackedModulesString( $modules ), $desc );
}
/**
* @dataProvider providePackedModules
* @covers ResourceLoaderContext::expandModuleNames
*/
public function testExpandModuleNames( $desc, $modules, $packed, $unpacked = null ) {
$this->assertEquals(
$unpacked ?: $modules,
ResourceLoaderContext::expandModuleNames( $packed ),
$desc
);
}
public static function provideAddSource() {
return [
[ 'foowiki', 'https://example.org/w/load.php', 'foowiki' ],
[ 'foowiki', [ 'loadScript' => 'https://example.org/w/load.php' ], 'foowiki' ],
[
[
'foowiki' => 'https://example.org/w/load.php',
'bazwiki' => 'https://example.com/w/load.php',
],
null,
[ 'foowiki', 'bazwiki' ]
]
];
}
/**
* @dataProvider provideAddSource
* @covers ResourceLoader::addSource
* @covers ResourceLoader::getSources
*/
public function testAddSource( $name, $info, $expected ) {
$rl = new ResourceLoader;
$rl->addSource( $name, $info );
if ( is_array( $expected ) ) {
foreach ( $expected as $source ) {
$this->assertArrayHasKey( $source, $rl->getSources() );
}
} else {
$this->assertArrayHasKey( $expected, $rl->getSources() );
}
}
/**
* @covers ResourceLoader::addSource
*/
public function testAddSourceDupe() {
$rl = new ResourceLoader;
$this->setExpectedException( 'MWException', 'ResourceLoader duplicate source addition error' );
$rl->addSource( 'foo', 'https://example.org/w/load.php' );
$rl->addSource( 'foo', 'https://example.com/w/load.php' );
}
/**
* @covers ResourceLoader::addSource
*/
public function testAddSourceInvalid() {
$rl = new ResourceLoader;
$this->setExpectedException( 'MWException', 'with no "loadScript" key' );
$rl->addSource( 'foo', [ 'x' => 'https://example.org/w/load.php' ] );
}
public static function provideLoaderImplement() {
return [
[ [
'title' => 'Implement scripts, styles and messages',
'name' => 'test.example',
'scripts' => 'mw.example();',
'styles' => [ 'css' => [ '.mw-example {}' ] ],
'messages' => [ 'example' => '' ],
'templates' => [],
'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
mw.example();
}, {
"css": [
".mw-example {}"
]
}, {
"example": ""
} );',
] ],
[ [
'title' => 'Implement scripts',
'name' => 'test.example',
'scripts' => 'mw.example();',
'styles' => [],
'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
mw.example();
} );',
] ],
[ [
'title' => 'Implement styles',
'name' => 'test.example',
'scripts' => [],
'styles' => [ 'css' => [ '.mw-example {}' ] ],
'messages' => new XmlJsCode( '{}' ),
'expected' => 'mw.loader.implement( "test.example", [], {
"css": [
".mw-example {}"
]
} );',
] ],
[ [
'title' => 'Implement scripts and messages',
'name' => 'test.example',
'scripts' => 'mw.example();',
'messages' => [ 'example' => '' ],
'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
mw.example();
}, {}, {
"example": ""
} );',
] ],
[ [
'title' => 'Implement scripts and templates',
'name' => 'test.example',
'scripts' => 'mw.example();',
'templates' => [ 'example.html' => '' ],
'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
mw.example();
}, {}, {}, {
"example.html": ""
} );',
] ],
[ [
'title' => 'Implement unwrapped user script',
'name' => 'user',
'scripts' => 'mw.example( 1 );',
'expected' => 'mw.loader.implement( "user", "mw.example( 1 );" );',
] ],
[ [
'title' => 'Implement unwrapped user script',
'debug' => false,
'name' => 'user',
'scripts' => 'mw.example( 1 );',
'expected' => 'mw.loader.implement("user","mw.example(1);");',
] ],
];
}
/**
* @dataProvider provideLoaderImplement
* @covers ResourceLoader::makeLoaderImplementScript
* @covers ResourceLoader::trimArray
*/
public function testMakeLoaderImplementScript( $case ) {
$case += [
'styles' => [], 'templates' => [], 'messages' => new XmlJsCode( '{}' ),
'debug' => true
];
ResourceLoader::clearCache();
$this->setMwGlobals( 'wgResourceLoaderDebug', $case['debug'] );
$this->assertEquals(
$case['expected'],
ResourceLoader::makeLoaderImplementScript(
$case['name'],
$case['scripts'],
$case['styles'],
$case['messages'],
$case['templates']
)
);
}
/**
* @covers ResourceLoader::makeLoaderImplementScript
*/
public function testMakeLoaderImplementScriptInvalid() {
$this->setExpectedException( 'MWException', 'Invalid scripts error' );
ResourceLoader::makeLoaderImplementScript(
'test', // name
123, // scripts
null, // styles
null, // messages
null // templates
);
}
/**
* @covers ResourceLoader::makeLoaderSourcesScript
*/
public function testMakeLoaderSourcesScript() {
$this->assertEquals(
'mw.loader.addSource( "local", "/w/load.php" );',
ResourceLoader::makeLoaderSourcesScript( 'local', '/w/load.php' )
);
$this->assertEquals(
'mw.loader.addSource( {
"local": "/w/load.php"
} );',
ResourceLoader::makeLoaderSourcesScript( [ 'local' => '/w/load.php' ] )
);
$this->assertEquals(
'mw.loader.addSource( {
"local": "/w/load.php",
"example": "https://example.org/w/load.php"
} );',
ResourceLoader::makeLoaderSourcesScript( [
'local' => '/w/load.php',
'example' => 'https://example.org/w/load.php'
] )
);
$this->assertEquals(
'mw.loader.addSource( [] );',
ResourceLoader::makeLoaderSourcesScript( [] )
);
}
private static function fakeSources() {
return [
'examplewiki' => [
'loadScript' => '//example.org/w/load.php',
'apiScript' => '//example.org/w/api.php',
],
'example2wiki' => [
'loadScript' => '//example.com/w/load.php',
'apiScript' => '//example.com/w/api.php',
],
];
}
/**
* @covers ResourceLoader::getLoadScript
*/
public function testGetLoadScript() {
$this->setMwGlobals( 'wgResourceLoaderSources', [] );
$rl = new ResourceLoader();
$sources = self::fakeSources();
$rl->addSource( $sources );
foreach ( [ 'examplewiki', 'example2wiki' ] as $name ) {
$this->assertEquals( $rl->getLoadScript( $name ), $sources[$name]['loadScript'] );
}
try {
$rl->getLoadScript( 'thiswasneverreigstered' );
$this->assertTrue( false, 'ResourceLoader::getLoadScript should have thrown an exception' );
} catch ( MWException $e ) {
$this->assertTrue( true );
}
}
}