Add block restriction classes
[lhc/web/wiklou.git] / tests / phpunit / includes / BlockTest.php
index b765494..8760d5e 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+use MediaWiki\Block\Restriction\PageRestriction;
+
 /**
  * @group Database
  * @group Blocking
@@ -35,6 +37,7 @@ class BlockTest extends MediaWikiLangTestCase {
                $blockOptions = [
                        'address' => $user->getName(),
                        'user' => $user->getId(),
+                       'by' => $this->getTestSysop()->getUser()->getId(),
                        'reason' => 'Parce que',
                        'expiry' => time() + 100500,
                ];
@@ -83,7 +86,7 @@ class BlockTest extends MediaWikiLangTestCase {
         * per T28425
         * @covers Block::__construct
         */
-       public function testBug26425BlockTimestampDefaultsToTime() {
+       public function testT28425BlockTimestampDefaultsToTime() {
                $user = $this->getUserForBlocking();
                $block = $this->addBlockForUser( $user );
                $madeAt = wfTimestamp( TS_MW );
@@ -102,10 +105,10 @@ class BlockTest extends MediaWikiLangTestCase {
         * because the new function didn't accept empty strings like Block::load()
         * had. Regression T31116.
         *
-        * @dataProvider provideBug29116Data
+        * @dataProvider provideT31116Data
         * @covers Block::newFromTarget
         */
-       public function testBug29116NewFromTargetWithEmptyIp( $vagueTarget ) {
+       public function testT31116NewFromTargetWithEmptyIp( $vagueTarget ) {
                $user = $this->getUserForBlocking();
                $initialBlock = $this->addBlockForUser( $user );
                $block = Block::newFromTarget( $user->getName(), $vagueTarget );
@@ -117,7 +120,7 @@ class BlockTest extends MediaWikiLangTestCase {
                );
        }
 
-       public static function provideBug29116Data() {
+       public static function provideT31116Data() {
                return [
                        [ null ],
                        [ '' ],
@@ -174,7 +177,7 @@ class BlockTest extends MediaWikiLangTestCase {
                );
 
                $this->assertInstanceOf(
-                       'Block',
+                       Block::class,
                        $userBlock,
                        "'$username' block block object should be existent"
                );
@@ -393,7 +396,7 @@ class BlockTest extends MediaWikiLangTestCase {
                $block = new Block(
                        /* address */ $username,
                        /* user */ 0,
-                       /* by */ 0,
+                       /* by */ $this->getTestSysop()->getUser()->getId(),
                        /* reason */ $reason,
                        /* timestamp */ 0,
                        /* auto */ false,
@@ -459,4 +462,152 @@ class BlockTest extends MediaWikiLangTestCase {
                }
        }
 
+       /**
+        * @covers Block::newFromRow
+        */
+       public function testNewFromRow() {
+               $badActor = $this->getTestUser()->getUser();
+               $sysop = $this->getTestSysop()->getUser();
+
+               $block = new Block( [
+                       'address' => $badActor->getName(),
+                       'user' => $badActor->getId(),
+                       'by' => $sysop->getId(),
+                       'expiry' => 'infinity',
+               ] );
+               $block->insert();
+
+               $blockQuery = Block::getQueryInfo();
+               $row = $this->db->select(
+                       $blockQuery['tables'],
+                       $blockQuery['fields'],
+                       [
+                               'ipb_id' => $block->getId(),
+                       ],
+                       __METHOD__,
+                       [],
+                       $blockQuery['joins']
+               )->fetchObject();
+
+               $block = Block::newFromRow( $row );
+               $this->assertInstanceOf( Block::class, $block );
+               $this->assertEquals( $block->getBy(), $sysop->getId() );
+               $this->assertEquals( $block->getTarget()->getName(), $badActor->getName() );
+               $block->delete();
+       }
+
+       /**
+        * @covers Block::equals
+        */
+       public function testEquals() {
+               $block = new Block();
+
+               $this->assertTrue( $block->equals( $block ) );
+
+               $partial = new Block( [
+                       'sitewide' => false,
+               ] );
+               $this->assertFalse( $block->equals( $partial ) );
+       }
+
+       /**
+        * @covers Block::isSitewide
+        */
+       public function testIsSitewide() {
+               $block = new Block();
+               $this->assertTrue( $block->isSitewide() );
+
+               $block = new Block( [
+                       'sitewide' => true,
+               ] );
+               $this->assertTrue( $block->isSitewide() );
+
+               $block = new Block( [
+                       'sitewide' => false,
+               ] );
+               $this->assertFalse( $block->isSitewide() );
+
+               $block = new Block( [
+                       'sitewide' => false,
+               ] );
+               $block->isSitewide( true );
+               $this->assertTrue( $block->isSitewide() );
+       }
+
+       /**
+        * @covers Block::getRestrictions
+        * @covers Block::setRestrictions
+        */
+       public function testRestrictions() {
+               $block = new Block();
+               $restrictions = [
+                       new PageRestriction( 0, 1 )
+               ];
+               $block->setRestrictions( $restrictions );
+
+               $this->assertSame( $restrictions, $block->getRestrictions() );
+       }
+
+       /**
+        * @covers Block::getRestrictions
+        * @covers Block::insert
+        */
+       public function testRestrictionsFromDatabase() {
+               $badActor = $this->getTestUser()->getUser();
+               $sysop = $this->getTestSysop()->getUser();
+
+               $block = new Block( [
+                       'address' => $badActor->getName(),
+                       'user' => $badActor->getId(),
+                       'by' => $sysop->getId(),
+                       'expiry' => 'infinity',
+               ] );
+               $page = $this->getExistingTestPage( 'Foo' );
+               $restriction = new PageRestriction( 0, $page->getId() );
+               $block->setRestrictions( [ $restriction ] );
+               $block->insert();
+
+               // Refresh the block from the database.
+               $block = Block::newFromID( $block->getId() );
+               $restrictions = $block->getRestrictions();
+               $this->assertCount( 1, $restrictions );
+               $this->assertTrue( $restriction->equals( $restrictions[0] ) );
+               $block->delete();
+       }
+
+       /**
+        * @covers Block::insert
+        */
+       public function testInsertExistingBlock() {
+               $badActor = $this->getTestUser()->getUser();
+               $sysop = $this->getTestSysop()->getUser();
+
+               $block = new Block( [
+                       'address' => $badActor->getName(),
+                       'user' => $badActor->getId(),
+                       'by' => $sysop->getId(),
+                       'expiry' => 'infinity',
+               ] );
+               $page = $this->getExistingTestPage( 'Foo' );
+               $restriction = new PageRestriction( 0, $page->getId() );
+               $block->setRestrictions( [ $restriction ] );
+               $block->insert();
+
+               // Insert the block again, which should result in a failur
+               $result = $block->insert();
+
+               $this->assertFalse( $result );
+
+               // Ensure that there are no restrictions where the blockId is 0.
+               $count = $this->db->selectRowCount(
+                       'ipblocks_restrictions',
+                       '*',
+                       [ 'ir_ipb_id' => 0 ],
+                       __METHOD__
+               );
+               $this->assertSame( 0, $count );
+
+               $block->delete();
+       }
+
 }