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 /**
155 * This tests funky parameters to CSSMin::remap. testRemapRemapping tests
156 * the basic functionality.
157 *
158 * @dataProvider provideRemapCases
159 * @covers CSSMin::remap
160 */
161 public function testRemap( $message, $params, $expectedOutput ) {
162 $remapped = call_user_func_array( 'CSSMin::remap', $params );
163
164 $messageAdd = " Case: $message";
165 $this->assertEquals(
166 $expectedOutput,
167 $remapped,
168 'CSSMin::remap should return the expected url form.' . $messageAdd
169 );
170 }
171
172 public static function provideRemapCases() {
173 // Parameter signature:
174 // CSSMin::remap( $code, $local, $remote, $embedData = true )
175 return [
176 [
177 'Simple case',
178 [ 'foo { prop: url(bar.png); }', false, 'http://example.org', false ],
179 'foo { prop: url(http://example.org/bar.png); }',
180 ],
181 [
182 'Without trailing slash',
183 [ 'foo { prop: url(../bar.png); }', false, 'http://example.org/quux', false ],
184 'foo { prop: url(http://example.org/bar.png); }',
185 ],
186 [
187 'With trailing slash on remote (T29052)',
188 [ 'foo { prop: url(../bar.png); }', false, 'http://example.org/quux/', false ],
189 'foo { prop: url(http://example.org/bar.png); }',
190 ],
191 [
192 'Guard against stripping double slashes from query',
193 [ 'foo { prop: url(bar.png?corge=//grault); }', false, 'http://example.org/quux/', false ],
194 'foo { prop: url(http://example.org/quux/bar.png?corge=//grault); }',
195 ],
196 [
197 'Expand absolute paths',
198 [ 'foo { prop: url(/w/skin/images/bar.png); }', false, 'http://example.org/quux', false ],
199 'foo { prop: url(http://doc.example.org/w/skin/images/bar.png); }',
200 ],
201 [
202 "Don't barf at behavior: url(#default#behaviorName) - T162973",
203 [ 'foo { behavior: url(#default#bar); }', false, '/w/', false ],
204 'foo { behavior: url("#default#bar"); }',
205 ],
206 ];
207 }
208
209 /**
210 * This tests basic functionality of CSSMin::remap. testRemapRemapping tests funky parameters.
211 *
212 * @dataProvider provideRemapRemappingCases
213 * @covers CSSMin::remap
214 */
215 public function testRemapRemapping( $message, $input, $expectedOutput ) {
216 $localPath = __DIR__ . '/../../data/cssmin';
217 $remotePath = 'http://localhost/w';
218
219 $realOutput = CSSMin::remap( $input, $localPath, $remotePath );
220 $this->assertEquals( $expectedOutput, $realOutput, "CSSMin::remap: $message" );
221 }
222
223 public static function provideIsRemoteUrl() {
224 return [
225 [ true, 'http://localhost/w/red.gif?123' ],
226 [ true, 'https://example.org/x.png' ],
227 [ true, '//example.org/x.y.z/image.png' ],
228 [ true, '//localhost/styles.css?query=yes' ],
229 [ true, 'data:image/gif;base64,R0lGODlhAQABAIAAAP8AADAAACwAAAAAAQABAAACAkQBADs=' ],
230 [ false, 'x.gif' ],
231 [ false, '/x.gif' ],
232 [ false, './x.gif' ],
233 [ false, '../x.gif' ],
234 ];
235 }
236
237 /**
238 * @dataProvider provideIsRemoteUrl
239 * @cover CSSMin::isRemoteUrl
240 */
241 public function testIsRemoteUrl( $expect, $url ) {
242 $this->assertEquals( CSSMinTestable::isRemoteUrl( $url ), $expect );
243 }
244
245 public static function provideIsLocalUrls() {
246 return [
247 [ false, 'x.gif' ],
248 [ true, '/x.gif' ],
249 [ false, './x.gif' ],
250 [ false, '../x.gif' ],
251 ];
252 }
253
254 /**
255 * @dataProvider provideIsLocalUrls
256 * @cover CSSMin::isLocalUrl
257 */
258 public function testIsLocalUrl( $expect, $url ) {
259 $this->assertEquals( CSSMinTestable::isLocalUrl( $url ), $expect );
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/foo.png"); }',
320 'foo { background: url(http://example.org/w/foo.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 URL (should we remap this?)',
423 '@import url(//localhost/styles.css?query=yes)',
424 '@import url(//localhost/styles.css?query=yes)',
425 ],
426 [
427 'Simple case with comments before url',
428 'foo { prop: /* some {funny;} comment */ url(bar.png); }',
429 'foo { prop: /* some {funny;} comment */ url(http://localhost/w/bar.png); }',
430 ],
431 [
432 'Simple case with comments after url',
433 'foo { prop: url(red.gif)/* some {funny;} comment */ ; }',
434 'foo { prop: url(http://localhost/w/red.gif?34ac6)/* some {funny;} comment */ ; }',
435 ],
436 [
437 'Embedded file with comment before url',
438 'foo { /* @embed */ background: /* some {funny;} comment */ url(red.gif); }',
439 "foo { background: /* some {funny;} comment */ url($red); background: /* some {funny;} comment */ url(http://localhost/w/red.gif?34ac6)!ie; }",
440 ],
441 [
442 'Embedded file with comments inside and outside the rule',
443 'foo { /* @embed */ background: url(red.gif) /* some {foo;} comment */; /* some {bar;} comment */ }',
444 "foo { background: url($red) /* some {foo;} comment */; background: url(http://localhost/w/red.gif?34ac6) /* some {foo;} comment */!ie; /* some {bar;} comment */ }",
445 ],
446 [
447 'Embedded file with comment outside the rule',
448 'foo { /* @embed */ background: url(red.gif); /* some {funny;} comment */ }',
449 "foo { background: url($red); background: url(http://localhost/w/red.gif?34ac6)!ie; /* some {funny;} comment */ }",
450 ],
451 [
452 'Rule with two urls, each with comments',
453 '{ background: /*asd*/ url(something.png); background: /*jkl*/ url(something.png); }',
454 '{ background: /*asd*/ url(http://localhost/w/something.png); background: /*jkl*/ url(http://localhost/w/something.png); }',
455 ],
456 [
457 'Sanity check for offending line from jquery.ui.theme.css (T62077)',
458 '.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}*/; }',
459 '.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}*/; }',
460 ],
461 ];
462 // @codingStandardsIgnoreEnd
463 }
464
465 /**
466 * This tests basic functionality of CSSMin::buildUrlValue.
467 *
468 * @dataProvider provideBuildUrlValueCases
469 * @covers CSSMin::buildUrlValue
470 */
471 public function testBuildUrlValue( $message, $input, $expectedOutput ) {
472 $this->assertEquals(
473 $expectedOutput,
474 CSSMin::buildUrlValue( $input ),
475 "CSSMin::buildUrlValue: $message"
476 );
477 }
478
479 public static function provideBuildUrlValueCases() {
480 return [
481 [
482 'Full URL',
483 'scheme://user@domain:port/~user/fi%20le.png?query=yes&really=y+s',
484 'url(scheme://user@domain:port/~user/fi%20le.png?query=yes&really=y+s)',
485 ],
486 [
487 'data: URI',
488 'data:image/png;base64,R0lGODlh/+==',
489 'url(data:image/png;base64,R0lGODlh/+==)',
490 ],
491 [
492 'URL with quotes',
493 "https://en.wikipedia.org/wiki/Wendy's",
494 "url(\"https://en.wikipedia.org/wiki/Wendy's\")",
495 ],
496 [
497 'URL with parentheses',
498 'https://en.wikipedia.org/wiki/Boston_(band)',
499 'url("https://en.wikipedia.org/wiki/Boston_(band)")',
500 ],
501 ];
502 }
503
504 /**
505 * Seperated because they are currently broken (T37492)
506 *
507 * @group Broken
508 * @dataProvider provideStringCases
509 * @covers CSSMin::remap
510 */
511 public function testMinifyWithCSSStringValues( $code, $expectedOutput ) {
512 $this->testMinifyOutput( $code, $expectedOutput );
513 }
514
515 public static function provideStringCases() {
516 return [
517 // String values should be respected
518 // - More than one space in a string value
519 [ 'foo { content: " "; }', 'foo{content:" "}' ],
520 // - Using a tab in a string value (turns into a space)
521 [ "foo { content: '\t'; }", "foo{content:'\t'}" ],
522 // - Using css-like syntax in string values
523 [
524 'foo::after { content: "{;}"; position: absolute; }',
525 'foo::after{content:"{;}";position:absolute}'
526 ],
527 ];
528 }
529 }
530
531 class CSSMinTestable extends CSSMin {
532 // Make some protected methods public
533 public static function isRemoteUrl( $maybeUrl ) {
534 return parent::isRemoteUrl( $maybeUrl );
535 }
536 public static function isLocalUrl( $maybeUrl ) {
537 return parent::isLocalUrl( $maybeUrl );
538 }
539 }