Tests for wfGetIP() follow up r89407
authorAntoine Musso <hashar@users.mediawiki.org>
Tue, 16 Aug 2011 14:15:07 +0000 (14:15 +0000)
committerAntoine Musso <hashar@users.mediawiki.org>
Tue, 16 Aug 2011 14:15:07 +0000 (14:15 +0000)
* 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
$

includes/ProxyTools.php
tests/phpunit/includes/ProxyTools/wfGetIPTest.php

index e68729f..ab9beff 100644 (file)
@@ -64,12 +64,19 @@ function wfGetAgent() {
 /**
  * Work out the IP address based on various globals
  * For trusted proxies, use the XFF client IP (first of the chain)
+ * @param $reset boolean Used to reset the internal static variable
+ * tracking the IP address. Set to anything non empty to reset it, for
+ * example: wfGetIP( 'reset' ); (default: false).
  * @return string
  */
-function wfGetIP() {
+function wfGetIP( $reset = false ) {
        global $wgUsePrivateIPs, $wgCommandLineMode;
        static $ip = false;
 
+       if( $reset ) {
+               $ip = false;
+       }
+
        # Return cached result
        if ( !empty( $ip ) ) {
                return $ip;
index edaf0b7..baa3d5d 100644 (file)
@@ -9,14 +9,12 @@
  * TODO: need to cover the part dealing with XFF headers.
  */
 class wfGetIPTest extends MediaWikiTestCase {
-       // constant used to reset wfGetIP() internal static variable
-       const doReset = true;
 
        /** helper */
        public function assertIP( $expected, $msg = '' ) {
                $this->assertEquals(
                        $expected,
-                       wfGetIP( wfGetIPTest::doReset ),
+                       wfGetIP( 'reset' ),
                        $msg );
        }
 
@@ -24,13 +22,10 @@ class wfGetIPTest extends MediaWikiTestCase {
        public function assertIPNot( $expected, $msg = '' ) {
                $this->assertNotEquals(
                        $expected,
-                       wfGetIP( wfGetIPTest::doReset ),
+                       wfGetIP( 'reset' ),
                        $msg );
        }
 
-       /**
-        * @group Broken
-        */
        function testGetLoopbackAddressWhenInCommandLine() {
                global $wgCommandLineMode;
                $save = $wgCommandLineMode;
@@ -42,9 +37,6 @@ class wfGetIPTest extends MediaWikiTestCase {
                $wgCommandLineMode = $save;
        }
 
-       /**
-        * @group Broken
-        */
        function testGetFromServerRemoteAddr() {
                global $_SERVER;
                $save = null;
@@ -70,11 +62,17 @@ class wfGetIPTest extends MediaWikiTestCase {
                } else {
                        $_SERVER['REMOTE_ADDR'] = $save;
                }
+               # Restore internal static altered above
+               wfGetIP( 'reset' );
        }
 
        /**
         * @expectedException MWException
         * @group Broken
+        * This is broken since it alter $wgCommandLineMode which is needed
+        * by our exception handler.  Until someone find a correct way to handle
+        * this case, the test should stay in the Broken group.
+        * 
         */
        function testLackOfRemoteAddrThrowAnException() {
                global $wgCommandLineMode;
@@ -83,7 +81,7 @@ class wfGetIPTest extends MediaWikiTestCase {
                # PHPUnit runs from the command line
                $wgCommandLineMode = false;
                # Next call throw an exception about lacking an IP
-               wfGetIP( wfGetIPTest::doReset );
+               wfGetIP( 'reset' );
 
                $wgCommandLineMode = $save;
        }