MediaWiki  REL1_31
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 
144  public function testGetScript() {
145  $module = new ResourceLoaderFileModule( [
146  'localBasePath' => __DIR__ . '/../../data/resourceloader',
147  'scripts' => [ 'script-nosemi.js', 'script-comment.js' ],
148  ] );
149  $module->setName( 'testing' );
150  $ctx = $this->getResourceLoaderContext();
151  $this->assertEquals(
152  "/* eslint-disable */\nmw.foo()\n" .
153  "\n" .
154  "/* eslint-disable */\nmw.foo()\n// mw.bar();\n" .
155  "\n",
156  $module->getScript( $ctx ),
157  'scripts are concatenated with a new-line'
158  );
159  }
160 
166  public function testGetAllSkinStyleFiles() {
167  $baseParams = [
168  'scripts' => [
169  'foo.js',
170  'bar.js',
171  ],
172  'styles' => [
173  'foo.css',
174  'bar.css' => [ 'media' => 'print' ],
175  'screen.less' => [ 'media' => 'screen' ],
176  'screen-query.css' => [ 'media' => 'screen and (min-width: 400px)' ],
177  ],
178  'skinStyles' => [
179  'default' => 'quux-fallback.less',
180  'fakeskin' => [
181  'baz-vector.css',
182  'quux-vector.less',
183  ],
184  ],
185  'messages' => [
186  'hello',
187  'world',
188  ],
189  ];
190 
191  $module = new ResourceLoaderFileModule( $baseParams );
192  $module->setName( 'testing' );
193 
194  $this->assertEquals(
195  [
196  'foo.css',
197  'baz-vector.css',
198  'quux-vector.less',
199  'quux-fallback.less',
200  'bar.css',
201  'screen.less',
202  'screen-query.css',
203  ],
204  array_map( 'basename', $module->getAllStyleFiles() )
205  );
206  }
207 
213  private static function stripNoflip( $css ) {
214  return str_replace( '/*@noflip*/ ', '', $css );
215  }
216 
224  public function testMixedCssAnnotations() {
225  $basePath = __DIR__ . '/../../data/css';
226  $testModule = new ResourceLoaderFileModule( [
227  'localBasePath' => $basePath,
228  'styles' => [ 'test.css' ],
229  ] );
230  $testModule->setName( 'testing' );
231  $expectedModule = new ResourceLoaderFileModule( [
232  'localBasePath' => $basePath,
233  'styles' => [ 'expected.css' ],
234  ] );
235  $expectedModule->setName( 'testing' );
236 
237  $contextLtr = $this->getResourceLoaderContext( [
238  'lang' => 'en',
239  'dir' => 'ltr',
240  ] );
241  $contextRtl = $this->getResourceLoaderContext( [
242  'lang' => 'he',
243  'dir' => 'rtl',
244  ] );
245 
246  // Since we want to compare the effect of @noflip+@embed against the effect of just @embed, and
247  // the @noflip annotations are always preserved, we need to strip them first.
248  $this->assertEquals(
249  $expectedModule->getStyles( $contextLtr ),
250  self::stripNoflip( $testModule->getStyles( $contextLtr ) ),
251  "/*@noflip*/ with /*@embed*/ gives correct results in LTR mode"
252  );
253  $this->assertEquals(
254  $expectedModule->getStyles( $contextLtr ),
255  self::stripNoflip( $testModule->getStyles( $contextRtl ) ),
256  "/*@noflip*/ with /*@embed*/ gives correct results in RTL mode"
257  );
258  }
259 
260  public static function providerGetTemplates() {
262 
263  return [
264  [
265  $modules['noTemplateModule'],
266  [],
267  ],
268  [
269  $modules['templateModuleHandlebars'],
270  [
271  'templates/template_awesome.handlebars' => "wow\n",
272  ],
273  ],
274  [
275  $modules['htmlTemplateModule'],
276  [
277  'templates/template.html' => "<strong>hello</strong>\n",
278  'templates/template2.html' => "<div>goodbye</div>\n",
279  ],
280  ],
281  [
282  $modules['aliasedHtmlTemplateModule'],
283  [
284  'foo.html' => "<strong>hello</strong>\n",
285  'bar.html' => "<div>goodbye</div>\n",
286  ],
287  ],
288  [
289  $modules['htmlTemplateUnknown'],
290  false,
291  ],
292  ];
293  }
294 
299  public function testGetTemplates( $module, $expected ) {
300  $rl = new ResourceLoaderFileModule( $module );
301  $rl->setName( 'testing' );
302 
303  if ( $expected === false ) {
304  $this->setExpectedException( MWException::class );
305  $rl->getTemplates();
306  } else {
307  $this->assertEquals( $rl->getTemplates(), $expected );
308  }
309  }
310 
314  public function testBomConcatenation() {
315  $basePath = __DIR__ . '/../../data/css';
316  $testModule = new ResourceLoaderFileModule( [
317  'localBasePath' => $basePath,
318  'styles' => [ 'bom.css' ],
319  ] );
320  $testModule->setName( 'testing' );
321  $this->assertEquals(
322  substr( file_get_contents( "$basePath/bom.css" ), 0, 10 ),
323  "\xef\xbb\xbf.efbbbf",
324  'File has leading BOM'
325  );
326 
328  $this->assertEquals(
329  $testModule->getStyles( $context ),
330  [ 'all' => ".efbbbf_bom_char_at_start_of_file {}\n" ],
331  'Leading BOM removed when concatenating files'
332  );
333  }
334 
338  public function testGetVersionHash() {
340 
341  // Less variables
342  $module = new ResourceLoaderFileTestModule();
343  $version = $module->getVersionHash( $context );
344  $module = new ResourceLoaderFileTestModule( [], [
345  'lessVars' => [ 'key' => 'value' ],
346  ] );
347  $this->assertNotEquals(
348  $version,
349  $module->getVersionHash( $context ),
350  'Using less variables is significant'
351  );
352  }
353 }
ResourceLoaderFileModuleTest\providerDeprecatedModules
static providerDeprecatedModules()
Definition: ResourceLoaderFileModuleTest.php:113
ResourceLoaderFileModuleTest\testTemplateDependencies
testTemplateDependencies( $module, $expected)
providerTemplateDependencies ResourceLoaderFileModule::__construct ResourceLoaderFileModule::getDepen...
Definition: ResourceLoaderFileModuleTest.php:107
ResourceLoaderFileModuleTest\testGetAllSkinStyleFiles
testGetAllSkinStyleFiles()
ResourceLoaderFileModule::getAllStyleFiles ResourceLoaderFileModule::getAllSkinStyleFiles ResourceLoa...
Definition: ResourceLoaderFileModuleTest.php:166
ResourceLoaderFileTestModule
Definition: ResourceLoaderTestCase.php:152
$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:37
ResourceLoaderFileModule
ResourceLoader module based on local JavaScript/CSS files.
Definition: ResourceLoaderFileModule.php:28
ResourceLoaderFileModuleTest\providerGetTemplates
static providerGetTemplates()
Definition: ResourceLoaderFileModuleTest.php:260
$css
$css
Definition: styleTest.css.php:50
$modules
$modules
Definition: HTMLFormElement.php:12
ResourceLoaderFileModuleTest\testGetVersionHash
testGetVersionHash()
ResourceLoaderFileModule::getDefinitionSummary.
Definition: ResourceLoaderFileModuleTest.php:338
ResourceLoaderFileModuleTest\testGetScript
testGetScript()
ResourceLoaderFileModule::getScript.
Definition: ResourceLoaderFileModuleTest.php:144
ResourceLoaderFileModuleTest\stripNoflip
static stripNoflip( $css)
Strip @noflip annotations from CSS code.
Definition: ResourceLoaderFileModuleTest.php:213
ResourceLoaderFileModuleTest\testMixedCssAnnotations
testMixedCssAnnotations()
What happens when you mix @embed and @noflip? This really is an integration test, but oh well.
Definition: ResourceLoaderFileModuleTest.php:224
ResourceLoaderFileModuleTest\setUp
setUp()
Definition: ResourceLoaderFileModuleTest.php:9
ResourceLoaderFileModuleTest\getModules
static getModules()
Definition: ResourceLoaderFileModuleTest.php:22
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
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
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:56
ResourceLoaderFileModuleTest\testGetTemplates
testGetTemplates( $module, $expected)
providerGetTemplates ResourceLoaderFileModule::getTemplates
Definition: ResourceLoaderFileModuleTest.php:299
ResourceLoaderFileModuleTest\providerTemplateDependencies
static providerTemplateDependencies()
Definition: ResourceLoaderFileModuleTest.php:71
$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:2811
ResourceLoaderFileModuleTest\testBomConcatenation
testBomConcatenation()
ResourceLoaderFileModule::stripBom.
Definition: ResourceLoaderFileModuleTest.php:314
SkinFactory\getDefaultInstance
static getDefaultInstance()
Definition: SkinFactory.php:50
ResourceLoaderTestCase
Definition: ResourceLoaderTestCase.php:7