MediaWiki  1.32.5
ResourceLoaderClientHtmlTest.php
Go to the documentation of this file.
1 <?php
2 
3 use Wikimedia\TestingAccessWrapper;
4 
8 class ResourceLoaderClientHtmlTest extends PHPUnit\Framework\TestCase {
9 
10  use MediaWikiCoversValidator;
11 
12  protected static function expandVariables( $text ) {
13  return strtr( $text, [
15  ] );
16  }
17 
18  protected static function makeContext( $extraQuery = [] ) {
19  $conf = new HashConfig( [
20  'ResourceLoaderSources' => [],
21  'ResourceModuleSkinStyles' => [],
22  'ResourceModules' => [],
23  'EnableJavaScriptTest' => false,
24  'ResourceLoaderDebug' => false,
25  'LoadScript' => '/w/load.php',
26  ] );
27  return new ResourceLoaderContext(
28  new ResourceLoader( $conf ),
29  new FauxRequest( array_merge( [
30  'lang' => 'nl',
31  'skin' => 'fallback',
32  'user' => 'Example',
33  'target' => 'phpunit',
34  ], $extraQuery ) )
35  );
36  }
37 
38  protected static function makeModule( array $options = [] ) {
39  return new ResourceLoaderTestModule( $options );
40  }
41 
42  protected static function makeSampleModules() {
43  $modules = [
44  'test' => [],
45  'test.private' => [ 'group' => 'private' ],
46  'test.shouldembed.empty' => [ 'shouldEmbed' => true, 'isKnownEmpty' => true ],
47  'test.shouldembed' => [ 'shouldEmbed' => true ],
48  'test.user' => [ 'group' => 'user' ],
49 
50  'test.styles.pure' => [ 'type' => ResourceLoaderModule::LOAD_STYLES ],
51  'test.styles.mixed' => [],
52  'test.styles.noscript' => [
54  'group' => 'noscript',
55  ],
56  'test.styles.user' => [
58  'group' => 'user',
59  ],
60  'test.styles.user.empty' => [
62  'group' => 'user',
63  'isKnownEmpty' => true,
64  ],
65  'test.styles.private' => [
67  'group' => 'private',
68  'styles' => '.private{}',
69  ],
70  'test.styles.shouldembed' => [
72  'shouldEmbed' => true,
73  'styles' => '.shouldembed{}',
74  ],
75  'test.styles.deprecated' => [
77  'deprecated' => 'Deprecation message.',
78  ],
79 
80  'test.scripts' => [],
81  'test.scripts.user' => [ 'group' => 'user' ],
82  'test.scripts.user.empty' => [ 'group' => 'user', 'isKnownEmpty' => true ],
83  'test.scripts.raw' => [ 'isRaw' => true ],
84  'test.scripts.shouldembed' => [ 'shouldEmbed' => true ],
85 
86  'test.ordering.a' => [ 'shouldEmbed' => false ],
87  'test.ordering.b' => [ 'shouldEmbed' => false ],
88  'test.ordering.c' => [ 'shouldEmbed' => true, 'styles' => '.orderingC{}' ],
89  'test.ordering.d' => [ 'shouldEmbed' => true, 'styles' => '.orderingD{}' ],
90  'test.ordering.e' => [ 'shouldEmbed' => false ],
91  ];
92  return array_map( function ( $options ) {
93  return self::makeModule( $options );
94  }, $modules );
95  }
96 
100  public function testGetDocumentAttributes() {
101  $client = new ResourceLoaderClientHtml( self::makeContext() );
102  $this->assertInternalType( 'array', $client->getDocumentAttributes() );
103  }
104 
113  public function testGetData() {
114  $context = self::makeContext();
115  $context->getResourceLoader()->register( self::makeSampleModules() );
116 
117  $client = new ResourceLoaderClientHtml( $context );
118  $client->setModules( [
119  'test',
120  'test.private',
121  'test.shouldembed.empty',
122  'test.shouldembed',
123  'test.user',
124  'test.unregistered',
125  ] );
126  $client->setModuleStyles( [
127  'test.styles.mixed',
128  'test.styles.user.empty',
129  'test.styles.private',
130  'test.styles.pure',
131  'test.styles.shouldembed',
132  'test.styles.deprecated',
133  'test.unregistered.styles',
134  ] );
135  $client->setModuleScripts( [
136  'test.scripts',
137  'test.scripts.user',
138  'test.scripts.user.empty',
139  'test.scripts.shouldembed',
140  'test.unregistered.scripts',
141  ] );
142 
143  $expected = [
144  'states' => [
145  'test.private' => 'loading',
146  'test.shouldembed.empty' => 'ready',
147  'test.shouldembed' => 'loading',
148  'test.user' => 'loading',
149  'test.styles.pure' => 'ready',
150  'test.styles.user.empty' => 'ready',
151  'test.styles.private' => 'ready',
152  'test.styles.shouldembed' => 'ready',
153  'test.styles.deprecated' => 'ready',
154  'test.scripts' => 'loading',
155  'test.scripts.user' => 'loading',
156  'test.scripts.user.empty' => 'ready',
157  'test.scripts.shouldembed' => 'loading',
158  ],
159  'general' => [
160  'test',
161  ],
162  'styles' => [
163  'test.styles.pure',
164  'test.styles.deprecated',
165  ],
166  'scripts' => [
167  'test.scripts',
168  'test.scripts.user',
169  'test.scripts.shouldembed',
170  ],
171  'embed' => [
172  'styles' => [ 'test.styles.private', 'test.styles.shouldembed' ],
173  'general' => [
174  'test.private',
175  'test.shouldembed',
176  'test.user',
177  ],
178  ],
179  'styleDeprecations' => [
181  'mw.log.warn',
182  [ 'This page is using the deprecated ResourceLoader module "test.styles.deprecated".
183 Deprecation message.' ]
184  )
185  ],
186  ];
187 
188  $access = TestingAccessWrapper::newFromObject( $client );
189  $this->assertEquals( $expected, $access->getData() );
190  }
191 
199  public function testGetHeadHtml() {
200  $context = self::makeContext();
201  $context->getResourceLoader()->register( self::makeSampleModules() );
202 
203  $client = new ResourceLoaderClientHtml( $context, [
204  'nonce' => false,
205  ] );
206  $client->setConfig( [ 'key' => 'value' ] );
207  $client->setModules( [
208  'test',
209  'test.private',
210  ] );
211  $client->setModuleStyles( [
212  'test.styles.pure',
213  'test.styles.private',
214  'test.styles.deprecated',
215  ] );
216  $client->setModuleScripts( [
217  'test.scripts',
218  ] );
219  $client->setExemptStates( [
220  'test.exempt' => 'ready',
221  ] );
222 
223  // phpcs:disable Generic.Files.LineLength
224  $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
225  . '<script>(window.RLQ=window.RLQ||[]).push(function(){'
226  . 'mw.config.set({"key":"value"});'
227  . 'mw.loader.state({"test.exempt":"ready","test.private":"loading","test.styles.pure":"ready","test.styles.private":"ready","test.styles.deprecated":"ready","test.scripts":"loading"});'
228  . 'mw.loader.implement("test.private@{blankVer}",function($,jQuery,require,module){},{"css":[]});'
229  . 'RLPAGEMODULES=["test"];mw.loader.load(RLPAGEMODULES);'
230  . 'mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts\u0026only=scripts\u0026skin=fallback");'
231  . '});</script>' . "\n"
232  . '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.styles.deprecated%2Cpure&amp;only=styles&amp;skin=fallback"/>' . "\n"
233  . '<style>.private{}</style>' . "\n"
234  . '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=startup&amp;only=scripts&amp;skin=fallback"></script>';
235  // phpcs:enable
236  $expected = self::expandVariables( $expected );
237 
238  $this->assertEquals( $expected, $client->getHeadHtml() );
239  }
240 
246  public function testGetHeadHtmlWithTarget() {
247  $client = new ResourceLoaderClientHtml(
248  self::makeContext(),
249  [ 'target' => 'example' ]
250  );
251 
252  // phpcs:disable Generic.Files.LineLength
253  $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
254  . '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=startup&amp;only=scripts&amp;skin=fallback&amp;target=example"></script>';
255  // phpcs:enable
256 
257  $this->assertEquals( $expected, $client->getHeadHtml() );
258  }
259 
265  public function testGetHeadHtmlWithSafemode() {
266  $client = new ResourceLoaderClientHtml(
267  self::makeContext(),
268  [ 'safemode' => '1' ]
269  );
270 
271  // phpcs:disable Generic.Files.LineLength
272  $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
273  . '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=startup&amp;only=scripts&amp;safemode=1&amp;skin=fallback"></script>';
274  // phpcs:enable
275 
276  $this->assertEquals( $expected, $client->getHeadHtml() );
277  }
278 
284  public function testGetHeadHtmlWithNullTarget() {
285  $client = new ResourceLoaderClientHtml(
286  self::makeContext(),
287  [ 'target' => null ]
288  );
289 
290  // phpcs:disable Generic.Files.LineLength
291  $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
292  . '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=startup&amp;only=scripts&amp;skin=fallback"></script>';
293  // phpcs:enable
294 
295  $this->assertEquals( $expected, $client->getHeadHtml() );
296  }
297 
302  public function testGetBodyHtml() {
303  $context = self::makeContext();
304  $context->getResourceLoader()->register( self::makeSampleModules() );
305 
306  $client = new ResourceLoaderClientHtml( $context, [ 'nonce' => false ] );
307  $client->setConfig( [ 'key' => 'value' ] );
308  $client->setModules( [
309  'test',
310  'test.private.bottom',
311  ] );
312  $client->setModuleStyles( [
313  'test.styles.deprecated',
314  ] );
315  $client->setModuleScripts( [
316  'test.scripts',
317  ] );
318  // phpcs:disable Generic.Files.LineLength
319  $expected = '<script>(window.RLQ=window.RLQ||[]).push(function(){'
320  . 'mw.log.warn("This page is using the deprecated ResourceLoader module \"test.styles.deprecated\".\nDeprecation message.");'
321  . '});</script>';
322  // phpcs:enable
323 
324  $this->assertEquals( $expected, $client->getBodyHtml() );
325  }
326 
327  public static function provideMakeLoad() {
328  // phpcs:disable Generic.Files.LineLength
329  return [
330  [
331  'context' => [],
332  'modules' => [ 'test.unknown' ],
334  'output' => '',
335  ],
336  [
337  'context' => [],
338  'modules' => [ 'test.styles.private' ],
340  'output' => '<style>.private{}</style>',
341  ],
342  [
343  'context' => [],
344  'modules' => [ 'test.private' ],
346  'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private@{blankVer}",function($,jQuery,require,module){},{"css":[]});});</script>',
347  ],
348  [
349  'context' => [],
350  // Eg. startup module
351  'modules' => [ 'test.scripts.raw' ],
353  'output' => '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.scripts.raw&amp;only=scripts&amp;skin=fallback"></script>',
354  ],
355  [
356  'context' => [ 'sync' => true ],
357  'modules' => [ 'test.scripts.raw' ],
359  'output' => '<script src="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.scripts.raw&amp;only=scripts&amp;skin=fallback&amp;sync=1"></script>',
360  ],
361  [
362  'context' => [],
363  'modules' => [ 'test.scripts.user' ],
365  'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts.user\u0026only=scripts\u0026skin=fallback\u0026user=Example\u0026version=0a56zyi");});</script>',
366  ],
367  [
368  'context' => [],
369  'modules' => [ 'test.user' ],
371  'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.user\u0026skin=fallback\u0026user=Example\u0026version=0a56zyi");});</script>',
372  ],
373  [
374  'context' => [ 'debug' => true ],
375  'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
377  'output' => '<link rel="stylesheet" href="/w/load.php?debug=true&amp;lang=nl&amp;modules=test.styles.mixed&amp;only=styles&amp;skin=fallback"/>' . "\n"
378  . '<link rel="stylesheet" href="/w/load.php?debug=true&amp;lang=nl&amp;modules=test.styles.pure&amp;only=styles&amp;skin=fallback"/>',
379  ],
380  [
381  'context' => [ 'debug' => false ],
382  'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
384  'output' => '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.styles.mixed%2Cpure&amp;only=styles&amp;skin=fallback"/>',
385  ],
386  [
387  'context' => [],
388  'modules' => [ 'test.styles.noscript' ],
390  'output' => '<noscript><link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.styles.noscript&amp;only=styles&amp;skin=fallback"/></noscript>',
391  ],
392  [
393  'context' => [],
394  'modules' => [ 'test.shouldembed' ],
396  'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.shouldembed@09p30q0",function($,jQuery,require,module){},{"css":[]});});</script>',
397  ],
398  [
399  'context' => [],
400  'modules' => [ 'test.styles.shouldembed' ],
402  'output' => '<style>.shouldembed{}</style>',
403  ],
404  [
405  'context' => [],
406  'modules' => [ 'test.scripts.shouldembed' ],
408  'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.state({"test.scripts.shouldembed":"ready"});});</script>',
409  ],
410  [
411  'context' => [],
412  'modules' => [ 'test', 'test.shouldembed' ],
414  'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test\u0026skin=fallback");mw.loader.implement("test.shouldembed@09p30q0",function($,jQuery,require,module){},{"css":[]});});</script>',
415  ],
416  [
417  'context' => [],
418  'modules' => [ 'test.styles.pure', 'test.styles.shouldembed' ],
420  'output' =>
421  '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.styles.pure&amp;only=styles&amp;skin=fallback"/>' . "\n"
422  . '<style>.shouldembed{}</style>'
423  ],
424  [
425  'context' => [],
426  'modules' => [ 'test.ordering.a', 'test.ordering.e', 'test.ordering.b', 'test.ordering.d', 'test.ordering.c' ],
428  'output' =>
429  '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.ordering.a%2Cb&amp;only=styles&amp;skin=fallback"/>' . "\n"
430  . '<style>.orderingC{}.orderingD{}</style>' . "\n"
431  . '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.ordering.e&amp;only=styles&amp;skin=fallback"/>'
432  ],
433  ];
434  // phpcs:enable
435  }
436 
449  public function testMakeLoad( array $extraQuery, array $modules, $type, $expected ) {
450  $context = self::makeContext( $extraQuery );
451  $context->getResourceLoader()->register( self::makeSampleModules() );
452  $actual = ResourceLoaderClientHtml::makeLoad( $context, $modules, $type, $extraQuery, false );
453  $expected = self::expandVariables( $expected );
454  $this->assertEquals( $expected, (string)$actual );
455  }
456 }
ResourceLoaderContext
Object passed around to modules which contains information about the state of a specific loader reque...
Definition: ResourceLoaderContext.php:32
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
ResourceLoaderClientHtml
Bootstrap a ResourceLoader client on an HTML page.
Definition: ResourceLoaderClientHtml.php:29
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
$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:2683
ResourceLoaderModule\TYPE_COMBINED
const TYPE_COMBINED
Definition: ResourceLoaderModule.php:39
HashConfig
A Config instance which stores all settings as a member variable.
Definition: HashConfig.php:28
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
Xml\encodeJsCall
static encodeJsCall( $name, $args, $pretty=false)
Create a call to a JavaScript function.
Definition: Xml.php:679
$modules
$modules
Definition: HTMLFormElement.php:12
ResourceLoaderModule\TYPE_SCRIPTS
const TYPE_SCRIPTS
Definition: ResourceLoaderModule.php:37
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
ResourceLoaderTestModule
Definition: ResourceLoaderTestCase.php:86
ResourceLoaderModule\LOAD_STYLES
const LOAD_STYLES
Definition: ResourceLoaderModule.php:43
ResourceLoaderClientHtml\makeLoad
static makeLoad(ResourceLoaderContext $mainContext, array $modules, $only, array $extraQuery=[], $nonce=null)
Explicily load or embed modules on a page.
Definition: ResourceLoaderClientHtml.php:415
$options
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 & $options
Definition: hooks.txt:2044
ResourceLoaderModule\TYPE_STYLES
const TYPE_STYLES
Definition: ResourceLoaderModule.php:38
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:2044
ResourceLoaderTestCase\BLANK_VERSION
const BLANK_VERSION
Definition: ResourceLoaderTestCase.php:11
$type
$type
Definition: testCompression.php:48