MediaWiki REL1_32
ActionTest.php
Go to the documentation of this file.
1<?php
2
13
14 protected function setUp() {
15 parent::setUp();
16
17 $context = $this->getContext();
18 $this->setMwGlobals( 'wgActions', [
19 'null' => null,
20 'disabled' => false,
21 'view' => true,
22 'edit' => true,
23 'revisiondelete' => SpecialPageAction::class,
24 'dummy' => true,
25 'string' => 'NamedDummyAction',
26 'declared' => 'NonExistingClassName',
27 'callable' => [ $this, 'dummyActionCallback' ],
28 'object' => new InstantiatedDummyAction( $context->getWikiPage(), $context ),
29 ] );
30 }
31
32 private function getPage() {
33 return WikiPage::factory( Title::makeTitle( 0, 'Title' ) );
34 }
35
36 private function getContext( $requestedAction = null ) {
37 $request = new FauxRequest( [ 'action' => $requestedAction ] );
38
39 $context = new DerivativeContext( RequestContext::getMain() );
40 $context->setRequest( $request );
41 $context->setWikiPage( $this->getPage() );
42
43 return $context;
44 }
45
46 public function actionProvider() {
47 return [
48 [ 'dummy', 'DummyAction' ],
49 [ 'string', 'NamedDummyAction' ],
50 [ 'callable', 'CalledDummyAction' ],
51 [ 'object', 'InstantiatedDummyAction' ],
52
53 // Capitalization is ignored
54 [ 'DUMMY', 'DummyAction' ],
55 [ 'STRING', 'NamedDummyAction' ],
56
57 // Null and non-existing values
58 [ 'null', null ],
59 [ 'undeclared', null ],
60 [ '', null ],
61 [ false, null ],
62 ];
63 }
64
70 public function testActionExists( $requestedAction, $expected ) {
71 $exists = Action::exists( $requestedAction );
72
73 $this->assertSame( $expected !== null, $exists );
74 }
75
77 // The method is not supposed to check if the action can be instantiated.
78 $exists = Action::exists( 'declared' );
79
80 $this->assertTrue( $exists );
81 }
82
88 public function testGetActionName( $requestedAction, $expected ) {
89 $context = $this->getContext( $requestedAction );
90 $actionName = Action::getActionName( $context );
91
92 $this->assertEquals( $expected ?: 'nosuchaction', $actionName );
93 }
94
96 // See https://phabricator.wikimedia.org/T22966
97 $context = $this->getContext( 'editredlink' );
98 $actionName = Action::getActionName( $context );
99
100 $this->assertEquals( 'edit', $actionName );
101 }
102
104 // See https://phabricator.wikimedia.org/T22966
105 $context = $this->getContext( 'historysubmit' );
106 $actionName = Action::getActionName( $context );
107
108 $this->assertEquals( 'view', $actionName );
109 }
110
112 // See https://phabricator.wikimedia.org/T22966
113 $context = $this->getContext( 'historysubmit' );
114 $context->getRequest()->setVal( 'revisiondelete', true );
115 $actionName = Action::getActionName( $context );
116
117 $this->assertEquals( 'revisiondelete', $actionName );
118 }
119
121 $request = new FauxRequest( [ 'action' => 'edit' ] );
122 $context = new DerivativeContext( RequestContext::getMain() );
123 $context->setRequest( $request );
124 $actionName = Action::getActionName( $context );
125
126 $this->assertEquals( 'view', $actionName );
127 }
128
134 public function testActionFactory( $requestedAction, $expected ) {
135 $context = $this->getContext();
136 $action = Action::factory( $requestedAction, $context->getWikiPage(), $context );
137
138 $this->assertType( $expected ?: 'null', $action );
139 }
140
141 public function testNull_doesNotExist() {
142 $exists = Action::exists( null );
143
144 $this->assertFalse( $exists );
145 }
146
147 public function testNull_defaultsToView() {
148 $context = $this->getContext( null );
149 $actionName = Action::getActionName( $context );
150
151 $this->assertEquals( 'view', $actionName );
152 }
153
155 $page = $this->getPage();
156 $action = Action::factory( null, $page );
157
158 $this->assertNull( $action );
159 }
160
161 public function testDisabledAction_exists() {
162 $exists = Action::exists( 'disabled' );
163
164 $this->assertTrue( $exists );
165 }
166
168 $context = $this->getContext( 'disabled' );
169 $actionName = Action::getActionName( $context );
170
171 $this->assertEquals( 'nosuchaction', $actionName );
172 }
173
175 $page = $this->getPage();
176 $action = Action::factory( 'disabled', $page );
177
178 $this->assertFalse( $action );
179 }
180
181 public function dummyActionCallback() {
182 $context = $this->getContext();
183 return new CalledDummyAction( $context->getWikiPage(), $context );
184 }
185
186}
187
188class DummyAction extends Action {
189
190 public function getName() {
191 return static::class;
192 }
193
194 public function show() {
195 }
196
197 public function execute() {
198 }
199}
200
202}
203
205}
206
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:39
static factory( $action, Page $page, IContextSource $context=null)
Get an appropriate Action subclass for the given action.
Definition Action.php:97
static getActionName(IContextSource $context)
Get the action that will be executed, not necessarily the one passed passed through the "action" requ...
Definition Action.php:124
static exists( $name)
Check if a given action is recognised, even if it's disabled.
Definition Action.php:171
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)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
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:2880
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:2885
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