Merge "Add 3D filetype for STL files"
[lhc/web/wiklou.git] / tests / phpunit / includes / resourceloader / ResourceLoaderClientHtmlTest.php
1 <?php
2
3 use Wikimedia\TestingAccessWrapper;
4
5 /**
6 * @group ResourceLoader
7 */
8 class ResourceLoaderClientHtmlTest extends PHPUnit_Framework_TestCase {
9
10 protected static function expandVariables( $text ) {
11 return strtr( $text, [
12 '{blankVer}' => ResourceLoaderTestCase::BLANK_VERSION
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
65 /**
66 * @covers ResourceLoaderClientHtml::getDocumentAttributes
67 */
68 public function testGetDocumentAttributes() {
69 $client = new ResourceLoaderClientHtml( self::makeContext() );
70 $this->assertInternalType( 'array', $client->getDocumentAttributes() );
71 }
72
73 /**
74 * @covers ResourceLoaderClientHtml::__construct
75 * @covers ResourceLoaderClientHtml::setModules
76 * @covers ResourceLoaderClientHtml::setModuleStyles
77 * @covers ResourceLoaderClientHtml::setModuleScripts
78 * @covers ResourceLoaderClientHtml::getData
79 * @covers ResourceLoaderClientHtml::getContext
80 */
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
143 /**
144 * @covers ResourceLoaderClientHtml::setConfig
145 * @covers ResourceLoaderClientHtml::setExemptStates
146 * @covers ResourceLoaderClientHtml::getHeadHtml
147 * @covers ResourceLoaderClientHtml::getLoad
148 * @covers ResourceLoader::makeLoaderStateScript
149 */
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
189 /**
190 * @covers ResourceLoaderClientHtml::getBodyHtml
191 * @covers ResourceLoaderClientHtml::getLoad
192 */
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' ],
219 'only' => ResourceLoaderModule::TYPE_STYLES,
220 'output' => '',
221 ],
222 [
223 'context' => [],
224 'modules' => [ 'test.styles.private' ],
225 'only' => ResourceLoaderModule::TYPE_STYLES,
226 'output' => '<style>.private{}</style>',
227 ],
228 [
229 'context' => [],
230 'modules' => [ 'test.private.top' ],
231 'only' => ResourceLoaderModule::TYPE_COMBINED,
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' ],
238 'only' => ResourceLoaderModule::TYPE_SCRIPTS,
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' ],
244 'only' => ResourceLoaderModule::TYPE_SCRIPTS,
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' ],
250 'only' => ResourceLoaderModule::TYPE_STYLES,
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' ],
257 'only' => ResourceLoaderModule::TYPE_STYLES,
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' ],
263 'only' => ResourceLoaderModule::TYPE_STYLES,
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
270 /**
271 * @dataProvider provideMakeLoad
272 * @covers ResourceLoaderClientHtml::makeLoad
273 * @covers ResourceLoaderClientHtml::makeContext
274 * @covers ResourceLoader::makeModuleResponse
275 * @covers ResourceLoaderModule::getModuleContent
276 * @covers ResourceLoader::getCombinedVersion
277 * @covers ResourceLoader::createLoaderURL
278 * @covers ResourceLoader::createLoaderQuery
279 * @covers ResourceLoader::makeLoaderQuery
280 * @covers ResourceLoader::makeInlineScript
281 */
282 public function testMakeLoad( array $extraQuery, array $modules, $type, $expected ) {
283 $context = self::makeContext( $extraQuery );
284 $context->getResourceLoader()->register( self::makeSampleModules() );
285 $actual = ResourceLoaderClientHtml::makeLoad( $context, $modules, $type );
286 $expected = self::expandVariables( $expected );
287 $this->assertEquals( $expected, (string)$actual );
288 }
289 }