528c3220e85c0e4c94a87c3998f107390dadf915
[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 expandVariables( $text ) {
9 return strtr( $text, [
10 '{blankVer}' => ResourceLoaderTestCase::BLANK_VERSION
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
63 /**
64 * @covers ResourceLoaderClientHtml::getDocumentAttributes
65 */
66 public function testGetDocumentAttributes() {
67 $client = new ResourceLoaderClientHtml( self::makeContext() );
68 $this->assertInternalType( 'array', $client->getDocumentAttributes() );
69 }
70
71 /**
72 * @covers ResourceLoaderClientHtml::__construct
73 * @covers ResourceLoaderClientHtml::setModules
74 * @covers ResourceLoaderClientHtml::setModuleStyles
75 * @covers ResourceLoaderClientHtml::setModuleScripts
76 * @covers ResourceLoaderClientHtml::getData
77 * @covers ResourceLoaderClientHtml::getContext
78 */
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 'test',
118 'test.top',
119 ],
120 'styles' => [
121 'test.styles.mixed',
122 'test.styles.pure',
123 ],
124 'scripts' => [
125 'test.scripts',
126 'test.scripts.top',
127 ],
128 'embed' => [
129 'styles' => [ 'test.styles.private' ],
130 'general' => [
131 'test.private.bottom',
132 'test.private.top',
133 ],
134 ],
135 ];
136
137 $access = TestingAccessWrapper::newFromObject( $client );
138 $this->assertEquals( $expected, $access->getData() );
139 }
140
141 /**
142 * @covers ResourceLoaderClientHtml::setConfig
143 * @covers ResourceLoaderClientHtml::setExemptStates
144 * @covers ResourceLoaderClientHtml::getHeadHtml
145 * @covers ResourceLoaderClientHtml::getLoad
146 * @covers ResourceLoader::makeLoaderStateScript
147 */
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
187 /**
188 * @covers ResourceLoaderClientHtml::getBodyHtml
189 * @covers ResourceLoaderClientHtml::getLoad
190 */
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 $expected = '';
206 $expected = self::expandVariables( $expected );
207
208 $this->assertEquals( $expected, $client->getBodyHtml() );
209 }
210
211 public static function provideMakeLoad() {
212 return [
213 // @codingStandardsIgnoreStart Generic.Files.LineLength
214 [
215 'context' => [],
216 'modules' => [ 'test.unknown' ],
217 'only' => ResourceLoaderModule::TYPE_STYLES,
218 'output' => '',
219 ],
220 [
221 'context' => [],
222 'modules' => [ 'test.styles.private' ],
223 'only' => ResourceLoaderModule::TYPE_STYLES,
224 'output' => '<style>.private{}</style>',
225 ],
226 [
227 'context' => [],
228 'modules' => [ 'test.private.top' ],
229 'only' => ResourceLoaderModule::TYPE_COMBINED,
230 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private.top@{blankVer}",function($,jQuery,require,module){},{"css":[]});});</script>',
231 ],
232 [
233 'context' => [],
234 // Eg. startup module
235 'modules' => [ 'test.scripts.raw' ],
236 'only' => ResourceLoaderModule::TYPE_SCRIPTS,
237 'output' => '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.scripts.raw&amp;only=scripts&amp;skin=fallback"></script>',
238 ],
239 [
240 'context' => [],
241 'modules' => [ 'test.scripts.mixed.user' ],
242 'only' => ResourceLoaderModule::TYPE_SCRIPTS,
243 '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>',
244 ],
245 [
246 'context' => [ 'debug' => true ],
247 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
248 'only' => ResourceLoaderModule::TYPE_STYLES,
249 '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"
250 . '<link rel="stylesheet" href="/w/load.php?debug=true&amp;lang=nl&amp;modules=test.styles.mixed&amp;only=styles&amp;skin=fallback"/>',
251 ],
252 [
253 'context' => [],
254 'modules' => [ 'test.styles.noscript' ],
255 'only' => ResourceLoaderModule::TYPE_STYLES,
256 '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>',
257 ],
258 // @codingStandardsIgnoreEnd
259 ];
260 }
261
262 /**
263 * @dataProvider provideMakeLoad
264 * @covers ResourceLoaderClientHtml::makeLoad
265 * @covers ResourceLoaderClientHtml::makeContext
266 * @covers ResourceLoader::makeModuleResponse
267 * @covers ResourceLoaderModule::getModuleContent
268 * @covers ResourceLoader::getCombinedVersion
269 * @covers ResourceLoader::createLoaderURL
270 * @covers ResourceLoader::createLoaderQuery
271 * @covers ResourceLoader::makeLoaderQuery
272 * @covers ResourceLoader::makeInlineScript
273 */
274 public function testMakeLoad( array $extraQuery, array $modules, $type, $expected ) {
275 $context = self::makeContext( $extraQuery );
276 $context->getResourceLoader()->register( self::makeSampleModules() );
277 $actual = ResourceLoaderClientHtml::makeLoad( $context, $modules, $type );
278 $expected = self::expandVariables( $expected );
279 $this->assertEquals( $expected, (string)$actual );
280 }
281 }