3 class ResourceLoaderTest
extends ResourceLoaderTestCase
{
5 protected function setUp() {
8 $this->setMwGlobals( array(
9 'wgResourceLoaderLESSImportPaths' => array(
10 dirname( dirname( __DIR__
) ) . '/data/less/common',
12 'wgResourceLoaderLESSVars' => array(
20 public static function provideValidModules() {
22 array( 'TEST.validModule1', new ResourceLoaderTestModule() ),
27 * Ensures that the ResourceLoaderRegisterModules hook is called when a new
28 * ResourceLoader object is constructed.
29 * @covers ResourceLoader::__construct
31 public function testCreatingNewResourceLoaderCallsRegistrationHook() {
32 $resourceLoaderRegisterModulesHook = false;
34 $this->setMwGlobals( 'wgHooks', array(
35 'ResourceLoaderRegisterModules' => array(
36 function ( &$resourceLoader ) use ( &$resourceLoaderRegisterModulesHook ) {
37 $resourceLoaderRegisterModulesHook = true;
42 $resourceLoader = new ResourceLoader();
44 $resourceLoaderRegisterModulesHook,
45 'Hook ResourceLoaderRegisterModules called'
48 return $resourceLoader;
52 * @dataProvider provideValidModules
53 * @depends testCreatingNewResourceLoaderCallsRegistrationHook
54 * @covers ResourceLoader::register
55 * @covers ResourceLoader::getModule
57 public function testRegisteredValidModulesAreAccessible(
58 $name, ResourceLoaderModule
$module, ResourceLoader
$resourceLoader
60 $resourceLoader->register( $name, $module );
61 $this->assertEquals( $module, $resourceLoader->getModule( $name ) );
65 * @covers ResourceLoaderFileModule::compileLessFile
67 public function testLessFileCompilation() {
68 $context = $this->getResourceLoaderContext();
69 $basePath = __DIR__
. '/../../data/less/module';
70 $module = new ResourceLoaderFileModule( array(
71 'localBasePath' => $basePath,
72 'styles' => array( 'styles.less' ),
74 $module->setName( 'test.less' );
75 $styles = $module->getStyles( $context );
76 $this->assertStringEqualsFile( $basePath . '/styles.css', $styles['all'] );
80 * Strip @noflip annotations from CSS code.
84 private static function stripNoflip( $css ) {
85 return str_replace( '/*@noflip*/ ', '', $css );
89 * @dataProvider providePackedModules
90 * @covers ResourceLoader::makePackedModulesString
92 public function testMakePackedModulesString( $desc, $modules, $packed ) {
93 $this->assertEquals( $packed, ResourceLoader
::makePackedModulesString( $modules ), $desc );
97 * @dataProvider providePackedModules
98 * @covers ResourceLoaderContext::expandModuleNames
100 public function testexpandModuleNames( $desc, $modules, $packed ) {
101 $this->assertEquals( $modules, ResourceLoaderContext
::expandModuleNames( $packed ), $desc );
104 public static function providePackedModules() {
107 'Example from makePackedModulesString doc comment',
108 array( 'foo.bar', 'foo.baz', 'bar.baz', 'bar.quux' ),
109 'foo.bar,baz|bar.baz,quux',
112 'Example from expandModuleNames doc comment',
113 array( 'jquery.foo', 'jquery.bar', 'jquery.ui.baz', 'jquery.ui.quux' ),
114 'jquery.foo,bar|jquery.ui.baz,quux',
117 'Regression fixed in r88706 with dotless names',
118 array( 'foo', 'bar', 'baz' ),
122 'Prefixless modules after a prefixed module',
123 array( 'single.module', 'foobar', 'foobaz' ),
124 'single.module|foobar,foobaz',
129 public static function provideAddSource() {
131 array( 'examplewiki', '//example.org/w/load.php', 'examplewiki' ),
132 array( 'example2wiki', array( 'loadScript' => '//example.com/w/load.php' ), 'example2wiki' ),
134 array( 'foowiki' => '//foo.org/w/load.php', 'bazwiki' => '//baz.org/w/load.php' ),
136 array( 'foowiki', 'bazwiki' )
139 array( 'foowiki' => '//foo.org/w/load.php' ),
147 * @dataProvider provideAddSource
148 * @covers ResourceLoader::addSource
149 * @covers ResourceLoader::getSources
151 public function testAddSource( $name, $info, $expected ) {
152 $rl = new ResourceLoader
;
153 if ( $expected === false ) {
154 $this->setExpectedException( 'MWException', 'ResourceLoader duplicate source addition error' );
155 $rl->addSource( $name, $info );
157 $rl->addSource( $name, $info );
158 if ( is_array( $expected ) ) {
159 foreach ( $expected as $source ) {
160 $this->assertArrayHasKey( $source, $rl->getSources() );
163 $this->assertArrayHasKey( $expected, $rl->getSources() );
167 public static function fakeSources() {
169 'examplewiki' => array(
170 'loadScript' => '//example.org/w/load.php',
171 'apiScript' => '//example.org/w/api.php',
173 'example2wiki' => array(
174 'loadScript' => '//example.com/w/load.php',
175 'apiScript' => '//example.com/w/api.php',
180 public static function provideLoaderImplement() {
183 'title' => 'Implement scripts, styles and messages',
185 'name' => 'test.example',
186 'scripts' => 'mw.example();',
187 'styles' => array( 'css' => array( '.mw-example {}' ) ),
188 'messages' => array( 'example' => '' ),
189 'templates' => array(),
191 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery ) {
202 'title' => 'Implement scripts',
204 'name' => 'test.example',
205 'scripts' => 'mw.example();',
207 'messages' => new XmlJsCode( '{}' ),
208 'templates' => array(),
209 'title' => 'scripts, styles and messags',
211 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery ) {
216 'title' => 'Implement styles',
218 'name' => 'test.example',
219 'scripts' => array(),
220 'styles' => array( 'css' => array( '.mw-example {}' ) ),
221 'messages' => new XmlJsCode( '{}' ),
222 'templates' => array(),
224 'expected' => 'mw.loader.implement( "test.example", [], {
231 'title' => 'Implement scripts and messages',
233 'name' => 'test.example',
234 'scripts' => 'mw.example();',
236 'messages' => array( 'example' => '' ),
237 'templates' => array(),
239 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery ) {
246 'title' => 'Implement scripts and templates',
248 'name' => 'test.example',
249 'scripts' => 'mw.example();',
251 'messages' => new XmlJsCode( '{}' ),
252 'templates' => array( 'example.html' => '' ),
254 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery ) {
264 * @dataProvider provideLoaderImplement
265 * @covers ResourceLoader::makeLoaderImplementScript
267 public function testMakeLoaderImplementScript( $case ) {
270 ResourceLoader
::makeLoaderImplementScript(
281 * @covers ResourceLoader::getLoadScript
283 public function testGetLoadScript() {
284 $this->setMwGlobals( 'wgResourceLoaderSources', array() );
285 $rl = new ResourceLoader();
286 $sources = self
::fakeSources();
287 $rl->addSource( $sources );
288 foreach ( array( 'examplewiki', 'example2wiki' ) as $name ) {
289 $this->assertEquals( $rl->getLoadScript( $name ), $sources[$name]['loadScript'] );
293 $rl->getLoadScript( 'thiswasneverreigstered' );
294 $this->assertTrue( false, 'ResourceLoader::getLoadScript should have thrown an exception' );
295 } catch ( MWException
$e ) {
296 $this->assertTrue( true );
301 * @covers ResourceLoader::isModuleRegistered
303 public function testIsModuleRegistered() {
304 $rl = new ResourceLoader();
305 $rl->register( 'test.module', new ResourceLoaderTestModule() );
306 $this->assertTrue( $rl->isModuleRegistered( 'test.module' ) );
307 $this->assertFalse( $rl->isModuleRegistered( 'test.modulenotregistered' ) );