0965b9f9147a644e69a31e92bf2e9b22d7da4902
[lhc/web/wiklou.git] / tests / phpunit / includes / resourceloader / ResourceLoaderClientHtmlTest.php
1 <?php
2
3 /**
4 * @group ResourceLoader
5 */
6 class ResourceLoaderClientHtmlTest extends PHPUnit_Framework_TestCase {
7
8 protected static function makeContext( $extraQuery = [] ) {
9 $conf = new HashConfig( [
10 'ResourceLoaderSources' => [],
11 'ResourceModuleSkinStyles' => [],
12 'ResourceModules' => [],
13 'EnableJavaScriptTest' => false,
14 'ResourceLoaderDebug' => false,
15 'LoadScript' => '/w/load.php',
16 ] );
17 return new ResourceLoaderContext(
18 new ResourceLoader( $conf ),
19 new FauxRequest( array_merge( [
20 'lang' => 'nl',
21 'skin' => 'fallback',
22 'user' => 'Example',
23 'target' => 'phpunit',
24 ], $extraQuery ) )
25 );
26 }
27
28 protected static function makeModule( array $options = [] ) {
29 return new ResourceLoaderTestModule( $options );
30 }
31
32 protected static function makeSampleModules() {
33 $modules = [
34 'test' => [],
35 'test.top' => [ 'position' => 'top' ],
36 'test.private.top' => [ 'group' => 'private', 'position' => 'top' ],
37 'test.private.bottom' => [ 'group' => 'private', 'position' => 'bottom' ],
38
39 'test.styles.pure' => [ 'type' => ResourceLoaderModule::LOAD_STYLES ],
40 'test.styles.mixed' => [],
41 'test.styles.noscript' => [ 'group' => 'noscript', 'type' => ResourceLoaderModule::LOAD_STYLES ],
42 'test.styles.mixed.user' => [ 'group' => 'user' ],
43 'test.styles.mixed.user.empty' => [ 'group' => 'user', 'isKnownEmpty' => true ],
44 'test.styles.private' => [ 'group' => 'private', 'styles' => '.private{}' ],
45
46 'test.scripts' => [],
47 'test.scripts.top' => [ 'position' => 'top' ],
48 'test.scripts.mixed.user' => [ 'group' => 'user' ],
49 'test.scripts.mixed.user.empty' => [ 'group' => 'user', 'isKnownEmpty' => true ],
50 'test.scripts.raw' => [ 'isRaw' => true ],
51 ];
52 return array_map( function ( $options ) {
53 return self::makeModule( $options );
54 }, $modules );
55 }
56
57 /**
58 * @covers ResourceLoaderClientHtml::getDocumentAttributes
59 */
60 public function testGetDocumentAttributes() {
61 $client = new ResourceLoaderClientHtml( self::makeContext() );
62 $this->assertInternalType( 'array', $client->getDocumentAttributes() );
63 }
64
65 /**
66 * @covers ResourceLoaderClientHtml::__construct
67 * @covers ResourceLoaderClientHtml::setModules
68 * @covers ResourceLoaderClientHtml::setModuleStyles
69 * @covers ResourceLoaderClientHtml::setModuleScripts
70 * @covers ResourceLoaderClientHtml::getData
71 * @covers ResourceLoaderClientHtml::getContext
72 */
73 public function testGetData() {
74 $context = self::makeContext();
75 $context->getResourceLoader()->register( self::makeSampleModules() );
76
77 $client = new ResourceLoaderClientHtml( $context );
78 $client->setModules( [
79 'test',
80 'test.private.bottom',
81 'test.private.top',
82 'test.top',
83 'test.unregistered',
84 ] );
85 $client->setModuleStyles( [
86 'test.styles.mixed',
87 'test.styles.mixed.user.empty',
88 'test.styles.private',
89 'test.styles.pure',
90 'test.unregistered.styles',
91 ] );
92 $client->setModuleScripts( [
93 'test.scripts',
94 'test.scripts.mixed.user.empty',
95 'test.scripts.top',
96 'test.unregistered.scripts',
97 ] );
98
99 $expected = [
100 'states' => [
101 'test.private.top' => 'loading',
102 'test.private.bottom' => 'loading',
103 'test.styles.pure' => 'ready',
104 'test.styles.mixed.user.empty' => 'ready',
105 'test.styles.private' => 'ready',
106 'test.scripts' => 'loading',
107 'test.scripts.top' => 'loading',
108 'test.scripts.mixed.user.empty' => 'ready',
109 ],
110 'general' => [
111 'top' => [ 'test.top' ],
112 'bottom' => [ 'test' ],
113 ],
114 'styles' => [
115 'test.styles.mixed',
116 'test.styles.pure',
117 ],
118 'scripts' => [
119 'top' => [ 'test.scripts.top' ],
120 'bottom' => [ 'test.scripts' ],
121 ],
122 'embed' => [
123 'styles' => [ 'test.styles.private' ],
124 'general' => [
125 'top' => [ 'test.private.top' ],
126 'bottom' => [ 'test.private.bottom' ],
127 ],
128 ],
129 ];
130
131 $access = TestingAccessWrapper::newFromObject( $client );
132 $this->assertEquals( $expected, $access->getData() );
133 }
134
135 /**
136 * @covers ResourceLoaderClientHtml::setConfig
137 * @covers ResourceLoaderClientHtml::setExemptStates
138 * @covers ResourceLoaderClientHtml::getHeadHtml
139 * @covers ResourceLoaderClientHtml::getLoad
140 * @covers ResourceLoader::makeLoaderStateScript
141 */
142 public function testGetHeadHtml() {
143 $context = self::makeContext();
144 $context->getResourceLoader()->register( self::makeSampleModules() );
145
146 $client = new ResourceLoaderClientHtml( $context );
147 $client->setConfig( [ 'key' => 'value' ] );
148 $client->setModules( [
149 'test.top',
150 'test.private.top',
151 ] );
152 $client->setModuleStyles( [
153 'test.styles.pure',
154 'test.styles.private',
155 ] );
156 $client->setModuleScripts( [
157 'test.scripts.top',
158 ] );
159 $client->setExemptStates( [
160 'test.exempt' => 'ready',
161 ] );
162
163 // @codingStandardsIgnoreStart Generic.Files.LineLength
164 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
165 . '<script>(window.RLQ=window.RLQ||[]).push(function(){'
166 . 'mw.config.set({"key":"value"});'
167 . 'mw.loader.state({"test.exempt":"ready","test.private.top":"loading","test.styles.pure":"ready","test.styles.private":"ready","test.scripts.top":"loading"});'
168 . 'mw.loader.implement("test.private.top",function($,jQuery,require,module){},{"css":[]});'
169 . 'mw.loader.load(["test.top"]);'
170 . 'mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts.top\u0026only=scripts\u0026skin=fallback");'
171 . '});</script>' . "\n"
172 . '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.styles.pure&amp;only=styles&amp;skin=fallback"/>' . "\n"
173 . '<style>.private{}</style>' . "\n"
174 . '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=startup&amp;only=scripts&amp;skin=fallback"></script>';
175 // @codingStandardsIgnoreEnd
176
177 $this->assertEquals( $expected, $client->getHeadHtml() );
178 }
179
180 /**
181 * @covers ResourceLoaderClientHtml::getBodyHtml
182 * @covers ResourceLoaderClientHtml::getLoad
183 */
184 public function testGetBodyHtml() {
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',
192 'test.private.bottom',
193 ] );
194 $client->setModuleScripts( [
195 'test.scripts',
196 ] );
197
198 // @codingStandardsIgnoreStart Generic.Files.LineLength
199 $expected = '<script>(window.RLQ=window.RLQ||[]).push(function(){'
200 . 'mw.loader.implement("test.private.bottom",function($,jQuery,require,module){},{"css":[]});'
201 . 'mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts\u0026only=scripts\u0026skin=fallback");'
202 . 'mw.loader.load(["test"]);'
203 . '});</script>';
204 // @codingStandardsIgnoreEnd
205
206 $this->assertEquals( $expected, $client->getBodyHtml() );
207 }
208
209 public static function provideMakeLoad() {
210 return [
211 // @codingStandardsIgnoreStart Generic.Files.LineLength
212 [
213 'context' => [],
214 'modules' => [ 'test.unknown' ],
215 'only' => ResourceLoaderModule::TYPE_STYLES,
216 'output' => '',
217 ],
218 [
219 'context' => [],
220 'modules' => [ 'test.styles.private' ],
221 'only' => ResourceLoaderModule::TYPE_STYLES,
222 'output' => '<style>.private{}</style>',
223 ],
224 [
225 'context' => [],
226 'modules' => [ 'test.private.top' ],
227 'only' => ResourceLoaderModule::TYPE_COMBINED,
228 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private.top",function($,jQuery,require,module){},{"css":[]});});</script>',
229 ],
230 [
231 'context' => [],
232 // Eg. startup module
233 'modules' => [ 'test.scripts.raw' ],
234 'only' => ResourceLoaderModule::TYPE_SCRIPTS,
235 'output' => '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.scripts.raw&amp;only=scripts&amp;skin=fallback"></script>',
236 ],
237 [
238 'context' => [],
239 'modules' => [ 'test.scripts.mixed.user' ],
240 'only' => ResourceLoaderModule::TYPE_SCRIPTS,
241 '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>',
242 ],
243 [
244 'context' => [ 'debug' => true ],
245 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
246 'only' => ResourceLoaderModule::TYPE_STYLES,
247 '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"
248 . '<link rel="stylesheet" href="/w/load.php?debug=true&amp;lang=nl&amp;modules=test.styles.mixed&amp;only=styles&amp;skin=fallback"/>',
249 ],
250 [
251 'context' => [],
252 'modules' => [ 'test.styles.noscript' ],
253 'only' => ResourceLoaderModule::TYPE_STYLES,
254 '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>',
255 ],
256 // @codingStandardsIgnoreEnd
257 ];
258 }
259
260 /**
261 * @dataProvider provideMakeLoad
262 * @covers ResourceLoaderClientHtml::makeLoad
263 * @covers ResourceLoaderClientHtml::makeContext
264 * @covers ResourceLoader::makeModuleResponse
265 * @covers ResourceLoaderModule::getModuleContent
266 * @covers ResourceLoader::getCombinedVersion
267 * @covers ResourceLoader::createLoaderURL
268 * @covers ResourceLoader::createLoaderQuery
269 * @covers ResourceLoader::makeLoaderQuery
270 * @covers ResourceLoader::makeInlineScript
271 */
272 public function testMakeLoad( array $extraQuery, array $modules, $type, $expected ) {
273 $context = self::makeContext( $extraQuery );
274 $context->getResourceLoader()->register( self::makeSampleModules() );
275 $actual = ResourceLoaderClientHtml::makeLoad( $context, $modules, $type );
276 $this->assertEquals( $expected, (string)$actual );
277 }
278 }