wiki.techinc.nl/tests/phpunit/includes/WebRequestTest.php

311 lines
7.1 KiB
PHP
Raw Normal View History

<?php
/**
* @group WebRequest
*/
class WebRequestTest extends MediaWikiTestCase {
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
protected $oldServer;
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
protected function setUp() {
parent::setUp();
$this->oldServer = $_SERVER;
}
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
protected function tearDown() {
$_SERVER = $this->oldServer;
parent::tearDown();
}
/**
* @dataProvider provideDetectServer
* @covers WebRequest::detectServer
*/
function testDetectServer( $expected, $input, $description ) {
$_SERVER = $input;
$result = WebRequest::detectServer();
$this->assertEquals( $expected, $result, $description );
}
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
public static function provideDetectServer() {
return array(
array(
'http://x',
array(
'HTTP_HOST' => 'x'
),
'Host header'
),
array(
'https://x',
array(
'HTTP_HOST' => 'x',
'HTTPS' => 'on',
),
'Host header with secure'
),
array(
'http://x',
array(
'HTTP_HOST' => 'x',
'SERVER_PORT' => 80,
),
'Default SERVER_PORT',
),
array(
'http://x',
array(
'HTTP_HOST' => 'x',
'HTTPS' => 'off',
),
'Secure off'
),
array(
'http://y',
array(
'SERVER_NAME' => 'y',
),
'Server name'
),
array(
'http://x',
array(
'HTTP_HOST' => 'x',
'SERVER_NAME' => 'y',
),
'Host server name precedence'
),
array(
'http://[::1]:81',
array(
'HTTP_HOST' => '[::1]',
'SERVER_NAME' => '::1',
'SERVER_PORT' => '81',
),
'Apache bug 26005'
),
array(
'http://localhost',
array(
'SERVER_NAME' => '[2001'
),
'Kind of like lighttpd per commit message in MW r83847',
),
array(
'http://[2a01:e35:2eb4:1::2]:777',
array(
'SERVER_NAME' => '[2a01:e35:2eb4:1::2]:777'
),
'Possible lighttpd environment per bug 14977 comment 13',
),
);
}
/**
* @dataProvider provideGetIP
* @covers WebRequest::getIP
*/
function testGetIP( $expected, $input, $squid, $xffList, $private, $description ) {
$_SERVER = $input;
$this->setMwGlobals( array(
'wgSquidServersNoPurge' => $squid,
'wgUsePrivateIPs' => $private,
'wgHooks' => array(
'IsTrustedProxy' => array(
function( &$ip, &$trusted ) use ( $xffList ) {
$trusted = $trusted || in_array( $ip, $xffList );
return true;
}
)
)
) );
$request = new WebRequest();
$result = $request->getIP();
$this->assertEquals( $expected, $result, $description );
}
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
public static function provideGetIP() {
return array(
array(
'127.0.0.1',
array(
'REMOTE_ADDR' => '127.0.0.1'
),
array(),
array(),
false,
'Simple IPv4'
),
array(
'::1',
array(
'REMOTE_ADDR' => '::1'
),
array(),
array(),
false,
'Simple IPv6'
),
array(
'12.0.0.1',
array(
'REMOTE_ADDR' => 'abcd:0001:002:03:4:555:6666:7777',
'HTTP_X_FORWARDED_FOR' => '12.0.0.1, abcd:0001:002:03:4:555:6666:7777',
),
array( 'ABCD:1:2:3:4:555:6666:7777' ),
array(),
false,
'IPv6 normalisation'
),
array(
'12.0.0.3',
array(
'REMOTE_ADDR' => '12.0.0.1',
'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
),
array( '12.0.0.1', '12.0.0.2' ),
array(),
false,
'With X-Forwaded-For'
),
array(
'12.0.0.1',
array(
'REMOTE_ADDR' => '12.0.0.1',
'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
),
array(),
array(),
false,
'With X-Forwaded-For and disallowed server'
),
array(
'12.0.0.2',
array(
'REMOTE_ADDR' => '12.0.0.1',
'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
),
array( '12.0.0.1' ),
array(),
false,
'With multiple X-Forwaded-For and only one allowed server'
),
array(
'10.0.0.3',
array(
'REMOTE_ADDR' => '12.0.0.2',
'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
),
array( '12.0.0.1', '12.0.0.2' ),
array(),
false,
'With X-Forwaded-For and private IP (from cache proxy)'
),
array(
'10.0.0.4',
array(
'REMOTE_ADDR' => '12.0.0.2',
'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
),
array( '12.0.0.1', '12.0.0.2', '10.0.0.3' ),
array(),
true,
'With X-Forwaded-For and private IP (allowed)'
),
array(
'10.0.0.4',
array(
'REMOTE_ADDR' => '12.0.0.2',
'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
),
array( '12.0.0.1', '12.0.0.2' ),
array( '10.0.0.3' ),
true,
'With X-Forwaded-For and private IP (allowed)'
),
array(
'10.0.0.3',
array(
'REMOTE_ADDR' => '12.0.0.2',
'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
),
array( '12.0.0.1', '12.0.0.2' ),
array( '10.0.0.3' ),
false,
'With X-Forwaded-For and private IP (disallowed)'
),
array(
'12.0.0.3',
array(
'REMOTE_ADDR' => '12.0.0.1',
'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
),
array(),
array( '12.0.0.1', '12.0.0.2' ),
false,
'With X-Forwaded-For'
),
array(
'12.0.0.2',
array(
'REMOTE_ADDR' => '12.0.0.1',
'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
),
array(),
array( '12.0.0.1' ),
false,
'With multiple X-Forwaded-For and only one allowed server'
),
array(
'12.0.0.2',
array(
'REMOTE_ADDR' => '12.0.0.2',
'HTTP_X_FORWARDED_FOR' => '10.0.0.3, 12.0.0.2'
),
array(),
array( '12.0.0.2' ),
false,
'With X-Forwaded-For and private IP and hook (disallowed)'
),
);
}
/**
* @expectedException MWException
* @covers WebRequest::getIP
*/
function testGetIpLackOfRemoteAddrThrowAnException() {
$request = new WebRequest();
# Next call throw an exception about lacking an IP
$request->getIP();
}
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
public static function provideLanguageData() {
return array(
array( '', array(), 'Empty Accept-Language header' ),
array( 'en', array( 'en' => 1 ), 'One language' ),
array( 'en, ar', array( 'en' => 1, 'ar' => 1 ), 'Two languages listed in appearance order.' ),
array( 'zh-cn,zh-tw', array( 'zh-cn' => 1, 'zh-tw' => 1 ), 'Two equally prefered languages, listed in appearance order per rfc3282. Checks c9119' ),
array( 'es, en; q=0.5', array( 'es' => 1, 'en' => '0.5' ), 'Spanish as first language and English and second' ),
array( 'en; q=0.5, es', array( 'es' => 1, 'en' => '0.5' ), 'Less prefered language first' ),
array( 'fr, en; q=0.5, es', array( 'fr' => 1, 'es' => 1, 'en' => '0.5' ), 'Three languages' ),
array( 'en; q=0.5, es', array( 'es' => 1, 'en' => '0.5' ), 'Two languages' ),
array( 'en, zh;q=0', array( 'en' => 1 ), "It's Chinese to me" ),
array( 'es; q=1, pt;q=0.7, it; q=0.6, de; q=0.1, ru;q=0', array( 'es' => '1', 'pt' => '0.7', 'it' => '0.6', 'de' => '0.1' ), 'Preference for romance languages' ),
array( 'en-gb, en-us; q=1', array( 'en-gb' => 1, 'en-us' => '1' ), 'Two equally prefered English variants' ),
);
}
/**
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
* @dataProvider provideLanguageData
* @covers WebRequest::getAcceptLang
*/
function testAcceptLang( $acceptLanguageHeader, $expectedLanguages, $description ) {
$_SERVER = array( 'HTTP_ACCEPT_LANGUAGE' => $acceptLanguageHeader );
$request = new WebRequest();
$this->assertSame( $request->getAcceptLang(), $expectedLanguages, $description );
}
}