RevisionTest: Make @covers tags absolute
[lhc/web/wiklou.git] / tests / phpunit / includes / BlockTest.php
index 9f38659..70715e2 100644 (file)
@@ -6,69 +6,62 @@
  */
 class BlockTest extends MediaWikiLangTestCase {
 
-       /** @var Block */
-       private $block;
-       private $madeAt;
-
-       /* variable used to save up the blockID we insert in this test suite */
-       private $blockId;
-
-       function addDBData() {
-               $user = User::newFromName( 'UTBlockee' );
-               if ( $user->getId() == 0 ) {
-                       $user->addToDatabase();
-                       TestUser::setPasswordForUser( $user, 'UTBlockeePassword' );
-
-                       $user->saveSettings();
-               }
+       /**
+        * @return User
+        */
+       private function getUserForBlocking() {
+               $testUser = $this->getMutableTestUser();
+               $user = $testUser->getUser();
+               $user->addToDatabase();
+               TestUser::setPasswordForUser( $user, 'UTBlockeePassword' );
+               $user->saveSettings();
+               return $user;
+       }
 
+       /**
+        * @param User $user
+        *
+        * @return Block
+        * @throws MWException
+        */
+       private function addBlockForUser( User $user ) {
                // Delete the last round's block if it's still there
-               $oldBlock = Block::newFromTarget( 'UTBlockee' );
+               $oldBlock = Block::newFromTarget( $user->getName() );
                if ( $oldBlock ) {
                        // An old block will prevent our new one from saving.
                        $oldBlock->delete();
                }
 
                $blockOptions = [
-                       'address' => 'UTBlockee',
+                       'address' => $user->getName(),
                        'user' => $user->getId(),
                        'reason' => 'Parce que',
                        'expiry' => time() + 100500,
                ];
-               $this->block = new Block( $blockOptions );
-               $this->madeAt = wfTimestamp( TS_MW );
+               $block = new Block( $blockOptions );
 
-               $this->block->insert();
+               $block->insert();
                // save up ID for use in assertion. Since ID is an autoincrement,
                // its value might change depending on the order the tests are run.
                // ApiBlockTest insert its own blocks!
-               $newBlockId = $this->block->getId();
-               if ( $newBlockId ) {
-                       $this->blockId = $newBlockId;
-               } else {
+               if ( !$block->getId() ) {
                        throw new MWException( "Failed to insert block for BlockTest; old leftover block remaining?" );
                }
 
                $this->addXffBlocks();
-       }
 
-       /**
-        * debug function : dump the ipblocks table
-        */
-       function dumpBlocks() {
-               $v = $this->db->select( 'ipblocks', '*' );
-               print "Got " . $v->numRows() . " rows. Full dump follow:\n";
-               foreach ( $v as $row ) {
-                       print_r( $row );
-               }
+               return $block;
        }
 
        /**
         * @covers Block::newFromTarget
         */
        public function testINewFromTargetReturnsCorrectBlock() {
+               $user = $this->getUserForBlocking();
+               $block = $this->addBlockForUser( $user );
+
                $this->assertTrue(
-                       $this->block->equals( Block::newFromTarget( 'UTBlockee' ) ),
+                       $block->equals( Block::newFromTarget( $user->getName() ) ),
                        "newFromTarget() returns the same block as the one that was made"
                );
        }
@@ -77,18 +70,25 @@ class BlockTest extends MediaWikiLangTestCase {
         * @covers Block::newFromID
         */
        public function testINewFromIDReturnsCorrectBlock() {
+               $user = $this->getUserForBlocking();
+               $block = $this->addBlockForUser( $user );
+
                $this->assertTrue(
-                       $this->block->equals( Block::newFromID( $this->blockId ) ),
+                       $block->equals( Block::newFromID( $block->getId() ) ),
                        "newFromID() returns the same block as the one that was made"
                );
        }
 
        /**
-        * per bug 26425
+        * per T28425
         */
        public function testBug26425BlockTimestampDefaultsToTime() {
+               $user = $this->getUserForBlocking();
+               $block = $this->addBlockForUser( $user );
+               $madeAt = wfTimestamp( TS_MW );
+
                // delta to stop one-off errors when things happen to go over a second mark.
-               $delta = abs( $this->madeAt - $this->block->mTimestamp );
+               $delta = abs( $madeAt - $block->mTimestamp );
                $this->assertLessThan(
                        2,
                        $delta,
@@ -99,15 +99,18 @@ class BlockTest extends MediaWikiLangTestCase {
        /**
         * CheckUser since being changed to use Block::newFromTarget started failing
         * because the new function didn't accept empty strings like Block::load()
-        * had. Regression bug 29116.
+        * had. Regression T31116.
         *
         * @dataProvider provideBug29116Data
         * @covers Block::newFromTarget
         */
        public function testBug29116NewFromTargetWithEmptyIp( $vagueTarget ) {
-               $block = Block::newFromTarget( 'UTBlockee', $vagueTarget );
+               $user = $this->getUserForBlocking();
+               $initialBlock = $this->addBlockForUser( $user );
+               $block = Block::newFromTarget( $user->getName(), $vagueTarget );
+
                $this->assertTrue(
-                       $this->block->equals( $block ),
+                       $initialBlock->equals( $block ),
                        "newFromTarget() returns the same block as the one that was made when "
                                . "given empty vagueTarget param " . var_export( $vagueTarget, true )
                );
@@ -157,7 +160,7 @@ class BlockTest extends MediaWikiLangTestCase {
                        'enableAutoblock' => true,
                        'hideName' => true,
                        'blockEmail' => true,
-                       'byText' => 'MetaWikiUser',
+                       'byText' => 'm>MetaWikiUser',
                ];
                $block = new Block( $blockOptions );
                $block->insert();
@@ -211,7 +214,7 @@ class BlockTest extends MediaWikiLangTestCase {
                        'enableAutoblock' => true,
                        'hideName' => true,
                        'blockEmail' => true,
-                       'byText' => 'MetaWikiUser',
+                       'byText' => 'Meta>MetaWikiUser',
                ];
                $block = new Block( $blockOptions );
 
@@ -227,8 +230,9 @@ class BlockTest extends MediaWikiLangTestCase {
                        'Correct blockee name'
                );
                $this->assertEquals( $userId, $block->getTarget()->getId(), 'Correct blockee id' );
-               $this->assertEquals( 'MetaWikiUser', $block->getBlocker(), 'Correct blocker name' );
-               $this->assertEquals( 'MetaWikiUser', $block->getByName(), 'Correct blocker name' );
+               $this->assertEquals( 'Meta>MetaWikiUser', $block->getBlocker()->getName(),
+                       'Correct blocker name' );
+               $this->assertEquals( 'Meta>MetaWikiUser', $block->getByName(), 'Correct blocker name' );
                $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
        }
 
@@ -279,6 +283,7 @@ class BlockTest extends MediaWikiLangTestCase {
                        ],
                ];
 
+               $blocker = $this->getTestUser()->getUser();
                foreach ( $blockList as $insBlock ) {
                        $target = $insBlock['target'];
 
@@ -290,7 +295,7 @@ class BlockTest extends MediaWikiLangTestCase {
 
                        $block = new Block();
                        $block->setTarget( $target );
-                       $block->setBlocker( 'testblocker@global' );
+                       $block->setBlocker( $blocker );
                        $block->mReason = $insBlock['desc'];
                        $block->mExpiry = 'infinity';
                        $block->prevents( 'createaccount', $insBlock['ACDisable'] );
@@ -351,6 +356,9 @@ class BlockTest extends MediaWikiLangTestCase {
         * @covers Block::chooseBlock
         */
        public function testBlocksOnXff( $xff, $exCount, $exResult ) {
+               $user = $this->getUserForBlocking();
+               $this->addBlockForUser( $user );
+
                $list = array_map( 'trim', explode( ',', $xff ) );
                $xffblocks = Block::getBlocksForIPList( $list, true );
                $this->assertEquals( $exCount, count( $xffblocks ), 'Number of blocks for ' . $xff );
@@ -409,4 +417,37 @@ class BlockTest extends MediaWikiLangTestCase {
                        "Account creation should not be blocked by default"
                );
        }
+
+       public function testSystemBlocks() {
+               $user = $this->getUserForBlocking();
+               $this->addBlockForUser( $user );
+
+               $blockOptions = [
+                       'address' => $user->getName(),
+                       'reason' => 'test system block',
+                       'timestamp' => wfTimestampNow(),
+                       'expiry' => $this->db->getInfinity(),
+                       'byText' => 'MediaWiki default',
+                       'systemBlock' => 'test',
+                       'enableAutoblock' => true,
+               ];
+               $block = new Block( $blockOptions );
+
+               $this->assertSame( 'test', $block->getSystemBlockType() );
+
+               try {
+                       $block->insert();
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( MWException $ex ) {
+                       $this->assertSame( 'Cannot insert a system block into the database', $ex->getMessage() );
+               }
+
+               try {
+                       $block->doAutoblock( '192.0.2.2' );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( MWException $ex ) {
+                       $this->assertSame( 'Cannot autoblock from a system block', $ex->getMessage() );
+               }
+       }
+
 }