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