Merge "Make LinkCache::isCacheable include namespaces like NS_CATEGORY/NS_MODULE"
[lhc/web/wiklou.git] / tests / phpunit / includes / BlockTest.php
index 7d844b5..1f11ef2 100644 (file)
@@ -2,6 +2,7 @@
 
 use MediaWiki\Block\BlockRestriction;
 use MediaWiki\Block\Restriction\PageRestriction;
+use MediaWiki\Block\Restriction\NamespaceRestriction;
 
 /**
  * @group Database
@@ -130,7 +131,7 @@ class BlockTest extends MediaWikiLangTestCase {
        }
 
        /**
-        * @covers Block::prevents
+        * @covers Block::appliesToRight
         */
        public function testBlockedUserCanNotCreateAccount() {
                $username = 'BlockedUserToCreateAccountWith';
@@ -173,8 +174,8 @@ class BlockTest extends MediaWikiLangTestCase {
                // Reload block from DB
                $userBlock = Block::newFromTarget( $username );
                $this->assertTrue(
-                       (bool)$block->prevents( 'createaccount' ),
-                       "Block object in DB should prevents 'createaccount'"
+                       (bool)$block->appliesToRight( 'createaccount' ),
+                       "Block object in DB should block right 'createaccount'"
                );
 
                $this->assertInstanceOf(
@@ -303,7 +304,7 @@ class BlockTest extends MediaWikiLangTestCase {
                        $block->setBlocker( $blocker );
                        $block->mReason = $insBlock['desc'];
                        $block->mExpiry = 'infinity';
-                       $block->prevents( 'createaccount', $insBlock['ACDisable'] );
+                       $block->isCreateAccountBlocked( $insBlock['ACDisable'] );
                        $block->isHardblock( $insBlock['isHardblock'] );
                        $block->isAutoblocking( $insBlock['isAutoBlocking'] );
                        $block->insert();
@@ -421,7 +422,7 @@ class BlockTest extends MediaWikiLangTestCase {
 
                # Check default parameter
                $this->assertFalse(
-                       (bool)$block->prevents( 'createaccount' ),
+                       (bool)$block->appliesToRight( 'createaccount' ),
                        "Account creation should not be blocked by default"
                );
        }
@@ -615,6 +616,9 @@ class BlockTest extends MediaWikiLangTestCase {
         * @covers Block::appliesToTitle
         */
        public function testAppliesToTitleReturnsTrueOnSitewideBlock() {
+               $this->setMwGlobals( [
+                       'wgBlockDisablesLogin' => false,
+               ] );
                $user = $this->getTestUser()->getUser();
                $block = new Block( [
                        'expiry' => wfTimestamp( TS_MW, wfTimestamp() + ( 40 * 60 * 60 ) ),
@@ -641,6 +645,9 @@ class BlockTest extends MediaWikiLangTestCase {
         * @covers Block::appliesToTitle
         */
        public function testAppliesToTitleOnPartialBlock() {
+               $this->setMwGlobals( [
+                       'wgBlockDisablesLogin' => false,
+               ] );
                $user = $this->getTestUser()->getUser();
                $block = new Block( [
                        'expiry' => wfTimestamp( TS_MW, wfTimestamp() + ( 40 * 60 * 60 ) ),
@@ -654,14 +661,114 @@ class BlockTest extends MediaWikiLangTestCase {
 
                $pageFoo = $this->getExistingTestPage( 'Foo' );
                $pageBar = $this->getExistingTestPage( 'Bar' );
+               $pageJohn = $this->getExistingTestPage( 'User:John' );
 
                $pageRestriction = new PageRestriction( $block->getId(), $pageFoo->getId() );
-               BlockRestriction::insert( [ $pageRestriction ] );
+               $namespaceRestriction = new NamespaceRestriction( $block->getId(), NS_USER );
+               BlockRestriction::insert( [ $pageRestriction, $namespaceRestriction ] );
 
                $this->assertTrue( $block->appliesToTitle( $pageFoo->getTitle() ) );
                $this->assertFalse( $block->appliesToTitle( $pageBar->getTitle() ) );
+               $this->assertTrue( $block->appliesToTitle( $pageJohn->getTitle() ) );
 
                $block->delete();
        }
 
+       /**
+        * @covers Block::appliesToNamespace
+        * @covers Block::appliesToPage
+        */
+       public function testAppliesToReturnsTrueOnSitewideBlock() {
+               $this->setMwGlobals( [
+                       'wgBlockDisablesLogin' => false,
+               ] );
+               $user = $this->getTestUser()->getUser();
+               $block = new Block( [
+                       'expiry' => wfTimestamp( TS_MW, wfTimestamp() + ( 40 * 60 * 60 ) ),
+                       'allowUsertalk' => true,
+                       'sitewide' => true
+               ] );
+
+               $block->setTarget( $user );
+               $block->setBlocker( $this->getTestSysop()->getUser() );
+               $block->insert();
+
+               $title = $this->getExistingTestPage()->getTitle();
+
+               $this->assertTrue( $block->appliesToPage( $title->getArticleID() ) );
+               $this->assertTrue( $block->appliesToNamespace( NS_MAIN ) );
+               $this->assertTrue( $block->appliesToNamespace( NS_USER_TALK ) );
+
+               $block->delete();
+       }
+
+       /**
+        * @covers Block::appliesToPage
+        */
+       public function testAppliesToPageOnPartialPageBlock() {
+               $this->setMwGlobals( [
+                       'wgBlockDisablesLogin' => false,
+               ] );
+               $user = $this->getTestUser()->getUser();
+               $block = new Block( [
+                       'expiry' => wfTimestamp( TS_MW, wfTimestamp() + ( 40 * 60 * 60 ) ),
+                       'allowUsertalk' => true,
+                       'sitewide' => false
+               ] );
+
+               $block->setTarget( $user );
+               $block->setBlocker( $this->getTestSysop()->getUser() );
+               $block->insert();
+
+               $title = $this->getExistingTestPage()->getTitle();
+
+               $pageRestriction = new PageRestriction(
+                       $block->getId(),
+                       $title->getArticleID()
+               );
+               BlockRestriction::insert( [ $pageRestriction ] );
+
+               $this->assertTrue( $block->appliesToPage( $title->getArticleID() ) );
+
+               $block->delete();
+       }
+
+       /**
+        * @covers Block::appliesToNamespace
+        */
+       public function testAppliesToNamespaceOnPartialNamespaceBlock() {
+               $this->setMwGlobals( [
+                       'wgBlockDisablesLogin' => false,
+               ] );
+               $user = $this->getTestUser()->getUser();
+               $block = new Block( [
+                       'expiry' => wfTimestamp( TS_MW, wfTimestamp() + ( 40 * 60 * 60 ) ),
+                       'allowUsertalk' => true,
+                       'sitewide' => false
+               ] );
+
+               $block->setTarget( $user );
+               $block->setBlocker( $this->getTestSysop()->getUser() );
+               $block->insert();
+
+               $namespaceRestriction = new NamespaceRestriction( $block->getId(), NS_MAIN );
+               BlockRestriction::insert( [ $namespaceRestriction ] );
+
+               $this->assertTrue( $block->appliesToNamespace( NS_MAIN ) );
+               $this->assertFalse( $block->appliesToNamespace( NS_USER ) );
+
+               $block->delete();
+       }
+
+       /**
+        * @covers Block::appliesToRight
+        */
+       public function testBlockAllowsPurge() {
+               $this->setMwGlobals( [
+                       'wgBlockDisablesLogin' => false,
+               ] );
+               $block = new Block();
+               $this->assertFalse( $block->appliesToRight( 'purge' ) );
+       }
+
 }