3 class ResourceLoaderTest
extends ResourceLoaderTestCase
{
5 protected function setUp() {
8 $this->setMwGlobals( array(
9 'wgResourceLoaderLESSFunctions' => array(
10 'test-sum' => function ( $frame, $less ) {
12 foreach ( $frame[2] as $arg ) {
18 'wgResourceLoaderLESSImportPaths' => array(
19 dirname( dirname( __DIR__
) ) . '/data/less/common',
21 'wgResourceLoaderLESSVars' => array(
29 public static function provideValidModules() {
31 array( 'TEST.validModule1', new ResourceLoaderTestModule() ),
36 * Ensures that the ResourceLoaderRegisterModules hook is called when a new
37 * ResourceLoader object is constructed.
38 * @covers ResourceLoader::__construct
40 public function testCreatingNewResourceLoaderCallsRegistrationHook() {
41 $resourceLoaderRegisterModulesHook = false;
43 $this->setMwGlobals( 'wgHooks', array(
44 'ResourceLoaderRegisterModules' => array(
45 function ( &$resourceLoader ) use ( &$resourceLoaderRegisterModulesHook ) {
46 $resourceLoaderRegisterModulesHook = true;
51 $resourceLoader = new ResourceLoader();
53 $resourceLoaderRegisterModulesHook,
54 'Hook ResourceLoaderRegisterModules called'
57 return $resourceLoader;
61 * @dataProvider provideValidModules
62 * @depends testCreatingNewResourceLoaderCallsRegistrationHook
63 * @covers ResourceLoader::register
64 * @covers ResourceLoader::getModule
66 public function testRegisteredValidModulesAreAccessible(
67 $name, ResourceLoaderModule
$module, ResourceLoader
$resourceLoader
69 $resourceLoader->register( $name, $module );
70 $this->assertEquals( $module, $resourceLoader->getModule( $name ) );
74 * @covers ResourceLoaderFileModule::compileLessFile
76 public function testLessFileCompilation() {
77 $context = $this->getResourceLoaderContext();
78 $basePath = __DIR__
. '/../../data/less/module';
79 $module = new ResourceLoaderFileModule( array(
80 'localBasePath' => $basePath,
81 'styles' => array( 'styles.less' ),
83 $module->setName( 'test.less' );
84 $styles = $module->getStyles( $context );
85 $this->assertStringEqualsFile( $basePath . '/styles.css', $styles['all'] );
89 * Strip @noflip annotations from CSS code.
93 private static function stripNoflip( $css ) {
94 return str_replace( '/*@noflip*/ ', '', $css );
98 * @dataProvider providePackedModules
99 * @covers ResourceLoader::makePackedModulesString
101 public function testMakePackedModulesString( $desc, $modules, $packed ) {
102 $this->assertEquals( $packed, ResourceLoader
::makePackedModulesString( $modules ), $desc );
106 * @dataProvider providePackedModules
107 * @covers ResourceLoaderContext::expandModuleNames
109 public function testexpandModuleNames( $desc, $modules, $packed ) {
110 $this->assertEquals( $modules, ResourceLoaderContext
::expandModuleNames( $packed ), $desc );
113 public static function providePackedModules() {
116 'Example from makePackedModulesString doc comment',
117 array( 'foo.bar', 'foo.baz', 'bar.baz', 'bar.quux' ),
118 'foo.bar,baz|bar.baz,quux',
121 'Example from expandModuleNames doc comment',
122 array( 'jquery.foo', 'jquery.bar', 'jquery.ui.baz', 'jquery.ui.quux' ),
123 'jquery.foo,bar|jquery.ui.baz,quux',
126 'Regression fixed in r88706 with dotless names',
127 array( 'foo', 'bar', 'baz' ),
131 'Prefixless modules after a prefixed module',
132 array( 'single.module', 'foobar', 'foobaz' ),
133 'single.module|foobar,foobaz',
138 public static function provideAddSource() {
140 array( 'examplewiki', '//example.org/w/load.php', 'examplewiki' ),
141 array( 'example2wiki', array( 'loadScript' => '//example.com/w/load.php' ), 'example2wiki' ),
143 array( 'foowiki' => '//foo.org/w/load.php', 'bazwiki' => '//baz.org/w/load.php' ),
145 array( 'foowiki', 'bazwiki' )
148 array( 'foowiki' => '//foo.org/w/load.php' ),
156 * @dataProvider provideAddSource
157 * @covers ResourceLoader::addSource
158 * @covers ResourceLoader::getSources
160 public function testAddSource( $name, $info, $expected ) {
161 $rl = new ResourceLoader
;
162 if ( $expected === false ) {
163 $this->setExpectedException( 'MWException', 'ResourceLoader duplicate source addition error' );
164 $rl->addSource( $name, $info );
166 $rl->addSource( $name, $info );
167 if ( is_array( $expected ) ) {
168 foreach ( $expected as $source ) {
169 $this->assertArrayHasKey( $source, $rl->getSources() );
172 $this->assertArrayHasKey( $expected, $rl->getSources() );
176 public static function fakeSources() {
178 'examplewiki' => array(
179 'loadScript' => '//example.org/w/load.php',
180 'apiScript' => '//example.org/w/api.php',
182 'example2wiki' => array(
183 'loadScript' => '//example.com/w/load.php',
184 'apiScript' => '//example.com/w/api.php',
189 public static function provideLoaderImplement() {
192 'title' => 'Implement scripts, styles and messages',
194 'name' => 'test.example',
195 'scripts' => 'mw.example();',
196 'styles' => array( 'css' => array( '.mw-example {}' ) ),
197 'messages' => array( 'example' => '' ),
198 'templates' => array(),
200 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery ) {
211 'title' => 'Implement scripts',
213 'name' => 'test.example',
214 'scripts' => 'mw.example();',
216 'messages' => new XmlJsCode( '{}' ),
217 'templates' => array(),
218 'title' => 'scripts, styles and messags',
220 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery ) {
225 'title' => 'Implement styles',
227 'name' => 'test.example',
228 'scripts' => array(),
229 'styles' => array( 'css' => array( '.mw-example {}' ) ),
230 'messages' => new XmlJsCode( '{}' ),
231 'templates' => array(),
233 'expected' => 'mw.loader.implement( "test.example", [], {
240 'title' => 'Implement scripts and messages',
242 'name' => 'test.example',
243 'scripts' => 'mw.example();',
245 'messages' => array( 'example' => '' ),
246 'templates' => array(),
248 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery ) {
255 'title' => 'Implement scripts and templates',
257 'name' => 'test.example',
258 'scripts' => 'mw.example();',
260 'messages' => new XmlJsCode( '{}' ),
261 'templates' => array( 'example.html' => '' ),
263 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery ) {
273 * @dataProvider provideLoaderImplement
274 * @covers ResourceLoader::makeLoaderImplementScript
276 public function testMakeLoaderImplementScript( $case ) {
279 ResourceLoader
::makeLoaderImplementScript(
290 * @covers ResourceLoader::getLoadScript
292 public function testGetLoadScript() {
293 $this->setMwGlobals( 'wgResourceLoaderSources', array() );
294 $rl = new ResourceLoader();
295 $sources = self
::fakeSources();
296 $rl->addSource( $sources );
297 foreach ( array( 'examplewiki', 'example2wiki' ) as $name ) {
298 $this->assertEquals( $rl->getLoadScript( $name ), $sources[$name]['loadScript'] );
302 $rl->getLoadScript( 'thiswasneverreigstered' );
303 $this->assertTrue( false, 'ResourceLoader::getLoadScript should have thrown an exception' );
304 } catch ( MWException
$e ) {
305 $this->assertTrue( true );
310 * @covers ResourceLoader::isModuleRegistered
312 public function testIsModuleRegistered() {
313 $rl = new ResourceLoader();
314 $rl->register( 'test.module', new ResourceLoaderTestModule() );
315 $this->assertTrue( $rl->isModuleRegistered( 'test.module' ) );
316 $this->assertFalse( $rl->isModuleRegistered( 'test.modulenotregistered' ) );