Merge "Removed half-assed $wgDBtransactions global."
[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 class CSSMinTest extends MediaWikiTestCase {
9 protected $oldServer = null, $oldCanServer = null;
10
11 function setUp() {
12 parent::setUp();
13
14 // Fake $wgServer and $wgCanonicalServer
15 global $wgServer, $wgCanonicalServer;
16 $this->oldServer = $wgServer;
17 $this->oldCanServer = $wgCanonicalServer;
18 $wgServer = $wgCanonicalServer = 'http://wiki.example.org';
19 }
20
21 function tearDown() {
22 // Restore $wgServer and $wgCanonicalServer
23 global $wgServer, $wgCanonicalServer;
24 $wgServer = $this->oldServer;
25 $wgCanonicalServer = $this->oldCanServer;
26
27 parent::tearDown();
28 }
29
30 /**
31 * @dataProvider provideMinifyCases
32 */
33 function testMinify( $code, $expectedOutput ) {
34 $minified = CSSMin::minify( $code );
35
36 $this->assertEquals( $expectedOutput, $minified, 'Minified output should be in the form expected.' );
37 }
38
39 function provideMinifyCases() {
40 return array(
41 // Whitespace
42 array( "\r\t\f \v\n\r", "" ),
43 array( "foo, bar {\n\tprop: value;\n}", "foo,bar{prop:value}" ),
44
45 // Loose comments
46 array( "/* foo */", "" ),
47 array( "/*******\n foo\n *******/", "" ),
48 array( "/*!\n foo\n */", "" ),
49
50 // Inline comments in various different places
51 array( "/* comment */foo, bar {\n\tprop: value;\n}", "foo,bar{prop:value}" ),
52 array( "foo/* comment */, bar {\n\tprop: value;\n}", "foo,bar{prop:value}" ),
53 array( "foo,/* comment */ bar {\n\tprop: value;\n}", "foo,bar{prop:value}" ),
54 array( "foo, bar/* comment */ {\n\tprop: value;\n}", "foo,bar{prop:value}" ),
55 array( "foo, bar {\n\t/* comment */prop: value;\n}", "foo,bar{prop:value}" ),
56 array( "foo, bar {\n\tprop: /* comment */value;\n}", "foo,bar{prop:value}" ),
57 array( "foo, bar {\n\tprop: value /* comment */;\n}", "foo,bar{prop:value }" ),
58 array( "foo, bar {\n\tprop: value; /* comment */\n}", "foo,bar{prop:value; }" ),
59
60 // Keep track of things that aren't as minified as much as they
61 // could be (bug 35493)
62 array( 'foo { prop: value ;}', 'foo{prop:value }' ),
63 array( 'foo { prop : value; }', 'foo{prop :value}' ),
64 array( 'foo { prop: value ; }', 'foo{prop:value }' ),
65 array( 'foo { font-family: "foo" , "bar"; }', 'foo{font-family:"foo" ,"bar"}' ),
66 array( "foo { src:\n\turl('foo') ,\n\turl('bar') ; }", "foo{src:url('foo') ,url('bar') }" ),
67
68 // Interesting cases with string values
69 // - Double quotes, single quotes
70 array( 'foo { content: ""; }', 'foo{content:""}' ),
71 array( "foo { content: ''; }", "foo{content:''}" ),
72 array( 'foo { content: "\'"; }', 'foo{content:"\'"}' ),
73 array( "foo { content: '\"'; }", "foo{content:'\"'}" ),
74 // - Whitespace in string values
75 array( 'foo { content: " "; }', 'foo{content:" "}' ),
76 );
77 }
78
79 /**
80 * @dataProvider provideRemapCases
81 */
82 function testRemap( $message, $params, $expectedOutput ) {
83 $remapped = call_user_func_array( 'CSSMin::remap', $params );
84
85 $messageAdd = " Case: $message";
86 $this->assertEquals( $expectedOutput, $remapped, 'CSSMin::remap should return the expected url form.' . $messageAdd );
87 }
88
89 function provideRemapCases() {
90 // Parameter signature:
91 // CSSMin::remap( $code, $local, $remote, $embedData = true )
92 return array(
93 array(
94 'Simple case',
95 array( 'foo { prop: url(bar.png); }', false, 'http://example.org', false ),
96 'foo { prop: url(http://example.org/bar.png); }',
97 ),
98 array(
99 'Without trailing slash',
100 array( 'foo { prop: url(../bar.png); }', false, 'http://example.org/quux', false ),
101 'foo { prop: url(http://example.org/quux/../bar.png); }',
102 ),
103 array(
104 'With trailing slash on remote (bug 27052)',
105 array( 'foo { prop: url(../bar.png); }', false, 'http://example.org/quux/', false ),
106 'foo { prop: url(http://example.org/quux/../bar.png); }',
107 ),
108 array(
109 'Guard against stripping double slashes from query',
110 array( 'foo { prop: url(bar.png?corge=//grault); }', false, 'http://example.org/quux/', false ),
111 'foo { prop: url(http://example.org/quux/bar.png?corge=//grault); }',
112 ),
113 array(
114 'Expand absolute paths',
115 array( 'foo { prop: url(/w/skin/images/bar.png); }', false, 'http://example.org/quux', false ),
116 'foo { prop: url(http://wiki.example.org/w/skin/images/bar.png); }',
117 ),
118 );
119 }
120
121 /**
122 * Seperated because they are currently broken (bug 35492)
123 *
124 * @group Broken
125 * @dataProvider provideStringCases
126 */
127 function testMinifyWithCSSStringValues( $code, $expectedOutput ) {
128 $this->testMinifyOutput( $code, $expectedOutput );
129 }
130
131 function provideStringCases() {
132 return array(
133 // String values should be respected
134 // - More than one space in a string value
135 array( 'foo { content: " "; }', 'foo{content:" "}' ),
136 // - Using a tab in a string value (turns into a space)
137 array( "foo { content: '\t'; }", "foo{content:'\t'}" ),
138 // - Using css-like syntax in string values
139 array( 'foo::after { content: "{;}"; position: absolute; }', 'foo::after{content:"{;}";position:absolute}' ),
140 );
141 }
142 }