MediaWiki  1.29.2
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 
47  'test.styles.pure' => [ 'type' => ResourceLoaderModule::LOAD_STYLES ],
48  'test.styles.mixed' => [],
49  'test.styles.noscript' => [ 'group' => 'noscript', 'type' => ResourceLoaderModule::LOAD_STYLES ],
50  'test.styles.mixed.user' => [ 'group' => 'user' ],
51  'test.styles.mixed.user.empty' => [ 'group' => 'user', 'isKnownEmpty' => true ],
52  'test.styles.private' => [ 'group' => 'private', 'styles' => '.private{}' ],
53 
54  'test.scripts' => [],
55  'test.scripts.top' => [ 'position' => 'top' ],
56  'test.scripts.mixed.user' => [ 'group' => 'user' ],
57  'test.scripts.mixed.user.empty' => [ 'group' => 'user', 'isKnownEmpty' => true ],
58  'test.scripts.raw' => [ 'isRaw' => true ],
59  ];
60  return array_map( function ( $options ) {
61  return self::makeModule( $options );
62  }, $modules );
63  }
64 
68  public function testGetDocumentAttributes() {
69  $client = new ResourceLoaderClientHtml( self::makeContext() );
70  $this->assertInternalType( 'array', $client->getDocumentAttributes() );
71  }
72 
81  public function testGetData() {
82  $context = self::makeContext();
83  $context->getResourceLoader()->register( self::makeSampleModules() );
84 
85  $client = new ResourceLoaderClientHtml( $context );
86  $client->setModules( [
87  'test',
88  'test.private.bottom',
89  'test.private.top',
90  'test.top',
91  'test.unregistered',
92  ] );
93  $client->setModuleStyles( [
94  'test.styles.mixed',
95  'test.styles.mixed.user.empty',
96  'test.styles.private',
97  'test.styles.pure',
98  'test.unregistered.styles',
99  ] );
100  $client->setModuleScripts( [
101  'test.scripts',
102  'test.scripts.mixed.user.empty',
103  'test.scripts.top',
104  'test.unregistered.scripts',
105  ] );
106 
107  $expected = [
108  'states' => [
109  'test.private.top' => 'loading',
110  'test.private.bottom' => 'loading',
111  'test.styles.pure' => 'ready',
112  'test.styles.mixed.user.empty' => 'ready',
113  'test.styles.private' => 'ready',
114  'test.scripts' => 'loading',
115  'test.scripts.top' => 'loading',
116  'test.scripts.mixed.user.empty' => 'ready',
117  ],
118  'general' => [
119  'test',
120  'test.top',
121  ],
122  'styles' => [
123  'test.styles.mixed',
124  'test.styles.pure',
125  ],
126  'scripts' => [
127  'test.scripts',
128  'test.scripts.top',
129  ],
130  'embed' => [
131  'styles' => [ 'test.styles.private' ],
132  'general' => [
133  'test.private.bottom',
134  'test.private.top',
135  ],
136  ],
137  ];
138 
139  $access = TestingAccessWrapper::newFromObject( $client );
140  $this->assertEquals( $expected, $access->getData() );
141  }
142 
150  public function testGetHeadHtml() {
151  $context = self::makeContext();
152  $context->getResourceLoader()->register( self::makeSampleModules() );
153 
154  $client = new ResourceLoaderClientHtml( $context );
155  $client->setConfig( [ 'key' => 'value' ] );
156  $client->setModules( [
157  'test.top',
158  'test.private.top',
159  ] );
160  $client->setModuleStyles( [
161  'test.styles.pure',
162  'test.styles.private',
163  ] );
164  $client->setModuleScripts( [
165  'test.scripts.top',
166  ] );
167  $client->setExemptStates( [
168  'test.exempt' => 'ready',
169  ] );
170 
171  // @codingStandardsIgnoreStart Generic.Files.LineLength
172  $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
173  . '<script>(window.RLQ=window.RLQ||[]).push(function(){'
174  . 'mw.config.set({"key":"value"});'
175  . 'mw.loader.state({"test.exempt":"ready","test.private.top":"loading","test.styles.pure":"ready","test.styles.private":"ready","test.scripts.top":"loading"});'
176  . 'mw.loader.implement("test.private.top@{blankVer}",function($,jQuery,require,module){},{"css":[]});'
177  . 'mw.loader.load(["test.top"]);'
178  . 'mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts.top\u0026only=scripts\u0026skin=fallback");'
179  . '});</script>' . "\n"
180  . '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.styles.pure&amp;only=styles&amp;skin=fallback"/>' . "\n"
181  . '<style>.private{}</style>' . "\n"
182  . '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=startup&amp;only=scripts&amp;skin=fallback"></script>';
183  // @codingStandardsIgnoreEnd
184  $expected = self::expandVariables( $expected );
185 
186  $this->assertEquals( $expected, $client->getHeadHtml() );
187  }
188 
193  public function testGetBodyHtml() {
194  $context = self::makeContext();
195  $context->getResourceLoader()->register( self::makeSampleModules() );
196 
197  $client = new ResourceLoaderClientHtml( $context );
198  $client->setConfig( [ 'key' => 'value' ] );
199  $client->setModules( [
200  'test',
201  'test.private.bottom',
202  ] );
203  $client->setModuleScripts( [
204  'test.scripts',
205  ] );
206 
207  $expected = '';
208  $expected = self::expandVariables( $expected );
209 
210  $this->assertEquals( $expected, $client->getBodyHtml() );
211  }
212 
213  public static function provideMakeLoad() {
214  return [
215  // @codingStandardsIgnoreStart Generic.Files.LineLength
216  [
217  'context' => [],
218  'modules' => [ 'test.unknown' ],
220  'output' => '',
221  ],
222  [
223  'context' => [],
224  'modules' => [ 'test.styles.private' ],
226  'output' => '<style>.private{}</style>',
227  ],
228  [
229  'context' => [],
230  'modules' => [ 'test.private.top' ],
232  'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private.top@{blankVer}",function($,jQuery,require,module){},{"css":[]});});</script>',
233  ],
234  [
235  'context' => [],
236  // Eg. startup module
237  'modules' => [ 'test.scripts.raw' ],
239  'output' => '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.scripts.raw&amp;only=scripts&amp;skin=fallback"></script>',
240  ],
241  [
242  'context' => [],
243  'modules' => [ 'test.scripts.mixed.user' ],
245  'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts.mixed.user\u0026only=scripts\u0026skin=fallback\u0026user=Example\u0026version=0a56zyi");});</script>',
246  ],
247  [
248  'context' => [ 'debug' => true ],
249  'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
251  '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"
252  . '<link rel="stylesheet" href="/w/load.php?debug=true&amp;lang=nl&amp;modules=test.styles.pure&amp;only=styles&amp;skin=fallback"/>',
253  ],
254  [
255  'context' => [ 'debug' => false ],
256  'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
258  '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"/>',
259  ],
260  [
261  'context' => [],
262  'modules' => [ 'test.styles.noscript' ],
264  '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>',
265  ],
266  // @codingStandardsIgnoreEnd
267  ];
268  }
269 
282  public function testMakeLoad( array $extraQuery, array $modules, $type, $expected ) {
283  $context = self::makeContext( $extraQuery );
284  $context->getResourceLoader()->register( self::makeSampleModules() );
286  $expected = self::expandVariables( $expected );
287  $this->assertEquals( $expected, (string)$actual );
288  }
289 }
ResourceLoaderContext
Object passed around to modules which contains information about the state of a specific loader reque...
Definition: ResourceLoaderContext.php:32
$context
error also a ContextSource you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition: hooks.txt:2612
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:189
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
$type
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition: hooks.txt:2536
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:84
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:362
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:1956
$options
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
Definition: hooks.txt:1049
ResourceLoaderTestCase\BLANK_VERSION
const BLANK_VERSION
Definition: ResourceLoaderTestCase.php:11
array
the array() calling protocol came about after MediaWiki 1.4rc1.