Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / tests / phpunit / includes / actions / ActionTest.php
index 9c8b957..4d977cb 100644 (file)
@@ -1,5 +1,8 @@
 <?php
 
+use MediaWiki\Block\DatabaseBlock;
+use MediaWiki\Block\Restriction\PageRestriction;
+
 /**
  * @covers Action
  *
@@ -22,6 +25,8 @@ class ActionTest extends MediaWikiTestCase {
                        'edit' => true,
                        'revisiondelete' => SpecialPageAction::class,
                        'dummy' => true,
+                       'access' => 'ControlledAccessDummyAction',
+                       'unblock' => 'RequiresUnblockDummyAction',
                        'string' => 'NamedDummyAction',
                        'declared' => 'NonExistingClassName',
                        'callable' => [ $this, 'dummyActionCallback' ],
@@ -183,6 +188,53 @@ class ActionTest extends MediaWikiTestCase {
                return new CalledDummyAction( $context->getWikiPage(), $context );
        }
 
+       public function testCanExecute() {
+               $user = $this->getTestUser()->getUser();
+               $this->overrideUserPermissions( $user, 'access' );
+               $action = Action::factory( 'access', $this->getPage(), $this->getContext() );
+               $this->assertNull( $action->canExecute( $user ) );
+       }
+
+       public function testCanExecuteNoRight() {
+               $user = $this->getTestUser()->getUser();
+               $this->overrideUserPermissions( $user, [] );
+               $action = Action::factory( 'access', $this->getPage(), $this->getContext() );
+
+               try {
+                       $action->canExecute( $user );
+               } catch ( Exception $e ) {
+                       $this->assertInstanceOf( PermissionsError::class, $e );
+               }
+       }
+
+       public function testCanExecuteRequiresUnblock() {
+               $user = $this->getTestUser()->getUser();
+               $this->overrideUserPermissions( $user, [] );
+
+               $page = $this->getExistingTestPage();
+               $action = Action::factory( 'unblock', $page, $this->getContext() );
+
+               $block = new DatabaseBlock( [
+                       'address' => $user,
+                       'by' => $this->getTestSysop()->getUser()->getId(),
+                       'expiry' => 'infinity',
+                       'sitewide' => false,
+               ] );
+               $block->setRestrictions( [
+                       new PageRestriction( 0, $page->getTitle()->getArticleID() ),
+               ] );
+
+               $block->insert();
+
+               try {
+                       $action->canExecute( $user );
+               } catch ( Exception $e ) {
+                       $this->assertInstanceOf( UserBlockedError::class, $e );
+               }
+
+               $block->delete();
+       }
+
 }
 
 class DummyAction extends Action {
@@ -196,6 +248,10 @@ class DummyAction extends Action {
 
        public function execute() {
        }
+
+       public function canExecute( User $user ) {
+               return $this->checkCanExecute( $user );
+       }
 }
 
 class NamedDummyAction extends DummyAction {
@@ -206,3 +262,15 @@ class CalledDummyAction extends DummyAction {
 
 class InstantiatedDummyAction extends DummyAction {
 }
+
+class ControlledAccessDummyAction extends DummyAction {
+       public function getRestriction() {
+               return 'access';
+       }
+}
+
+class RequiresUnblockDummyAction extends DummyAction {
+       public function requiresUnblock() {
+               return true;
+       }
+}