MediaWiki  1.33.0
ActionTest.php
Go to the documentation of this file.
1 <?php
2 
4 
15 
16  protected function setUp() {
17  parent::setUp();
18 
19  $context = $this->getContext();
20  $this->setMwGlobals( 'wgActions', [
21  'null' => null,
22  'disabled' => false,
23  'view' => true,
24  'edit' => true,
25  'revisiondelete' => SpecialPageAction::class,
26  'dummy' => true,
27  'access' => 'ControlledAccessDummyAction',
28  'unblock' => 'RequiresUnblockDummyAction',
29  'string' => 'NamedDummyAction',
30  'declared' => 'NonExistingClassName',
31  'callable' => [ $this, 'dummyActionCallback' ],
32  'object' => new InstantiatedDummyAction( $context->getWikiPage(), $context ),
33  ] );
34  }
35 
36  private function getPage() {
37  return WikiPage::factory( Title::makeTitle( 0, 'Title' ) );
38  }
39 
40  private function getContext( $requestedAction = null ) {
41  $request = new FauxRequest( [ 'action' => $requestedAction ] );
42 
44  $context->setRequest( $request );
45  $context->setWikiPage( $this->getPage() );
46 
47  return $context;
48  }
49 
50  public function actionProvider() {
51  return [
52  [ 'dummy', 'DummyAction' ],
53  [ 'string', 'NamedDummyAction' ],
54  [ 'callable', 'CalledDummyAction' ],
55  [ 'object', 'InstantiatedDummyAction' ],
56 
57  // Capitalization is ignored
58  [ 'DUMMY', 'DummyAction' ],
59  [ 'STRING', 'NamedDummyAction' ],
60 
61  // Null and non-existing values
62  [ 'null', null ],
63  [ 'undeclared', null ],
64  [ '', null ],
65  [ false, null ],
66  ];
67  }
68 
74  public function testActionExists( $requestedAction, $expected ) {
75  $exists = Action::exists( $requestedAction );
76 
77  $this->assertSame( $expected !== null, $exists );
78  }
79 
81  // The method is not supposed to check if the action can be instantiated.
82  $exists = Action::exists( 'declared' );
83 
84  $this->assertTrue( $exists );
85  }
86 
92  public function testGetActionName( $requestedAction, $expected ) {
93  $context = $this->getContext( $requestedAction );
94  $actionName = Action::getActionName( $context );
95 
96  $this->assertEquals( $expected ?: 'nosuchaction', $actionName );
97  }
98 
100  // See https://phabricator.wikimedia.org/T22966
101  $context = $this->getContext( 'editredlink' );
102  $actionName = Action::getActionName( $context );
103 
104  $this->assertEquals( 'edit', $actionName );
105  }
106 
108  // See https://phabricator.wikimedia.org/T22966
109  $context = $this->getContext( 'historysubmit' );
110  $actionName = Action::getActionName( $context );
111 
112  $this->assertEquals( 'view', $actionName );
113  }
114 
116  // See https://phabricator.wikimedia.org/T22966
117  $context = $this->getContext( 'historysubmit' );
118  $context->getRequest()->setVal( 'revisiondelete', true );
119  $actionName = Action::getActionName( $context );
120 
121  $this->assertEquals( 'revisiondelete', $actionName );
122  }
123 
125  $request = new FauxRequest( [ 'action' => 'edit' ] );
127  $context->setRequest( $request );
128  $actionName = Action::getActionName( $context );
129 
130  $this->assertEquals( 'view', $actionName );
131  }
132 
138  public function testActionFactory( $requestedAction, $expected ) {
139  $context = $this->getContext();
140  $action = Action::factory( $requestedAction, $context->getWikiPage(), $context );
141 
142  $this->assertType( $expected ?: 'null', $action );
143  }
144 
145  public function testNull_doesNotExist() {
146  $exists = Action::exists( null );
147 
148  $this->assertFalse( $exists );
149  }
150 
151  public function testNull_defaultsToView() {
152  $context = $this->getContext( null );
153  $actionName = Action::getActionName( $context );
154 
155  $this->assertEquals( 'view', $actionName );
156  }
157 
158  public function testNull_canNotBeInstantiated() {
159  $page = $this->getPage();
160  $action = Action::factory( null, $page );
161 
162  $this->assertNull( $action );
163  }
164 
165  public function testDisabledAction_exists() {
166  $exists = Action::exists( 'disabled' );
167 
168  $this->assertTrue( $exists );
169  }
170 
172  $context = $this->getContext( 'disabled' );
173  $actionName = Action::getActionName( $context );
174 
175  $this->assertEquals( 'nosuchaction', $actionName );
176  }
177 
179  $page = $this->getPage();
180  $action = Action::factory( 'disabled', $page );
181 
182  $this->assertFalse( $action );
183  }
184 
185  public function dummyActionCallback() {
186  $context = $this->getContext();
187  return new CalledDummyAction( $context->getWikiPage(), $context );
188  }
189 
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 ) );
195  }
196 
197  public function testCanExecuteNoRight() {
198  $user = $this->getTestUser()->getUser();
199  $user->mRights = [];
200  $action = Action::factory( 'access', $this->getPage(), $this->getContext() );
201 
202  try {
203  $action->canExecute( $user );
204  } catch ( Exception $e ) {
205  $this->assertInstanceOf( PermissionsError::class, $e );
206  }
207  }
208 
209  public function testCanExecuteRequiresUnblock() {
210  $user = $this->getTestUser()->getUser();
211  $user->mRights = [];
212 
213  $page = $this->getExistingTestPage();
214  $action = Action::factory( 'unblock', $page, $this->getContext() );
215 
216  $block = new Block( [
217  'address' => $user,
218  'by' => $this->getTestSysop()->getUser()->getId(),
219  'expiry' => 'infinity',
220  'sitewide' => false,
221  ] );
222  $block->setRestrictions( [
223  new PageRestriction( 0, $page->getTitle()->getArticleID() ),
224  ] );
225 
226  $block->insert();
227 
228  try {
229  $action->canExecute( $user );
230  } catch ( Exception $e ) {
231  $this->assertInstanceOf( UserBlockedError::class, $e );
232  }
233 
234  $block->delete();
235  }
236 
237 }
238 
239 class DummyAction extends Action {
240 
241  public function getName() {
242  return static::class;
243  }
244 
245  public function show() {
246  }
247 
248  public function execute() {
249  }
250 
251  public function canExecute( User $user ) {
252  return $this->checkCanExecute( $user );
253  }
254 }
255 
257 }
258 
260 }
261 
263 }
264 
266  public function getRestriction() {
267  return 'access';
268  }
269 }
270 
272  public function requiresUnblock() {
273  return true;
274  }
275 }
Action\getActionName
static getActionName(IContextSource $context)
Get the action that will be executed, not necessarily the one passed passed through the "action" requ...
Definition: Action.php:123
DummyAction\execute
execute()
Definition: ActionTest.php:248
ActionTest\dummyActionCallback
dummyActionCallback()
Definition: ActionTest.php:185
$user
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1476
ActionTest\testCanExecute
testCanExecute()
Definition: ActionTest.php:190
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
ActionTest\testActionFactory
testActionFactory( $requestedAction, $expected)
actionProvider
Definition: ActionTest.php:138
$context
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition: hooks.txt:2636
MediaWikiTestCase\getTestUser
static getTestUser( $groups=[])
Convenience method for getting an immutable test user.
Definition: MediaWikiTestCase.php:180
DummyAction\getName
getName()
Return the name of the action this object responds to.
Definition: ActionTest.php:241
ActionTest\getPage
getPage()
Definition: ActionTest.php:36
ControlledAccessDummyAction
Definition: ActionTest.php:265
ActionTest\testGetActionName_historysubmitWorkaround
testGetActionName_historysubmitWorkaround()
Definition: ActionTest.php:107
DummyAction\canExecute
canExecute(User $user)
Definition: ActionTest.php:251
InstantiatedDummyAction
Definition: ActionTest.php:262
ActionTest\testDisabledAction_factoryReturnsFalse
testDisabledAction_factoryReturnsFalse()
Definition: ActionTest.php:178
ActionTest\testGetActionName
testGetActionName( $requestedAction, $expected)
actionProvider
Definition: ActionTest.php:92
ActionTest\testNull_doesNotExist
testNull_doesNotExist()
Definition: ActionTest.php:145
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Action
Actions are things which can be done to pages (edit, delete, rollback, etc).
Definition: Action.php:39
ActionTest\testGetActionName_editredlinkWorkaround
testGetActionName_editredlinkWorkaround()
Definition: ActionTest.php:99
CalledDummyAction
Definition: ActionTest.php:259
DerivativeContext
An IContextSource implementation which will inherit context from another source but allow individual ...
Definition: DerivativeContext.php:30
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:138
ActionTest\actionProvider
actionProvider()
Definition: ActionTest.php:50
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:709
ActionTest\testActionExists_doesNotRequireInstantiation
testActionExists_doesNotRequireInstantiation()
Definition: ActionTest.php:80
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
ActionTest\testCanExecuteNoRight
testCanExecuteNoRight()
Definition: ActionTest.php:197
RequiresUnblockDummyAction
Definition: ActionTest.php:271
Action\checkCanExecute
checkCanExecute(User $user)
Checks if the given user (identified by an object) can perform this action.
Definition: Action.php:307
ActionTest\testNull_canNotBeInstantiated
testNull_canNotBeInstantiated()
Definition: ActionTest.php:158
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
ActionTest\testActionExists
testActionExists( $requestedAction, $expected)
actionProvider
Definition: ActionTest.php:74
ActionTest\testGetActionName_whenCanNotUseWikiPage_defaultsToView
testGetActionName_whenCanNotUseWikiPage_defaultsToView()
Definition: ActionTest.php:124
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:576
ActionTest\testGetActionName_revisiondeleteWorkaround
testGetActionName_revisiondeleteWorkaround()
Definition: ActionTest.php:115
ControlledAccessDummyAction\getRestriction
getRestriction()
Get the permission required to perform this action.
Definition: ActionTest.php:266
null
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
$request
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2636
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2162
RequiresUnblockDummyAction\requiresUnblock
requiresUnblock()
Whether this action can still be executed by a blocked user.
Definition: ActionTest.php:272
ActionTest\setUp
setUp()
Definition: ActionTest.php:16
MediaWikiTestCase\getTestSysop
static getTestSysop()
Convenience method for getting an immutable admin test user.
Definition: MediaWikiTestCase.php:204
ActionTest\getContext
getContext( $requestedAction=null)
Definition: ActionTest.php:40
ActionTest\testDisabledAction_exists
testDisabledAction_exists()
Definition: ActionTest.php:165
ActionTest\testCanExecuteRequiresUnblock
testCanExecuteRequiresUnblock()
Definition: ActionTest.php:209
NamedDummyAction
Definition: ActionTest.php:256
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:430
ActionTest\testDisabledAction_isNotResolved
testDisabledAction_isNotResolved()
Definition: ActionTest.php:171
MediaWikiTestCase\getExistingTestPage
getExistingTestPage( $title=null)
Returns a WikiPage representing an existing page.
Definition: MediaWikiTestCase.php:220
Action\exists
static exists( $name)
Check if a given action is recognised, even if it's disabled.
Definition: Action.php:170
MediaWiki\Block\Restriction\PageRestriction
Definition: PageRestriction.php:25
Block
Definition: Block.php:31
DummyAction
Definition: ActionTest.php:239
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
DummyAction\show
show()
The main action entry point.
Definition: ActionTest.php:245
MediaWikiTestCase\assertType
assertType( $type, $actual, $message='')
Asserts the type of the provided value.
Definition: MediaWikiTestCase.php:2177
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:48
Action\factory
static factory( $action, Page $page, IContextSource $context=null)
Get an appropriate Action subclass for the given action.
Definition: Action.php:97
ActionTest\testNull_defaultsToView
testNull_defaultsToView()
Definition: ActionTest.php:151
ActionTest
Action.
Definition: ActionTest.php:14