TitleTest: Break secure and split test into two tests with providers
[mediawiki.git] / tests / phpunit / includes / libs / CSSJanusTest.php
blobe4283b0bf57587af2e50925add72ebce6434d5bf
1 <?php
2 /**
3 * Based on the test suite of the original Python
4 * CSSJanus libary:
5 * http://code.google.com/p/cssjanus/source/browse/trunk/cssjanus_test.py
6 * Ported to PHP for ResourceLoader and has been extended since.
8 * @covers CSSJanus
9 */
10 class CSSJanusTest extends MediaWikiTestCase {
11 /**
12 * @dataProvider provideTransformCases
14 public function testTransform( $cssA, $cssB = null ) {
16 if ( $cssB ) {
17 $transformedA = CSSJanus::transform( $cssA );
18 $this->assertEquals(
19 $transformedA,
20 str_replace( '/* @noflip */ ', '', $cssB ),
21 'Test A-B transformation'
24 $transformedB = CSSJanus::transform( $cssB );
25 $this->assertEquals(
26 $transformedB,
27 str_replace( '/* @noflip */ ', '', $cssA ),
28 'Test B-A transformation'
30 } else {
31 // If no B version is provided, it means
32 // the output should equal the input (modulo @noflip annotations).
33 $transformedA = CSSJanus::transform( $cssA );
34 $this->assertEquals(
35 $transformedA,
36 str_replace( '/* @noflip */ ', '', $cssA ),
37 'Nothing was flipped'
42 /**
43 * @dataProvider provideTransformAdvancedCases
45 public function testTransformAdvanced( $code, $expectedOutput, $options = array() ) {
46 $swapLtrRtlInURL = isset( $options['swapLtrRtlInURL'] ) ?
47 $options['swapLtrRtlInURL'] : false;
48 $swapLeftRightInURL = isset( $options['swapLeftRightInURL'] ) ?
49 $options['swapLeftRightInURL'] : false;
51 $flipped = CSSJanus::transform( $code, $swapLtrRtlInURL, $swapLeftRightInURL );
53 $this->assertEquals( $expectedOutput, $flipped,
54 'Test flipping, options: url-ltr-rtl=' . ( $swapLtrRtlInURL ? 'true' : 'false' )
55 . ' url-left-right=' . ( $swapLeftRightInURL ? 'true' : 'false' )
59 /**
60 * @dataProvider provideTransformBrokenCases
61 * @group Broken
63 public function testTransformBroken( $code, $expectedOutput ) {
64 $flipped = CSSJanus::transform( $code );
66 $this->assertEquals( $expectedOutput, $flipped, 'Test flipping' );
69 /**
70 * These transform cases are tested *in both directions*
71 * No need to declare a principle twice in both directions here.
73 public static function provideTransformCases() {
74 return array(
75 // Property keys
76 array(
77 '.foo { left: 0; }',
78 '.foo { right: 0; }'
80 // Guard against partial keys
81 // (CSS currently doesn't have flippable properties
82 // that contain the direction as part of the key without
83 // dash separation)
84 array(
85 '.foo { alright: 0; }'
87 array(
88 '.foo { balleft: 0; }'
91 // Dashed property keys
92 array(
93 '.foo { padding-left: 0; }',
94 '.foo { padding-right: 0; }'
96 array(
97 '.foo { margin-left: 0; }',
98 '.foo { margin-right: 0; }'
100 array(
101 '.foo { border-left: 0; }',
102 '.foo { border-right: 0; }'
105 // Double-dashed property keys
106 array(
107 '.foo { border-left-color: red; }',
108 '.foo { border-right-color: red; }'
110 array(
111 // Includes unknown properties?
112 '.foo { x-left-y: 0; }',
113 '.foo { x-right-y: 0; }'
116 // Multi-value properties
117 array(
118 '.foo { padding: 0; }'
120 array(
121 '.foo { padding: 0 1px; }'
123 array(
124 '.foo { padding: 0 1px 2px; }'
126 array(
127 '.foo { padding: 0 1px 2px 3px; }',
128 '.foo { padding: 0 3px 2px 1px; }'
131 // Shorthand / Four notation
132 array(
133 '.foo { padding: .25em 15px 0pt 0ex; }',
134 '.foo { padding: .25em 0ex 0pt 15px; }'
136 array(
137 '.foo { margin: 1px -4px 3px 2px; }',
138 '.foo { margin: 1px 2px 3px -4px; }'
140 array(
141 '.foo { padding: 0 15px .25em 0; }',
142 '.foo { padding: 0 0 .25em 15px; }'
144 array(
145 '.foo { padding: 1px 4.1grad 3px 2%; }',
146 '.foo { padding: 1px 2% 3px 4.1grad; }'
148 array(
149 '.foo { padding: 1px 2px 3px auto; }',
150 '.foo { padding: 1px auto 3px 2px; }'
152 array(
153 '.foo { padding: 1px inherit 3px auto; }',
154 '.foo { padding: 1px auto 3px inherit; }'
156 // border-radius assigns different meanings to the values
157 array(
158 '.foo { border-radius: .25em 15px 0pt 0ex; }',
159 '.foo { border-radius: 15px .25em 0ex 0pt; }'
161 array(
162 '.foo { border-radius: 0px 0px 5px 5px; }',
164 // Ensure the rule doesn't break other stuff
165 array(
166 '.foo { x-unknown: a b c d; }'
168 array(
169 '.foo barpx 0 2% { opacity: 0; }'
171 array(
172 '#settings td p strong'
174 array(
175 // Color names
176 '.foo { border-color: red green blue white }',
177 '.foo { border-color: red white blue green }',
179 array(
180 // Color name, hexdecimal, RGB & RGBA
181 '.foo { border-color: red #f00 rgb(255, 0, 0) rgba(255, 0, 0, 0.5) }',
182 '.foo { border-color: red rgba(255, 0, 0, 0.5) rgb(255, 0, 0) #f00 }',
184 array(
185 // Color name, hexdecimal, HSL & HSLA
186 '.foo { border-color: red #f00 hsl(0, 100%, 50%) hsla(0, 100%, 50%, 0.5) }',
187 '.foo { border-color: red hsla(0, 100%, 50%, 0.5) hsl(0, 100%, 50%) #f00 }',
189 array(
190 // Do not mangle 5 or more values
191 '.foo { -x-unknown: 1 2 3 4 5; }'
193 array(
194 '.foo { -x-unknown: 1 2 3 4 5 6; }'
197 // Shorthand / Three notation
198 array(
199 '.foo { margin: 1em 0 .25em; }'
201 array(
202 '.foo { margin:-1.5em 0 -.75em; }'
205 // Shorthand / Two notation
206 array(
207 '.foo { padding: 1px 2px; }'
210 // Shorthand / One notation
211 array(
212 '.foo { padding: 1px; }'
215 // text-shadow and box-shadow
216 array(
217 '.foo { box-shadow: -6px 3px 8px 5px rgba(0, 0, 0, 0.25); }',
218 '.foo { box-shadow: 6px 3px 8px 5px rgba(0, 0, 0, 0.25); }',
220 array(
221 '.foo { box-shadow: inset -6px 3px 8px 5px rgba(0, 0, 0, 0.25); }',
222 '.foo { box-shadow: inset 6px 3px 8px 5px rgba(0, 0, 0, 0.25); }',
224 array(
225 '.foo { text-shadow: orange 2px 0; }',
226 '.foo { text-shadow: orange -2px 0; }',
228 array(
229 '.foo { text-shadow: 2px 0 orange; }',
230 '.foo { text-shadow: -2px 0 orange; }',
232 array(
233 // Don't mangle zeroes
234 '.foo { text-shadow: orange 0 2px; }'
236 array(
237 // Make sure floats are not considered zero
238 '.foo { box-shadow: inset .5em 0 0 white; }',
239 '.foo { box-shadow: inset -.5em 0 0 white; }',
242 // Direction
243 // Note: This differs from the Python implementation,
244 // see also CSSJanus::fixDirection for more info.
245 array(
246 '.foo { direction: ltr; }',
247 '.foo { direction: rtl; }'
249 array(
250 '.foo { direction: rtl; }',
251 '.foo { direction: ltr; }'
253 array(
254 'input { direction: ltr; }',
255 'input { direction: rtl; }'
257 array(
258 'input { direction: rtl; }',
259 'input { direction: ltr; }'
261 array(
262 'body { direction: ltr; }',
263 'body { direction: rtl; }'
265 array(
266 '.foo, body, input { direction: ltr; }',
267 '.foo, body, input { direction: rtl; }'
269 array(
270 'body { padding: 10px; direction: ltr; }',
271 'body { padding: 10px; direction: rtl; }'
273 array(
274 'body { direction: ltr } .myClass { direction: ltr }',
275 'body { direction: rtl } .myClass { direction: rtl }'
278 // Left/right values
279 array(
280 '.foo { float: left; }',
281 '.foo { float: right; }'
283 array(
284 '.foo { text-align: left; }',
285 '.foo { text-align: right; }'
287 array(
288 '.foo { -x-unknown: left; }',
289 '.foo { -x-unknown: right; }'
291 // Guard against selectors that look flippable
292 array(
293 '.column-left { width: 0; }'
295 array(
296 'a.left { width: 0; }'
298 array(
299 'a.leftification { width: 0; }'
301 array(
302 'a.ltr { width: 0; }'
304 array(
305 # <div class="a-ltr png">
306 '.a-ltr.png { width: 0; }'
308 array(
309 # <foo-ltr attr="x">
310 'foo-ltr[attr="x"] { width: 0; }'
312 array(
313 'div.left > span.right+span.left { width: 0; }'
315 array(
316 '.thisclass .left .myclass { width: 0; }'
318 array(
319 '.thisclass .left .myclass #myid { width: 0; }'
322 // Cursor values (east/west)
323 array(
324 '.foo { cursor: e-resize; }',
325 '.foo { cursor: w-resize; }'
327 array(
328 '.foo { cursor: se-resize; }',
329 '.foo { cursor: sw-resize; }'
331 array(
332 '.foo { cursor: ne-resize; }',
333 '.foo { cursor: nw-resize; }'
336 // Background
337 array(
338 '.foo { background-position: top left; }',
339 '.foo { background-position: top right; }'
341 array(
342 '.foo { background: url(/foo/bar.png) top left; }',
343 '.foo { background: url(/foo/bar.png) top right; }'
345 array(
346 '.foo { background: url(/foo/bar.png) top left no-repeat; }',
347 '.foo { background: url(/foo/bar.png) top right no-repeat; }'
349 array(
350 '.foo { background: url(/foo/bar.png) no-repeat top left; }',
351 '.foo { background: url(/foo/bar.png) no-repeat top right; }'
353 array(
354 '.foo { background: #fff url(/foo/bar.png) no-repeat top left; }',
355 '.foo { background: #fff url(/foo/bar.png) no-repeat top right; }'
357 array(
358 '.foo { background-position: 100% 40%; }',
359 '.foo { background-position: 0% 40%; }'
361 array(
362 '.foo { background-position: 23% 0; }',
363 '.foo { background-position: 77% 0; }'
365 array(
366 '.foo { background-position: 23% auto; }',
367 '.foo { background-position: 77% auto; }'
369 array(
370 '.foo { background-position-x: 23%; }',
371 '.foo { background-position-x: 77%; }'
373 array(
374 '.foo { background-position-y: 23%; }',
375 '.foo { background-position-y: 23%; }'
377 array(
378 '.foo { background:url(../foo.png) no-repeat 75% 50%; }',
379 '.foo { background:url(../foo.png) no-repeat 25% 50%; }'
381 array(
382 '.foo { background: 10% 20% } .bar { background: 40% 30% }',
383 '.foo { background: 90% 20% } .bar { background: 60% 30% }'
386 // Multiple rules
387 array(
388 'body { direction: rtl; float: right; } .foo { direction: ltr; float: right; }',
389 'body { direction: ltr; float: left; } .foo { direction: rtl; float: left; }',
392 // Duplicate properties
393 array(
394 '.foo { float: left; float: right; float: left; }',
395 '.foo { float: right; float: left; float: right; }',
398 // Preserve comments
399 array(
400 '/* left /* right */left: 10px',
401 '/* left /* right */right: 10px'
403 array(
404 '/*left*//*left*/left: 10px',
405 '/*left*//*left*/right: 10px'
407 array(
408 '/* Going right is cool */ .foo { width: 0 }',
410 array(
411 "/* padding-right 1 2 3 4 */\n#test { width: 0}\n/*right*/"
413 array(
414 "/** Two line comment\n * left\n \*/\n#test {width: 0}"
417 // @noflip annotation
418 array(
419 // before selector (single)
420 '/* @noflip */ div { float: left; }'
422 array(
423 // before selector (multiple)
424 '/* @noflip */ div, .notme { float: left; }'
426 array(
427 // inside selector
428 'div, /* @noflip */ .foo { float: left; }'
430 array(
431 // after selector
432 'div, .notme /* @noflip */ { float: left; }'
434 array(
435 // before multiple rules
436 '/* @noflip */ div { float: left; } .foo { float: left; }',
437 '/* @noflip */ div { float: left; } .foo { float: right; }'
439 array(
440 // support parentheses in selector
441 '/* @noflip */ .test:not(:first) { margin-right: -0.25em; margin-left: 0.25em; }',
442 '/* @noflip */ .test:not(:first) { margin-right: -0.25em; margin-left: 0.25em; }'
444 array(
445 // after multiple rules
446 '.foo { float: left; } /* @noflip */ div { float: left; }',
447 '.foo { float: right; } /* @noflip */ div { float: left; }'
449 array(
450 // before multiple properties
451 'div { /* @noflip */ float: left; text-align: left; }',
452 'div { /* @noflip */ float: left; text-align: right; }'
454 array(
455 // after multiple properties
456 'div { float: left; /* @noflip */ text-align: left; }',
457 'div { float: right; /* @noflip */ text-align: left; }'
459 array(
460 // before a *= attribute selector with multiple properties
461 '/* @noflip */ div.foo[bar*=baz] { float:left; clear: left; }'
463 array(
464 // before a ^= attribute selector with multiple properties
465 '/* @noflip */ div.foo[bar^=baz] { float:left; clear: left; }'
467 array(
468 // before a ~= attribute selector with multiple properties
469 '/* @noflip */ div.foo[bar~=baz] { float:left; clear: left; }'
471 array(
472 // before a = attribute selector with multiple properties
473 '/* @noflip */ div.foo[bar=baz] { float:left; clear: left; }'
475 array(
476 // before a quoted attribute selector with multiple properties
477 '/* @noflip */ div.foo[bar=\'baz{quux\'] { float:left; clear: left; }'
480 // Guard against css3 stuff
481 array(
482 'background-image: -moz-linear-gradient(#326cc1, #234e8c);'
484 array(
485 'background-image: -webkit-gradient(linear, 100% 0%, 0% 0%, from(#666666), to(#ffffff));'
488 // CSS syntax / white-space variations
489 // spaces, no spaces, tabs, new lines, omitting semi-colons
490 array(
491 ".foo { left: 0; }",
492 ".foo { right: 0; }"
494 array(
495 ".foo{ left: 0; }",
496 ".foo{ right: 0; }"
498 array(
499 ".foo{ left: 0 }",
500 ".foo{ right: 0 }"
502 array(
503 ".foo{left:0 }",
504 ".foo{right:0 }"
506 array(
507 ".foo{left:0}",
508 ".foo{right:0}"
510 array(
511 ".foo { left : 0 ; }",
512 ".foo { right : 0 ; }"
514 array(
515 ".foo\n { left : 0 ; }",
516 ".foo\n { right : 0 ; }"
518 array(
519 ".foo\n { \nleft : 0 ; }",
520 ".foo\n { \nright : 0 ; }"
522 array(
523 ".foo\n { \n left : 0 ; }",
524 ".foo\n { \n right : 0 ; }"
526 array(
527 ".foo\n { \n left\n : 0; }",
528 ".foo\n { \n right\n : 0; }"
530 array(
531 ".foo \n { \n left\n : 0; }",
532 ".foo \n { \n right\n : 0; }"
534 array(
535 ".foo\n{\nleft\n:\n0;}",
536 ".foo\n{\nright\n:\n0;}"
538 array(
539 ".foo\n.bar {\n\tleft: 0;\n}",
540 ".foo\n.bar {\n\tright: 0;\n}"
542 array(
543 ".foo\t{\tleft\t:\t0;}",
544 ".foo\t{\tright\t:\t0;}"
547 // Guard against partial keys
548 array(
549 '.foo { leftxx: 0; }',
550 '.foo { leftxx: 0; }'
552 array(
553 '.foo { rightxx: 0; }',
554 '.foo { rightxx: 0; }'
560 * These cases are tested in one way only (format: actual, expected, msg).
561 * If both ways can be tested, either put both versions in here or move
562 * it to provideTransformCases().
564 public static function provideTransformAdvancedCases() {
565 $bgPairs = array(
566 # [ - _ . ] <-> [ left right ltr rtl ]
567 'foo.jpg' => 'foo.jpg',
568 'left.jpg' => 'right.jpg',
569 'ltr.jpg' => 'rtl.jpg',
571 'foo-left.png' => 'foo-right.png',
572 'foo_left.png' => 'foo_right.png',
573 'foo.left.png' => 'foo.right.png',
575 'foo-ltr.png' => 'foo-rtl.png',
576 'foo_ltr.png' => 'foo_rtl.png',
577 'foo.ltr.png' => 'foo.rtl.png',
579 'left-foo.png' => 'right-foo.png',
580 'left_foo.png' => 'right_foo.png',
581 'left.foo.png' => 'right.foo.png',
583 'ltr-foo.png' => 'rtl-foo.png',
584 'ltr_foo.png' => 'rtl_foo.png',
585 'ltr.foo.png' => 'rtl.foo.png',
587 'foo-ltr-left.gif' => 'foo-rtl-right.gif',
588 'foo_ltr_left.gif' => 'foo_rtl_right.gif',
589 'foo.ltr.left.gif' => 'foo.rtl.right.gif',
590 'foo-ltr_left.gif' => 'foo-rtl_right.gif',
591 'foo_ltr.left.gif' => 'foo_rtl.right.gif',
593 $provider = array();
594 foreach ( $bgPairs as $left => $right ) {
595 # By default '-rtl' and '-left' etc. are not touched,
596 # Only when the appropiate parameter is set.
597 $provider[] = array(
598 ".foo { background: url(images/$left); }",
599 ".foo { background: url(images/$left); }"
601 $provider[] = array(
602 ".foo { background: url(images/$right); }",
603 ".foo { background: url(images/$right); }"
605 $provider[] = array(
606 ".foo { background: url(images/$left); }",
607 ".foo { background: url(images/$right); }",
608 array(
609 'swapLtrRtlInURL' => true,
610 'swapLeftRightInURL' => true,
613 $provider[] = array(
614 ".foo { background: url(images/$right); }",
615 ".foo { background: url(images/$left); }",
616 array(
617 'swapLtrRtlInURL' => true,
618 'swapLeftRightInURL' => true,
623 return $provider;
627 * Cases that are currently failing, but
628 * should be looked at in the future as enhancements and/or bug fix
630 public static function provideTransformBrokenCases() {
631 return array(
632 // Guard against selectors that look flippable
633 array(
634 # <foo-left-x attr="x">
635 'foo-left-x[attr="x"] { width: 0; }',
636 'foo-left-x[attr="x"] { width: 0; }'
638 array(
639 # <div class="foo" data-left="x">
640 '.foo[data-left="x"] { width: 0; }',
641 '.foo[data-left="x"] { width: 0; }'