MediaWiki  1.28.1
ResourceLoaderClientHtmlTest.php
Go to the documentation of this file.
1 <?php
2 
6 class ResourceLoaderClientHtmlTest extends PHPUnit_Framework_TestCase {
7 
8  protected static function expandVariables( $text ) {
9  return strtr( $text, [
11  ] );
12  }
13 
14  protected static function makeContext( $extraQuery = [] ) {
15  $conf = new HashConfig( [
16  'ResourceLoaderSources' => [],
17  'ResourceModuleSkinStyles' => [],
18  'ResourceModules' => [],
19  'EnableJavaScriptTest' => false,
20  'ResourceLoaderDebug' => false,
21  'LoadScript' => '/w/load.php',
22  ] );
23  return new ResourceLoaderContext(
24  new ResourceLoader( $conf ),
25  new FauxRequest( array_merge( [
26  'lang' => 'nl',
27  'skin' => 'fallback',
28  'user' => 'Example',
29  'target' => 'phpunit',
30  ], $extraQuery ) )
31  );
32  }
33 
34  protected static function makeModule( array $options = [] ) {
35  return new ResourceLoaderTestModule( $options );
36  }
37 
38  protected static function makeSampleModules() {
39  $modules = [
40  'test' => [],
41  'test.top' => [ 'position' => 'top' ],
42  'test.private.top' => [ 'group' => 'private', 'position' => 'top' ],
43  'test.private.bottom' => [ 'group' => 'private', 'position' => 'bottom' ],
44 
45  'test.styles.pure' => [ 'type' => ResourceLoaderModule::LOAD_STYLES ],
46  'test.styles.mixed' => [],
47  'test.styles.noscript' => [ 'group' => 'noscript', 'type' => ResourceLoaderModule::LOAD_STYLES ],
48  'test.styles.mixed.user' => [ 'group' => 'user' ],
49  'test.styles.mixed.user.empty' => [ 'group' => 'user', 'isKnownEmpty' => true ],
50  'test.styles.private' => [ 'group' => 'private', 'styles' => '.private{}' ],
51 
52  'test.scripts' => [],
53  'test.scripts.top' => [ 'position' => 'top' ],
54  'test.scripts.mixed.user' => [ 'group' => 'user' ],
55  'test.scripts.mixed.user.empty' => [ 'group' => 'user', 'isKnownEmpty' => true ],
56  'test.scripts.raw' => [ 'isRaw' => true ],
57  ];
58  return array_map( function ( $options ) {
59  return self::makeModule( $options );
60  }, $modules );
61  }
62 
66  public function testGetDocumentAttributes() {
67  $client = new ResourceLoaderClientHtml( self::makeContext() );
68  $this->assertInternalType( 'array', $client->getDocumentAttributes() );
69  }
70 
79  public function testGetData() {
80  $context = self::makeContext();
81  $context->getResourceLoader()->register( self::makeSampleModules() );
82 
83  $client = new ResourceLoaderClientHtml( $context );
84  $client->setModules( [
85  'test',
86  'test.private.bottom',
87  'test.private.top',
88  'test.top',
89  'test.unregistered',
90  ] );
91  $client->setModuleStyles( [
92  'test.styles.mixed',
93  'test.styles.mixed.user.empty',
94  'test.styles.private',
95  'test.styles.pure',
96  'test.unregistered.styles',
97  ] );
98  $client->setModuleScripts( [
99  'test.scripts',
100  'test.scripts.mixed.user.empty',
101  'test.scripts.top',
102  'test.unregistered.scripts',
103  ] );
104 
105  $expected = [
106  'states' => [
107  'test.private.top' => 'loading',
108  'test.private.bottom' => 'loading',
109  'test.styles.pure' => 'ready',
110  'test.styles.mixed.user.empty' => 'ready',
111  'test.styles.private' => 'ready',
112  'test.scripts' => 'loading',
113  'test.scripts.top' => 'loading',
114  'test.scripts.mixed.user.empty' => 'ready',
115  ],
116  'general' => [
117  'top' => [ 'test.top' ],
118  'bottom' => [ 'test' ],
119  ],
120  'styles' => [
121  'test.styles.mixed',
122  'test.styles.pure',
123  ],
124  'scripts' => [
125  'top' => [ 'test.scripts.top' ],
126  'bottom' => [ 'test.scripts' ],
127  ],
128  'embed' => [
129  'styles' => [ 'test.styles.private' ],
130  'general' => [
131  'top' => [ 'test.private.top' ],
132  'bottom' => [ 'test.private.bottom' ],
133  ],
134  ],
135  ];
136 
137  $access = TestingAccessWrapper::newFromObject( $client );
138  $this->assertEquals( $expected, $access->getData() );
139  }
140 
148  public function testGetHeadHtml() {
149  $context = self::makeContext();
150  $context->getResourceLoader()->register( self::makeSampleModules() );
151 
152  $client = new ResourceLoaderClientHtml( $context );
153  $client->setConfig( [ 'key' => 'value' ] );
154  $client->setModules( [
155  'test.top',
156  'test.private.top',
157  ] );
158  $client->setModuleStyles( [
159  'test.styles.pure',
160  'test.styles.private',
161  ] );
162  $client->setModuleScripts( [
163  'test.scripts.top',
164  ] );
165  $client->setExemptStates( [
166  'test.exempt' => 'ready',
167  ] );
168 
169  // @codingStandardsIgnoreStart Generic.Files.LineLength
170  $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
171  . '<script>(window.RLQ=window.RLQ||[]).push(function(){'
172  . 'mw.config.set({"key":"value"});'
173  . 'mw.loader.state({"test.exempt":"ready","test.private.top":"loading","test.styles.pure":"ready","test.styles.private":"ready","test.scripts.top":"loading"});'
174  . 'mw.loader.implement("test.private.top@{blankVer}",function($,jQuery,require,module){},{"css":[]});'
175  . 'mw.loader.load(["test.top"]);'
176  . 'mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts.top\u0026only=scripts\u0026skin=fallback");'
177  . '});</script>' . "\n"
178  . '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.styles.pure&amp;only=styles&amp;skin=fallback"/>' . "\n"
179  . '<style>.private{}</style>' . "\n"
180  . '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=startup&amp;only=scripts&amp;skin=fallback"></script>';
181  // @codingStandardsIgnoreEnd
182  $expected = self::expandVariables( $expected );
183 
184  $this->assertEquals( $expected, $client->getHeadHtml() );
185  }
186 
191  public function testGetBodyHtml() {
192  $context = self::makeContext();
193  $context->getResourceLoader()->register( self::makeSampleModules() );
194 
195  $client = new ResourceLoaderClientHtml( $context );
196  $client->setConfig( [ 'key' => 'value' ] );
197  $client->setModules( [
198  'test',
199  'test.private.bottom',
200  ] );
201  $client->setModuleScripts( [
202  'test.scripts',
203  ] );
204 
205  // @codingStandardsIgnoreStart Generic.Files.LineLength
206  $expected = '<script>(window.RLQ=window.RLQ||[]).push(function(){'
207  . 'mw.loader.implement("test.private.bottom@{blankVer}",function($,jQuery,require,module){},{"css":[]});'
208  . 'mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts\u0026only=scripts\u0026skin=fallback");'
209  . 'mw.loader.load(["test"]);'
210  . '});</script>';
211  // @codingStandardsIgnoreEnd
212  $expected = self::expandVariables( $expected );
213 
214  $this->assertEquals( $expected, $client->getBodyHtml() );
215  }
216 
217  public static function provideMakeLoad() {
218  return [
219  // @codingStandardsIgnoreStart Generic.Files.LineLength
220  [
221  'context' => [],
222  'modules' => [ 'test.unknown' ],
224  'output' => '',
225  ],
226  [
227  'context' => [],
228  'modules' => [ 'test.styles.private' ],
230  'output' => '<style>.private{}</style>',
231  ],
232  [
233  'context' => [],
234  'modules' => [ 'test.private.top' ],
236  'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private.top@{blankVer}",function($,jQuery,require,module){},{"css":[]});});</script>',
237  ],
238  [
239  'context' => [],
240  // Eg. startup module
241  'modules' => [ 'test.scripts.raw' ],
243  'output' => '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.scripts.raw&amp;only=scripts&amp;skin=fallback"></script>',
244  ],
245  [
246  'context' => [],
247  'modules' => [ 'test.scripts.mixed.user' ],
249  '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>',
250  ],
251  [
252  'context' => [ 'debug' => true ],
253  'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
255  'output' => '<link rel="stylesheet" href="/w/load.php?debug=true&amp;lang=nl&amp;modules=test.styles.pure&amp;only=styles&amp;skin=fallback"/>' . "\n"
256  . '<link rel="stylesheet" href="/w/load.php?debug=true&amp;lang=nl&amp;modules=test.styles.mixed&amp;only=styles&amp;skin=fallback"/>',
257  ],
258  [
259  'context' => [],
260  'modules' => [ 'test.styles.noscript' ],
262  '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>',
263  ],
264  // @codingStandardsIgnoreEnd
265  ];
266  }
267 
280  public function testMakeLoad( array $extraQuery, array $modules, $type, $expected ) {
281  $context = self::makeContext( $extraQuery );
282  $context->getResourceLoader()->register( self::makeSampleModules() );
283  $actual = ResourceLoaderClientHtml::makeLoad( $context, $modules, $type );
284  $expected = self::expandVariables( $expected );
285  $this->assertEquals( $expected, (string)$actual );
286  }
287 }
testGetData()
ResourceLoaderClientHtml::__construct ResourceLoaderClientHtml::setModules ResourceLoaderClientHtml::...
testGetDocumentAttributes()
ResourceLoaderClientHtml::getDocumentAttributes.
the array() calling protocol came about after MediaWiki 1.4rc1.
$context
Definition: load.php:50
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:1936
testGetHeadHtml()
ResourceLoaderClientHtml::setConfig ResourceLoaderClientHtml::setExemptStates ResourceLoaderClientHtm...
$modules
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist 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:1046
Bootstrap a ResourceLoader client on an HTML page.
testMakeLoad(array $extraQuery, array $modules, $type, $expected)
provideMakeLoad ResourceLoaderClientHtml::makeLoad ResourceLoaderClientHtml::makeContext ResourceLoad...
testGetBodyHtml()
ResourceLoaderClientHtml::getBodyHtml ResourceLoaderClientHtml::getLoad.
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
static makeLoad(ResourceLoaderContext $mainContext, array $modules, $only, array $extraQuery=[])
Explicily load or embed modules on a page.
static newFromObject($object)
Return the same object, without access restrictions.
A Config instance which stores all settings as a member variable.
Definition: HashConfig.php:28
Dynamic JavaScript and CSS resource loading system.
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 one of or reset 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:2491
Object passed around to modules which contains information about the state of a specific loader reque...