Merge "Add support for 'hu-formal'"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / CSSMinTest.php
1 <?php
2 /**
3 * This file test the CSSMin library shipped with Mediawiki.
4 *
5 * @author Timo Tijhof
6 */
7
8 /**
9 * @group ResourceLoader
10 * @group CSSMin
11 */
12 class CSSMinTest extends MediaWikiTestCase {
13
14 protected function setUp() {
15 parent::setUp();
16
17 $server = 'http://doc.example.org';
18
19 $this->setMwGlobals( [
20 'wgServer' => $server,
21 'wgCanonicalServer' => $server,
22 ] );
23 }
24
25 /**
26 * @dataProvider mimeTypeProvider
27 * @covers CSSMin::getMimeType
28 */
29 public function testGetMimeType( $fileContents, $fileExtension, $expected ) {
30 $fileName = wfTempDir() . DIRECTORY_SEPARATOR . uniqid( 'MW_PHPUnit_CSSMinTest_' ) . '.'
31 . $fileExtension;
32 $this->addTmpFiles( $fileName );
33 file_put_contents( $fileName, $fileContents );
34 $this->assertSame( $expected, CSSMin::getMimeType( $fileName ) );
35 }
36
37 public function mimeTypeProvider() {
38 return [
39 'JPEG with short extension' => [
40 "\xFF\xD8\xFF",
41 'jpg',
42 'image/jpeg'
43 ],
44 'JPEG with long extension' => [
45 "\xFF\xD8\xFF",
46 'jpeg',
47 'image/jpeg'
48 ],
49 'PNG' => [
50 "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A",
51 'png',
52 'image/png'
53 ],
54
55 'PNG extension but JPEG content' => [
56 "\xFF\xD8\xFF",
57 'png',
58 'image/png'
59 ],
60 'JPEG extension but PNG content' => [
61 "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A",
62 'jpg',
63 'image/jpeg'
64 ],
65 'PNG extension but SVG content' => [
66 '<?xml version="1.0"?><svg></svg>',
67 'png',
68 'image/png'
69 ],
70 'SVG extension but PNG content' => [
71 "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A",
72 'svg',
73 'image/svg+xml'
74 ],
75
76 'SVG with all headers' => [
77 '<?xml version="1.0"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" '
78 . '"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg></svg>',
79 'svg',
80 'image/svg+xml'
81 ],
82 'SVG with XML header only' => [
83 '<?xml version="1.0"?><svg></svg>',
84 'svg',
85 'image/svg+xml'
86 ],
87 'SVG with DOCTYPE only' => [
88 '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" '
89 . '"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg></svg>',
90 'svg',
91 'image/svg+xml'
92 ],
93 'SVG without any header' => [
94 '<svg></svg>',
95 'svg',
96 'image/svg+xml'
97 ],
98 ];
99 }
100
101 /**
102 * @dataProvider provideMinifyCases
103 * @covers CSSMin::minify
104 */
105 public function testMinify( $code, $expectedOutput ) {
106 $minified = CSSMin::minify( $code );
107
108 $this->assertEquals(
109 $expectedOutput,
110 $minified,
111 'Minified output should be in the form expected.'
112 );
113 }
114
115 public static function provideMinifyCases() {
116 return [
117 // Whitespace
118 [ "\r\t\f \v\n\r", "" ],
119 [ "foo, bar {\n\tprop: value;\n}", "foo,bar{prop:value}" ],
120
121 // Loose comments
122 [ "/* foo */", "" ],
123 [ "/*******\n foo\n *******/", "" ],
124 [ "/*!\n foo\n */", "" ],
125
126 // Inline comments in various different places
127 [ "/* comment */foo, bar {\n\tprop: value;\n}", "foo,bar{prop:value}" ],
128 [ "foo/* comment */, bar {\n\tprop: value;\n}", "foo,bar{prop:value}" ],
129 [ "foo,/* comment */ bar {\n\tprop: value;\n}", "foo,bar{prop:value}" ],
130 [ "foo, bar/* comment */ {\n\tprop: value;\n}", "foo,bar{prop:value}" ],
131 [ "foo, bar {\n\t/* comment */prop: value;\n}", "foo,bar{prop:value}" ],
132 [ "foo, bar {\n\tprop: /* comment */value;\n}", "foo,bar{prop:value}" ],
133 [ "foo, bar {\n\tprop: value /* comment */;\n}", "foo,bar{prop:value }" ],
134 [ "foo, bar {\n\tprop: value; /* comment */\n}", "foo,bar{prop:value; }" ],
135
136 // Keep track of things that aren't as minified as much as they
137 // could be (T37493)
138 [ 'foo { prop: value ;}', 'foo{prop:value }' ],
139 [ 'foo { prop : value; }', 'foo{prop :value}' ],
140 [ 'foo { prop: value ; }', 'foo{prop:value }' ],
141 [ 'foo { font-family: "foo" , "bar"; }', 'foo{font-family:"foo" ,"bar"}' ],
142 [ "foo { src:\n\turl('foo') ,\n\turl('bar') ; }", "foo{src:url('foo') ,url('bar') }" ],
143
144 // Interesting cases with string values
145 // - Double quotes, single quotes
146 [ 'foo { content: ""; }', 'foo{content:""}' ],
147 [ "foo { content: ''; }", "foo{content:''}" ],
148 [ 'foo { content: "\'"; }', 'foo{content:"\'"}' ],
149 [ "foo { content: '\"'; }", "foo{content:'\"'}" ],
150 // - Whitespace in string values
151 [ 'foo { content: " "; }', 'foo{content:" "}' ],
152
153 // Whitespaces after opening and before closing parentheses and brackets
154 [ 'a:not( [ href ] ) { prop: url( foobar.png ); }', 'a:not([href]){prop:url(foobar.png)}' ],
155
156 // Ensure that the invalid "url (" will not become the valid "url(" by minification
157 [ 'foo { prop: url ( foobar.png ); }', 'foo{prop:url (foobar.png)}' ],
158 ];
159 }
160
161 public static function provideIsRemoteUrl() {
162 return [
163 [ true, 'http://localhost/w/red.gif?123' ],
164 [ true, 'https://example.org/x.png' ],
165 [ true, '//example.org/x.y.z/image.png' ],
166 [ true, '//localhost/styles.css?query=yes' ],
167 [ true, 'data:image/gif;base64,R0lGODlhAQABAIAAAP8AADAAACwAAAAAAQABAAACAkQBADs=' ],
168 [ false, 'x.gif' ],
169 [ false, '/x.gif' ],
170 [ false, './x.gif' ],
171 [ false, '../x.gif' ],
172 ];
173 }
174
175 /**
176 * @dataProvider provideIsRemoteUrl
177 * @covers CSSMin::isRemoteUrl
178 */
179 public function testIsRemoteUrl( $expect, $url ) {
180 $this->assertEquals( CSSMinTestable::isRemoteUrl( $url ), $expect );
181 }
182
183 public static function provideIsLocalUrls() {
184 return [
185 [ false, 'x.gif' ],
186 [ true, '/x.gif' ],
187 [ false, './x.gif' ],
188 [ false, '../x.gif' ],
189 ];
190 }
191
192 /**
193 * @dataProvider provideIsLocalUrls
194 * @covers CSSMin::isLocalUrl
195 */
196 public function testIsLocalUrl( $expect, $url ) {
197 $this->assertEquals( CSSMinTestable::isLocalUrl( $url ), $expect );
198 }
199
200 /**
201 * This test tests funky parameters to CSSMin::remap.
202 *
203 * @see testRemapRemapping for testing of the basic functionality
204 * @dataProvider provideRemapCases
205 * @covers CSSMin::remap
206 * @covers CSSMin::remapOne
207 */
208 public function testRemap( $message, $params, $expectedOutput ) {
209 $remapped = call_user_func_array( 'CSSMin::remap', $params );
210
211 $messageAdd = " Case: $message";
212 $this->assertEquals(
213 $expectedOutput,
214 $remapped,
215 'CSSMin::remap should return the expected url form.' . $messageAdd
216 );
217 }
218
219 public static function provideRemapCases() {
220 // Parameter signature:
221 // CSSMin::remap( $code, $local, $remote, $embedData = true )
222 return [
223 [
224 'Simple case',
225 [ 'foo { prop: url(bar.png); }', false, 'http://example.org', false ],
226 'foo { prop: url(http://example.org/bar.png); }',
227 ],
228 [
229 'Without trailing slash',
230 [ 'foo { prop: url(../bar.png); }', false, 'http://example.org/quux', false ],
231 'foo { prop: url(http://example.org/bar.png); }',
232 ],
233 [
234 'With trailing slash on remote (T29052)',
235 [ 'foo { prop: url(../bar.png); }', false, 'http://example.org/quux/', false ],
236 'foo { prop: url(http://example.org/bar.png); }',
237 ],
238 [
239 'Guard against stripping double slashes from query',
240 [ 'foo { prop: url(bar.png?corge=//grault); }', false, 'http://example.org/quux/', false ],
241 'foo { prop: url(http://example.org/quux/bar.png?corge=//grault); }',
242 ],
243 [
244 'Expand absolute paths',
245 [ 'foo { prop: url(/w/skin/images/bar.png); }', false, 'http://example.org/quux', false ],
246 'foo { prop: url(http://doc.example.org/w/skin/images/bar.png); }',
247 ],
248 [
249 "Don't barf at behavior: url(#default#behaviorName) - T162973",
250 [ 'foo { behavior: url(#default#bar); }', false, '/w/', false ],
251 'foo { behavior: url("#default#bar"); }',
252 ],
253 ];
254 }
255
256 /**
257 * This tests the basic functionality of CSSMin::remap.
258 *
259 * @see testRemap for testing of funky parameters
260 * @dataProvider provideRemapRemappingCases
261 * @covers CSSMin
262 */
263 public function testRemapRemapping( $message, $input, $expectedOutput ) {
264 $localPath = __DIR__ . '/../../data/cssmin';
265 $remotePath = 'http://localhost/w';
266
267 $realOutput = CSSMin::remap( $input, $localPath, $remotePath );
268 $this->assertEquals( $expectedOutput, $realOutput, "CSSMin::remap: $message" );
269 }
270
271 public static function provideRemapRemappingCases() {
272 // red.gif and green.gif are one-pixel 35-byte GIFs.
273 // large.png is a 35K PNG that should be non-embeddable.
274 // Full paths start with http://localhost/w/.
275 // Timestamps in output are replaced with 'timestamp'.
276
277 // data: URIs for red.gif, green.gif, circle.svg
278 $red = 'data:image/gif;base64,R0lGODlhAQABAIAAAP8AADAAACwAAAAAAQABAAACAkQBADs=';
279 $green = 'data:image/gif;base64,R0lGODlhAQABAIAAAACAADAAACwAAAAAAQABAAACAkQBADs=';
280 $svg = 'data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 width=%228'
281 . '%22 height=%228%22 viewBox=%220 0 8 8%22%3E %3Ccircle cx=%224%22 cy=%224%22 '
282 . 'r=%222%22/%3E %3Ca xmlns:xlink=%22http://www.w3.org/1999/xlink%22 xlink:title='
283 . '%22%3F%3E%22%3Etest%3C/a%3E %3C/svg%3E';
284
285 // phpcs:disable Generic.Files.LineLength
286 return [
287 [
288 'Regular file',
289 'foo { background: url(red.gif); }',
290 'foo { background: url(http://localhost/w/red.gif?34ac6); }',
291 ],
292 [
293 'Regular file (missing)',
294 'foo { background: url(theColorOfHerHair.gif); }',
295 'foo { background: url(http://localhost/w/theColorOfHerHair.gif); }',
296 ],
297 [
298 'Remote URL',
299 'foo { background: url(http://example.org/w/foo.png); }',
300 'foo { background: url(http://example.org/w/foo.png); }',
301 ],
302 [
303 'Protocol-relative remote URL',
304 'foo { background: url(//example.org/w/foo.png); }',
305 'foo { background: url(//example.org/w/foo.png); }',
306 ],
307 [
308 'Remote URL with query',
309 'foo { background: url(http://example.org/w/foo.png?query=yes); }',
310 'foo { background: url(http://example.org/w/foo.png?query=yes); }',
311 ],
312 [
313 'Protocol-relative remote URL with query',
314 'foo { background: url(//example.org/w/foo.png?query=yes); }',
315 'foo { background: url(//example.org/w/foo.png?query=yes); }',
316 ],
317 [
318 'Domain-relative URL',
319 'foo { background: url(/static/foo.png); }',
320 'foo { background: url(http://doc.example.org/static/foo.png); }',
321 ],
322 [
323 'Domain-relative URL with query',
324 'foo { background: url(/static/foo.png?query=yes); }',
325 'foo { background: url(http://doc.example.org/static/foo.png?query=yes); }',
326 ],
327 [
328 'Remote URL (unnecessary quotes not preserved)',
329 'foo { background: url("http://example.org/w/unnecessary-quotes.png"); }',
330 'foo { background: url(http://example.org/w/unnecessary-quotes.png); }',
331 ],
332 [
333 'Embedded file',
334 'foo { /* @embed */ background: url(red.gif); }',
335 "foo { background: url($red); background: url(http://localhost/w/red.gif?34ac6)!ie; }",
336 ],
337 [
338 'Embedded file, other comments before the rule',
339 "foo { /* Bar. */ /* @embed */ background: url(red.gif); }",
340 "foo { /* Bar. */ background: url($red); /* Bar. */ background: url(http://localhost/w/red.gif?34ac6)!ie; }",
341 ],
342 [
343 'Can not re-embed data: URIs',
344 "foo { /* @embed */ background: url($red); }",
345 "foo { background: url($red); }",
346 ],
347 [
348 'Can not remap data: URIs',
349 "foo { background: url($red); }",
350 "foo { background: url($red); }",
351 ],
352 [
353 'Can not embed remote URLs',
354 'foo { /* @embed */ background: url(http://example.org/w/foo.png); }',
355 'foo { background: url(http://example.org/w/foo.png); }',
356 ],
357 [
358 'Embedded file (inline @embed)',
359 'foo { background: /* @embed */ url(red.gif); }',
360 "foo { background: url($red); "
361 . "background: url(http://localhost/w/red.gif?34ac6)!ie; }",
362 ],
363 [
364 'Can not embed large files',
365 'foo { /* @embed */ background: url(large.png); }',
366 "foo { background: url(http://localhost/w/large.png?e3d1f); }",
367 ],
368 [
369 'SVG files are embedded without base64 encoding and unnecessary IE 6 and 7 fallback',
370 'foo { /* @embed */ background: url(circle.svg); }',
371 "foo { background: url(\"$svg\"); }",
372 ],
373 [
374 'Two regular files in one rule',
375 'foo { background: url(red.gif), url(green.gif); }',
376 'foo { background: url(http://localhost/w/red.gif?34ac6), '
377 . 'url(http://localhost/w/green.gif?13651); }',
378 ],
379 [
380 'Two embedded files in one rule',
381 'foo { /* @embed */ background: url(red.gif), url(green.gif); }',
382 "foo { background: url($red), url($green); "
383 . "background: url(http://localhost/w/red.gif?34ac6), "
384 . "url(http://localhost/w/green.gif?13651)!ie; }",
385 ],
386 [
387 'Two embedded files in one rule (inline @embed)',
388 'foo { background: /* @embed */ url(red.gif), /* @embed */ url(green.gif); }',
389 "foo { background: url($red), url($green); "
390 . "background: url(http://localhost/w/red.gif?34ac6), "
391 . "url(http://localhost/w/green.gif?13651)!ie; }",
392 ],
393 [
394 'Two embedded files in one rule (inline @embed), one too large',
395 'foo { background: /* @embed */ url(red.gif), /* @embed */ url(large.png); }',
396 "foo { background: url($red), url(http://localhost/w/large.png?e3d1f); "
397 . "background: url(http://localhost/w/red.gif?34ac6), "
398 . "url(http://localhost/w/large.png?e3d1f)!ie; }",
399 ],
400 [
401 'Practical example with some noise',
402 'foo { /* @embed */ background: #f9f9f9 url(red.gif) 0 0 no-repeat; }',
403 "foo { background: #f9f9f9 url($red) 0 0 no-repeat; "
404 . "background: #f9f9f9 url(http://localhost/w/red.gif?34ac6) 0 0 no-repeat!ie; }",
405 ],
406 [
407 'Does not mess with other properties',
408 'foo { color: red; background: url(red.gif); font-size: small; }',
409 'foo { color: red; background: url(http://localhost/w/red.gif?34ac6); font-size: small; }',
410 ],
411 [
412 'Spacing and miscellanea not changed (1)',
413 'foo { background: url(red.gif); }',
414 'foo { background: url(http://localhost/w/red.gif?34ac6); }',
415 ],
416 [
417 'Spacing and miscellanea not changed (2)',
418 'foo {background:url(red.gif)}',
419 'foo {background:url(http://localhost/w/red.gif?34ac6)}',
420 ],
421 [
422 'Spaces within url() parentheses are ignored',
423 'foo { background: url( red.gif ); }',
424 'foo { background: url(http://localhost/w/red.gif?34ac6); }',
425 ],
426 [
427 '@import rule to local file (should we remap this?)',
428 '@import url(/styles.css)',
429 '@import url(http://doc.example.org/styles.css)',
430 ],
431 [
432 '@import rule to local file (should we remap this?)',
433 '@import url(/styles.css)',
434 '@import url(http://doc.example.org/styles.css)',
435 ],
436 [
437 '@import rule to URL',
438 '@import url(//localhost/styles.css?query=val)',
439 '@import url(//localhost/styles.css?query=val)',
440 ],
441 [
442 'Background URL (double quotes)',
443 'foo { background: url("//localhost/styles.css?quoted=double") }',
444 'foo { background: url(//localhost/styles.css?quoted=double) }',
445 ],
446 [
447 'Background URL (single quotes)',
448 'foo { background: url(\'//localhost/styles.css?quoted=single\') }',
449 'foo { background: url(//localhost/styles.css?quoted=single) }',
450 ],
451 [
452 'Background URL (containing parentheses; T60473)',
453 'foo { background: url("//localhost/styles.css?query=(parens)") }',
454 'foo { background: url("//localhost/styles.css?query=(parens)") }',
455 ],
456 [
457 'Background URL (double quoted, containing single quotes; T60473)',
458 'foo { background: url("//localhost/styles.css?quote=\'") }',
459 'foo { background: url("//localhost/styles.css?quote=\'") }',
460 ],
461 [
462 'Background URL (single quoted, containing double quotes; T60473)',
463 'foo { background: url(\'//localhost/styles.css?quote="\') }',
464 'foo { background: url("//localhost/styles.css?quote=\"") }',
465 ],
466 [
467 'Simple case with comments before url',
468 'foo { prop: /* some {funny;} comment */ url(bar.png); }',
469 'foo { prop: /* some {funny;} comment */ url(http://localhost/w/bar.png); }',
470 ],
471 [
472 'Simple case with comments after url',
473 'foo { prop: url(red.gif)/* some {funny;} comment */ ; }',
474 'foo { prop: url(http://localhost/w/red.gif?34ac6)/* some {funny;} comment */ ; }',
475 ],
476 [
477 'Embedded file with comment before url',
478 'foo { /* @embed */ background: /* some {funny;} comment */ url(red.gif); }',
479 "foo { background: /* some {funny;} comment */ url($red); background: /* some {funny;} comment */ url(http://localhost/w/red.gif?34ac6)!ie; }",
480 ],
481 [
482 'Embedded file with comments inside and outside the rule',
483 'foo { /* @embed */ background: url(red.gif) /* some {foo;} comment */; /* some {bar;} comment */ }',
484 "foo { background: url($red) /* some {foo;} comment */; background: url(http://localhost/w/red.gif?34ac6) /* some {foo;} comment */!ie; /* some {bar;} comment */ }",
485 ],
486 [
487 'Embedded file with comment outside the rule',
488 'foo { /* @embed */ background: url(red.gif); /* some {funny;} comment */ }',
489 "foo { background: url($red); background: url(http://localhost/w/red.gif?34ac6)!ie; /* some {funny;} comment */ }",
490 ],
491 [
492 'Rule with two urls, each with comments',
493 '{ background: /*asd*/ url(something.png); background: /*jkl*/ url(something.png); }',
494 '{ background: /*asd*/ url(http://localhost/w/something.png); background: /*jkl*/ url(http://localhost/w/something.png); }',
495 ],
496 [
497 'Sanity check for offending line from jquery.ui.theme.css (T62077)',
498 '.ui-state-default, .ui-widget-content .ui-state-default, .ui-widget-header .ui-state-default { border: 1px solid #d3d3d3/*{borderColorDefault}*/; background: #e6e6e6/*{bgColorDefault}*/ url(images/ui-bg_glass_75_e6e6e6_1x400.png)/*{bgImgUrlDefault}*/ 50%/*{bgDefaultXPos}*/ 50%/*{bgDefaultYPos}*/ repeat-x/*{bgDefaultRepeat}*/; font-weight: normal/*{fwDefault}*/; color: #555555/*{fcDefault}*/; }',
499 '.ui-state-default, .ui-widget-content .ui-state-default, .ui-widget-header .ui-state-default { border: 1px solid #d3d3d3/*{borderColorDefault}*/; background: #e6e6e6/*{bgColorDefault}*/ url(http://localhost/w/images/ui-bg_glass_75_e6e6e6_1x400.png)/*{bgImgUrlDefault}*/ 50%/*{bgDefaultXPos}*/ 50%/*{bgDefaultYPos}*/ repeat-x/*{bgDefaultRepeat}*/; font-weight: normal/*{fwDefault}*/; color: #555555/*{fcDefault}*/; }',
500 ],
501 ];
502 // phpcs:enable
503 }
504
505 /**
506 * This tests basic functionality of CSSMin::buildUrlValue.
507 *
508 * @dataProvider provideBuildUrlValueCases
509 * @covers CSSMin::buildUrlValue
510 */
511 public function testBuildUrlValue( $message, $input, $expectedOutput ) {
512 $this->assertEquals(
513 $expectedOutput,
514 CSSMin::buildUrlValue( $input ),
515 "CSSMin::buildUrlValue: $message"
516 );
517 }
518
519 public static function provideBuildUrlValueCases() {
520 return [
521 [
522 'Full URL',
523 'scheme://user@domain:port/~user/fi%20le.png?query=yes&really=y+s',
524 'url(scheme://user@domain:port/~user/fi%20le.png?query=yes&really=y+s)',
525 ],
526 [
527 'data: URI',
528 'data:image/png;base64,R0lGODlh/+==',
529 'url(data:image/png;base64,R0lGODlh/+==)',
530 ],
531 [
532 'URL with quotes',
533 "https://en.wikipedia.org/wiki/Wendy's",
534 "url(\"https://en.wikipedia.org/wiki/Wendy's\")",
535 ],
536 [
537 'URL with parentheses',
538 'https://en.wikipedia.org/wiki/Boston_(band)',
539 'url("https://en.wikipedia.org/wiki/Boston_(band)")',
540 ],
541 ];
542 }
543
544 /**
545 * Seperated because they are currently broken (T37492)
546 *
547 * @group Broken
548 * @dataProvider provideStringCases
549 * @covers CSSMin::remap
550 */
551 public function testMinifyWithCSSStringValues( $code, $expectedOutput ) {
552 $this->testMinifyOutput( $code, $expectedOutput );
553 }
554
555 public static function provideStringCases() {
556 return [
557 // String values should be respected
558 // - More than one space in a string value
559 [ 'foo { content: " "; }', 'foo{content:" "}' ],
560 // - Using a tab in a string value (turns into a space)
561 [ "foo { content: '\t'; }", "foo{content:'\t'}" ],
562 // - Using css-like syntax in string values
563 [
564 'foo::after { content: "{;}"; position: absolute; }',
565 'foo::after{content:"{;}";position:absolute}'
566 ],
567 ];
568 }
569 }
570
571 class CSSMinTestable extends CSSMin {
572 // Make some protected methods public
573 public static function isRemoteUrl( $maybeUrl ) {
574 return parent::isRemoteUrl( $maybeUrl );
575 }
576 public static function isLocalUrl( $maybeUrl ) {
577 return parent::isLocalUrl( $maybeUrl );
578 }
579 }