MediaWiki  1.33.1
ResourceLoaderFileModuleTest.php
Go to the documentation of this file.
1 <?php
2 
8 
9  protected function setUp() {
10  parent::setUp();
11 
12  // The return value of the closure shouldn't matter since this test should
13  // never call it
15  'fakeskin',
16  'FakeSkin',
17  function () {
18  }
19  );
20  }
21 
22  private static function getModules() {
23  $base = [
24  'localBasePath' => realpath( __DIR__ ),
25  ];
26 
27  return [
28  'noTemplateModule' => [],
29 
30  'deprecatedModule' => $base + [
31  'deprecated' => true,
32  ],
33  'deprecatedTomorrow' => $base + [
34  'deprecated' => 'Will be removed tomorrow.'
35  ],
36 
37  'htmlTemplateModule' => $base + [
38  'templates' => [
39  'templates/template.html',
40  'templates/template2.html',
41  ]
42  ],
43 
44  'htmlTemplateUnknown' => $base + [
45  'templates' => [
46  'templates/notfound.html',
47  ]
48  ],
49 
50  'aliasedHtmlTemplateModule' => $base + [
51  'templates' => [
52  'foo.html' => 'templates/template.html',
53  'bar.html' => 'templates/template2.html',
54  ]
55  ],
56 
57  'templateModuleHandlebars' => $base + [
58  'templates' => [
59  'templates/template_awesome.handlebars',
60  ],
61  ],
62 
63  'aliasFooFromBar' => $base + [
64  'templates' => [
65  'foo.foo' => 'templates/template.bar',
66  ],
67  ],
68  ];
69  }
70 
71  public static function providerTemplateDependencies() {
73 
74  return [
75  [
76  $modules['noTemplateModule'],
77  [],
78  ],
79  [
80  $modules['htmlTemplateModule'],
81  [
82  'mediawiki.template',
83  ],
84  ],
85  [
86  $modules['templateModuleHandlebars'],
87  [
88  'mediawiki.template',
89  'mediawiki.template.handlebars',
90  ],
91  ],
92  [
93  $modules['aliasFooFromBar'],
94  [
95  'mediawiki.template',
96  'mediawiki.template.foo',
97  ],
98  ],
99  ];
100  }
101 
107  public function testTemplateDependencies( $module, $expected ) {
108  $rl = new ResourceLoaderFileModule( $module );
109  $rl->setName( 'testing' );
110  $this->assertEquals( $rl->getDependencies(), $expected );
111  }
112 
113  public static function providerDeprecatedModules() {
114  return [
115  [
116  'deprecatedModule',
117  'mw.log.warn("This page is using the deprecated ResourceLoader module \"deprecatedModule\".");',
118  ],
119  [
120  'deprecatedTomorrow',
121  'mw.log.warn(' .
122  '"This page is using the deprecated ResourceLoader module \"deprecatedTomorrow\".\\n' .
123  "Will be removed tomorrow." .
124  '");'
125  ]
126  ];
127  }
128 
133  public function testDeprecatedModules( $name, $expected ) {
135  $module = new ResourceLoaderFileModule( $modules[$name] );
136  $module->setName( $name );
137  $ctx = $this->getResourceLoaderContext();
138  $this->assertEquals( $module->getScript( $ctx ), $expected );
139  }
140 
146  public function testGetScript() {
147  $module = new ResourceLoaderFileModule( [
148  'localBasePath' => __DIR__ . '/../../data/resourceloader',
149  'scripts' => [ 'script-nosemi.js', 'script-comment.js' ],
150  ] );
151  $module->setName( 'testing' );
152  $ctx = $this->getResourceLoaderContext();
153  $this->assertEquals(
154  "/* eslint-disable */\nmw.foo()\n" .
155  "\n" .
156  "/* eslint-disable */\nmw.foo()\n// mw.bar();\n" .
157  "\n",
158  $module->getScript( $ctx ),
159  'scripts are concatenated with a new-line'
160  );
161  }
162 
168  public function testGetAllSkinStyleFiles() {
169  $baseParams = [
170  'scripts' => [
171  'foo.js',
172  'bar.js',
173  ],
174  'styles' => [
175  'foo.css',
176  'bar.css' => [ 'media' => 'print' ],
177  'screen.less' => [ 'media' => 'screen' ],
178  'screen-query.css' => [ 'media' => 'screen and (min-width: 400px)' ],
179  ],
180  'skinStyles' => [
181  'default' => 'quux-fallback.less',
182  'fakeskin' => [
183  'baz-vector.css',
184  'quux-vector.less',
185  ],
186  ],
187  'messages' => [
188  'hello',
189  'world',
190  ],
191  ];
192 
193  $module = new ResourceLoaderFileModule( $baseParams );
194  $module->setName( 'testing' );
195 
196  $this->assertEquals(
197  [
198  'foo.css',
199  'baz-vector.css',
200  'quux-vector.less',
201  'quux-fallback.less',
202  'bar.css',
203  'screen.less',
204  'screen-query.css',
205  ],
206  array_map( 'basename', $module->getAllStyleFiles() )
207  );
208  }
209 
215  private static function stripNoflip( $css ) {
216  return str_replace( '/*@noflip*/ ', '', $css );
217  }
218 
228  public function testMixedCssAnnotations() {
229  $basePath = __DIR__ . '/../../data/css';
230  $testModule = new ResourceLoaderFileModule( [
231  'localBasePath' => $basePath,
232  'styles' => [ 'test.css' ],
233  ] );
234  $testModule->setName( 'testing' );
235  $expectedModule = new ResourceLoaderFileModule( [
236  'localBasePath' => $basePath,
237  'styles' => [ 'expected.css' ],
238  ] );
239  $expectedModule->setName( 'testing' );
240 
241  $contextLtr = $this->getResourceLoaderContext( [
242  'lang' => 'en',
243  'dir' => 'ltr',
244  ] );
245  $contextRtl = $this->getResourceLoaderContext( [
246  'lang' => 'he',
247  'dir' => 'rtl',
248  ] );
249 
250  // Since we want to compare the effect of @noflip+@embed against the effect of just @embed, and
251  // the @noflip annotations are always preserved, we need to strip them first.
252  $this->assertEquals(
253  $expectedModule->getStyles( $contextLtr ),
254  self::stripNoflip( $testModule->getStyles( $contextLtr ) ),
255  "/*@noflip*/ with /*@embed*/ gives correct results in LTR mode"
256  );
257  $this->assertEquals(
258  $expectedModule->getStyles( $contextLtr ),
259  self::stripNoflip( $testModule->getStyles( $contextRtl ) ),
260  "/*@noflip*/ with /*@embed*/ gives correct results in RTL mode"
261  );
262  }
263 
264  public static function providerGetTemplates() {
266 
267  return [
268  [
269  $modules['noTemplateModule'],
270  [],
271  ],
272  [
273  $modules['templateModuleHandlebars'],
274  [
275  'templates/template_awesome.handlebars' => "wow\n",
276  ],
277  ],
278  [
279  $modules['htmlTemplateModule'],
280  [
281  'templates/template.html' => "<strong>hello</strong>\n",
282  'templates/template2.html' => "<div>goodbye</div>\n",
283  ],
284  ],
285  [
286  $modules['aliasedHtmlTemplateModule'],
287  [
288  'foo.html' => "<strong>hello</strong>\n",
289  'bar.html' => "<div>goodbye</div>\n",
290  ],
291  ],
292  [
293  $modules['htmlTemplateUnknown'],
294  false,
295  ],
296  ];
297  }
298 
303  public function testGetTemplates( $module, $expected ) {
304  $rl = new ResourceLoaderFileModule( $module );
305  $rl->setName( 'testing' );
306 
307  if ( $expected === false ) {
308  $this->setExpectedException( MWException::class );
309  $rl->getTemplates();
310  } else {
311  $this->assertEquals( $rl->getTemplates(), $expected );
312  }
313  }
314 
318  public function testBomConcatenation() {
319  $basePath = __DIR__ . '/../../data/css';
320  $testModule = new ResourceLoaderFileModule( [
321  'localBasePath' => $basePath,
322  'styles' => [ 'bom.css' ],
323  ] );
324  $testModule->setName( 'testing' );
325  $this->assertEquals(
326  substr( file_get_contents( "$basePath/bom.css" ), 0, 10 ),
327  "\xef\xbb\xbf.efbbbf",
328  'File has leading BOM'
329  );
330 
332  $this->assertEquals(
333  $testModule->getStyles( $context ),
334  [ 'all' => ".efbbbf_bom_char_at_start_of_file {}\n" ],
335  'Leading BOM removed when concatenating files'
336  );
337  }
338 
342  public function testLessFileCompilation() {
344  $basePath = __DIR__ . '/../../data/less/module';
345  $module = new ResourceLoaderFileTestModule( [
346  'localBasePath' => $basePath,
347  'styles' => [ 'styles.less' ],
348  ], [
349  'lessVars' => [ 'foo' => '2px', 'Foo' => '#eeeeee' ]
350  ] );
351  $module->setName( 'test.less' );
352  $styles = $module->getStyles( $context );
353  $this->assertStringEqualsFile( $basePath . '/styles.css', $styles['all'] );
354  }
355 
360  public function testGetVersionHash() {
362 
363  // Less variables
364  $module = new ResourceLoaderFileTestModule();
365  $version = $module->getVersionHash( $context );
366  $module = new ResourceLoaderFileTestModule( [], [
367  'lessVars' => [ 'key' => 'value' ],
368  ] );
369  $this->assertNotEquals(
370  $version,
371  $module->getVersionHash( $context ),
372  'Using less variables is significant'
373  );
374  }
375 
376  public function providerGetScriptPackageFiles() {
377  $basePath = __DIR__ . '/../../data/resourceloader';
378  $base = [ 'localBasePath' => $basePath ];
379  $commentScript = file_get_contents( "$basePath/script-comment.js" );
380  $nosemiScript = file_get_contents( "$basePath/script-nosemi.js" );
381  $config = RequestContext::getMain()->getConfig();
382  return [
383  [
384  $base + [
385  'packageFiles' => [
386  'script-comment.js',
387  'script-nosemi.js'
388  ]
389  ],
390  [
391  'files' => [
392  'script-comment.js' => [
393  'type' => 'script',
394  'content' => $commentScript,
395  ],
396  'script-nosemi.js' => [
397  'type' => 'script',
398  'content' => $nosemiScript
399  ]
400  ],
401  'main' => 'script-comment.js'
402  ]
403  ],
404  [
405  $base + [
406  'packageFiles' => [
407  'script-comment.js',
408  [ 'name' => 'script-nosemi.js', 'main' => true ]
409  ],
410  'deprecated' => 'Deprecation test',
411  'name' => 'test-deprecated'
412  ],
413  [
414  'files' => [
415  'script-comment.js' => [
416  'type' => 'script',
417  'content' => $commentScript,
418  ],
419  'script-nosemi.js' => [
420  'type' => 'script',
421  'content' => 'mw.log.warn(' .
422  '"This page is using the deprecated ResourceLoader module \"test-deprecated\".\\n' .
423  "Deprecation test" .
424  '");' .
425  $nosemiScript
426  ]
427  ],
428  'main' => 'script-nosemi.js'
429  ]
430  ],
431  [
432  $base + [
433  'packageFiles' => [
434  [ 'name' => 'init.js', 'file' => 'script-comment.js', 'main' => true ],
435  [ 'name' => 'nosemi.js', 'file' => 'script-nosemi.js' ],
436  ]
437  ],
438  [
439  'files' => [
440  'init.js' => [
441  'type' => 'script',
442  'content' => $commentScript,
443  ],
444  'nosemi.js' => [
445  'type' => 'script',
446  'content' => $nosemiScript
447  ]
448  ],
449  'main' => 'init.js'
450  ]
451  ],
452  [
453  $base + [
454  'packageFiles' => [
455  [ 'name' => 'foo.json', 'content' => [ 'Hello' => 'world' ] ],
456  'sample.json',
457  [ 'name' => 'bar.js', 'content' => "console.log('Hello');" ],
458  [ 'name' => 'data.json', 'callback' => function ( $context ) {
459  return [ 'langCode' => $context->getLanguage() ];
460  } ],
461  [ 'name' => 'config.json', 'config' => [
462  'Sitename',
463  'wgVersion' => 'Version',
464  ] ],
465  ]
466  ],
467  [
468  'files' => [
469  'foo.json' => [
470  'type' => 'data',
471  'content' => [ 'Hello' => 'world' ],
472  ],
473  'sample.json' => [
474  'type' => 'data',
475  'content' => (object)[ 'foo' => 'bar', 'answer' => 42 ],
476  ],
477  'bar.js' => [
478  'type' => 'script',
479  'content' => "console.log('Hello');",
480  ],
481  'data.json' => [
482  'type' => 'data',
483  'content' => [ 'langCode' => 'fy' ]
484  ],
485  'config.json' => [
486  'type' => 'data',
487  'content' => [
488  'Sitename' => $config->get( 'Sitename' ),
489  'wgVersion' => $config->get( 'Version' ),
490  ]
491  ]
492  ],
493  'main' => 'bar.js'
494  ],
495  [
496  'lang' => 'fy'
497  ]
498  ],
499  [
500  $base + [
501  'packageFiles' => [
502  [ 'file' => 'script-comment.js' ]
503  ]
504  ],
505  false
506  ],
507  [
508  $base + [
509  'packageFiles' => [
510  [ 'name' => 'foo.json', 'callback' => 'functionThatDoesNotExist142857' ]
511  ]
512  ],
513  false
514  ],
515  [
516  $base + [
517  'packageFiles' => [
518  'foo.json' => [ 'type' => 'script', 'config' => [ 'Sitename' ] ]
519  ]
520  ],
521  false
522  ],
523  [
524  $base + [
525  'packageFiles' => [
526  [ 'name' => 'foo.js', 'config' => 'Sitename' ]
527  ]
528  ],
529  false
530  ],
531  [
532  $base + [
533  'packageFiles' => [
534  'foo.js' => [ 'garbage' => 'data' ]
535  ]
536  ],
537  false
538  ],
539  [
540  $base + [
541  'packageFiles' => [
542  'filethatdoesnotexist142857.js'
543  ]
544  ],
545  false
546  ],
547  [
548  $base + [
549  'packageFiles' => [
550  'script-nosemi.js',
551  [ 'name' => 'foo.json', 'content' => [ 'Hello' => 'world' ], 'main' => true ]
552  ]
553  ],
554  false
555  ]
556  ];
557  }
558 
565  public function testGetScriptPackageFiles( $moduleDefinition, $expected, $contextOptions = [] ) {
566  $module = new ResourceLoaderFileModule( $moduleDefinition );
567  $context = $this->getResourceLoaderContext( $contextOptions );
568  if ( isset( $moduleDefinition['name'] ) ) {
569  $module->setName( $moduleDefinition['name'] );
570  }
571  if ( $expected === false ) {
572  $this->setExpectedException( MWException::class );
573  $module->getScript( $context );
574  } else {
575  $this->assertEquals( $expected, $module->getScript( $context ) );
576  }
577  }
578 }
ResourceLoaderFileModuleTest\providerGetScriptPackageFiles
providerGetScriptPackageFiles()
Definition: ResourceLoaderFileModuleTest.php:376
ResourceLoaderFileModuleTest\providerDeprecatedModules
static providerDeprecatedModules()
Definition: ResourceLoaderFileModuleTest.php:113
ResourceLoaderFileModuleTest\testLessFileCompilation
testLessFileCompilation()
ResourceLoaderFileModule::compileLessFile.
Definition: ResourceLoaderFileModuleTest.php:342
ResourceLoaderFileModuleTest\testTemplateDependencies
testTemplateDependencies( $module, $expected)
providerTemplateDependencies ResourceLoaderFileModule::__construct ResourceLoaderFileModule::getDepen...
Definition: ResourceLoaderFileModuleTest.php:107
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
ResourceLoaderFileModuleTest\testGetAllSkinStyleFiles
testGetAllSkinStyleFiles()
ResourceLoaderFileModule::getAllStyleFiles ResourceLoaderFileModule::getAllSkinStyleFiles ResourceLoa...
Definition: ResourceLoaderFileModuleTest.php:168
$context
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition: hooks.txt:2644
ResourceLoaderFileTestModule
Definition: ResourceLoaderTestCase.php:153
$base
$base
Definition: generateLocalAutoload.php:11
ResourceLoaderFileModuleTest
Database ResourceLoader.
Definition: ResourceLoaderFileModuleTest.php:7
php
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
ResourceLoaderFileModule
ResourceLoader module based on local JavaScript/CSS files.
Definition: ResourceLoaderFileModule.php:28
ResourceLoaderFileModuleTest\providerGetTemplates
static providerGetTemplates()
Definition: ResourceLoaderFileModuleTest.php:264
ResourceLoaderFileModuleTest\testGetScriptPackageFiles
testGetScriptPackageFiles( $moduleDefinition, $expected, $contextOptions=[])
providerGetScriptPackageFiles ResourceLoaderFileModule::getScript ResourceLoaderFileModule::getPackag...
Definition: ResourceLoaderFileModuleTest.php:565
$css
$css
Definition: styleTest.css.php:54
$modules
$modules
Definition: HTMLFormElement.php:12
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
ResourceLoaderFileModuleTest\testGetVersionHash
testGetVersionHash()
ResourceLoaderFileModule::getDefinitionSummary ResourceLoaderFileModule::getFileHashes.
Definition: ResourceLoaderFileModuleTest.php:360
ResourceLoaderFileModuleTest\testGetScript
testGetScript()
ResourceLoaderFileModule::getScript ResourceLoaderFileModule::getScriptFiles ResourceLoaderFileModule...
Definition: ResourceLoaderFileModuleTest.php:146
ResourceLoaderFileModuleTest\stripNoflip
static stripNoflip( $css)
Strip @noflip annotations from CSS code.
Definition: ResourceLoaderFileModuleTest.php:215
ResourceLoaderFileModuleTest\testMixedCssAnnotations
testMixedCssAnnotations()
What happens when you mix @embed and @noflip? This really is an integration test, but oh well.
Definition: ResourceLoaderFileModuleTest.php:228
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:430
ResourceLoaderFileModuleTest\setUp
setUp()
Definition: ResourceLoaderFileModuleTest.php:9
ResourceLoaderFileModuleTest\getModules
static getModules()
Definition: ResourceLoaderFileModuleTest.php:22
ResourceLoaderFileModuleTest\testDeprecatedModules
testDeprecatedModules( $name, $expected)
providerDeprecatedModules ResourceLoaderFileModule::getScript
Definition: ResourceLoaderFileModuleTest.php:133
ResourceLoaderTestCase\getResourceLoaderContext
getResourceLoaderContext( $options=[], ResourceLoader $rl=null)
Definition: ResourceLoaderTestCase.php:23
$basePath
$basePath
Definition: addSite.php:5
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1993
class
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
object
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest object
Definition: globals.txt:25
ResourceLoaderFileModuleTest\testGetTemplates
testGetTemplates( $module, $expected)
providerGetTemplates ResourceLoaderFileModule::getTemplates
Definition: ResourceLoaderFileModuleTest.php:303
ResourceLoaderFileModuleTest\providerTemplateDependencies
static providerTemplateDependencies()
Definition: ResourceLoaderFileModuleTest.php:71
ResourceLoaderFileModuleTest\testBomConcatenation
testBomConcatenation()
ResourceLoaderFileModule::stripBom.
Definition: ResourceLoaderFileModuleTest.php:318
SkinFactory\getDefaultInstance
static getDefaultInstance()
Definition: SkinFactory.php:50
ResourceLoaderTestCase
Definition: ResourceLoaderTestCase.php:7