Merge "SpecialMyLanguage: Get content language from service"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / JavaScriptMinifierTest.php
1 <?php
2
3 class JavaScriptMinifierTest extends PHPUnit\Framework\TestCase {
4
5 use MediaWikiCoversValidator;
6
7 protected function tearDown() {
8 parent::tearDown();
9 // Reset
10 $this->setMaxLineLength( 1000 );
11 }
12
13 private function setMaxLineLength( $val ) {
14 $classReflect = new ReflectionClass( JavaScriptMinifier::class );
15 $propertyReflect = $classReflect->getProperty( 'maxLineLength' );
16 $propertyReflect->setAccessible( true );
17 $propertyReflect->setValue( JavaScriptMinifier::class, $val );
18 }
19
20 public static function provideCases() {
21 return [
22
23 // Basic whitespace and comments that should be stripped entirely
24 [ "\r\t\f \v\n\r", "" ],
25 [ "/* Foo *\n*bar\n*/", "" ],
26
27 /**
28 * Slashes used inside block comments (T28931).
29 * At some point there was a bug that caused this comment to be ended at '* /',
30 * causing /M... to be left as the beginning of a regex.
31 */
32 [
33 "/**\n * Foo\n * {\n * 'bar' : {\n * "
34 . "//Multiple rules with configurable operators\n * 'baz' : false\n * }\n */",
35 "" ],
36
37 /**
38 * ' Foo \' bar \
39 * baz \' quox ' .
40 */
41 [
42 "' Foo \\' bar \\\n baz \\' quox ' .length",
43 "' Foo \\' bar \\\n baz \\' quox '.length"
44 ],
45 [
46 "\" Foo \\\" bar \\\n baz \\\" quox \" .length",
47 "\" Foo \\\" bar \\\n baz \\\" quox \".length"
48 ],
49 [ "// Foo b/ar baz", "" ],
50 [
51 "/ Foo \\/ bar [ / \\] / ] baz / .length",
52 "/ Foo \\/ bar [ / \\] / ] baz /.length"
53 ],
54
55 // HTML comments
56 [ "<!-- Foo bar", "" ],
57 [ "<!-- Foo --> bar", "" ],
58 [ "--> Foo", "" ],
59 [ "x --> y", "x-->y" ],
60
61 // Semicolon insertion
62 [ "(function(){return\nx;})", "(function(){return\nx;})" ],
63 [ "throw\nx;", "throw\nx;" ],
64 [ "while(p){continue\nx;}", "while(p){continue\nx;}" ],
65 [ "while(p){break\nx;}", "while(p){break\nx;}" ],
66 [ "var\nx;", "var x;" ],
67 [ "x\ny;", "x\ny;" ],
68 [ "x\n++y;", "x\n++y;" ],
69 [ "x\n!y;", "x\n!y;" ],
70 [ "x\n{y}", "x\n{y}" ],
71 [ "x\n+y;", "x+y;" ],
72 [ "x\n(y);", "x(y);" ],
73 [ "5.\nx;", "5.\nx;" ],
74 [ "0xFF.\nx;", "0xFF.x;" ],
75 [ "5.3.\nx;", "5.3.x;" ],
76
77 // Cover failure case for incomplete hex literal
78 [ "0x;", false, false ],
79
80 // Cover failure case for number with no digits after E
81 [ "1.4E", false, false ],
82
83 // Cover failure case for number with several E
84 [ "1.4EE2", false, false ],
85 [ "1.4EE", false, false ],
86
87 // Cover failure case for number with several E (nonconsecutive)
88 // FIXME: This is invalid, but currently tolerated
89 [ "1.4E2E3", "1.4E2 E3", false ],
90
91 // Semicolon insertion between an expression having an inline
92 // comment after it, and a statement on the next line (T29046).
93 [
94 "var a = this //foo bar \n for ( b = 0; c < d; b++ ) {}",
95 "var a=this\nfor(b=0;c<d;b++){}"
96 ],
97
98 // Cover failure case of incomplete regexp at end of file (T75556)
99 // FIXME: This is invalid, but currently tolerated
100 [ "*/", "*/", false ],
101
102 // Cover failure case of incomplete char class in regexp (T75556)
103 // FIXME: This is invalid, but currently tolerated
104 [ "/a[b/.test", "/a[b/.test", false ],
105
106 // Cover failure case of incomplete string at end of file (T75556)
107 // FIXME: This is invalid, but currently tolerated
108 [ "'a", "'a", false ],
109
110 // Token separation
111 [ "x in y", "x in y" ],
112 [ "/x/g in y", "/x/g in y" ],
113 [ "x in 30", "x in 30" ],
114 [ "x + ++ y", "x+ ++y" ],
115 [ "x ++ + y", "x++ +y" ],
116 [ "x / /y/.exec(z)", "x/ /y/.exec(z)" ],
117
118 // State machine
119 [ "/ x/g", "/ x/g" ],
120 [ "(function(){return/ x/g})", "(function(){return/ x/g})" ],
121 [ "+/ x/g", "+/ x/g" ],
122 [ "++/ x/g", "++/ x/g" ],
123 [ "x/ x/g", "x/x/g" ],
124 [ "(/ x/g)", "(/ x/g)" ],
125 [ "if(/ x/g);", "if(/ x/g);" ],
126 [ "(x/ x/g)", "(x/x/g)" ],
127 [ "([/ x/g])", "([/ x/g])" ],
128 [ "+x/ x/g", "+x/x/g" ],
129 [ "{}/ x/g", "{}/ x/g" ],
130 [ "+{}/ x/g", "+{}/x/g" ],
131 [ "(x)/ x/g", "(x)/x/g" ],
132 [ "if(x)/ x/g", "if(x)/ x/g" ],
133 [ "for(x;x;{}/ x/g);", "for(x;x;{}/x/g);" ],
134 [ "x;x;{}/ x/g", "x;x;{}/ x/g" ],
135 [ "x:{}/ x/g", "x:{}/ x/g" ],
136 [ "switch(x){case y?z:{}/ x/g:{}/ x/g;}", "switch(x){case y?z:{}/x/g:{}/ x/g;}" ],
137 [ "function x(){}/ x/g", "function x(){}/ x/g" ],
138 [ "+function x(){}/ x/g", "+function x(){}/x/g" ],
139
140 // Multiline quoted string
141 [ "var foo=\"\\\nblah\\\n\";", "var foo=\"\\\nblah\\\n\";" ],
142
143 // Multiline quoted string followed by string with spaces
144 [
145 "var foo=\"\\\nblah\\\n\";\nvar baz = \" foo \";\n",
146 "var foo=\"\\\nblah\\\n\";var baz=\" foo \";"
147 ],
148
149 // URL in quoted string ( // is not a comment)
150 [
151 "aNode.setAttribute('href','http://foo.bar.org/baz');",
152 "aNode.setAttribute('href','http://foo.bar.org/baz');"
153 ],
154
155 // URL in quoted string after multiline quoted string
156 [
157 "var foo=\"\\\nblah\\\n\";\naNode.setAttribute('href','http://foo.bar.org/baz');",
158 "var foo=\"\\\nblah\\\n\";aNode.setAttribute('href','http://foo.bar.org/baz');"
159 ],
160
161 // Division vs. regex nastiness
162 [
163 "alert( (10+10) / '/'.charCodeAt( 0 ) + '//' );",
164 "alert((10+10)/'/'.charCodeAt(0)+'//');"
165 ],
166 [ "if(1)/a /g.exec('Pa ss');", "if(1)/a /g.exec('Pa ss');" ],
167
168 // newline insertion after 1000 chars: break after the "++", not before
169 [ str_repeat( ';', 996 ) . "if(x++);", str_repeat( ';', 996 ) . "if(x++\n);" ],
170
171 // Unicode letter characters should pass through ok in identifiers (T33187)
172 [ "var KaŝSkatolVal = {}", 'var KaŝSkatolVal={}' ],
173
174 // Per spec unicode char escape values should work in identifiers,
175 // as long as it's a valid char. In future it might get normalized.
176 [ "var Ka\\u015dSkatolVal = {}", 'var Ka\\u015dSkatolVal={}' ],
177
178 // Some structures that might look invalid at first sight
179 [ "var a = 5.;", "var a=5.;" ],
180 [ "5.0.toString();", "5.0.toString();" ],
181 [ "5..toString();", "5..toString();" ],
182 // Cover failure case for too many decimal points
183 [ "5...toString();", false ],
184 [ "5.\n.toString();", '5..toString();' ],
185
186 // Boolean minification (!0 / !1)
187 [ "var a = { b: true };", "var a={b:!0};" ],
188 [ "var a = { true: 12 };", "var a={true:12};", false ],
189 [ "a.true = 12;", "a.true=12;", false ],
190 [ "a.foo = true;", "a.foo=!0;" ],
191 [ "a.foo = false;", "a.foo=!1;" ],
192 ];
193 }
194
195 /**
196 * @dataProvider provideCases
197 * @covers JavaScriptMinifier::minify
198 * @covers JavaScriptMinifier::parseError
199 */
200 public function testMinifyOutput( $code, $expectedOutput, $expectedValid = true ) {
201 $minified = JavaScriptMinifier::minify( $code );
202
203 // JSMin+'s parser will throw an exception if output is not valid JS.
204 // suppression of warnings needed for stupid crap
205 if ( $expectedValid ) {
206 Wikimedia\suppressWarnings();
207 $parser = new JSParser();
208 Wikimedia\restoreWarnings();
209 $parser->parse( $minified, 'minify-test.js', 1 );
210 }
211
212 $this->assertEquals(
213 $expectedOutput,
214 $minified,
215 "Minified output should be in the form expected."
216 );
217 }
218
219 public static function provideLineBreaker() {
220 return [
221 [
222 // Regression tests for T34548.
223 // Must not break between 'E' and '+'.
224 'var name = 1.23456789E55;',
225 [
226 'var',
227 'name',
228 '=',
229 '1.23456789E55',
230 ';',
231 ],
232 ],
233 [
234 'var name = 1.23456789E+5;',
235 [
236 'var',
237 'name',
238 '=',
239 '1.23456789E+5',
240 ';',
241 ],
242 ],
243 [
244 'var name = 1.23456789E-5;',
245 [
246 'var',
247 'name',
248 '=',
249 '1.23456789E-5',
250 ';',
251 ],
252 ],
253 [
254 // Regression test for T201606.
255 // Must not break between 'return' and Expression.
256 <<<JAVASCRIPT
257 call( function () {
258 try {
259 } catch (e) {
260 obj = {
261 key: 1 ? 0 : {}
262 };
263 }
264 return name === 'input';
265 } );
266 JAVASCRIPT
267 ,
268 [
269 'call',
270 '(',
271 'function',
272 '(',
273 ')',
274 '{',
275 'try',
276 '{',
277 '}',
278 'catch',
279 '(',
280 'e',
281 ')',
282 '{',
283 'obj',
284 '=',
285 '{',
286 'key',
287 ':',
288 '1',
289 '?',
290 '0',
291 ':',
292 '{',
293 '}',
294 '}',
295 ';',
296 '}',
297 // The return Statement:
298 // return [no LineTerminator here] Expression
299 'return name',
300 '===',
301 "'input'",
302 ';',
303 '}',
304 ')',
305 ';',
306 ]
307 ]
308 ];
309 }
310
311 /**
312 * @dataProvider provideLineBreaker
313 * @covers JavaScriptMinifier::minify
314 */
315 public function testLineBreaker( $code, array $expectedLines ) {
316 $this->setMaxLineLength( 1 );
317 $actual = JavaScriptMinifier::minify( $code );
318 $this->assertEquals(
319 array_merge( [ '' ], $expectedLines ),
320 explode( "\n", $actual )
321 );
322 }
323 }