(bug 47070) check content model namespace on import.
[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
10 protected function setUp() {
11 parent::setUp();
12
13 $server = 'http://doc.example.org';
14
15 $this->setMwGlobals( array(
16 'wgServer' => $server,
17 'wgCanonicalServer' => $server,
18 ) );
19 }
20
21 /**
22 * @dataProvider provideMinifyCases
23 * @covers CSSMin::minify
24 */
25 public function testMinify( $code, $expectedOutput ) {
26 $minified = CSSMin::minify( $code );
27
28 $this->assertEquals( $expectedOutput, $minified, 'Minified output should be in the form expected.' );
29 }
30
31 public static function provideMinifyCases() {
32 return array(
33 // Whitespace
34 array( "\r\t\f \v\n\r", "" ),
35 array( "foo, bar {\n\tprop: value;\n}", "foo,bar{prop:value}" ),
36
37 // Loose comments
38 array( "/* foo */", "" ),
39 array( "/*******\n foo\n *******/", "" ),
40 array( "/*!\n foo\n */", "" ),
41
42 // Inline comments in various different places
43 array( "/* comment */foo, bar {\n\tprop: value;\n}", "foo,bar{prop:value}" ),
44 array( "foo/* comment */, bar {\n\tprop: value;\n}", "foo,bar{prop:value}" ),
45 array( "foo,/* comment */ bar {\n\tprop: value;\n}", "foo,bar{prop:value}" ),
46 array( "foo, bar/* comment */ {\n\tprop: value;\n}", "foo,bar{prop:value}" ),
47 array( "foo, bar {\n\t/* comment */prop: value;\n}", "foo,bar{prop:value}" ),
48 array( "foo, bar {\n\tprop: /* comment */value;\n}", "foo,bar{prop:value}" ),
49 array( "foo, bar {\n\tprop: value /* comment */;\n}", "foo,bar{prop:value }" ),
50 array( "foo, bar {\n\tprop: value; /* comment */\n}", "foo,bar{prop:value; }" ),
51
52 // Keep track of things that aren't as minified as much as they
53 // could be (bug 35493)
54 array( 'foo { prop: value ;}', 'foo{prop:value }' ),
55 array( 'foo { prop : value; }', 'foo{prop :value}' ),
56 array( 'foo { prop: value ; }', 'foo{prop:value }' ),
57 array( 'foo { font-family: "foo" , "bar"; }', 'foo{font-family:"foo" ,"bar"}' ),
58 array( "foo { src:\n\turl('foo') ,\n\turl('bar') ; }", "foo{src:url('foo') ,url('bar') }" ),
59
60 // Interesting cases with string values
61 // - Double quotes, single quotes
62 array( 'foo { content: ""; }', 'foo{content:""}' ),
63 array( "foo { content: ''; }", "foo{content:''}" ),
64 array( 'foo { content: "\'"; }', 'foo{content:"\'"}' ),
65 array( "foo { content: '\"'; }", "foo{content:'\"'}" ),
66 // - Whitespace in string values
67 array( 'foo { content: " "; }', 'foo{content:" "}' ),
68 );
69 }
70
71 /**
72 * This tests funky parameters to CSSMin::remap. testRemapRemapping tests the basic functionality.
73 *
74 * @dataProvider provideRemapCases
75 * @covers CSSMin::remap
76 */
77 public function testRemap( $message, $params, $expectedOutput ) {
78 $remapped = call_user_func_array( 'CSSMin::remap', $params );
79
80 $messageAdd = " Case: $message";
81 $this->assertEquals( $expectedOutput, $remapped, 'CSSMin::remap should return the expected url form.' . $messageAdd );
82 }
83
84 public static function provideRemapCases() {
85 // Parameter signature:
86 // CSSMin::remap( $code, $local, $remote, $embedData = true )
87 return array(
88 array(
89 'Simple case',
90 array( 'foo { prop: url(bar.png); }', false, 'http://example.org', false ),
91 'foo { prop: url(http://example.org/bar.png); }',
92 ),
93 array(
94 'Without trailing slash',
95 array( 'foo { prop: url(../bar.png); }', false, 'http://example.org/quux', false ),
96 'foo { prop: url(http://example.org/quux/../bar.png); }',
97 ),
98 array(
99 'With trailing slash on remote (bug 27052)',
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 'Guard against stripping double slashes from query',
105 array( 'foo { prop: url(bar.png?corge=//grault); }', false, 'http://example.org/quux/', false ),
106 'foo { prop: url(http://example.org/quux/bar.png?corge=//grault); }',
107 ),
108 array(
109 'Expand absolute paths',
110 array( 'foo { prop: url(/w/skin/images/bar.png); }', false, 'http://example.org/quux', false ),
111 'foo { prop: url(http://doc.example.org/w/skin/images/bar.png); }',
112 ),
113 );
114 }
115
116 /**
117 * This tests basic functionality of CSSMin::remap. testRemapRemapping tests funky parameters.
118 *
119 * @dataProvider provideRemapRemappingCases
120 * @covers CSSMin::remap
121 */
122 public function testRemapRemapping( $message, $input, $expectedOutput ) {
123 $localPath = __DIR__ . '/../../data/cssmin/';
124 $remotePath = 'http://localhost/w/';
125
126 $realOutput = CSSMin::remap( $input, $localPath, $remotePath );
127
128 $this->assertEquals(
129 $expectedOutput,
130 preg_replace( '/\d+-\d+-\d+T\d+:\d+:\d+Z/', 'timestamp', $realOutput ),
131 "CSSMin::remap: $message"
132 );
133 }
134
135 public static function provideRemapRemappingCases() {
136 // red.gif and green.gif are one-pixel 35-byte GIFs.
137 // large.png is a 35K PNG that should be non-embeddable.
138 // Full paths start with http://localhost/w/.
139 // Timestamps in output are replaced with 'timestamp'.
140
141 // data: URIs for red.gif and green.gif
142 $red = 'data:image/gif;base64,R0lGODlhAQABAIAAAP8AADAAACwAAAAAAQABAAACAkQBADs=';
143 $green = 'data:image/gif;base64,R0lGODlhAQABAIAAAACAADAAACwAAAAAAQABAAACAkQBADs=';
144
145 return array(
146 array(
147 'Regular file',
148 'foo { background: url(red.gif); }',
149 'foo { background: url(http://localhost/w/red.gif?timestamp); }',
150 ),
151 array(
152 'Remote URL',
153 'foo { background: url(http://example.org/w/foo.png); }',
154 'foo { background: url(http://example.org/w/foo.png); }',
155 ),
156 array(
157 'Embedded file',
158 'foo { /* @embed */ background: url(red.gif); }',
159 "foo { background: url($red); background: url(http://localhost/w/red.gif?timestamp)!ie; }",
160 ),
161 array(
162 'Can not embed remote URLs',
163 'foo { /* @embed */ background: url(http://example.org/w/foo.png); }',
164 'foo { background: url(http://example.org/w/foo.png); }',
165 ),
166 array(
167 'Embedded file (inline @embed)',
168 'foo { background: /* @embed */ url(red.gif); }',
169 "foo { background: url($red); background: url(http://localhost/w/red.gif?timestamp)!ie; }",
170 ),
171 array(
172 'Can not embed large files',
173 'foo { /* @embed */ background: url(large.png); }',
174 "foo { background: url(http://localhost/w/large.png?timestamp); }",
175 ),
176 array(
177 'Two regular files in one rule',
178 'foo { background: url(red.gif), url(green.gif); }',
179 'foo { background: url(http://localhost/w/red.gif?timestamp), url(http://localhost/w/green.gif?timestamp); }',
180 ),
181 array(
182 'Two embedded files in one rule',
183 'foo { /* @embed */ background: url(red.gif), url(green.gif); }',
184 "foo { background: url($red), url($green); background: url(http://localhost/w/red.gif?timestamp), url(http://localhost/w/green.gif?timestamp)!ie; }",
185 ),
186 array(
187 'Two embedded files in one rule (inline @embed)',
188 'foo { background: /* @embed */ url(red.gif), /* @embed */ url(green.gif); }',
189 "foo { background: url($red), url($green); background: url(http://localhost/w/red.gif?timestamp), url(http://localhost/w/green.gif?timestamp)!ie; }",
190 ),
191 array(
192 'Two embedded files in one rule (inline @embed), one too large',
193 'foo { background: /* @embed */ url(red.gif), /* @embed */ url(large.png); }',
194 "foo { background: url($red), url(http://localhost/w/large.png?timestamp); background: url(http://localhost/w/red.gif?timestamp), url(http://localhost/w/large.png?timestamp)!ie; }",
195 ),
196 array(
197 'Practical example with some noise',
198 'foo { /* @embed */ background: #f9f9f9 url(red.gif) 0 0 no-repeat; }',
199 "foo { background: #f9f9f9 url($red) 0 0 no-repeat; background: #f9f9f9 url(http://localhost/w/red.gif?timestamp) 0 0 no-repeat!ie; }",
200 ),
201 array(
202 'Does not mess with other properties',
203 'foo { color: red; background: url(red.gif); font-size: small; }',
204 'foo { color: red; background: url(http://localhost/w/red.gif?timestamp); font-size: small; }',
205 ),
206 array(
207 'Spacing and miscellanea not changed (1)',
208 'foo { background: url(red.gif); }',
209 'foo { background: url(http://localhost/w/red.gif?timestamp); }',
210 ),
211 array(
212 'Spacing and miscellanea not changed (2)',
213 'foo {background:url(red.gif)}',
214 'foo {background:url(http://localhost/w/red.gif?timestamp)}',
215 ),
216 array(
217 'Spaces within url() parentheses are ignored',
218 'foo { background: url( red.gif ); }',
219 'foo { background: url(http://localhost/w/red.gif?timestamp); }',
220 ),
221 );
222 }
223
224 /**
225 * Seperated because they are currently broken (bug 35492)
226 *
227 * @group Broken
228 * @dataProvider provideStringCases
229 * @covers CSSMin::remap
230 */
231 public function testMinifyWithCSSStringValues( $code, $expectedOutput ) {
232 $this->testMinifyOutput( $code, $expectedOutput );
233 }
234
235 public static function provideStringCases() {
236 return array(
237 // String values should be respected
238 // - More than one space in a string value
239 array( 'foo { content: " "; }', 'foo{content:" "}' ),
240 // - Using a tab in a string value (turns into a space)
241 array( "foo { content: '\t'; }", "foo{content:'\t'}" ),
242 // - Using css-like syntax in string values
243 array( 'foo::after { content: "{;}"; position: absolute; }', 'foo::after{content:"{;}";position:absolute}' ),
244 );
245 }
246 }