MediaWiki  1.23.13
WebRequestTest.php
Go to the documentation of this file.
1 <?php
2 
7  protected $oldServer;
8 
9  protected function setUp() {
10  parent::setUp();
11 
12  $this->oldServer = $_SERVER;
13  }
14 
15  protected function tearDown() {
16  $_SERVER = $this->oldServer;
17 
18  parent::tearDown();
19  }
20 
25  public function testDetectServer( $expected, $input, $description ) {
26  $_SERVER = $input;
28  $this->assertEquals( $expected, $result, $description );
29  }
30 
31  public static function provideDetectServer() {
32  return array(
33  array(
34  'http://x',
35  array(
36  'HTTP_HOST' => 'x'
37  ),
38  'Host header'
39  ),
40  array(
41  'https://x',
42  array(
43  'HTTP_HOST' => 'x',
44  'HTTPS' => 'on',
45  ),
46  'Host header with secure'
47  ),
48  array(
49  'http://x',
50  array(
51  'HTTP_HOST' => 'x',
52  'SERVER_PORT' => 80,
53  ),
54  'Default SERVER_PORT',
55  ),
56  array(
57  'http://x',
58  array(
59  'HTTP_HOST' => 'x',
60  'HTTPS' => 'off',
61  ),
62  'Secure off'
63  ),
64  array(
65  'http://y',
66  array(
67  'SERVER_NAME' => 'y',
68  ),
69  'Server name'
70  ),
71  array(
72  'http://x',
73  array(
74  'HTTP_HOST' => 'x',
75  'SERVER_NAME' => 'y',
76  ),
77  'Host server name precedence'
78  ),
79  array(
80  'http://[::1]:81',
81  array(
82  'HTTP_HOST' => '[::1]',
83  'SERVER_NAME' => '::1',
84  'SERVER_PORT' => '81',
85  ),
86  'Apache bug 26005'
87  ),
88  array(
89  'http://localhost',
90  array(
91  'SERVER_NAME' => '[2001'
92  ),
93  'Kind of like lighttpd per commit message in MW r83847',
94  ),
95  array(
96  'http://[2a01:e35:2eb4:1::2]:777',
97  array(
98  'SERVER_NAME' => '[2a01:e35:2eb4:1::2]:777'
99  ),
100  'Possible lighttpd environment per bug 14977 comment 13',
101  ),
102  );
103  }
104 
109  public function testGetIP( $expected, $input, $squid, $xffList, $private, $description ) {
110  $_SERVER = $input;
111  $this->setMwGlobals( array(
112  'wgSquidServersNoPurge' => $squid,
113  'wgUsePrivateIPs' => $private,
114  'wgHooks' => array(
115  'IsTrustedProxy' => array(
116  function( &$ip, &$trusted ) use ( $xffList ) {
117  $trusted = $trusted || in_array( $ip, $xffList );
118  return true;
119  }
120  )
121  )
122  ) );
123 
124  $request = new WebRequest();
125  $result = $request->getIP();
126  $this->assertEquals( $expected, $result, $description );
127  }
128 
129  public static function provideGetIP() {
130  return array(
131  array(
132  '127.0.0.1',
133  array(
134  'REMOTE_ADDR' => '127.0.0.1'
135  ),
136  array(),
137  array(),
138  false,
139  'Simple IPv4'
140  ),
141  array(
142  '::1',
143  array(
144  'REMOTE_ADDR' => '::1'
145  ),
146  array(),
147  array(),
148  false,
149  'Simple IPv6'
150  ),
151  array(
152  '12.0.0.1',
153  array(
154  'REMOTE_ADDR' => 'abcd:0001:002:03:4:555:6666:7777',
155  'HTTP_X_FORWARDED_FOR' => '12.0.0.1, abcd:0001:002:03:4:555:6666:7777',
156  ),
157  array( 'ABCD:1:2:3:4:555:6666:7777' ),
158  array(),
159  false,
160  'IPv6 normalisation'
161  ),
162  array(
163  '12.0.0.3',
164  array(
165  'REMOTE_ADDR' => '12.0.0.1',
166  'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
167  ),
168  array( '12.0.0.1', '12.0.0.2' ),
169  array(),
170  false,
171  'With X-Forwaded-For'
172  ),
173  array(
174  '12.0.0.1',
175  array(
176  'REMOTE_ADDR' => '12.0.0.1',
177  'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
178  ),
179  array(),
180  array(),
181  false,
182  'With X-Forwaded-For and disallowed server'
183  ),
184  array(
185  '12.0.0.2',
186  array(
187  'REMOTE_ADDR' => '12.0.0.1',
188  'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
189  ),
190  array( '12.0.0.1' ),
191  array(),
192  false,
193  'With multiple X-Forwaded-For and only one allowed server'
194  ),
195  array(
196  '10.0.0.3',
197  array(
198  'REMOTE_ADDR' => '12.0.0.2',
199  'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
200  ),
201  array( '12.0.0.1', '12.0.0.2' ),
202  array(),
203  false,
204  'With X-Forwaded-For and private IP (from cache proxy)'
205  ),
206  array(
207  '10.0.0.4',
208  array(
209  'REMOTE_ADDR' => '12.0.0.2',
210  'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
211  ),
212  array( '12.0.0.1', '12.0.0.2', '10.0.0.3' ),
213  array(),
214  true,
215  'With X-Forwaded-For and private IP (allowed)'
216  ),
217  array(
218  '10.0.0.4',
219  array(
220  'REMOTE_ADDR' => '12.0.0.2',
221  'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
222  ),
223  array( '12.0.0.1', '12.0.0.2' ),
224  array( '10.0.0.3' ),
225  true,
226  'With X-Forwaded-For and private IP (allowed)'
227  ),
228  array(
229  '10.0.0.3',
230  array(
231  'REMOTE_ADDR' => '12.0.0.2',
232  'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
233  ),
234  array( '12.0.0.1', '12.0.0.2' ),
235  array( '10.0.0.3' ),
236  false,
237  'With X-Forwaded-For and private IP (disallowed)'
238  ),
239  array(
240  '12.0.0.3',
241  array(
242  'REMOTE_ADDR' => '12.0.0.1',
243  'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
244  ),
245  array(),
246  array( '12.0.0.1', '12.0.0.2' ),
247  false,
248  'With X-Forwaded-For'
249  ),
250  array(
251  '12.0.0.2',
252  array(
253  'REMOTE_ADDR' => '12.0.0.1',
254  'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
255  ),
256  array(),
257  array( '12.0.0.1' ),
258  false,
259  'With multiple X-Forwaded-For and only one allowed server'
260  ),
261  array(
262  '12.0.0.2',
263  array(
264  'REMOTE_ADDR' => '12.0.0.2',
265  'HTTP_X_FORWARDED_FOR' => '10.0.0.3, 12.0.0.2'
266  ),
267  array(),
268  array( '12.0.0.2' ),
269  false,
270  'With X-Forwaded-For and private IP and hook (disallowed)'
271  ),
272  array(
273  '12.0.0.1',
274  array(
275  'REMOTE_ADDR' => 'abcd:0001:002:03:4:555:6666:7777',
276  'HTTP_X_FORWARDED_FOR' => '12.0.0.1, abcd:0001:002:03:4:555:6666:7777',
277  ),
278  array( 'ABCD:1:2:3::/64' ),
279  array(),
280  false,
281  'IPv6 CIDR'
282  ),
283  array(
284  '12.0.0.3',
285  array(
286  'REMOTE_ADDR' => '12.0.0.1',
287  'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
288  ),
289  array( '12.0.0.0/24' ),
290  array(),
291  false,
292  'IPv4 CIDR'
293  ),
294  );
295  }
296 
302  // ensure that local install state doesn't interfere with test
303  $this->setMwGlobals( array(
304  'wgSquidServersNoPurge' => array(),
305  'wgSquidServers' => array(),
306  'wgUsePrivateIPs' => false,
307  'wgHooks' => array(),
308  ) );
309 
310  $request = new WebRequest();
311  # Next call throw an exception about lacking an IP
312  $request->getIP();
313  }
314 
315  public static function provideLanguageData() {
316  return array(
317  array( '', array(), 'Empty Accept-Language header' ),
318  array( 'en', array( 'en' => 1 ), 'One language' ),
319  array( 'en, ar', array( 'en' => 1, 'ar' => 1 ), 'Two languages listed in appearance order.' ),
320  array( 'zh-cn,zh-tw', array( 'zh-cn' => 1, 'zh-tw' => 1 ), 'Two equally prefered languages, listed in appearance order per rfc3282. Checks c9119' ),
321  array( 'es, en; q=0.5', array( 'es' => 1, 'en' => '0.5' ), 'Spanish as first language and English and second' ),
322  array( 'en; q=0.5, es', array( 'es' => 1, 'en' => '0.5' ), 'Less prefered language first' ),
323  array( 'fr, en; q=0.5, es', array( 'fr' => 1, 'es' => 1, 'en' => '0.5' ), 'Three languages' ),
324  array( 'en; q=0.5, es', array( 'es' => 1, 'en' => '0.5' ), 'Two languages' ),
325  array( 'en, zh;q=0', array( 'en' => 1 ), "It's Chinese to me" ),
326  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' ),
327  array( 'en-gb, en-us; q=1', array( 'en-gb' => 1, 'en-us' => '1' ), 'Two equally prefered English variants' ),
328  );
329  }
330 
335  public function testAcceptLang( $acceptLanguageHeader, $expectedLanguages, $description ) {
336  $_SERVER = array( 'HTTP_ACCEPT_LANGUAGE' => $acceptLanguageHeader );
337  $request = new WebRequest();
338  $this->assertSame( $request->getAcceptLang(), $expectedLanguages, $description );
339  }
340 }
$result
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message. Please note the header message cannot receive/use parameters. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item. $reader:XMLReader object $logInfo:Array of information Return false to stop further processing of the tag 'ImportHandlePageXMLTag':When parsing a XML tag in a page. $reader:XMLReader object $pageInfo:Array of information Return false to stop further processing of the tag 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision. $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information Return false to stop further processing of the tag 'ImportHandleToplevelXMLTag':When parsing a top level XML tag. $reader:XMLReader object Return false to stop further processing of the tag 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload. $reader:XMLReader object $revisionInfo:Array of information Return false to stop further processing of the tag 'InfoAction':When building information to display on the action=info page. $context:IContextSource object & $pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect. $title:Title object for the current page $request:WebRequest $ignoreRedirect:boolean to skip redirect check $target:Title/string of redirect target $article:Article object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not. Return true without providing an interwiki to continue interwiki search. $prefix:interwiki prefix we are looking for. & $iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InternalParseBeforeSanitize':during Parser 's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings. Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InternalParseBeforeLinks':during Parser 's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InvalidateEmailComplete':Called after a user 's email has been invalidated successfully. $user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification. Callee may modify $url and $query, URL will be constructed as $url . $query & $url:URL to index.php & $query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) $article:article(object) being checked 'IsTrustedProxy':Override the result of wfIsTrustedProxy() $ip:IP being check $result:Change this value to override the result of wfIsTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from & $allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of User::isValidEmailAddr(), for instance to return false if the domain name doesn 't match your organization. $addr:The e-mail address entered by the user & $result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user & $result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we 're looking for a messages file for & $file:The messages file path, you can override this to change the location. 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces. Do not use this hook to add namespaces. Use CanonicalNamespaces for that. & $namespaces:Array of namespaces indexed by their numbers 'LanguageGetMagic':DEPRECATED, use $magicWords in a file listed in $wgExtensionMessagesFiles instead. Use this to define synonyms of magic words depending of the language $magicExtensions:associative array of magic words synonyms $lang:language code(string) 'LanguageGetSpecialPageAliases':DEPRECATED, use $specialPageAliases in a file listed in $wgExtensionMessagesFiles instead. Use to define aliases of special pages names depending of the language $specialPageAliases:associative array of magic words synonyms $lang:language code(string) 'LanguageGetTranslatedLanguageNames':Provide translated language names. & $names:array of language code=> language name $code language of the preferred translations 'LanguageLinks':Manipulate a page 's language links. This is called in various places to allow extensions to define the effective language links for a page. $title:The page 's Title. & $links:Associative array mapping language codes to prefixed links of the form "language:title". & $linkFlags:Associative array mapping prefixed links to arrays of flags. Currently unused, but planned to provide support for marking individual language links in the UI, e.g. for featured articles. 'LinkBegin':Used when generating internal and interwiki links in Linker::link(), before processing starts. Return false to skip default processing and return $ret. See documentation for Linker::link() for details on the expected meanings of parameters. $skin:the Skin object $target:the Title that the link is pointing to & $html:the contents that the< a > tag should have(raw HTML) $result
Definition: hooks.txt:1528
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
WebRequestTest
@group WebRequest
Definition: WebRequestTest.php:6
WebRequestTest\testGetIpLackOfRemoteAddrThrowAnException
testGetIpLackOfRemoteAddrThrowAnException()
@expectedException MWException @covers WebRequest::getIP
Definition: WebRequestTest.php:301
WebRequestTest\testAcceptLang
testAcceptLang( $acceptLanguageHeader, $expectedLanguages, $description)
@dataProvider provideLanguageData @covers WebRequest::getAcceptLang
Definition: WebRequestTest.php:335
WebRequestTest\tearDown
tearDown()
Definition: WebRequestTest.php:15
WebRequestTest\setUp
setUp()
Definition: WebRequestTest.php:9
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Definition: MediaWikiTestCase.php:302
MediaWikiTestCase
Definition: MediaWikiTestCase.php:6
WebRequestTest\provideLanguageData
static provideLanguageData()
Definition: WebRequestTest.php:315
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
WebRequestTest\$oldServer
$oldServer
Definition: WebRequestTest.php:7
WebRequestTest\provideDetectServer
static provideDetectServer()
Definition: WebRequestTest.php:31
WebRequestTest\testDetectServer
testDetectServer( $expected, $input, $description)
@dataProvider provideDetectServer @covers WebRequest::detectServer
Definition: WebRequestTest.php:25
WebRequestTest\provideGetIP
static provideGetIP()
Definition: WebRequestTest.php:129
WebRequest
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form,...
Definition: WebRequest.php:38
WebRequest\detectServer
static detectServer()
Work out an appropriate URL prefix containing scheme and host, based on information detected from $_S...
Definition: WebRequest.php:165
WebRequestTest\testGetIP
testGetIP( $expected, $input, $squid, $xffList, $private, $description)
@dataProvider provideGetIP @covers WebRequest::getIP
Definition: WebRequestTest.php:109