From e82c2cc7eef7d0d58101f43b5fdba289b2c3967e Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Mon, 9 Apr 2012 17:12:06 -0700 Subject: [PATCH] Merged r114672 from wmf1.19 plus tests; crosswiki block name/id fix. Change 1: * Delete any pre-existing block for the tests * Renamed test function and fixed comment typo Change-Id: I4804ccae81dd0455e9d3ddf48960c4b5cd6e4f5f --- includes/Block.php | 13 ++++++-- tests/phpunit/includes/BlockTest.php | 46 +++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/includes/Block.php b/includes/Block.php index 522316fcc5..dff1a5d336 100644 --- a/includes/Block.php +++ b/includes/Block.php @@ -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, diff --git a/tests/phpunit/includes/BlockTest.php b/tests/phpunit/includes/BlockTest.php index 749f40b480..f197da80c4 100644 --- a/tests/phpunit/includes/BlockTest.php +++ b/tests/phpunit/includes/BlockTest.php @@ -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' ); + } } -- 2.20.1