MediaWiki REL1_33
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
43 $context = new DerivativeContext( RequestContext::getMain() );
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' ] );
126 $context = new DerivativeContext( RequestContext::getMain() );
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
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
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
239class 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
264
266 public function getRestriction() {
267 return 'access';
268 }
269}
270
272 public function requiresUnblock() {
273 return true;
274 }
275}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
getContext()
Action.
testActionExists_doesNotRequireInstantiation()
testDisabledAction_exists()
dummyActionCallback()
testGetActionName_revisiondeleteWorkaround()
testGetActionName( $requestedAction, $expected)
actionProvider
testCanExecuteRequiresUnblock()
testGetActionName_editredlinkWorkaround()
testGetActionName_whenCanNotUseWikiPage_defaultsToView()
testDisabledAction_factoryReturnsFalse()
testActionFactory( $requestedAction, $expected)
actionProvider
testGetActionName_historysubmitWorkaround()
getContext( $requestedAction=null)
testNull_defaultsToView()
testDisabledAction_isNotResolved()
testCanExecuteNoRight()
testNull_doesNotExist()
testActionExists( $requestedAction, $expected)
actionProvider
testNull_canNotBeInstantiated()
Actions are things which can be done to pages (edit, delete, rollback, etc).
Definition Action.php:39
checkCanExecute(User $user)
Checks if the given user (identified by an object) can perform this action.
Definition Action.php:307
getRestriction()
Get the permission required to perform this action.
An IContextSource implementation which will inherit context from another source but allow individual ...
getName()
Return the name of the action this object responds to.
show()
The main action entry point.
canExecute(User $user)
WebRequest clone which takes values from a provided array.
getExistingTestPage( $title=null)
Returns a WikiPage representing an existing page.
static getTestSysop()
Convenience method for getting an immutable admin test user.
assertType( $type, $actual, $message='')
Asserts the type of the provided value.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
static getTestUser( $groups=[])
Convenience method for getting an immutable test user.
requiresUnblock()
Whether this action can still be executed by a blocked user.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:48
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:2843
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:2848
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:783
return true to allow those checks to and false if checking is done & $user
Definition hooks.txt:1510
returning false will NOT prevent logging $e
Definition hooks.txt:2175
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:37