MediaWiki  1.30.0
ActionTest.php
Go to the documentation of this file.
1 <?php
2 
12 
13  protected function setUp() {
14  parent::setUp();
15 
16  $context = $this->getContext();
17  $this->setMwGlobals( 'wgActions', [
18  'null' => null,
19  'disabled' => false,
20  'view' => true,
21  'edit' => true,
22  'revisiondelete' => 'SpecialPageAction',
23  'dummy' => true,
24  'string' => 'NamedDummyAction',
25  'declared' => 'NonExistingClassName',
26  'callable' => [ $this, 'dummyActionCallback' ],
27  'object' => new InstantiatedDummyAction( $context->getWikiPage(), $context ),
28  ] );
29  }
30 
31  private function getPage() {
32  return WikiPage::factory( Title::makeTitle( 0, 'Title' ) );
33  }
34 
35  private function getContext( $requestedAction = null ) {
36  $request = new FauxRequest( [ 'action' => $requestedAction ] );
37 
39  $context->setRequest( $request );
40  $context->setWikiPage( $this->getPage() );
41 
42  return $context;
43  }
44 
45  public function actionProvider() {
46  return [
47  [ 'dummy', 'DummyAction' ],
48  [ 'string', 'NamedDummyAction' ],
49  [ 'callable', 'CalledDummyAction' ],
50  [ 'object', 'InstantiatedDummyAction' ],
51 
52  // Capitalization is ignored
53  [ 'DUMMY', 'DummyAction' ],
54  [ 'STRING', 'NamedDummyAction' ],
55 
56  // Null and non-existing values
57  [ 'null', null ],
58  [ 'undeclared', null ],
59  [ '', null ],
60  [ false, null ],
61  ];
62  }
63 
69  public function testActionExists( $requestedAction, $expected ) {
70  $exists = Action::exists( $requestedAction );
71 
72  $this->assertSame( $expected !== null, $exists );
73  }
74 
76  // The method is not supposed to check if the action can be instantiated.
77  $exists = Action::exists( 'declared' );
78 
79  $this->assertTrue( $exists );
80  }
81 
87  public function testGetActionName( $requestedAction, $expected ) {
88  $context = $this->getContext( $requestedAction );
89  $actionName = Action::getActionName( $context );
90 
91  $this->assertEquals( $expected ?: 'nosuchaction', $actionName );
92  }
93 
95  // See https://phabricator.wikimedia.org/T22966
96  $context = $this->getContext( 'editredlink' );
97  $actionName = Action::getActionName( $context );
98 
99  $this->assertEquals( 'edit', $actionName );
100  }
101 
103  // See https://phabricator.wikimedia.org/T22966
104  $context = $this->getContext( 'historysubmit' );
105  $actionName = Action::getActionName( $context );
106 
107  $this->assertEquals( 'view', $actionName );
108  }
109 
111  // See https://phabricator.wikimedia.org/T22966
112  $context = $this->getContext( 'historysubmit' );
113  $context->getRequest()->setVal( 'revisiondelete', true );
114  $actionName = Action::getActionName( $context );
115 
116  $this->assertEquals( 'revisiondelete', $actionName );
117  }
118 
120  $request = new FauxRequest( [ 'action' => 'edit' ] );
122  $context->setRequest( $request );
123  $actionName = Action::getActionName( $context );
124 
125  $this->assertEquals( 'view', $actionName );
126  }
127 
133  public function testActionFactory( $requestedAction, $expected ) {
134  $context = $this->getContext();
135  $action = Action::factory( $requestedAction, $context->getWikiPage(), $context );
136 
137  $this->assertType( $expected ?: 'null', $action );
138  }
139 
140  public function testNull_doesNotExist() {
141  $exists = Action::exists( null );
142 
143  $this->assertFalse( $exists );
144  }
145 
146  public function testNull_defaultsToView() {
147  $context = $this->getContext( null );
148  $actionName = Action::getActionName( $context );
149 
150  $this->assertEquals( 'view', $actionName );
151  }
152 
153  public function testNull_canNotBeInstantiated() {
154  $page = $this->getPage();
155  $action = Action::factory( null, $page );
156 
157  $this->assertNull( $action );
158  }
159 
160  public function testDisabledAction_exists() {
161  $exists = Action::exists( 'disabled' );
162 
163  $this->assertTrue( $exists );
164  }
165 
167  $context = $this->getContext( 'disabled' );
168  $actionName = Action::getActionName( $context );
169 
170  $this->assertEquals( 'nosuchaction', $actionName );
171  }
172 
174  $page = $this->getPage();
175  $action = Action::factory( 'disabled', $page );
176 
177  $this->assertFalse( $action );
178  }
179 
180  public function dummyActionCallback() {
181  $context = $this->getContext();
182  return new CalledDummyAction( $context->getWikiPage(), $context );
183  }
184 
185 }
186 
187 class DummyAction extends Action {
188 
189  public function getName() {
190  return static::class;
191  }
192 
193  public function show() {
194  }
195 
196  public function execute() {
197  }
198 }
199 
201 }
202 
204 }
205 
207 }
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:122
DummyAction\execute
execute()
Definition: ActionTest.php:196
ActionTest\dummyActionCallback
dummyActionCallback()
Definition: ActionTest.php:180
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
ActionTest\testActionFactory
testActionFactory( $requestedAction, $expected)
actionProvider
Definition: ActionTest.php:133
$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:2581
DummyAction\getName
getName()
Return the name of the action this object responds to.
Definition: ActionTest.php:189
ActionTest\getPage
getPage()
Definition: ActionTest.php:31
ActionTest\testGetActionName_historysubmitWorkaround
testGetActionName_historysubmitWorkaround()
Definition: ActionTest.php:102
InstantiatedDummyAction
Definition: ActionTest.php:206
ActionTest\testDisabledAction_factoryReturnsFalse
testDisabledAction_factoryReturnsFalse()
Definition: ActionTest.php:173
ActionTest\testGetActionName
testGetActionName( $requestedAction, $expected)
actionProvider
Definition: ActionTest.php:87
ActionTest\testNull_doesNotExist
testNull_doesNotExist()
Definition: ActionTest.php:140
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:37
ActionTest\testGetActionName_editredlinkWorkaround
testGetActionName_editredlinkWorkaround()
Definition: ActionTest.php:94
CalledDummyAction
Definition: ActionTest.php:203
DerivativeContext
An IContextSource implementation which will inherit context from another source but allow individual ...
Definition: DerivativeContext.php:31
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:121
ActionTest\actionProvider
actionProvider()
Definition: ActionTest.php:45
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:672
ActionTest\testActionExists_doesNotRequireInstantiation
testActionExists_doesNotRequireInstantiation()
Definition: ActionTest.php:75
MediaWikiTestCase
Definition: MediaWikiTestCase.php:15
ActionTest\testNull_canNotBeInstantiated
testNull_canNotBeInstantiated()
Definition: ActionTest.php:153
ActionTest\testActionExists
testActionExists( $requestedAction, $expected)
actionProvider
Definition: ActionTest.php:69
ActionTest\testGetActionName_whenCanNotUseWikiPage_defaultsToView
testGetActionName_whenCanNotUseWikiPage_defaultsToView()
Definition: ActionTest.php:119
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:529
ActionTest\testGetActionName_revisiondeleteWorkaround
testGetActionName_revisiondeleteWorkaround()
Definition: ActionTest.php:110
$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:2581
ActionTest\setUp
setUp()
Definition: ActionTest.php:13
ActionTest\getContext
getContext( $requestedAction=null)
Definition: ActionTest.php:35
ActionTest\testDisabledAction_exists
testDisabledAction_exists()
Definition: ActionTest.php:160
NamedDummyAction
Definition: ActionTest.php:200
RequestContext\getMain
static getMain()
Static methods.
Definition: RequestContext.php:470
ActionTest\testDisabledAction_isNotResolved
testDisabledAction_isNotResolved()
Definition: ActionTest.php:166
Action\exists
static exists( $name)
Check if a given action is recognised, even if it's disabled.
Definition: Action.php:169
DummyAction
Definition: ActionTest.php:187
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:193
MediaWikiTestCase\assertType
assertType( $type, $actual, $message='')
Asserts the type of the provided value.
Definition: MediaWikiTestCase.php:1659
Action\factory
static factory( $action, Page $page, IContextSource $context=null)
Get an appropriate Action subclass for the given action.
Definition: Action.php:95
ActionTest\testNull_defaultsToView
testNull_defaultsToView()
Definition: ActionTest.php:146
ActionTest
Action.
Definition: ActionTest.php:11