Make improvements to BlockManagerTest
[lhc/web/wiklou.git] / includes / block / BlockManager.php
index 3ef35d7..9823b3c 100644 (file)
@@ -22,10 +22,10 @@ namespace MediaWiki\Block;
 
 use Block;
 use IP;
+use MediaWiki\User\UserIdentity;
 use User;
 use WebRequest;
 use Wikimedia\IPSet;
-use MediaWiki\User\UserIdentity;
 
 /**
  * A service class for checking blocks.
@@ -111,7 +111,7 @@ class BlockManager {
         * @param bool $fromReplica Whether to check the replica DB first.
         *  To improve performance, non-critical checks are done against replica DBs.
         *  Check when actually saving should be done against master.
-        * @return Block|null The most relevant block, or null if there is no block.
+        * @return AbstractBlock|null The most relevant block, or null if there is no block.
         */
        public function getUserBlock( User $user, $fromReplica ) {
                $isAnon = $user->getId() === 0;
@@ -139,22 +139,25 @@ class BlockManager {
                $block = Block::newFromTarget( $user, $ip, !$fromReplica );
 
                // Cookie blocking
-               if ( !$block instanceof Block ) {
+               if ( !$block instanceof AbstractBlock ) {
                        $block = $this->getBlockFromCookieValue( $user, $request );
                }
 
                // Proxy blocking
-               if ( !$block instanceof Block && $ip !== null && !in_array( $ip, $this->proxyWhitelist ) ) {
+               if ( !$block instanceof AbstractBlock
+                       && $ip !== null
+                       && !in_array( $ip, $this->proxyWhitelist )
+               ) {
                        // Local list
                        if ( $this->isLocallyBlockedProxy( $ip ) ) {
-                               $block = new Block( [
+                               $block = new SystemBlock( [
                                        'byText' => wfMessage( 'proxyblocker' )->text(),
                                        'reason' => wfMessage( 'proxyblockreason' )->plain(),
                                        'address' => $ip,
                                        'systemBlock' => 'proxy',
                                ] );
                        } elseif ( $isAnon && $this->isDnsBlacklisted( $ip ) ) {
-                               $block = new Block( [
+                               $block = new SystemBlock( [
                                        'byText' => wfMessage( 'sorbs' )->text(),
                                        'reason' => wfMessage( 'sorbsreason' )->plain(),
                                        'address' => $ip,
@@ -164,7 +167,7 @@ class BlockManager {
                }
 
                // (T25343) Apply IP blocks to the contents of XFF headers, if enabled
-               if ( !$block instanceof Block
+               if ( !$block instanceof AbstractBlock
                        && $this->applyIpBlocksToXff
                        && $ip !== null
                        && !in_array( $ip, $this->proxyWhitelist )
@@ -176,19 +179,19 @@ class BlockManager {
                        $xffblocks = Block::getBlocksForIPList( $xff, $isAnon, !$fromReplica );
                        // TODO: remove dependency on Block
                        $block = Block::chooseBlock( $xffblocks, $xff );
-                       if ( $block instanceof Block ) {
+                       if ( $block instanceof AbstractBlock ) {
                                # Mangle the reason to alert the user that the block
                                # originated from matching the X-Forwarded-For header.
                                $block->setReason( wfMessage( 'xffblockreason', $block->getReason() )->plain() );
                        }
                }
 
-               if ( !$block instanceof Block
+               if ( !$block instanceof AbstractBlock
                        && $ip !== null
                        && $isAnon
                        && IP::isInRanges( $ip, $this->softBlockRanges )
                ) {
-                       $block = new Block( [
+                       $block = new SystemBlock( [
                                'address' => $ip,
                                'byText' => 'MediaWiki default',
                                'reason' => wfMessage( 'softblockrangesreason', $ip )->plain(),
@@ -342,29 +345,42 @@ class BlockManager {
                                if ( is_array( $base ) ) {
                                        if ( count( $base ) >= 2 ) {
                                                // Access key is 1, base URL is 0
-                                               $host = "{$base[1]}.$ipReversed.{$base[0]}";
+                                               $hostname = "{$base[1]}.$ipReversed.{$base[0]}";
                                        } else {
-                                               $host = "$ipReversed.{$base[0]}";
+                                               $hostname = "$ipReversed.{$base[0]}";
                                        }
                                        $basename = $base[0];
                                } else {
-                                       $host = "$ipReversed.$base";
+                                       $hostname = "$ipReversed.$base";
                                }
 
                                // Send query
-                               $ipList = gethostbynamel( $host );
+                               $ipList = $this->checkHost( $hostname );
 
                                if ( $ipList ) {
-                                       wfDebugLog( 'dnsblacklist', "Hostname $host is {$ipList[0]}, it's a proxy says $basename!" );
+                                       wfDebugLog(
+                                               'dnsblacklist',
+                                               "Hostname $hostname is {$ipList[0]}, it's a proxy says $basename!"
+                                       );
                                        $found = true;
                                        break;
                                }
 
-                               wfDebugLog( 'dnsblacklist', "Requested $host, not found in $basename." );
+                               wfDebugLog( 'dnsblacklist', "Requested $hostname, not found in $basename." );
                        }
                }
 
                return $found;
        }
 
+       /**
+        * Wrapper for mocking in tests.
+        *
+        * @param string $hostname DNSBL query
+        * @return string[]|bool IPv4 array, or false if the IP is not blacklisted
+        */
+       protected function checkHost( $hostname ) {
+               return gethostbynamel( $hostname );
+       }
+
 }