3 use MediaWiki\Block\Restriction\PageRestriction
;
11 * @license GPL-2.0-or-later
12 * @author Thiemo Kreuz
14 class ActionTest
extends MediaWikiTestCase
{
16 protected function setUp() {
19 $context = $this->getContext();
20 $this->setMwGlobals( 'wgActions', [
25 'revisiondelete' => SpecialPageAction
::class,
27 'access' => 'ControlledAccessDummyAction',
28 'unblock' => 'RequiresUnblockDummyAction',
29 'string' => 'NamedDummyAction',
30 'declared' => 'NonExistingClassName',
31 'callable' => [ $this, 'dummyActionCallback' ],
32 'object' => new InstantiatedDummyAction( $context->getWikiPage(), $context ),
36 private function getPage() {
37 return WikiPage
::factory( Title
::makeTitle( 0, 'Title' ) );
40 private function getContext( $requestedAction = null ) {
41 $request = new FauxRequest( [ 'action' => $requestedAction ] );
43 $context = new DerivativeContext( RequestContext
::getMain() );
44 $context->setRequest( $request );
45 $context->setWikiPage( $this->getPage() );
50 public function actionProvider() {
52 [ 'dummy', 'DummyAction' ],
53 [ 'string', 'NamedDummyAction' ],
54 [ 'callable', 'CalledDummyAction' ],
55 [ 'object', 'InstantiatedDummyAction' ],
57 // Capitalization is ignored
58 [ 'DUMMY', 'DummyAction' ],
59 [ 'STRING', 'NamedDummyAction' ],
61 // Null and non-existing values
63 [ 'undeclared', null ],
70 * @dataProvider actionProvider
71 * @param string $requestedAction
72 * @param string|null $expected
74 public function testActionExists( $requestedAction, $expected ) {
75 $exists = Action
::exists( $requestedAction );
77 $this->assertSame( $expected !== null, $exists );
80 public function testActionExists_doesNotRequireInstantiation() {
81 // The method is not supposed to check if the action can be instantiated.
82 $exists = Action
::exists( 'declared' );
84 $this->assertTrue( $exists );
88 * @dataProvider actionProvider
89 * @param string $requestedAction
90 * @param string|null $expected
92 public function testGetActionName( $requestedAction, $expected ) {
93 $context = $this->getContext( $requestedAction );
94 $actionName = Action
::getActionName( $context );
96 $this->assertEquals( $expected ?
: 'nosuchaction', $actionName );
99 public function testGetActionName_editredlinkWorkaround() {
100 // See https://phabricator.wikimedia.org/T22966
101 $context = $this->getContext( 'editredlink' );
102 $actionName = Action
::getActionName( $context );
104 $this->assertEquals( 'edit', $actionName );
107 public function testGetActionName_historysubmitWorkaround() {
108 // See https://phabricator.wikimedia.org/T22966
109 $context = $this->getContext( 'historysubmit' );
110 $actionName = Action
::getActionName( $context );
112 $this->assertEquals( 'view', $actionName );
115 public function testGetActionName_revisiondeleteWorkaround() {
116 // See https://phabricator.wikimedia.org/T22966
117 $context = $this->getContext( 'historysubmit' );
118 $context->getRequest()->setVal( 'revisiondelete', true );
119 $actionName = Action
::getActionName( $context );
121 $this->assertEquals( 'revisiondelete', $actionName );
124 public function testGetActionName_whenCanNotUseWikiPage_defaultsToView() {
125 $request = new FauxRequest( [ 'action' => 'edit' ] );
126 $context = new DerivativeContext( RequestContext
::getMain() );
127 $context->setRequest( $request );
128 $actionName = Action
::getActionName( $context );
130 $this->assertEquals( 'view', $actionName );
134 * @dataProvider actionProvider
135 * @param string $requestedAction
136 * @param string|null $expected
138 public function testActionFactory( $requestedAction, $expected ) {
139 $context = $this->getContext();
140 $action = Action
::factory( $requestedAction, $context->getWikiPage(), $context );
142 $this->assertType( $expected ?
: 'null', $action );
145 public function testNull_doesNotExist() {
146 $exists = Action
::exists( null );
148 $this->assertFalse( $exists );
151 public function testNull_defaultsToView() {
152 $context = $this->getContext( null );
153 $actionName = Action
::getActionName( $context );
155 $this->assertEquals( 'view', $actionName );
158 public function testNull_canNotBeInstantiated() {
159 $page = $this->getPage();
160 $action = Action
::factory( null, $page );
162 $this->assertNull( $action );
165 public function testDisabledAction_exists() {
166 $exists = Action
::exists( 'disabled' );
168 $this->assertTrue( $exists );
171 public function testDisabledAction_isNotResolved() {
172 $context = $this->getContext( 'disabled' );
173 $actionName = Action
::getActionName( $context );
175 $this->assertEquals( 'nosuchaction', $actionName );
178 public function testDisabledAction_factoryReturnsFalse() {
179 $page = $this->getPage();
180 $action = Action
::factory( 'disabled', $page );
182 $this->assertFalse( $action );
185 public function dummyActionCallback() {
186 $context = $this->getContext();
187 return new CalledDummyAction( $context->getWikiPage(), $context );
190 public function testCanExecute() {
191 $user = $this->getTestUser()->getUser();
192 $user->mRights
= [ 'access' ];
193 $action = Action
::factory( 'access', $this->getPage(), $this->getContext() );
194 $this->assertNull( $action->canExecute( $user ) );
197 public function testCanExecuteNoRight() {
198 $user = $this->getTestUser()->getUser();
200 $action = Action
::factory( 'access', $this->getPage(), $this->getContext() );
203 $action->canExecute( $user );
204 } catch ( Exception
$e ) {
205 $this->assertInstanceOf( PermissionsError
::class, $e );
209 public function testCanExecuteRequiresUnblock() {
210 $user = $this->getTestUser()->getUser();
213 $page = $this->getExistingTestPage();
214 $action = Action
::factory( 'unblock', $page, $this->getContext() );
216 $block = new Block( [
218 'by' => $this->getTestSysop()->getUser()->getId(),
219 'expiry' => 'infinity',
222 $block->setRestrictions( [
223 new PageRestriction( 0, $page->getTitle()->getArticleID() ),
229 $action->canExecute( $user );
230 } catch ( Exception
$e ) {
231 $this->assertInstanceOf( UserBlockedError
::class, $e );
239 class DummyAction
extends Action
{
241 public function getName() {
242 return static::class;
245 public function show() {
248 public function execute() {
251 public function canExecute( User
$user ) {
252 return $this->checkCanExecute( $user );
256 class NamedDummyAction
extends DummyAction
{
259 class CalledDummyAction
extends DummyAction
{
262 class InstantiatedDummyAction
extends DummyAction
{
265 class ControlledAccessDummyAction
extends DummyAction
{
266 public function getRestriction() {
271 class RequiresUnblockDummyAction
extends DummyAction
{
272 public function requiresUnblock() {