MediaWiki  1.28.1
ResourceLoaderTest.php
Go to the documentation of this file.
1 <?php
2 
4 
5  protected function setUp() {
6  parent::setUp();
7 
8  $this->setMwGlobals( [
9  'wgResourceLoaderLESSImportPaths' => [
10  dirname( dirname( __DIR__ ) ) . '/data/less/common',
11  ],
12  'wgResourceLoaderLESSVars' => [
13  'foo' => '2px',
14  'Foo' => '#eeeeee',
15  'bar' => 5,
16  ],
17  ] );
18  }
19 
25  public function testConstructRegistrationHook() {
26  $resourceLoaderRegisterModulesHook = false;
27 
28  $this->setMwGlobals( 'wgHooks', [
29  'ResourceLoaderRegisterModules' => [
30  function ( &$resourceLoader ) use ( &$resourceLoaderRegisterModulesHook ) {
31  $resourceLoaderRegisterModulesHook = true;
32  }
33  ]
34  ] );
35 
36  $unused = new ResourceLoader();
37  $this->assertTrue(
38  $resourceLoaderRegisterModulesHook,
39  'Hook ResourceLoaderRegisterModules called'
40  );
41  }
42 
47  public function testRegisterValid() {
48  $module = new ResourceLoaderTestModule();
50  $resourceLoader->register( 'test', $module );
51  $this->assertEquals( $module, $resourceLoader->getModule( 'test' ) );
52  }
53 
57  public function testRegisterInvalidName() {
59  $this->setExpectedException( 'MWException', "name 'test!invalid' is invalid" );
60  $resourceLoader->register( 'test!invalid', new ResourceLoaderTestModule() );
61  }
62 
66  public function testRegisterInvalidType() {
68  $this->setExpectedException( 'MWException', 'ResourceLoader module info type error' );
69  $resourceLoader->register( 'test', new stdClass() );
70  }
71 
75  public function testGetModuleNames() {
76  // Use an empty one so that core and extension modules don't get in.
78  $resourceLoader->register( 'test.foo', new ResourceLoaderTestModule() );
79  $resourceLoader->register( 'test.bar', new ResourceLoaderTestModule() );
80  $this->assertEquals(
81  [ 'test.foo', 'test.bar' ],
82  $resourceLoader->getModuleNames()
83  );
84  }
85 
89  public function testIsModuleRegistered() {
90  $rl = new EmptyResourceLoader();
91  $rl->register( 'test', new ResourceLoaderTestModule() );
92  $this->assertTrue( $rl->isModuleRegistered( 'test' ) );
93  $this->assertFalse( $rl->isModuleRegistered( 'test.unknown' ) );
94  }
95 
99  public function testGetModuleUnknown() {
100  $rl = new EmptyResourceLoader();
101  $this->assertSame( null, $rl->getModule( 'test' ) );
102  }
103 
107  public function testGetModuleClass() {
108  $rl = new EmptyResourceLoader();
109  $rl->register( 'test', [ 'class' => ResourceLoaderTestModule::class ] );
110  $this->assertInstanceOf(
112  $rl->getModule( 'test' )
113  );
114  }
115 
119  public function testGetModuleClassDefault() {
120  $rl = new EmptyResourceLoader();
121  $rl->register( 'test', [] );
122  $this->assertInstanceOf(
124  $rl->getModule( 'test' ),
125  'Array-style module registrations default to FileModule'
126  );
127  }
128 
132  public function testLessFileCompilation() {
134  $basePath = __DIR__ . '/../../data/less/module';
135  $module = new ResourceLoaderFileModule( [
136  'localBasePath' => $basePath,
137  'styles' => [ 'styles.less' ],
138  ] );
139  $module->setName( 'test.less' );
140  $styles = $module->getStyles( $context );
141  $this->assertStringEqualsFile( $basePath . '/styles.css', $styles['all'] );
142  }
143 
144  public static function providePackedModules() {
145  return [
146  [
147  'Example from makePackedModulesString doc comment',
148  [ 'foo.bar', 'foo.baz', 'bar.baz', 'bar.quux' ],
149  'foo.bar,baz|bar.baz,quux',
150  ],
151  [
152  'Example from expandModuleNames doc comment',
153  [ 'jquery.foo', 'jquery.bar', 'jquery.ui.baz', 'jquery.ui.quux' ],
154  'jquery.foo,bar|jquery.ui.baz,quux',
155  ],
156  [
157  'Regression fixed in r88706 with dotless names',
158  [ 'foo', 'bar', 'baz' ],
159  'foo,bar,baz',
160  ],
161  [
162  'Prefixless modules after a prefixed module',
163  [ 'single.module', 'foobar', 'foobaz' ],
164  'single.module|foobar,foobaz',
165  ],
166  [
167  'Ordering',
168  [ 'foo', 'foo.baz', 'baz.quux', 'foo.bar' ],
169  'foo|foo.baz,bar|baz.quux',
170  [ 'foo', 'foo.baz', 'foo.bar', 'baz.quux' ],
171  ]
172  ];
173  }
174 
179  public function testMakePackedModulesString( $desc, $modules, $packed ) {
180  $this->assertEquals( $packed, ResourceLoader::makePackedModulesString( $modules ), $desc );
181  }
182 
187  public function testExpandModuleNames( $desc, $modules, $packed, $unpacked = null ) {
188  $this->assertEquals(
189  $unpacked ?: $modules,
191  $desc
192  );
193  }
194 
195  public static function provideAddSource() {
196  return [
197  [ 'foowiki', 'https://example.org/w/load.php', 'foowiki' ],
198  [ 'foowiki', [ 'loadScript' => 'https://example.org/w/load.php' ], 'foowiki' ],
199  [
200  [
201  'foowiki' => 'https://example.org/w/load.php',
202  'bazwiki' => 'https://example.com/w/load.php',
203  ],
204  null,
205  [ 'foowiki', 'bazwiki' ]
206  ]
207  ];
208  }
209 
215  public function testAddSource( $name, $info, $expected ) {
216  $rl = new ResourceLoader;
217  $rl->addSource( $name, $info );
218  if ( is_array( $expected ) ) {
219  foreach ( $expected as $source ) {
220  $this->assertArrayHasKey( $source, $rl->getSources() );
221  }
222  } else {
223  $this->assertArrayHasKey( $expected, $rl->getSources() );
224  }
225  }
226 
230  public function testAddSourceDupe() {
231  $rl = new ResourceLoader;
232  $this->setExpectedException( 'MWException', 'ResourceLoader duplicate source addition error' );
233  $rl->addSource( 'foo', 'https://example.org/w/load.php' );
234  $rl->addSource( 'foo', 'https://example.com/w/load.php' );
235  }
236 
240  public function testAddSourceInvalid() {
241  $rl = new ResourceLoader;
242  $this->setExpectedException( 'MWException', 'with no "loadScript" key' );
243  $rl->addSource( 'foo', [ 'x' => 'https://example.org/w/load.php' ] );
244  }
245 
246  public static function provideLoaderImplement() {
247  return [
248  [ [
249  'title' => 'Implement scripts, styles and messages',
250 
251  'name' => 'test.example',
252  'scripts' => 'mw.example();',
253  'styles' => [ 'css' => [ '.mw-example {}' ] ],
254  'messages' => [ 'example' => '' ],
255  'templates' => [],
256 
257  'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
258 mw.example();
259 }, {
260  "css": [
261  ".mw-example {}"
262  ]
263 }, {
264  "example": ""
265 } );',
266  ] ],
267  [ [
268  'title' => 'Implement scripts',
269 
270  'name' => 'test.example',
271  'scripts' => 'mw.example();',
272  'styles' => [],
273 
274  'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
275 mw.example();
276 } );',
277  ] ],
278  [ [
279  'title' => 'Implement styles',
280 
281  'name' => 'test.example',
282  'scripts' => [],
283  'styles' => [ 'css' => [ '.mw-example {}' ] ],
284 
285  'expected' => 'mw.loader.implement( "test.example", [], {
286  "css": [
287  ".mw-example {}"
288  ]
289 } );',
290  ] ],
291  [ [
292  'title' => 'Implement scripts and messages',
293 
294  'name' => 'test.example',
295  'scripts' => 'mw.example();',
296  'messages' => [ 'example' => '' ],
297 
298  'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
299 mw.example();
300 }, {}, {
301  "example": ""
302 } );',
303  ] ],
304  [ [
305  'title' => 'Implement scripts and templates',
306 
307  'name' => 'test.example',
308  'scripts' => 'mw.example();',
309  'templates' => [ 'example.html' => '' ],
310 
311  'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
312 mw.example();
313 }, {}, {}, {
314  "example.html": ""
315 } );',
316  ] ],
317  [ [
318  'title' => 'Implement unwrapped user script',
319 
320  'name' => 'user',
321  'scripts' => 'mw.example( 1 );',
322  'wrap' => false,
323 
324  'expected' => 'mw.loader.implement( "user", "mw.example( 1 );" );',
325  ] ],
326  ];
327  }
328 
334  public function testMakeLoaderImplementScript( $case ) {
335  $case += [
336  'wrap' => true,
337  'styles' => [], 'templates' => [], 'messages' => new XmlJsCode( '{}' )
338  ];
340  $this->setMwGlobals( 'wgResourceLoaderDebug', true );
341 
342  $rl = TestingAccessWrapper::newFromClass( 'ResourceLoader' );
343  $this->assertEquals(
344  $case['expected'],
345  $rl->makeLoaderImplementScript(
346  $case['name'],
347  ( $case['wrap'] && is_string( $case['scripts'] ) )
348  ? new XmlJsCode( $case['scripts'] )
349  : $case['scripts'],
350  $case['styles'],
351  $case['messages'],
352  $case['templates']
353  )
354  );
355  }
356 
361  $this->setExpectedException( 'MWException', 'Invalid scripts error' );
362  $rl = TestingAccessWrapper::newFromClass( 'ResourceLoader' );
363  $rl->makeLoaderImplementScript(
364  'test', // name
365  123, // scripts
366  null, // styles
367  null, // messages
368  null // templates
369  );
370  }
371 
375  public function testMakeLoaderSourcesScript() {
376  $this->assertEquals(
377  'mw.loader.addSource( "local", "/w/load.php" );',
378  ResourceLoader::makeLoaderSourcesScript( 'local', '/w/load.php' )
379  );
380  $this->assertEquals(
381  'mw.loader.addSource( {
382  "local": "/w/load.php"
383 } );',
384  ResourceLoader::makeLoaderSourcesScript( [ 'local' => '/w/load.php' ] )
385  );
386  $this->assertEquals(
387  'mw.loader.addSource( {
388  "local": "/w/load.php",
389  "example": "https://example.org/w/load.php"
390 } );',
392  'local' => '/w/load.php',
393  'example' => 'https://example.org/w/load.php'
394  ] )
395  );
396  $this->assertEquals(
397  'mw.loader.addSource( [] );',
399  );
400  }
401 
402  private static function fakeSources() {
403  return [
404  'examplewiki' => [
405  'loadScript' => '//example.org/w/load.php',
406  'apiScript' => '//example.org/w/api.php',
407  ],
408  'example2wiki' => [
409  'loadScript' => '//example.com/w/load.php',
410  'apiScript' => '//example.com/w/api.php',
411  ],
412  ];
413  }
414 
418  public function testGetLoadScript() {
419  $this->setMwGlobals( 'wgResourceLoaderSources', [] );
420  $rl = new ResourceLoader();
421  $sources = self::fakeSources();
422  $rl->addSource( $sources );
423  foreach ( [ 'examplewiki', 'example2wiki' ] as $name ) {
424  $this->assertEquals( $rl->getLoadScript( $name ), $sources[$name]['loadScript'] );
425  }
426 
427  try {
428  $rl->getLoadScript( 'thiswasneverreigstered' );
429  $this->assertTrue( false, 'ResourceLoader::getLoadScript should have thrown an exception' );
430  } catch ( MWException $e ) {
431  $this->assertTrue( true );
432  }
433  }
434 }
testMakeLoaderImplementScript($case)
provideLoaderImplement ResourceLoader::makeLoaderImplementScript ResourceLoader::trimArray ...
testConstructRegistrationHook()
Ensure the ResourceLoaderRegisterModules hook is called.
testAddSourceInvalid()
ResourceLoader::addSource.
$context
Definition: load.php:50
static expandModuleNames($modules)
Expand a string of the form jquery.foo,bar|jquery.ui.baz,quux to an array of module names like [ 'jqu...
error also a ContextSource you ll probably need to make sure the header is varied on such as when responding to a resource loader request or generating HTML output & $resourceLoader
Definition: hooks.txt:2573
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException'returning false will NOT prevent logging $e
Definition: hooks.txt:2102
$source
static makeLoaderSourcesScript($id, $loadUrl=null)
Returns JS code which calls mw.loader.addSource() with the given parameters.
testGetModuleNames()
ResourceLoader::getModuleNames.
testRegisterValid()
ResourceLoader::register ResourceLoader::getModule.
static newFromClass($className)
Allow access to non-public static methods and properties of the class.
testAddSource($name, $info, $expected)
provideAddSource ResourceLoader::addSource ResourceLoader::getSources
testGetLoadScript()
ResourceLoader::getLoadScript.
$basePath
Definition: exportSites.php:3
testMakeLoaderImplementScriptInvalid()
ResourceLoader::makeLoaderImplementScript.
testIsModuleRegistered()
ResourceLoader::isModuleRegistered.
testGetModuleClass()
ResourceLoader::getModule.
getResourceLoaderContext($lang= 'en', $dir= 'ltr')
$modules
testLessFileCompilation()
ResourceLoaderFileModule::compileLessFile.
ResourceLoader module based on local JavaScript/CSS files.
testRegisterInvalidType()
ResourceLoader::register.
A wrapper class which causes Xml::encodeJsVar() and Xml::encodeJsCall() to interpret a given string a...
Definition: Xml.php:884
static makePackedModulesString($modules)
Convert an array of module names to a packed query string.
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 as
Definition: distributors.txt:9
static clearCache()
Reset static members used for caching.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
testMakePackedModulesString($desc, $modules, $packed)
providePackedModules ResourceLoader::makePackedModulesString
testGetModuleClassDefault()
ResourceLoader::getModule.
addSource($id, $loadUrl=null)
Add a foreign source of modules.
testAddSourceDupe()
ResourceLoader::addSource.
setMwGlobals($pairs, $value=null)
Dynamic JavaScript and CSS resource loading system.
testMakeLoaderSourcesScript()
ResourceLoader::makeLoaderSourcesScript.
testExpandModuleNames($desc, $modules, $packed, $unpacked=null)
providePackedModules ResourceLoaderContext::expandModuleNames
testRegisterInvalidName()
ResourceLoader::register.
testGetModuleUnknown()
ResourceLoader::getModule.
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:300