MediaWiki  REL1_20
BlockTest.php
Go to the documentation of this file.
00001 <?php
00002 
00007 class BlockTest extends MediaWikiLangTestCase {
00008 
00009         private $block, $madeAt;
00010 
00011         /* variable used to save up the blockID we insert in this test suite */
00012         private $blockId;
00013 
00014         function setUp() {
00015                 global $wgContLang;
00016                 parent::setUp();
00017                 $wgContLang = Language::factory( 'en' );
00018         }
00019 
00020         function addDBData() {
00021                 //$this->dumpBlocks();
00022 
00023                 $user = User::newFromName( 'UTBlockee' );
00024                 if( $user->getID() == 0 ) {
00025                         $user->addToDatabase();
00026                         $user->setPassword( 'UTBlockeePassword' );
00027 
00028                         $user->saveSettings();
00029                 }
00030 
00031                 // Delete the last round's block if it's still there
00032                 $oldBlock = Block::newFromTarget( 'UTBlockee' );
00033                 if ( $oldBlock ) {
00034                         // An old block will prevent our new one from saving.
00035                         $oldBlock->delete();
00036                 }
00037 
00038                 $this->block = new Block( 'UTBlockee', $user->getID(), 0,
00039                         'Parce que', 0, false, time() + 100500
00040                 );
00041                 $this->madeAt = wfTimestamp( TS_MW );
00042 
00043                 $this->block->insert();
00044                 // save up ID for use in assertion. Since ID is an autoincrement,
00045                 // its value might change depending on the order the tests are run.
00046                 // ApiBlockTest insert its own blocks!
00047                 $newBlockId = $this->block->getId();
00048                 if ($newBlockId) {
00049                         $this->blockId = $newBlockId;
00050                 } else {
00051                         throw new MWException( "Failed to insert block for BlockTest; old leftover block remaining?" );
00052                 }
00053         }
00054 
00058         function dumpBlocks() {
00059                 $v = $this->db->query( 'SELECT * FROM unittest_ipblocks' );
00060                 print "Got " . $v->numRows() . " rows. Full dump follow:\n";
00061                 foreach( $v as $row ) {
00062                         print_r( $row );
00063                 }
00064         }
00065 
00066         function testInitializerFunctionsReturnCorrectBlock() {
00067                 // $this->dumpBlocks();
00068 
00069                 $this->assertTrue( $this->block->equals( Block::newFromTarget('UTBlockee') ), "newFromTarget() returns the same block as the one that was made");
00070 
00071                 $this->assertTrue( $this->block->equals( Block::newFromID( $this->blockId ) ), "newFromID() returns the same block as the one that was made");
00072 
00073         }
00074 
00078         function testBug26425BlockTimestampDefaultsToTime() {
00079                 // delta to stop one-off errors when things happen to go over a second mark.
00080                 $delta = abs( $this->madeAt - $this->block->mTimestamp );
00081                 $this->assertLessThan( 2, $delta, "If no timestamp is specified, the block is recorded as time()");
00082 
00083         }
00084 
00093         function testBug29116LoadWithEmptyIp( $vagueTarget ) {
00094                 $this->hideDeprecated( 'Block::load' );
00095 
00096                 $uid = User::idFromName( 'UTBlockee' );
00097                 $this->assertTrue( ($uid > 0), 'Must be able to look up the target user during tests' );
00098 
00099                 $block = new Block();
00100                 $ok = $block->load( $vagueTarget, $uid );
00101                 $this->assertTrue( $ok, "Block->load() with empty IP and user ID '$uid' should return a block" );
00102 
00103                 $this->assertTrue( $this->block->equals( $block ), "Block->load() returns the same block as the one that was made when given empty ip param " . var_export( $vagueTarget, true ) );
00104         }
00105 
00113         function testBug29116NewFromTargetWithEmptyIp( $vagueTarget ) {
00114                 $block = Block::newFromTarget('UTBlockee', $vagueTarget);
00115                 $this->assertTrue( $this->block->equals( $block ), "newFromTarget() returns the same block as the one that was made when given empty vagueTarget param " . var_export( $vagueTarget, true ) );
00116         }
00117 
00118         function dataBug29116() {
00119                 return array(
00120                         array( null ),
00121                         array( '' ),
00122                         array( false )
00123                 );
00124         }
00125 
00126         function testBlockedUserCanNotCreateAccount() {
00127                 $username = 'BlockedUserToCreateAccountWith';
00128                 $u = User::newFromName( $username );
00129                 $u->setPassword( 'NotRandomPass' );
00130                 $u->addToDatabase();
00131                 unset( $u );
00132 
00133 
00134                 // Sanity check
00135                 $this->assertNull(
00136                         Block::newFromTarget( $username ),
00137                         "$username should not be blocked"
00138                 );
00139 
00140                 // Reload user
00141                 $u = User::newFromName( $username );
00142                 $this->assertFalse(
00143                         $u->isBlockedFromCreateAccount(),
00144                         "Our sandbox user should be able to create account before being blocked"
00145                 );
00146 
00147                 // Foreign perspective (blockee not on current wiki)...
00148                 $block = new Block(
00149                         /* $address */ $username,
00150                         /* $user */ 14146,
00151                         /* $by */ 0,
00152                         /* $reason */ 'crosswiki block...',
00153                         /* $timestamp */ wfTimestampNow(),
00154                         /* $auto */ false,
00155                         /* $expiry */ $this->db->getInfinity(),
00156                         /* anonOnly */ false,
00157                         /* $createAccount */ true,
00158                         /* $enableAutoblock */ true,
00159                         /* $hideName (ipb_deleted) */ true,
00160                         /* $blockEmail */ true,
00161                         /* $allowUsertalk */ false,
00162                         /* $byName */ 'MetaWikiUser'
00163                 );
00164                 $block->insert();
00165 
00166                 // Reload block from DB
00167                 $userBlock = Block::newFromTarget( $username );
00168                 $this->assertTrue(
00169                         (bool) $block->prevents( 'createaccount' ),
00170                         "Block object in DB should prevents 'createaccount'"
00171                 );
00172 
00173                 $this->assertInstanceOf(
00174                         'Block',
00175                         $userBlock,
00176                         "'$username' block block object should be existent"
00177                 );
00178 
00179                 // Reload user
00180                 $u = User::newFromName( $username );
00181                 $this->assertTrue(
00182                         (bool) $u->isBlockedFromCreateAccount(),
00183                         "Our sandbox user '$username' should NOT be able to create account"
00184                 );
00185         }
00186 
00187         function testCrappyCrossWikiBlocks() {
00188                 // Delete the last round's block if it's still there
00189                 $oldBlock = Block::newFromTarget( 'UserOnForeignWiki' );
00190                 if ( $oldBlock ) {
00191                         // An old block will prevent our new one from saving.
00192                         $oldBlock->delete();
00193                 }
00194 
00195                 // Foreign perspective (blockee not on current wiki)...
00196                 $block = new Block(
00197                         /* $address */ 'UserOnForeignWiki',
00198                         /* $user */ 14146,
00199                         /* $by */ 0,
00200                         /* $reason */ 'crosswiki block...',
00201                         /* $timestamp */ wfTimestampNow(),
00202                         /* $auto */ false,
00203                         /* $expiry */ $this->db->getInfinity(),
00204                         /* anonOnly */ false,
00205                         /* $createAccount */ true,
00206                         /* $enableAutoblock */ true,
00207                         /* $hideName (ipb_deleted) */ true,
00208                         /* $blockEmail */ true,
00209                         /* $allowUsertalk */ false,
00210                         /* $byName */ 'MetaWikiUser'
00211                 );
00212 
00213                 $res = $block->insert( $this->db );
00214                 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
00215 
00216                 // Local perspective (blockee on current wiki)...
00217                 $user = User::newFromName( 'UserOnForeignWiki' );
00218                 $user->addToDatabase();
00219                 // Set user ID to match the test value
00220                 $this->db->update( 'user', array( 'user_id' => 14146 ), array( 'user_id' => $user->getId() ) );
00221                 $user = null; // clear
00222 
00223                 $block = Block::newFromID( $res['id'] );
00224                 $this->assertEquals( 'UserOnForeignWiki', $block->getTarget()->getName(), 'Correct blockee name' );
00225                 $this->assertEquals( '14146',  $block->getTarget()->getId(), 'Correct blockee id' );
00226                 $this->assertEquals( 'MetaWikiUser', $block->getBlocker(), 'Correct blocker name' );
00227                 $this->assertEquals( 'MetaWikiUser', $block->getByName(), 'Correct blocker name' );
00228                 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
00229         }
00230 }