Merge "Chinese Conversion Table Update 2016-6"
[lhc/web/wiklou.git] / tests / phpunit / includes / WebRequestTest.php
1 <?php
2
3 /**
4 * @group WebRequest
5 */
6 class WebRequestTest extends MediaWikiTestCase {
7 protected $oldServer;
8
9 protected function setUp() {
10 parent::setUp();
11
12 $this->oldServer = $_SERVER;
13 IP::clearCaches();
14 }
15
16 protected function tearDown() {
17 $_SERVER = $this->oldServer;
18 IP::clearCaches();
19
20 parent::tearDown();
21 }
22
23 /**
24 * @dataProvider provideDetectServer
25 * @covers WebRequest::detectServer
26 * @covers WebRequest::detectProtocol
27 */
28 public function testDetectServer( $expected, $input, $description ) {
29 $this->setMwGlobals( 'wgAssumeProxiesUseDefaultProtocolPorts', true );
30
31 $_SERVER = $input;
32 $result = WebRequest::detectServer();
33 $this->assertEquals( $expected, $result, $description );
34 }
35
36 public static function provideDetectServer() {
37 return [
38 [
39 'http://x',
40 [
41 'HTTP_HOST' => 'x'
42 ],
43 'Host header'
44 ],
45 [
46 'https://x',
47 [
48 'HTTP_HOST' => 'x',
49 'HTTPS' => 'on',
50 ],
51 'Host header with secure'
52 ],
53 [
54 'http://x',
55 [
56 'HTTP_HOST' => 'x',
57 'SERVER_PORT' => 80,
58 ],
59 'Default SERVER_PORT',
60 ],
61 [
62 'http://x',
63 [
64 'HTTP_HOST' => 'x',
65 'HTTPS' => 'off',
66 ],
67 'Secure off'
68 ],
69 [
70 'https://x',
71 [
72 'HTTP_HOST' => 'x',
73 'HTTP_X_FORWARDED_PROTO' => 'https',
74 ],
75 'Forwarded HTTPS'
76 ],
77 [
78 'https://x',
79 [
80 'HTTP_HOST' => 'x',
81 'HTTPS' => 'off',
82 'SERVER_PORT' => '81',
83 'HTTP_X_FORWARDED_PROTO' => 'https',
84 ],
85 'Forwarded HTTPS'
86 ],
87 [
88 'http://y',
89 [
90 'SERVER_NAME' => 'y',
91 ],
92 'Server name'
93 ],
94 [
95 'http://x',
96 [
97 'HTTP_HOST' => 'x',
98 'SERVER_NAME' => 'y',
99 ],
100 'Host server name precedence'
101 ],
102 [
103 'http://[::1]:81',
104 [
105 'HTTP_HOST' => '[::1]',
106 'SERVER_NAME' => '::1',
107 'SERVER_PORT' => '81',
108 ],
109 'Apache bug 26005'
110 ],
111 [
112 'http://localhost',
113 [
114 'SERVER_NAME' => '[2001'
115 ],
116 'Kind of like lighttpd per commit message in MW r83847',
117 ],
118 [
119 'http://[2a01:e35:2eb4:1::2]:777',
120 [
121 'SERVER_NAME' => '[2a01:e35:2eb4:1::2]:777'
122 ],
123 'Possible lighttpd environment per bug 14977 comment 13',
124 ],
125 ];
126 }
127
128 protected function mockWebRequest( $data = [] ) {
129 // Cannot use PHPUnit getMockBuilder() as it does not support
130 // overriding protected properties afterwards
131 $reflection = new ReflectionClass( 'WebRequest' );
132 $req = $reflection->newInstanceWithoutConstructor();
133
134 $prop = $reflection->getProperty( 'data' );
135 $prop->setAccessible( true );
136 $prop->setValue( $req, $data );
137
138 return $req;
139 }
140
141 /**
142 * @covers WebRequest::getElapsedTime
143 */
144 public function testGetElapsedTime() {
145 $req = new FauxRequest();
146 $this->assertGreaterThanOrEqual( 0.0, $req->getElapsedTime() );
147 $this->assertEquals( 0.0, $req->getElapsedTime(), '', /*delta*/ 0.2 );
148 }
149
150 /**
151 * @covers WebRequest::getVal
152 * @covers WebRequest::getGPCVal
153 * @covers WebRequest::normalizeUnicode
154 */
155 public function testGetValNormal() {
156 // Assert that WebRequest normalises GPC data using UtfNormal\Validator
157 $input = "a \x00 null";
158 $normal = "a \xef\xbf\xbd null";
159 $req = new FauxRequest( [ 'x' => $input, 'y' => [ $input, $input ] ] );
160 $this->assertSame( $normal, $req->getVal( 'x' ) );
161 $this->assertNotSame( $input, $req->getVal( 'x' ) );
162 $this->assertSame( [ $normal, $normal ], $req->getArray( 'y' ) );
163 }
164
165 /**
166 * @covers WebRequest::getVal
167 * @covers WebRequest::getGPCVal
168 */
169 public function testGetVal() {
170 $req = new FauxRequest( [ 'x' => 'Value', 'y' => [ 'a' ], 'crlf' => "A\r\nb" ] );
171 $this->assertSame( 'Value', $req->getVal( 'x' ), 'Simple value' );
172 $this->assertSame( null, $req->getVal( 'z' ), 'Not found' );
173 $this->assertSame( null, $req->getVal( 'y' ), 'Array is ignored' );
174 $this->assertSame( "A\r\nb", $req->getVal( 'crlf' ), 'CRLF' );
175 }
176
177 /**
178 * @covers WebRequest::getRawVal
179 */
180 public function testGetRawVal() {
181 $req = new FauxRequest( [
182 'x' => 'Value',
183 'y' => [ 'a' ],
184 'crlf' => "A\r\nb"
185 ] );
186 $this->assertSame( 'Value', $req->getRawVal( 'x' ) );
187 $this->assertSame( null, $req->getRawVal( 'z' ), 'Not found' );
188 $this->assertSame( null, $req->getRawVal( 'y' ), 'Array is ignored' );
189 $this->assertSame( "A\r\nb", $req->getRawVal( 'crlf' ), 'CRLF' );
190 }
191
192 /**
193 * @covers WebRequest::getArray
194 */
195 public function testGetArray() {
196 $req = new FauxRequest( [ 'x' => 'Value', 'y' => [ 'a', 'b' ] ] );
197 $this->assertSame( [ 'Value' ], $req->getArray( 'x' ), 'Value becomes array' );
198 $this->assertSame( null, $req->getArray( 'z' ), 'Not found' );
199 $this->assertSame( [ 'a', 'b' ], $req->getArray( 'y' ) );
200 }
201
202 /**
203 * @covers WebRequest::getIntArray
204 */
205 public function testGetIntArray() {
206 $req = new FauxRequest( [ 'x' => [ 'Value' ], 'y' => [ '0', '4.2', '-2' ] ] );
207 $this->assertSame( [ 0 ], $req->getIntArray( 'x' ), 'Text becomes 0' );
208 $this->assertSame( null, $req->getIntArray( 'z' ), 'Not found' );
209 $this->assertSame( [ 0, 4, -2 ], $req->getIntArray( 'y' ) );
210 }
211
212 /**
213 * @covers WebRequest::getInt
214 */
215 public function testGetInt() {
216 $req = new FauxRequest( [
217 'x' => 'Value',
218 'y' => [ 'a' ],
219 'zero' => '0',
220 'answer' => '4.2',
221 'neg' => '-2',
222 ] );
223 $this->assertSame( 0, $req->getInt( 'x' ), 'Text' );
224 $this->assertSame( 0, $req->getInt( 'y' ), 'Array' );
225 $this->assertSame( 0, $req->getInt( 'z' ), 'Not found' );
226 $this->assertSame( 0, $req->getInt( 'zero' ) );
227 $this->assertSame( 4, $req->getInt( 'answer' ) );
228 $this->assertSame( -2, $req->getInt( 'neg' ) );
229 }
230
231 /**
232 * @covers WebRequest::getIntOrNull
233 */
234 public function testGetIntOrNull() {
235 $req = new FauxRequest( [
236 'x' => 'Value',
237 'y' => [ 'a' ],
238 'zero' => '0',
239 'answer' => '4.2',
240 'neg' => '-2',
241 ] );
242 $this->assertSame( null, $req->getIntOrNull( 'x' ), 'Text' );
243 $this->assertSame( null, $req->getIntOrNull( 'y' ), 'Array' );
244 $this->assertSame( null, $req->getIntOrNull( 'z' ), 'Not found' );
245 $this->assertSame( 0, $req->getIntOrNull( 'zero' ) );
246 $this->assertSame( 4, $req->getIntOrNull( 'answer' ) );
247 $this->assertSame( -2, $req->getIntOrNull( 'neg' ) );
248 }
249
250 /**
251 * @covers WebRequest::getFloat
252 */
253 public function testGetFloat() {
254 $req = new FauxRequest( [
255 'x' => 'Value',
256 'y' => [ 'a' ],
257 'zero' => '0',
258 'answer' => '4.2',
259 'neg' => '-2',
260 ] );
261 $this->assertSame( 0.0, $req->getFloat( 'x' ), 'Text' );
262 $this->assertSame( 0.0, $req->getFloat( 'y' ), 'Array' );
263 $this->assertSame( 0.0, $req->getFloat( 'z' ), 'Not found' );
264 $this->assertSame( 0.0, $req->getFloat( 'zero' ) );
265 $this->assertSame( 4.2, $req->getFloat( 'answer' ) );
266 $this->assertSame( -2.0, $req->getFloat( 'neg' ) );
267 }
268
269 /**
270 * @covers WebRequest::getBool
271 */
272 public function testGetBool() {
273 $req = new FauxRequest( [
274 'x' => 'Value',
275 'y' => [ 'a' ],
276 'zero' => '0',
277 'f' => 'false',
278 't' => 'true',
279 ] );
280 $this->assertSame( true, $req->getBool( 'x' ), 'Text' );
281 $this->assertSame( false, $req->getBool( 'y' ), 'Array' );
282 $this->assertSame( false, $req->getBool( 'z' ), 'Not found' );
283 $this->assertSame( false, $req->getBool( 'zero' ) );
284 $this->assertSame( true, $req->getBool( 'f' ) );
285 $this->assertSame( true, $req->getBool( 't' ) );
286 }
287
288 /**
289 * @covers WebRequest::getFuzzyBool
290 */
291 public function testGetFuzzyBool() {
292 $req = new FauxRequest( [ 'x' => 'Value', 'f' => 'false', 't' => 'true' ] );
293 $this->assertSame( true, $req->getFuzzyBool( 'x' ), 'Text' );
294 $this->assertSame( false, $req->getFuzzyBool( 'z' ), 'Not found' );
295 $this->assertSame( false, $req->getFuzzyBool( 'f' ) );
296 $this->assertSame( true, $req->getFuzzyBool( 't' ) );
297 }
298
299 /**
300 * @covers WebRequest::getCheck
301 */
302 public function testGetCheck() {
303 $req = new FauxRequest( [ 'x' => 'Value', 'zero' => '0' ] );
304 $this->assertSame( false, $req->getCheck( 'z' ), 'Not found' );
305 $this->assertSame( true, $req->getCheck( 'x' ), 'Text' );
306 $this->assertSame( true, $req->getCheck( 'zero' ) );
307 }
308
309 /**
310 * @covers WebRequest::getText
311 */
312 public function testGetText() {
313 // FauxRequest overrides getText
314 $req = $this->mockWebRequest( [ 'crlf' => "Va\r\nlue" ] );
315 $this->assertSame( "Va\nlue", $req->getText( 'crlf' ), 'CR stripped' );
316 }
317
318 /**
319 * @covers WebRequest::getValues
320 */
321 public function testGetValues() {
322 $values = [ 'x' => 'Value', 'y' => '' ];
323 // FauxRequest overrides getValues
324 $req = $this->mockWebRequest( $values );
325 $this->assertSame( $values, $req->getValues() );
326 $this->assertSame( [ 'x' => 'Value' ], $req->getValues( 'x' ), 'Specific keys' );
327 }
328
329 /**
330 * @covers WebRequest::getValueNames
331 */
332 public function testGetValueNames() {
333 // FauxRequest overrides getValues
334 $req = new FauxRequest( [ 'x' => 'Value', 'y' => '' ] );
335 $this->assertSame( [ 'x', 'y' ], $req->getValueNames() );
336 $this->assertSame( [ 'x' ], $req->getValueNames( [ 'y' ] ), 'Exclude keys' );
337 }
338
339 /**
340 * @dataProvider provideGetIP
341 * @covers WebRequest::getIP
342 */
343 public function testGetIP( $expected, $input, $squid, $xffList, $private, $description ) {
344 $_SERVER = $input;
345 $this->setMwGlobals( [
346 'wgSquidServersNoPurge' => $squid,
347 'wgUsePrivateIPs' => $private,
348 'wgHooks' => [
349 'IsTrustedProxy' => [
350 function ( &$ip, &$trusted ) use ( $xffList ) {
351 $trusted = $trusted || in_array( $ip, $xffList );
352 return true;
353 }
354 ]
355 ]
356 ] );
357
358 $request = new WebRequest();
359 $result = $request->getIP();
360 $this->assertEquals( $expected, $result, $description );
361 }
362
363 public static function provideGetIP() {
364 return [
365 [
366 '127.0.0.1',
367 [
368 'REMOTE_ADDR' => '127.0.0.1'
369 ],
370 [],
371 [],
372 false,
373 'Simple IPv4'
374 ],
375 [
376 '::1',
377 [
378 'REMOTE_ADDR' => '::1'
379 ],
380 [],
381 [],
382 false,
383 'Simple IPv6'
384 ],
385 [
386 '12.0.0.1',
387 [
388 'REMOTE_ADDR' => 'abcd:0001:002:03:4:555:6666:7777',
389 'HTTP_X_FORWARDED_FOR' => '12.0.0.1, abcd:0001:002:03:4:555:6666:7777',
390 ],
391 [ 'ABCD:1:2:3:4:555:6666:7777' ],
392 [],
393 false,
394 'IPv6 normalisation'
395 ],
396 [
397 '12.0.0.3',
398 [
399 'REMOTE_ADDR' => '12.0.0.1',
400 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
401 ],
402 [ '12.0.0.1', '12.0.0.2' ],
403 [],
404 false,
405 'With X-Forwaded-For'
406 ],
407 [
408 '12.0.0.1',
409 [
410 'REMOTE_ADDR' => '12.0.0.1',
411 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
412 ],
413 [],
414 [],
415 false,
416 'With X-Forwaded-For and disallowed server'
417 ],
418 [
419 '12.0.0.2',
420 [
421 'REMOTE_ADDR' => '12.0.0.1',
422 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
423 ],
424 [ '12.0.0.1' ],
425 [],
426 false,
427 'With multiple X-Forwaded-For and only one allowed server'
428 ],
429 [
430 '10.0.0.3',
431 [
432 'REMOTE_ADDR' => '12.0.0.2',
433 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
434 ],
435 [ '12.0.0.1', '12.0.0.2' ],
436 [],
437 false,
438 'With X-Forwaded-For and private IP (from cache proxy)'
439 ],
440 [
441 '10.0.0.4',
442 [
443 'REMOTE_ADDR' => '12.0.0.2',
444 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
445 ],
446 [ '12.0.0.1', '12.0.0.2', '10.0.0.3' ],
447 [],
448 true,
449 'With X-Forwaded-For and private IP (allowed)'
450 ],
451 [
452 '10.0.0.4',
453 [
454 'REMOTE_ADDR' => '12.0.0.2',
455 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
456 ],
457 [ '12.0.0.1', '12.0.0.2' ],
458 [ '10.0.0.3' ],
459 true,
460 'With X-Forwaded-For and private IP (allowed)'
461 ],
462 [
463 '10.0.0.3',
464 [
465 'REMOTE_ADDR' => '12.0.0.2',
466 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
467 ],
468 [ '12.0.0.1', '12.0.0.2' ],
469 [ '10.0.0.3' ],
470 false,
471 'With X-Forwaded-For and private IP (disallowed)'
472 ],
473 [
474 '12.0.0.3',
475 [
476 'REMOTE_ADDR' => '12.0.0.1',
477 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
478 ],
479 [],
480 [ '12.0.0.1', '12.0.0.2' ],
481 false,
482 'With X-Forwaded-For'
483 ],
484 [
485 '12.0.0.2',
486 [
487 'REMOTE_ADDR' => '12.0.0.1',
488 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
489 ],
490 [],
491 [ '12.0.0.1' ],
492 false,
493 'With multiple X-Forwaded-For and only one allowed server'
494 ],
495 [
496 '12.0.0.2',
497 [
498 'REMOTE_ADDR' => '12.0.0.2',
499 'HTTP_X_FORWARDED_FOR' => '10.0.0.3, 12.0.0.2'
500 ],
501 [],
502 [ '12.0.0.2' ],
503 false,
504 'With X-Forwaded-For and private IP and hook (disallowed)'
505 ],
506 [
507 '12.0.0.1',
508 [
509 'REMOTE_ADDR' => 'abcd:0001:002:03:4:555:6666:7777',
510 'HTTP_X_FORWARDED_FOR' => '12.0.0.1, abcd:0001:002:03:4:555:6666:7777',
511 ],
512 [ 'ABCD:1:2:3::/64' ],
513 [],
514 false,
515 'IPv6 CIDR'
516 ],
517 [
518 '12.0.0.3',
519 [
520 'REMOTE_ADDR' => '12.0.0.1',
521 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
522 ],
523 [ '12.0.0.0/24' ],
524 [],
525 false,
526 'IPv4 CIDR'
527 ],
528 ];
529 }
530
531 /**
532 * @expectedException MWException
533 * @covers WebRequest::getIP
534 */
535 public function testGetIpLackOfRemoteAddrThrowAnException() {
536 // ensure that local install state doesn't interfere with test
537 $this->setMwGlobals( [
538 'wgSquidServersNoPurge' => [],
539 'wgSquidServers' => [],
540 'wgUsePrivateIPs' => false,
541 'wgHooks' => [],
542 ] );
543
544 $request = new WebRequest();
545 # Next call throw an exception about lacking an IP
546 $request->getIP();
547 }
548
549 public static function provideLanguageData() {
550 return [
551 [ '', [], 'Empty Accept-Language header' ],
552 [ 'en', [ 'en' => 1 ], 'One language' ],
553 [ 'en, ar', [ 'en' => 1, 'ar' => 1 ], 'Two languages listed in appearance order.' ],
554 [
555 'zh-cn,zh-tw',
556 [ 'zh-cn' => 1, 'zh-tw' => 1 ],
557 'Two equally prefered languages, listed in appearance order per rfc3282. Checks c9119'
558 ],
559 [
560 'es, en; q=0.5',
561 [ 'es' => 1, 'en' => '0.5' ],
562 'Spanish as first language and English and second'
563 ],
564 [ 'en; q=0.5, es', [ 'es' => 1, 'en' => '0.5' ], 'Less prefered language first' ],
565 [ 'fr, en; q=0.5, es', [ 'fr' => 1, 'es' => 1, 'en' => '0.5' ], 'Three languages' ],
566 [ 'en; q=0.5, es', [ 'es' => 1, 'en' => '0.5' ], 'Two languages' ],
567 [ 'en, zh;q=0', [ 'en' => 1 ], "It's Chinese to me" ],
568 [
569 'es; q=1, pt;q=0.7, it; q=0.6, de; q=0.1, ru;q=0',
570 [ 'es' => '1', 'pt' => '0.7', 'it' => '0.6', 'de' => '0.1' ],
571 'Preference for Romance languages'
572 ],
573 [
574 'en-gb, en-us; q=1',
575 [ 'en-gb' => 1, 'en-us' => '1' ],
576 'Two equally prefered English variants'
577 ],
578 [ '_', [], 'Invalid input' ],
579 ];
580 }
581
582 /**
583 * @dataProvider provideLanguageData
584 * @covers WebRequest::getAcceptLang
585 */
586 public function testAcceptLang( $acceptLanguageHeader, $expectedLanguages, $description ) {
587 $_SERVER = [ 'HTTP_ACCEPT_LANGUAGE' => $acceptLanguageHeader ];
588 $request = new WebRequest();
589 $this->assertSame( $request->getAcceptLang(), $expectedLanguages, $description );
590 }
591 }