r94638 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r94637‎ | r94638 | r94639 >
Date:14:15, 16 August 2011
Author:hashar
Status:ok
Tags:
Comment:
Tests for wfGetIP() follow up r89407

* wfGetIP() now support resetting its internal static variable. Thanks to
Platonides which introduced this trick with r92960.
* Various tests for $_SERVER['REMOTE_ADDR'] and $wgCommandLineMode.

revert r94575:
- reenable testGetFromServerRemoteAddr() which was not an issue

reintroduce r94558:
- per CR on r94558 by Aaron use meaningful parameter to wfGetIP() when
resetting the static variable ( 'reset' instead of true).
- keep testLackOfRemoteAddrThrowAnException() test in the broken group
with a comment for later fixing.

TODO:

- implements tests for XFF headers.


TEST PLAN:

$ ./phpunit.php --filter wfGetIP --testdox
PHPUnit 3.5.14 by Sebastian Bergmann.

wfGetIP
[x] Get loopback address when in command line
[x] Get from server remote addr
[x] Lack of remote addr throw an exception
$
Modified paths:
  • /trunk/phase3/includes/ProxyTools.php (modified) (history)
  • /trunk/phase3/tests/phpunit/includes/ProxyTools/wfGetIPTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/phpunit/includes/ProxyTools/wfGetIPTest.php
@@ -9,14 +9,12 @@
1010 * TODO: need to cover the part dealing with XFF headers.
1111 */
1212 class wfGetIPTest extends MediaWikiTestCase {
13 - // constant used to reset wfGetIP() internal static variable
14 - const doReset = true;
1513
1614 /** helper */
1715 public function assertIP( $expected, $msg = '' ) {
1816 $this->assertEquals(
1917 $expected,
20 - wfGetIP( wfGetIPTest::doReset ),
 18+ wfGetIP( 'reset' ),
2119 $msg );
2220 }
2321
@@ -24,13 +22,10 @@
2523 public function assertIPNot( $expected, $msg = '' ) {
2624 $this->assertNotEquals(
2725 $expected,
28 - wfGetIP( wfGetIPTest::doReset ),
 26+ wfGetIP( 'reset' ),
2927 $msg );
3028 }
3129
32 - /**
33 - * @group Broken
34 - */
3530 function testGetLoopbackAddressWhenInCommandLine() {
3631 global $wgCommandLineMode;
3732 $save = $wgCommandLineMode;
@@ -42,9 +37,6 @@
4338 $wgCommandLineMode = $save;
4439 }
4540
46 - /**
47 - * @group Broken
48 - */
4941 function testGetFromServerRemoteAddr() {
5042 global $_SERVER;
5143 $save = null;
@@ -70,11 +62,17 @@
7163 } else {
7264 $_SERVER['REMOTE_ADDR'] = $save;
7365 }
 66+ # Restore internal static altered above
 67+ wfGetIP( 'reset' );
7468 }
7569
7670 /**
7771 * @expectedException MWException
7872 * @group Broken
 73+ * This is broken since it alter $wgCommandLineMode which is needed
 74+ * by our exception handler. Until someone find a correct way to handle
 75+ * this case, the test should stay in the Broken group.
 76+ *
7977 */
8078 function testLackOfRemoteAddrThrowAnException() {
8179 global $wgCommandLineMode;
@@ -83,7 +81,7 @@
8482 # PHPUnit runs from the command line
8583 $wgCommandLineMode = false;
8684 # Next call throw an exception about lacking an IP
87 - wfGetIP( wfGetIPTest::doReset );
 85+ wfGetIP( 'reset' );
8886
8987 $wgCommandLineMode = $save;
9088 }
Index: trunk/phase3/includes/ProxyTools.php
@@ -64,12 +64,19 @@
6565 /**
6666 * Work out the IP address based on various globals
6767 * For trusted proxies, use the XFF client IP (first of the chain)
 68+ * @param $reset boolean Used to reset the internal static variable
 69+ * tracking the IP address. Set to anything non empty to reset it, for
 70+ * example: wfGetIP( 'reset' ); (default: false).
6871 * @return string
6972 */
70 -function wfGetIP() {
 73+function wfGetIP( $reset = false ) {
7174 global $wgUsePrivateIPs, $wgCommandLineMode;
7275 static $ip = false;
7376
 77+ if( $reset ) {
 78+ $ip = false;
 79+ }
 80+
7481 # Return cached result
7582 if ( !empty( $ip ) ) {
7683 return $ip;

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r89407Don't execute the loop if there's no X-Forwarded-For header, also don't use i...ialex10:55, 3 June 2011
r92960Make wfUrlEncode(null) reset the static. Two skipped tests work now.platonides20:14, 23 July 2011
r94558Tests for wfGetIP() follow up r89407...hashar20:16, 15 August 2011
r94575avoid playing with $_SERVER in test for now...hashar21:18, 15 August 2011

Status & tagging log