MediaWiki  1.30.0
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  protected static function expandVariables( $text ) {
11  return strtr( $text, [
13  ] );
14  }
15 
16  protected static function makeContext( $extraQuery = [] ) {
17  $conf = new HashConfig( [
18  'ResourceLoaderSources' => [],
19  'ResourceModuleSkinStyles' => [],
20  'ResourceModules' => [],
21  'EnableJavaScriptTest' => false,
22  'ResourceLoaderDebug' => false,
23  'LoadScript' => '/w/load.php',
24  ] );
25  return new ResourceLoaderContext(
26  new ResourceLoader( $conf ),
27  new FauxRequest( array_merge( [
28  'lang' => 'nl',
29  'skin' => 'fallback',
30  'user' => 'Example',
31  'target' => 'phpunit',
32  ], $extraQuery ) )
33  );
34  }
35 
36  protected static function makeModule( array $options = [] ) {
37  return new ResourceLoaderTestModule( $options );
38  }
39 
40  protected static function makeSampleModules() {
41  $modules = [
42  'test' => [],
43  'test.top' => [ 'position' => 'top' ],
44  'test.private.top' => [ 'group' => 'private', 'position' => 'top' ],
45  'test.private.bottom' => [ 'group' => 'private', 'position' => 'bottom' ],
46  'test.shouldembed' => [ 'shouldEmbed' => true ],
47 
48  'test.styles.pure' => [ 'type' => ResourceLoaderModule::LOAD_STYLES ],
49  'test.styles.mixed' => [],
50  'test.styles.noscript' => [
52  'group' => 'noscript',
53  ],
54  'test.styles.user' => [
56  'group' => 'user',
57  ],
58  'test.styles.user.empty' => [
60  'group' => 'user',
61  'isKnownEmpty' => true,
62  ],
63  'test.styles.private' => [
65  'group' => 'private',
66  'styles' => '.private{}',
67  ],
68  'test.styles.shouldembed' => [
70  'shouldEmbed' => true,
71  'styles' => '.shouldembed{}',
72  ],
73 
74  'test.scripts' => [],
75  'test.scripts.top' => [ 'position' => 'top' ],
76  'test.scripts.user' => [ 'group' => 'user' ],
77  'test.scripts.user.empty' => [ 'group' => 'user', 'isKnownEmpty' => true ],
78  'test.scripts.raw' => [ 'isRaw' => true ],
79  'test.scripts.shouldembed' => [ 'shouldEmbed' => true ],
80 
81  'test.ordering.a' => [ 'shouldEmbed' => false ],
82  'test.ordering.b' => [ 'shouldEmbed' => false ],
83  'test.ordering.c' => [ 'shouldEmbed' => true, 'styles' => '.orderingC{}' ],
84  'test.ordering.d' => [ 'shouldEmbed' => true, 'styles' => '.orderingD{}' ],
85  'test.ordering.e' => [ 'shouldEmbed' => false ],
86  ];
87  return array_map( function ( $options ) {
88  return self::makeModule( $options );
89  }, $modules );
90  }
91 
95  public function testGetDocumentAttributes() {
96  $client = new ResourceLoaderClientHtml( self::makeContext() );
97  $this->assertInternalType( 'array', $client->getDocumentAttributes() );
98  }
99 
108  public function testGetData() {
109  $context = self::makeContext();
110  $context->getResourceLoader()->register( self::makeSampleModules() );
111 
112  $client = new ResourceLoaderClientHtml( $context );
113  $client->setModules( [
114  'test',
115  'test.private.bottom',
116  'test.private.top',
117  'test.top',
118  'test.shouldembed',
119  'test.unregistered',
120  ] );
121  $client->setModuleStyles( [
122  'test.styles.mixed',
123  'test.styles.user.empty',
124  'test.styles.private',
125  'test.styles.pure',
126  'test.styles.shouldembed',
127  'test.unregistered.styles',
128  ] );
129  $client->setModuleScripts( [
130  'test.scripts',
131  'test.scripts.user.empty',
132  'test.scripts.top',
133  'test.scripts.shouldembed',
134  'test.unregistered.scripts',
135  ] );
136 
137  $expected = [
138  'states' => [
139  'test.private.top' => 'loading',
140  'test.private.bottom' => 'loading',
141  'test.shouldembed' => 'loading',
142  'test.styles.pure' => 'ready',
143  'test.styles.user.empty' => 'ready',
144  'test.styles.private' => 'ready',
145  'test.styles.shouldembed' => 'ready',
146  'test.scripts' => 'loading',
147  'test.scripts.top' => 'loading',
148  'test.scripts.user.empty' => 'ready',
149  'test.scripts.shouldembed' => 'loading',
150  ],
151  'general' => [
152  'test',
153  'test.top',
154  ],
155  'styles' => [
156  'test.styles.pure',
157  ],
158  'scripts' => [
159  'test.scripts',
160  'test.scripts.top',
161  'test.scripts.shouldembed',
162  ],
163  'embed' => [
164  'styles' => [ 'test.styles.private', 'test.styles.shouldembed' ],
165  'general' => [
166  'test.private.bottom',
167  'test.private.top',
168  'test.shouldembed',
169  ],
170  ],
171  ];
172 
173  $access = TestingAccessWrapper::newFromObject( $client );
174  $this->assertEquals( $expected, $access->getData() );
175  }
176 
184  public function testGetHeadHtml() {
185  $context = self::makeContext();
186  $context->getResourceLoader()->register( self::makeSampleModules() );
187 
188  $client = new ResourceLoaderClientHtml( $context );
189  $client->setConfig( [ 'key' => 'value' ] );
190  $client->setModules( [
191  'test.top',
192  'test.private.top',
193  ] );
194  $client->setModuleStyles( [
195  'test.styles.pure',
196  'test.styles.private',
197  ] );
198  $client->setModuleScripts( [
199  'test.scripts.top',
200  ] );
201  $client->setExemptStates( [
202  'test.exempt' => 'ready',
203  ] );
204 
205  // @codingStandardsIgnoreStart Generic.Files.LineLength
206  $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
207  . '<script>(window.RLQ=window.RLQ||[]).push(function(){'
208  . 'mw.config.set({"key":"value"});'
209  . 'mw.loader.state({"test.exempt":"ready","test.private.top":"loading","test.styles.pure":"ready","test.styles.private":"ready","test.scripts.top":"loading"});'
210  . 'mw.loader.implement("test.private.top@{blankVer}",function($,jQuery,require,module){},{"css":[]});'
211  . 'mw.loader.load(["test.top"]);'
212  . 'mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts.top\u0026only=scripts\u0026skin=fallback");'
213  . '});</script>' . "\n"
214  . '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.styles.pure&amp;only=styles&amp;skin=fallback"/>' . "\n"
215  . '<style>.private{}</style>' . "\n"
216  . '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=startup&amp;only=scripts&amp;skin=fallback"></script>';
217  // @codingStandardsIgnoreEnd
218  $expected = self::expandVariables( $expected );
219 
220  $this->assertEquals( $expected, $client->getHeadHtml() );
221  }
222 
227  public function testGetBodyHtml() {
228  $context = self::makeContext();
229  $context->getResourceLoader()->register( self::makeSampleModules() );
230 
231  $client = new ResourceLoaderClientHtml( $context );
232  $client->setConfig( [ 'key' => 'value' ] );
233  $client->setModules( [
234  'test',
235  'test.private.bottom',
236  ] );
237  $client->setModuleScripts( [
238  'test.scripts',
239  ] );
240 
241  $expected = '';
242  $expected = self::expandVariables( $expected );
243 
244  $this->assertEquals( $expected, $client->getBodyHtml() );
245  }
246 
247  public static function provideMakeLoad() {
248  return [
249  // @codingStandardsIgnoreStart Generic.Files.LineLength
250  [
251  'context' => [],
252  'modules' => [ 'test.unknown' ],
254  'output' => '',
255  ],
256  [
257  'context' => [],
258  'modules' => [ 'test.styles.private' ],
260  'output' => '<style>.private{}</style>',
261  ],
262  [
263  'context' => [],
264  'modules' => [ 'test.private.top' ],
266  'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private.top@{blankVer}",function($,jQuery,require,module){},{"css":[]});});</script>',
267  ],
268  [
269  'context' => [],
270  // Eg. startup module
271  'modules' => [ 'test.scripts.raw' ],
273  'output' => '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.scripts.raw&amp;only=scripts&amp;skin=fallback"></script>',
274  ],
275  [
276  'context' => [],
277  'modules' => [ 'test.scripts.user' ],
279  '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>',
280  ],
281  [
282  'context' => [ 'debug' => true ],
283  'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
285  '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"
286  . '<link rel="stylesheet" href="/w/load.php?debug=true&amp;lang=nl&amp;modules=test.styles.pure&amp;only=styles&amp;skin=fallback"/>',
287  ],
288  [
289  'context' => [ 'debug' => false ],
290  'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
292  '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"/>',
293  ],
294  [
295  'context' => [],
296  'modules' => [ 'test.styles.noscript' ],
298  '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>',
299  ],
300  [
301  'context' => [],
302  'modules' => [ 'test.shouldembed' ],
304  'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.shouldembed@09p30q0",function($,jQuery,require,module){},{"css":[]});});</script>',
305  ],
306  [
307  'context' => [],
308  'modules' => [ 'test.styles.shouldembed' ],
310  'output' => '<style>.shouldembed{}</style>',
311  ],
312  [
313  'context' => [],
314  'modules' => [ 'test.scripts.shouldembed' ],
316  'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.state({"test.scripts.shouldembed":"ready"});});</script>',
317  ],
318  [
319  'context' => [],
320  'modules' => [ 'test', 'test.shouldembed' ],
322  '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>',
323  ],
324  [
325  'context' => [],
326  'modules' => [ 'test.styles.pure', 'test.styles.shouldembed' ],
328  'output' =>
329  '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.styles.pure&amp;only=styles&amp;skin=fallback"/>' . "\n"
330  . '<style>.shouldembed{}</style>'
331  ],
332  [
333  'context' => [],
334  'modules' => [ 'test.ordering.a', 'test.ordering.e', 'test.ordering.b', 'test.ordering.d', 'test.ordering.c' ],
336  'output' =>
337  '<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"
338  . '<style>.orderingC{}.orderingD{}</style>' . "\n"
339  . '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.ordering.e&amp;only=styles&amp;skin=fallback"/>'
340  ],
341  // @codingStandardsIgnoreEnd
342  ];
343  }
344 
357  public function testMakeLoad( array $extraQuery, array $modules, $type, $expected ) {
358  $context = self::makeContext( $extraQuery );
359  $context->getResourceLoader()->register( self::makeSampleModules() );
361  $expected = self::expandVariables( $expected );
362  $this->assertEquals( $expected, (string)$actual );
363  }
364 }
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:28
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:2581
ResourceLoaderModule\TYPE_COMBINED
const TYPE_COMBINED
Definition: ResourceLoaderModule.php:38
HashConfig
A Config instance which stores all settings as a member variable.
Definition: HashConfig.php:28
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
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
$modules
$modules
Definition: HTMLFormElement.php:12
ResourceLoaderModule\TYPE_SCRIPTS
const TYPE_SCRIPTS
Definition: ResourceLoaderModule.php:36
ResourceLoaderTestModule
Definition: ResourceLoaderTestCase.php:85
ResourceLoaderModule\LOAD_STYLES
const LOAD_STYLES
Definition: ResourceLoaderModule.php:42
ResourceLoaderClientHtml\makeLoad
static makeLoad(ResourceLoaderContext $mainContext, array $modules, $only, array $extraQuery=[])
Explicily load or embed modules on a page.
Definition: ResourceLoaderClientHtml.php:361
$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:1965
ResourceLoaderModule\TYPE_STYLES
const TYPE_STYLES
Definition: ResourceLoaderModule.php:37
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:1965
ResourceLoaderTestCase\BLANK_VERSION
const BLANK_VERSION
Definition: ResourceLoaderTestCase.php:11
array
the array() calling protocol came about after MediaWiki 1.4rc1.
$type
$type
Definition: testCompression.php:48