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