Merge "add IGNORE INDEX option to mysql database handler"
[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 $prop = $reflection->getProperty( 'requestTime' );
139 $prop->setAccessible( true );
140 $prop->setValue( $req, microtime( true ) );
141
142 return $req;
143 }
144
145 /**
146 * @covers WebRequest::getElapsedTime
147 */
148 public function testGetElapsedTime() {
149 $req = $this->mockWebRequest();
150 $this->assertGreaterThanOrEqual( 0.0, $req->getElapsedTime() );
151 $this->assertEquals( 0.0, $req->getElapsedTime(), '', /*delta*/ 0.2 );
152 }
153
154 /**
155 * @covers WebRequest::getVal
156 * @covers WebRequest::getGPCVal
157 * @covers WebRequest::normalizeUnicode
158 */
159 public function testGetValNormal() {
160 // Assert that WebRequest normalises GPC data using UtfNormal\Validator
161 $input = "a \x00 null";
162 $normal = "a \xef\xbf\xbd null";
163 $req = $this->mockWebRequest( [ 'x' => $input, 'y' => [ $input, $input ] ] );
164 $this->assertSame( $normal, $req->getVal( 'x' ) );
165 $this->assertNotSame( $input, $req->getVal( 'x' ) );
166 $this->assertSame( [ $normal, $normal ], $req->getArray( 'y' ) );
167 }
168
169 /**
170 * @covers WebRequest::getVal
171 * @covers WebRequest::getGPCVal
172 */
173 public function testGetVal() {
174 $req = $this->mockWebRequest( [ 'x' => 'Value', 'y' => [ 'a' ], 'crlf' => "A\r\nb" ] );
175 $this->assertSame( 'Value', $req->getVal( 'x' ), 'Simple value' );
176 $this->assertSame( null, $req->getVal( 'z' ), 'Not found' );
177 $this->assertSame( null, $req->getVal( 'y' ), 'Array is ignored' );
178 $this->assertSame( "A\r\nb", $req->getVal( 'crlf' ), 'CRLF' );
179 }
180
181 /**
182 * @covers WebRequest::getRawVal
183 */
184 public function testGetRawVal() {
185 $req = $this->mockWebRequest( [
186 'x' => 'Value',
187 'y' => [ 'a' ],
188 'crlf' => "A\r\nb"
189 ] );
190 $this->assertSame( 'Value', $req->getRawVal( 'x' ) );
191 $this->assertSame( null, $req->getRawVal( 'z' ), 'Not found' );
192 $this->assertSame( null, $req->getRawVal( 'y' ), 'Array is ignored' );
193 $this->assertSame( "A\r\nb", $req->getRawVal( 'crlf' ), 'CRLF' );
194 }
195
196 /**
197 * @covers WebRequest::getArray
198 */
199 public function testGetArray() {
200 $req = $this->mockWebRequest( [ 'x' => 'Value', 'y' => [ 'a', 'b' ] ] );
201 $this->assertSame( [ 'Value' ], $req->getArray( 'x' ), 'Value becomes array' );
202 $this->assertSame( null, $req->getArray( 'z' ), 'Not found' );
203 $this->assertSame( [ 'a', 'b' ], $req->getArray( 'y' ) );
204 }
205
206 /**
207 * @covers WebRequest::getIntArray
208 */
209 public function testGetIntArray() {
210 $req = $this->mockWebRequest( [ 'x' => [ 'Value' ], 'y' => [ '0', '4.2', '-2' ] ] );
211 $this->assertSame( [ 0 ], $req->getIntArray( 'x' ), 'Text becomes 0' );
212 $this->assertSame( null, $req->getIntArray( 'z' ), 'Not found' );
213 $this->assertSame( [ 0, 4, -2 ], $req->getIntArray( 'y' ) );
214 }
215
216 /**
217 * @covers WebRequest::getInt
218 */
219 public function testGetInt() {
220 $req = $this->mockWebRequest( [
221 'x' => 'Value',
222 'y' => [ 'a' ],
223 'zero' => '0',
224 'answer' => '4.2',
225 'neg' => '-2',
226 ] );
227 $this->assertSame( 0, $req->getInt( 'x' ), 'Text' );
228 $this->assertSame( 0, $req->getInt( 'y' ), 'Array' );
229 $this->assertSame( 0, $req->getInt( 'z' ), 'Not found' );
230 $this->assertSame( 0, $req->getInt( 'zero' ) );
231 $this->assertSame( 4, $req->getInt( 'answer' ) );
232 $this->assertSame( -2, $req->getInt( 'neg' ) );
233 }
234
235 /**
236 * @covers WebRequest::getIntOrNull
237 */
238 public function testGetIntOrNull() {
239 $req = $this->mockWebRequest( [
240 'x' => 'Value',
241 'y' => [ 'a' ],
242 'zero' => '0',
243 'answer' => '4.2',
244 'neg' => '-2',
245 ] );
246 $this->assertSame( null, $req->getIntOrNull( 'x' ), 'Text' );
247 $this->assertSame( null, $req->getIntOrNull( 'y' ), 'Array' );
248 $this->assertSame( null, $req->getIntOrNull( 'z' ), 'Not found' );
249 $this->assertSame( 0, $req->getIntOrNull( 'zero' ) );
250 $this->assertSame( 4, $req->getIntOrNull( 'answer' ) );
251 $this->assertSame( -2, $req->getIntOrNull( 'neg' ) );
252 }
253
254 /**
255 * @covers WebRequest::getFloat
256 */
257 public function testGetFloat() {
258 $req = $this->mockWebRequest( [
259 'x' => 'Value',
260 'y' => [ 'a' ],
261 'zero' => '0',
262 'answer' => '4.2',
263 'neg' => '-2',
264 ] );
265 $this->assertSame( 0.0, $req->getFloat( 'x' ), 'Text' );
266 $this->assertSame( 0.0, $req->getFloat( 'y' ), 'Array' );
267 $this->assertSame( 0.0, $req->getFloat( 'z' ), 'Not found' );
268 $this->assertSame( 0.0, $req->getFloat( 'zero' ) );
269 $this->assertSame( 4.2, $req->getFloat( 'answer' ) );
270 $this->assertSame( -2.0, $req->getFloat( 'neg' ) );
271 }
272
273 /**
274 * @covers WebRequest::getBool
275 */
276 public function testGetBool() {
277 $req = $this->mockWebRequest( [
278 'x' => 'Value',
279 'y' => [ 'a' ],
280 'zero' => '0',
281 'f' => 'false',
282 't' => 'true',
283 ] );
284 $this->assertSame( true, $req->getBool( 'x' ), 'Text' );
285 $this->assertSame( false, $req->getBool( 'y' ), 'Array' );
286 $this->assertSame( false, $req->getBool( 'z' ), 'Not found' );
287 $this->assertSame( false, $req->getBool( 'zero' ) );
288 $this->assertSame( true, $req->getBool( 'f' ) );
289 $this->assertSame( true, $req->getBool( 't' ) );
290 }
291
292 public static function provideFuzzyBool() {
293 return [
294 [ 'Text', true ],
295 [ '', false, '(empty string)' ],
296 [ '0', false ],
297 [ '1', true ],
298 [ 'false', false ],
299 [ 'true', true ],
300 [ 'False', false ],
301 [ 'True', true ],
302 [ 'FALSE', false ],
303 [ 'TRUE', true ],
304 ];
305 }
306
307 /**
308 * @dataProvider provideFuzzyBool
309 * @covers WebRequest::getFuzzyBool
310 */
311 public function testGetFuzzyBool( $value, $expected, $message = null ) {
312 $req = $this->mockWebRequest( [ 'x' => $value ] );
313 $this->assertSame( $expected, $req->getFuzzyBool( 'x' ), $message ?: "Value: '$value'" );
314 }
315
316 /**
317 * @covers WebRequest::getFuzzyBool
318 */
319 public function testGetFuzzyBoolDefault() {
320 $req = $this->mockWebRequest();
321 $this->assertSame( false, $req->getFuzzyBool( 'z' ), 'Not found' );
322 }
323
324 /**
325 * @covers WebRequest::getCheck
326 */
327 public function testGetCheck() {
328 $req = $this->mockWebRequest( [ 'x' => 'Value', 'zero' => '0' ] );
329 $this->assertSame( false, $req->getCheck( 'z' ), 'Not found' );
330 $this->assertSame( true, $req->getCheck( 'x' ), 'Text' );
331 $this->assertSame( true, $req->getCheck( 'zero' ) );
332 }
333
334 /**
335 * @covers WebRequest::getText
336 */
337 public function testGetText() {
338 // Avoid FauxRequest (overrides getText)
339 $req = $this->mockWebRequest( [ 'crlf' => "Va\r\nlue" ] );
340 $this->assertSame( "Va\nlue", $req->getText( 'crlf' ), 'CR stripped' );
341 }
342
343 /**
344 * @covers WebRequest::getValues
345 */
346 public function testGetValues() {
347 $values = [ 'x' => 'Value', 'y' => '' ];
348 // Avoid FauxRequest (overrides getValues)
349 $req = $this->mockWebRequest( $values );
350 $this->assertSame( $values, $req->getValues() );
351 $this->assertSame( [ 'x' => 'Value' ], $req->getValues( 'x' ), 'Specific keys' );
352 }
353
354 /**
355 * @covers WebRequest::getValueNames
356 */
357 public function testGetValueNames() {
358 $req = $this->mockWebRequest( [ 'x' => 'Value', 'y' => '' ] );
359 $this->assertSame( [ 'x', 'y' ], $req->getValueNames() );
360 $this->assertSame( [ 'x' ], $req->getValueNames( [ 'y' ] ), 'Exclude keys' );
361 }
362
363 /**
364 * @dataProvider provideGetIP
365 * @covers WebRequest::getIP
366 */
367 public function testGetIP( $expected, $input, $squid, $xffList, $private, $description ) {
368 $_SERVER = $input;
369 $this->setMwGlobals( [
370 'wgSquidServersNoPurge' => $squid,
371 'wgUsePrivateIPs' => $private,
372 'wgHooks' => [
373 'IsTrustedProxy' => [
374 function ( &$ip, &$trusted ) use ( $xffList ) {
375 $trusted = $trusted || in_array( $ip, $xffList );
376 return true;
377 }
378 ]
379 ]
380 ] );
381
382 $request = new WebRequest();
383 $result = $request->getIP();
384 $this->assertEquals( $expected, $result, $description );
385 }
386
387 public static function provideGetIP() {
388 return [
389 [
390 '127.0.0.1',
391 [
392 'REMOTE_ADDR' => '127.0.0.1'
393 ],
394 [],
395 [],
396 false,
397 'Simple IPv4'
398 ],
399 [
400 '::1',
401 [
402 'REMOTE_ADDR' => '::1'
403 ],
404 [],
405 [],
406 false,
407 'Simple IPv6'
408 ],
409 [
410 '12.0.0.1',
411 [
412 'REMOTE_ADDR' => 'abcd:0001:002:03:4:555:6666:7777',
413 'HTTP_X_FORWARDED_FOR' => '12.0.0.1, abcd:0001:002:03:4:555:6666:7777',
414 ],
415 [ 'ABCD:1:2:3:4:555:6666:7777' ],
416 [],
417 false,
418 'IPv6 normalisation'
419 ],
420 [
421 '12.0.0.3',
422 [
423 'REMOTE_ADDR' => '12.0.0.1',
424 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
425 ],
426 [ '12.0.0.1', '12.0.0.2' ],
427 [],
428 false,
429 'With X-Forwaded-For'
430 ],
431 [
432 '12.0.0.1',
433 [
434 'REMOTE_ADDR' => '12.0.0.1',
435 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
436 ],
437 [],
438 [],
439 false,
440 'With X-Forwaded-For and disallowed server'
441 ],
442 [
443 '12.0.0.2',
444 [
445 'REMOTE_ADDR' => '12.0.0.1',
446 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
447 ],
448 [ '12.0.0.1' ],
449 [],
450 false,
451 'With multiple X-Forwaded-For and only one allowed server'
452 ],
453 [
454 '10.0.0.3',
455 [
456 'REMOTE_ADDR' => '12.0.0.2',
457 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
458 ],
459 [ '12.0.0.1', '12.0.0.2' ],
460 [],
461 false,
462 'With X-Forwaded-For and private IP (from cache proxy)'
463 ],
464 [
465 '10.0.0.4',
466 [
467 'REMOTE_ADDR' => '12.0.0.2',
468 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
469 ],
470 [ '12.0.0.1', '12.0.0.2', '10.0.0.3' ],
471 [],
472 true,
473 'With X-Forwaded-For and private IP (allowed)'
474 ],
475 [
476 '10.0.0.4',
477 [
478 'REMOTE_ADDR' => '12.0.0.2',
479 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
480 ],
481 [ '12.0.0.1', '12.0.0.2' ],
482 [ '10.0.0.3' ],
483 true,
484 'With X-Forwaded-For and private IP (allowed)'
485 ],
486 [
487 '10.0.0.3',
488 [
489 'REMOTE_ADDR' => '12.0.0.2',
490 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
491 ],
492 [ '12.0.0.1', '12.0.0.2' ],
493 [ '10.0.0.3' ],
494 false,
495 'With X-Forwaded-For and private IP (disallowed)'
496 ],
497 [
498 '12.0.0.3',
499 [
500 'REMOTE_ADDR' => '12.0.0.1',
501 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
502 ],
503 [],
504 [ '12.0.0.1', '12.0.0.2' ],
505 false,
506 'With X-Forwaded-For'
507 ],
508 [
509 '12.0.0.2',
510 [
511 'REMOTE_ADDR' => '12.0.0.1',
512 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
513 ],
514 [],
515 [ '12.0.0.1' ],
516 false,
517 'With multiple X-Forwaded-For and only one allowed server'
518 ],
519 [
520 '12.0.0.2',
521 [
522 'REMOTE_ADDR' => '12.0.0.2',
523 'HTTP_X_FORWARDED_FOR' => '10.0.0.3, 12.0.0.2'
524 ],
525 [],
526 [ '12.0.0.2' ],
527 false,
528 'With X-Forwaded-For and private IP and hook (disallowed)'
529 ],
530 [
531 '12.0.0.1',
532 [
533 'REMOTE_ADDR' => 'abcd:0001:002:03:4:555:6666:7777',
534 'HTTP_X_FORWARDED_FOR' => '12.0.0.1, abcd:0001:002:03:4:555:6666:7777',
535 ],
536 [ 'ABCD:1:2:3::/64' ],
537 [],
538 false,
539 'IPv6 CIDR'
540 ],
541 [
542 '12.0.0.3',
543 [
544 'REMOTE_ADDR' => '12.0.0.1',
545 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
546 ],
547 [ '12.0.0.0/24' ],
548 [],
549 false,
550 'IPv4 CIDR'
551 ],
552 ];
553 }
554
555 /**
556 * @expectedException MWException
557 * @covers WebRequest::getIP
558 */
559 public function testGetIpLackOfRemoteAddrThrowAnException() {
560 // ensure that local install state doesn't interfere with test
561 $this->setMwGlobals( [
562 'wgSquidServersNoPurge' => [],
563 'wgSquidServers' => [],
564 'wgUsePrivateIPs' => false,
565 'wgHooks' => [],
566 ] );
567
568 $request = new WebRequest();
569 # Next call throw an exception about lacking an IP
570 $request->getIP();
571 }
572
573 public static function provideLanguageData() {
574 return [
575 [ '', [], 'Empty Accept-Language header' ],
576 [ 'en', [ 'en' => 1 ], 'One language' ],
577 [ 'en, ar', [ 'en' => 1, 'ar' => 1 ], 'Two languages listed in appearance order.' ],
578 [
579 'zh-cn,zh-tw',
580 [ 'zh-cn' => 1, 'zh-tw' => 1 ],
581 'Two equally prefered languages, listed in appearance order per rfc3282. Checks c9119'
582 ],
583 [
584 'es, en; q=0.5',
585 [ 'es' => 1, 'en' => '0.5' ],
586 'Spanish as first language and English and second'
587 ],
588 [ 'en; q=0.5, es', [ 'es' => 1, 'en' => '0.5' ], 'Less prefered language first' ],
589 [ 'fr, en; q=0.5, es', [ 'fr' => 1, 'es' => 1, 'en' => '0.5' ], 'Three languages' ],
590 [ 'en; q=0.5, es', [ 'es' => 1, 'en' => '0.5' ], 'Two languages' ],
591 [ 'en, zh;q=0', [ 'en' => 1 ], "It's Chinese to me" ],
592 [
593 'es; q=1, pt;q=0.7, it; q=0.6, de; q=0.1, ru;q=0',
594 [ 'es' => '1', 'pt' => '0.7', 'it' => '0.6', 'de' => '0.1' ],
595 'Preference for Romance languages'
596 ],
597 [
598 'en-gb, en-us; q=1',
599 [ 'en-gb' => 1, 'en-us' => '1' ],
600 'Two equally prefered English variants'
601 ],
602 [ '_', [], 'Invalid input' ],
603 ];
604 }
605
606 /**
607 * @dataProvider provideLanguageData
608 * @covers WebRequest::getAcceptLang
609 */
610 public function testAcceptLang( $acceptLanguageHeader, $expectedLanguages, $description ) {
611 $_SERVER = [ 'HTTP_ACCEPT_LANGUAGE' => $acceptLanguageHeader ];
612 $request = new WebRequest();
613 $this->assertSame( $request->getAcceptLang(), $expectedLanguages, $description );
614 }
615 }