MediaWiki REL1_28
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
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
187class 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
getContext()
Action.
testActionExists_doesNotRequireInstantiation()
testDisabledAction_exists()
dummyActionCallback()
testGetActionName_revisiondeleteWorkaround()
testGetActionName( $requestedAction, $expected)
actionProvider
testGetActionName_editredlinkWorkaround()
testGetActionName_whenCanNotUseWikiPage_defaultsToView()
testDisabledAction_factoryReturnsFalse()
testActionFactory( $requestedAction, $expected)
actionProvider
testGetActionName_historysubmitWorkaround()
getContext( $requestedAction=null)
testNull_defaultsToView()
testDisabledAction_isNotResolved()
testNull_doesNotExist()
testActionExists( $requestedAction, $expected)
actionProvider
testNull_canNotBeInstantiated()
Actions are things which can be done to pages (edit, delete, rollback, etc).
Definition Action.php:37
static factory( $action, Page $page, IContextSource $context=null)
Get an appropriate Action subclass for the given action.
Definition Action.php:95
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
static exists( $name)
Check if a given action is recognised, even if it's disabled.
Definition Action.php:169
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.
WebRequest clone which takes values from a provided array.
assertType( $type, $actual, $message='')
Asserts the type of the provided value.
setMwGlobals( $pairs, $value=null)
static getMain()
Static methods.
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition WikiPage.php:115
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition hooks.txt:2685
namespace are movable Hooks may change this value to override the return value of MWNamespace::isMovable(). 'NewDifferenceEngine' do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached $page
Definition hooks.txt:2534
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
$context
Definition load.php:50