MediaWiki  1.27.2
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 
20  public static function provideValidModules() {
21  return [
22  [ 'TEST.validModule1', new ResourceLoaderTestModule() ],
23  ];
24  }
25 
32  $resourceLoaderRegisterModulesHook = false;
33 
34  $this->setMwGlobals( 'wgHooks', [
35  'ResourceLoaderRegisterModules' => [
36  function ( &$resourceLoader ) use ( &$resourceLoaderRegisterModulesHook ) {
37  $resourceLoaderRegisterModulesHook = true;
38  }
39  ]
40  ] );
41 
43  $this->assertTrue(
44  $resourceLoaderRegisterModulesHook,
45  'Hook ResourceLoaderRegisterModules called'
46  );
47 
48  return $resourceLoader;
49  }
50 
59  ) {
60  $resourceLoader->register( $name, $module );
61  $this->assertEquals( $module, $resourceLoader->getModule( $name ) );
62  }
63 
67  public function testLessFileCompilation() {
69  $basePath = __DIR__ . '/../../data/less/module';
70  $module = new ResourceLoaderFileModule( [
71  'localBasePath' => $basePath,
72  'styles' => [ 'styles.less' ],
73  ] );
74  $module->setName( 'test.less' );
75  $styles = $module->getStyles( $context );
76  $this->assertStringEqualsFile( $basePath . '/styles.css', $styles['all'] );
77  }
78 
84  private static function stripNoflip( $css ) {
85  return str_replace( '/*@noflip*/ ', '', $css );
86  }
87 
92  public function testMakePackedModulesString( $desc, $modules, $packed ) {
93  $this->assertEquals( $packed, ResourceLoader::makePackedModulesString( $modules ), $desc );
94  }
95 
100  public function testexpandModuleNames( $desc, $modules, $packed ) {
101  $this->assertEquals( $modules, ResourceLoaderContext::expandModuleNames( $packed ), $desc );
102  }
103 
104  public static function providePackedModules() {
105  return [
106  [
107  'Example from makePackedModulesString doc comment',
108  [ 'foo.bar', 'foo.baz', 'bar.baz', 'bar.quux' ],
109  'foo.bar,baz|bar.baz,quux',
110  ],
111  [
112  'Example from expandModuleNames doc comment',
113  [ 'jquery.foo', 'jquery.bar', 'jquery.ui.baz', 'jquery.ui.quux' ],
114  'jquery.foo,bar|jquery.ui.baz,quux',
115  ],
116  [
117  'Regression fixed in r88706 with dotless names',
118  [ 'foo', 'bar', 'baz' ],
119  'foo,bar,baz',
120  ],
121  [
122  'Prefixless modules after a prefixed module',
123  [ 'single.module', 'foobar', 'foobaz' ],
124  'single.module|foobar,foobaz',
125  ],
126  ];
127  }
128 
129  public static function provideAddSource() {
130  return [
131  [ 'examplewiki', '//example.org/w/load.php', 'examplewiki' ],
132  [ 'example2wiki', [ 'loadScript' => '//example.com/w/load.php' ], 'example2wiki' ],
133  [
134  [ 'foowiki' => '//foo.org/w/load.php', 'bazwiki' => '//baz.org/w/load.php' ],
135  null,
136  [ 'foowiki', 'bazwiki' ]
137  ],
138  [
139  [ 'foowiki' => '//foo.org/w/load.php' ],
140  null,
141  false,
142  ],
143  ];
144  }
145 
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 );
156  }
157  $rl->addSource( $name, $info );
158  if ( is_array( $expected ) ) {
159  foreach ( $expected as $source ) {
160  $this->assertArrayHasKey( $source, $rl->getSources() );
161  }
162  } else {
163  $this->assertArrayHasKey( $expected, $rl->getSources() );
164  }
165  }
166 
167  public static function fakeSources() {
168  return [
169  'examplewiki' => [
170  'loadScript' => '//example.org/w/load.php',
171  'apiScript' => '//example.org/w/api.php',
172  ],
173  'example2wiki' => [
174  'loadScript' => '//example.com/w/load.php',
175  'apiScript' => '//example.com/w/api.php',
176  ],
177  ];
178  }
179 
180  public static function provideLoaderImplement() {
181  return [
182  [ [
183  'title' => 'Implement scripts, styles and messages',
184 
185  'name' => 'test.example',
186  'scripts' => 'mw.example();',
187  'styles' => [ 'css' => [ '.mw-example {}' ] ],
188  'messages' => [ 'example' => '' ],
189  'templates' => [],
190 
191  'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
192 mw.example();
193 }, {
194  "css": [
195  ".mw-example {}"
196  ]
197 }, {
198  "example": ""
199 } );',
200  ] ],
201  [ [
202  'title' => 'Implement scripts',
203 
204  'name' => 'test.example',
205  'scripts' => 'mw.example();',
206  'styles' => [],
207  'messages' => new XmlJsCode( '{}' ),
208  'templates' => [],
209 
210  'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
211 mw.example();
212 } );',
213  ] ],
214  [ [
215  'title' => 'Implement styles',
216 
217  'name' => 'test.example',
218  'scripts' => [],
219  'styles' => [ 'css' => [ '.mw-example {}' ] ],
220  'messages' => new XmlJsCode( '{}' ),
221  'templates' => [],
222 
223  'expected' => 'mw.loader.implement( "test.example", [], {
224  "css": [
225  ".mw-example {}"
226  ]
227 } );',
228  ] ],
229  [ [
230  'title' => 'Implement scripts and messages',
231 
232  'name' => 'test.example',
233  'scripts' => 'mw.example();',
234  'styles' => [],
235  'messages' => [ 'example' => '' ],
236  'templates' => [],
237 
238  'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
239 mw.example();
240 }, {}, {
241  "example": ""
242 } );',
243  ] ],
244  [ [
245  'title' => 'Implement scripts and templates',
246 
247  'name' => 'test.example',
248  'scripts' => 'mw.example();',
249  'styles' => [],
250  'messages' => new XmlJsCode( '{}' ),
251  'templates' => [ 'example.html' => '' ],
252 
253  'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
254 mw.example();
255 }, {}, {}, {
256  "example.html": ""
257 } );',
258  ] ],
259  ];
260  }
261 
266  public function testMakeLoaderImplementScript( $case ) {
267  $this->assertEquals(
268  $case['expected'],
270  $case['name'],
271  $case['scripts'],
272  $case['styles'],
273  $case['messages'],
274  $case['templates']
275  )
276  );
277  }
278 
282  public function testGetLoadScript() {
283  $this->setMwGlobals( 'wgResourceLoaderSources', [] );
284  $rl = new ResourceLoader();
285  $sources = self::fakeSources();
286  $rl->addSource( $sources );
287  foreach ( [ 'examplewiki', 'example2wiki' ] as $name ) {
288  $this->assertEquals( $rl->getLoadScript( $name ), $sources[$name]['loadScript'] );
289  }
290 
291  try {
292  $rl->getLoadScript( 'thiswasneverreigstered' );
293  $this->assertTrue( false, 'ResourceLoader::getLoadScript should have thrown an exception' );
294  } catch ( MWException $e ) {
295  $this->assertTrue( true );
296  }
297  }
298 
302  public function testIsModuleRegistered() {
303  $rl = new ResourceLoader();
304  $rl->register( 'test.module', new ResourceLoaderTestModule() );
305  $this->assertTrue( $rl->isModuleRegistered( 'test.module' ) );
306  $this->assertFalse( $rl->isModuleRegistered( 'test.modulenotregistered' ) );
307  }
308 }
testMakeLoaderImplementScript($case)
provideLoaderImplement ResourceLoader::makeLoaderImplementScript
$context
Definition: load.php:44
Abstraction for ResourceLoader modules, with name registration and maxage functionality.
testRegisteredValidModulesAreAccessible($name, ResourceLoaderModule $module, ResourceLoader $resourceLoader)
provideValidModules testCreatingNewResourceLoaderCallsRegistrationHook ResourceLoader::register Reso...
static expandModuleNames($modules)
Expand a string of the form jquery.foo,bar|jquery.ui.baz,quux to an array of module names like array(...
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:2418
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:1932
getModule($name)
Get the ResourceLoaderModule object for a given module name.
testCreatingNewResourceLoaderCallsRegistrationHook()
Ensures that the ResourceLoaderRegisterModules hook is called when a new ResourceLoader object is con...
$source
static makeLoaderImplementScript($name, $scripts, $styles, $messages, $templates)
Return JS code that calls mw.loader.implement with given module properties.
testAddSource($name, $info, $expected)
provideAddSource ResourceLoader::addSource ResourceLoader::getSources
testGetLoadScript()
ResourceLoader::getLoadScript.
$basePath
Definition: exportSites.php:3
testIsModuleRegistered()
ResourceLoader::isModuleRegistered.
getResourceLoaderContext($lang= 'en', $dir= 'ltr')
testLessFileCompilation()
ResourceLoaderFileModule::compileLessFile.
$css
ResourceLoader module based on local JavaScript/CSS files.
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
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
register($name, $info=null)
Register a module with the ResourceLoader system.
testexpandModuleNames($desc, $modules, $packed)
providePackedModules ResourceLoaderContext::expandModuleNames
testMakePackedModulesString($desc, $modules, $packed)
providePackedModules ResourceLoader::makePackedModulesString
setMwGlobals($pairs, $value=null)
Dynamic JavaScript and CSS resource loading system.
static stripNoflip($css)
Strip annotations from CSS code.
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:310