X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Factions%2FActionTest.php;h=d80d627b4fac96628d3bf0a37e3b1432832e65c2;hb=dfec83932fd38a9086eb5a2e212889ad00f35b0e;hp=9c8b957b27417d2d16d7da7eda5fdcd1a2d0f86d;hpb=e0193327bd2661fc68a7b3541cf977f2baf0ac4f;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/actions/ActionTest.php b/tests/phpunit/includes/actions/ActionTest.php index 9c8b957b27..4d977cbf1e 100644 --- a/tests/phpunit/includes/actions/ActionTest.php +++ b/tests/phpunit/includes/actions/ActionTest.php @@ -1,5 +1,8 @@ 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; + } +}