Merged r114672 from wmf1.19 plus tests; crosswiki block name/id fix.
authorAaron Schulz <aschulz@wikimedia.org>
Tue, 10 Apr 2012 00:12:06 +0000 (17:12 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Tue, 10 Apr 2012 16:49:52 +0000 (09:49 -0700)
Change 1:
* Delete any pre-existing block for the tests
* Renamed test function and fixed comment typo

Change-Id: I4804ccae81dd0455e9d3ddf48960c4b5cd6e4f5f

includes/Block.php
tests/phpunit/includes/BlockTest.php

index 522316f..dff1a5d 100644 (file)
@@ -33,6 +33,9 @@ class Block {
        /// @var User|String
        protected $target;
 
+       // @var Integer Hack for foreign blocking (CentralAuth)
+       protected $forcedTargetID;
+
        /// @var Block::TYPE_ constant.  Can only be USER, IP or RANGE internally
        protected $type;
 
@@ -72,7 +75,7 @@ class Block {
 
                $this->setTarget( $address );
                if ( $this->target instanceof User && $user ) {
-                       $this->target->setId( $user ); // needed for foreign users
+                       $this->forcedTargetID = $user; // needed for foreign users
                }
                if ( $by ) { // local user
                        $this->setBlocker( User::newFromID( $by ) );
@@ -483,9 +486,15 @@ class Block {
                }
                $expiry = $db->encodeExpiry( $this->mExpiry );
 
+               if ( $this->forcedTargetID ) {
+                       $uid = $this->forcedTargetID;
+               } else {
+                       $uid = $this->target instanceof User ? $this->target->getID() : 0;
+               }
+
                $a = array(
                        'ipb_address'          => (string)$this->target,
-                       'ipb_user'             => $this->target instanceof User ? $this->target->getID() : 0,
+                       'ipb_user'             => $uid,
                        'ipb_by'               => $this->getBy(),
                        'ipb_by_text'          => $this->getByName(),
                        'ipb_reason'           => $this->mReason,
index 749f40b..f197da8 100644 (file)
@@ -67,7 +67,7 @@ class BlockTest extends MediaWikiLangTestCase {
                // $this->dumpBlocks();
 
                $this->assertTrue( $this->block->equals( Block::newFromTarget('UTBlockee') ), "newFromTarget() returns the same block as the one that was made");
-               
+
                $this->assertTrue( $this->block->equals( Block::newFromID( $this->blockId ) ), "newFromID() returns the same block as the one that was made");
 
        }
@@ -122,4 +122,48 @@ class BlockTest extends MediaWikiLangTestCase {
                        array( false )
                );
        }
+
+       function testCrappyCrossWikiBlocks() {
+               // Delete the last round's block if it's still there
+               $oldBlock = Block::newFromTarget( 'UserOnForeignWiki' );
+               if ( $oldBlock ) {
+                       // An old block will prevent our new one from saving.
+                       $oldBlock->delete();
+               }
+
+               // Foreign perspective (blockee not on current wiki)...
+               $block = new Block(
+                       /* $address */ 'UserOnForeignWiki',
+                       /* $user */ 14146,
+                       /* $by */ 0,
+                       /* $reason */ 'crosswiki block...',
+                       /* $timestamp */ wfTimestampNow(),
+                       /* $auto */ false,
+                       /* $expiry */ $this->db->getInfinity(),
+                       /* anonOnly */ false,
+                       /* $createAccount */ true,
+                       /* $enableAutoblock */ true,
+                       /* $hideName (ipb_deleted) */ true,
+                       /* $blockEmail */ true,
+                       /* $allowUsertalk */ false,
+                       /* $byName */ 'MetaWikiUser'
+               );
+
+               $res = $block->insert( $this->db );
+               $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
+
+               // Local perspective (blockee on current wiki)...
+               $user = User::newFromName( 'UserOnForeignWiki' );
+               $user->addToDatabase();
+               // Set user ID to match the test value
+               $this->db->update( 'user', array( 'user_id' => 14146 ), array( 'user_id' => $user->getId() ) );
+               $user = null; // clear
+
+               $block = Block::newFromID( $res['id'] );
+               $this->assertEquals( 'UserOnForeignWiki', $block->getTarget()->getName(), 'Correct blockee name' );
+               $this->assertEquals( '14146',  $block->getTarget()->getId(), 'Correct blockee id' );
+               $this->assertEquals( 'MetaWikiUser', $block->getBlocker(), 'Correct blocker name' );
+               $this->assertEquals( 'MetaWikiUser', $block->getByName(), 'Correct blocker name' );
+               $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
+       }
 }