2010-12-14 16:26:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
2014-03-07 17:18:31 +00:00
|
|
|
class ResourceLoaderTest extends ResourceLoaderTestCase {
|
2010-12-14 16:26:35 +00:00
|
|
|
|
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();
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( [
|
|
|
|
|
'wgResourceLoaderLESSImportPaths' => [
|
2015-06-16 19:06:19 +00:00
|
|
|
dirname( dirname( __DIR__ ) ) . '/data/less/common',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'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,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
] );
|
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
|
|
|
}
|
|
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
/**
|
2016-08-25 01:50:30 +00:00
|
|
|
* Ensure the ResourceLoaderRegisterModules hook is called.
|
|
|
|
|
*
|
2010-12-14 16:26:35 +00:00
|
|
|
* @covers ResourceLoader::__construct
|
|
|
|
|
*/
|
2016-08-25 01:50:30 +00:00
|
|
|
public function testConstructRegistrationHook() {
|
2014-10-15 15:05:04 +00:00
|
|
|
$resourceLoaderRegisterModulesHook = false;
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( 'wgHooks', [
|
|
|
|
|
'ResourceLoaderRegisterModules' => [
|
2014-10-15 15:05:04 +00:00
|
|
|
function ( &$resourceLoader ) use ( &$resourceLoaderRegisterModulesHook ) {
|
|
|
|
|
$resourceLoaderRegisterModulesHook = true;
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
]
|
|
|
|
|
] );
|
2014-10-15 15:05:04 +00:00
|
|
|
|
2016-08-25 01:50:30 +00:00
|
|
|
$unused = new ResourceLoader();
|
2014-10-15 15:05:04 +00:00
|
|
|
$this->assertTrue(
|
|
|
|
|
$resourceLoaderRegisterModulesHook,
|
|
|
|
|
'Hook ResourceLoaderRegisterModules called'
|
|
|
|
|
);
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::register
|
|
|
|
|
* @covers ResourceLoader::getModule
|
|
|
|
|
*/
|
2016-08-25 01:50:30 +00:00
|
|
|
public function testRegisterValid() {
|
|
|
|
|
$module = new ResourceLoaderTestModule();
|
|
|
|
|
$resourceLoader = new EmptyResourceLoader();
|
|
|
|
|
$resourceLoader->register( 'test', $module );
|
|
|
|
|
$this->assertEquals( $module, $resourceLoader->getModule( 'test' ) );
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
2011-06-07 17:56:40 +00:00
|
|
|
|
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
|
|
|
/**
|
2016-08-25 01:50:30 +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
|
|
|
*/
|
2016-08-25 01:50:30 +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
|
|
|
}
|
|
|
|
|
|
2014-09-20 21:26:13 +00:00
|
|
|
/**
|
2016-08-25 01:50:30 +00:00
|
|
|
* @covers ResourceLoader::register
|
2014-09-20 21:26:13 +00:00
|
|
|
*/
|
2016-08-25 01:50:30 +00:00
|
|
|
public function testRegisterInvalidType() {
|
|
|
|
|
$resourceLoader = new EmptyResourceLoader();
|
|
|
|
|
$this->setExpectedException( 'MWException', 'ResourceLoader module info type error' );
|
|
|
|
|
$resourceLoader->register( 'test', new stdClass() );
|
2014-09-20 21:26:13 +00:00
|
|
|
}
|
|
|
|
|
|
2011-06-07 17:56:40 +00:00
|
|
|
/**
|
2016-08-25 01:50:30 +00:00
|
|
|
* @covers ResourceLoader::getModuleNames
|
2011-06-07 17:56:40 +00:00
|
|
|
*/
|
2016-08-25 01:50:30 +00:00
|
|
|
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()
|
|
|
|
|
);
|
2011-06-07 17:56:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-08-25 01:50:30 +00:00
|
|
|
* @covers ResourceLoader::isModuleRegistered
|
2011-06-07 17:56:40 +00:00
|
|
|
*/
|
2016-08-25 01:50:30 +00:00
|
|
|
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'] );
|
2011-06-07 17:56:40 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
public static function providePackedModules() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[
|
2011-06-07 17:56:40 +00:00
|
|
|
'Example from makePackedModulesString doc comment',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'foo.bar', 'foo.baz', 'bar.baz', 'bar.quux' ],
|
2011-06-07 17:56:40 +00:00
|
|
|
'foo.bar,baz|bar.baz,quux',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2011-06-07 17:56:40 +00:00
|
|
|
'Example from expandModuleNames doc comment',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'jquery.foo', 'jquery.bar', 'jquery.ui.baz', 'jquery.ui.quux' ],
|
2011-06-07 17:56:40 +00:00
|
|
|
'jquery.foo,bar|jquery.ui.baz,quux',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2011-06-07 17:56:40 +00:00
|
|
|
'Regression fixed in r88706 with dotless names',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'foo', 'bar', 'baz' ],
|
2011-06-07 17:56:40 +00:00
|
|
|
'foo,bar,baz',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2013-05-27 15:13:59 +00:00
|
|
|
'Prefixless modules after a prefixed module',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'single.module', 'foobar', 'foobaz' ],
|
2013-05-27 15:13:59 +00:00
|
|
|
'single.module|foobar,foobaz',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2011-06-07 17:56:40 +00:00
|
|
|
}
|
2014-06-28 02:57:40 +00:00
|
|
|
|
2016-08-25 01:50:30 +00:00
|
|
|
/**
|
|
|
|
|
* @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 ) {
|
|
|
|
|
$this->assertEquals( $modules, ResourceLoaderContext::expandModuleNames( $packed ), $desc );
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-25 08:02:48 +00:00
|
|
|
public static function provideAddSource() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2016-08-25 01:50:30 +00:00
|
|
|
[ 'foowiki', 'https://example.org/w/load.php', 'foowiki' ],
|
|
|
|
|
[ 'foowiki', [ 'loadScript' => 'https://example.org/w/load.php' ], 'foowiki' ],
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2016-08-25 01:50:30 +00:00
|
|
|
[
|
|
|
|
|
'foowiki' => 'https://example.org/w/load.php',
|
|
|
|
|
'bazwiki' => 'https://example.com/w/load.php',
|
|
|
|
|
],
|
2014-08-25 08:02:48 +00:00
|
|
|
null,
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'foowiki', 'bazwiki' ]
|
2016-08-25 01:50:30 +00:00
|
|
|
]
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2014-08-25 08:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideAddSource
|
|
|
|
|
* @covers ResourceLoader::addSource
|
2015-03-23 03:57:45 +00:00
|
|
|
* @covers ResourceLoader::getSources
|
2014-08-25 08:02:48 +00:00
|
|
|
*/
|
|
|
|
|
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() );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-25 01:50:30 +00:00
|
|
|
/**
|
|
|
|
|
* @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' ] );
|
2014-06-28 02:57:40 +00:00
|
|
|
}
|
|
|
|
|
|
2014-12-09 01:17:53 +00:00
|
|
|
public static function provideLoaderImplement() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ [
|
2014-12-09 01:17:53 +00:00
|
|
|
'title' => 'Implement scripts, styles and messages',
|
|
|
|
|
|
|
|
|
|
'name' => 'test.example',
|
|
|
|
|
'scripts' => 'mw.example();',
|
2016-02-17 09:09:32 +00:00
|
|
|
'styles' => [ 'css' => [ '.mw-example {}' ] ],
|
|
|
|
|
'messages' => [ 'example' => '' ],
|
|
|
|
|
'templates' => [],
|
2014-12-09 01:17:53 +00:00
|
|
|
|
2016-01-22 19:29:28 +00:00
|
|
|
'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
|
2014-12-09 01:17:53 +00:00
|
|
|
mw.example();
|
|
|
|
|
}, {
|
|
|
|
|
"css": [
|
|
|
|
|
".mw-example {}"
|
|
|
|
|
]
|
|
|
|
|
}, {
|
|
|
|
|
"example": ""
|
|
|
|
|
} );',
|
2016-02-17 09:09:32 +00:00
|
|
|
] ],
|
|
|
|
|
[ [
|
2014-12-09 01:17:53 +00:00
|
|
|
'title' => 'Implement scripts',
|
|
|
|
|
|
|
|
|
|
'name' => 'test.example',
|
|
|
|
|
'scripts' => 'mw.example();',
|
2016-02-17 09:09:32 +00:00
|
|
|
'styles' => [],
|
2014-12-09 01:17:53 +00:00
|
|
|
|
2016-01-22 19:29:28 +00:00
|
|
|
'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
|
2014-12-09 01:17:53 +00:00
|
|
|
mw.example();
|
|
|
|
|
} );',
|
2016-02-17 09:09:32 +00:00
|
|
|
] ],
|
|
|
|
|
[ [
|
2014-12-09 01:17:53 +00:00
|
|
|
'title' => 'Implement styles',
|
|
|
|
|
|
|
|
|
|
'name' => 'test.example',
|
2016-02-17 09:09:32 +00:00
|
|
|
'scripts' => [],
|
|
|
|
|
'styles' => [ 'css' => [ '.mw-example {}' ] ],
|
2014-12-09 01:17:53 +00:00
|
|
|
'messages' => new XmlJsCode( '{}' ),
|
|
|
|
|
|
|
|
|
|
'expected' => 'mw.loader.implement( "test.example", [], {
|
|
|
|
|
"css": [
|
|
|
|
|
".mw-example {}"
|
|
|
|
|
]
|
|
|
|
|
} );',
|
2016-02-17 09:09:32 +00:00
|
|
|
] ],
|
|
|
|
|
[ [
|
2014-12-09 01:17:53 +00:00
|
|
|
'title' => 'Implement scripts and messages',
|
|
|
|
|
|
|
|
|
|
'name' => 'test.example',
|
|
|
|
|
'scripts' => 'mw.example();',
|
2016-02-17 09:09:32 +00:00
|
|
|
'messages' => [ 'example' => '' ],
|
2014-12-09 01:17:53 +00:00
|
|
|
|
2016-01-22 19:29:28 +00:00
|
|
|
'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
|
2014-12-09 01:17:53 +00:00
|
|
|
mw.example();
|
|
|
|
|
}, {}, {
|
|
|
|
|
"example": ""
|
|
|
|
|
} );',
|
2016-02-17 09:09:32 +00:00
|
|
|
] ],
|
|
|
|
|
[ [
|
2014-12-09 01:17:53 +00:00
|
|
|
'title' => 'Implement scripts and templates',
|
|
|
|
|
|
|
|
|
|
'name' => 'test.example',
|
|
|
|
|
'scripts' => 'mw.example();',
|
2016-02-17 09:09:32 +00:00
|
|
|
'templates' => [ 'example.html' => '' ],
|
2014-12-09 01:17:53 +00:00
|
|
|
|
2016-01-22 19:29:28 +00:00
|
|
|
'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
|
2014-12-09 01:17:53 +00:00
|
|
|
mw.example();
|
|
|
|
|
}, {}, {}, {
|
|
|
|
|
"example.html": ""
|
|
|
|
|
} );',
|
2016-02-17 09:09:32 +00:00
|
|
|
] ],
|
2016-08-25 01:50:30 +00:00
|
|
|
[ [
|
|
|
|
|
'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);");',
|
|
|
|
|
] ],
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2014-12-09 01:17:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideLoaderImplement
|
|
|
|
|
* @covers ResourceLoader::makeLoaderImplementScript
|
2016-08-25 01:50:30 +00:00
|
|
|
* @covers ResourceLoader::trimArray
|
2014-12-09 01:17:53 +00:00
|
|
|
*/
|
|
|
|
|
public function testMakeLoaderImplementScript( $case ) {
|
2016-08-25 01:50:30 +00:00
|
|
|
$case += [
|
|
|
|
|
'styles' => [], 'templates' => [], 'messages' => new XmlJsCode( '{}' ),
|
|
|
|
|
'debug' => true
|
|
|
|
|
];
|
|
|
|
|
ResourceLoader::clearCache();
|
|
|
|
|
$this->setMwGlobals( 'wgResourceLoaderDebug', $case['debug'] );
|
|
|
|
|
|
2014-12-09 01:17:53 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
$case['expected'],
|
|
|
|
|
ResourceLoader::makeLoaderImplementScript(
|
|
|
|
|
$case['name'],
|
|
|
|
|
$case['scripts'],
|
|
|
|
|
$case['styles'],
|
|
|
|
|
$case['messages'],
|
|
|
|
|
$case['templates']
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-25 01:50:30 +00:00
|
|
|
/**
|
|
|
|
|
* @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',
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-28 02:57:40 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::getLoadScript
|
|
|
|
|
*/
|
|
|
|
|
public function testGetLoadScript() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( 'wgResourceLoaderSources', [] );
|
2014-06-28 02:57:40 +00:00
|
|
|
$rl = new ResourceLoader();
|
|
|
|
|
$sources = self::fakeSources();
|
|
|
|
|
$rl->addSource( $sources );
|
2016-02-17 09:09:32 +00:00
|
|
|
foreach ( [ 'examplewiki', 'example2wiki' ] as $name ) {
|
2014-06-28 02:57:40 +00:00
|
|
|
$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 );
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|