MediaWiki  1.32.0
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 
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' ] );
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 
154  public function testNull_canNotBeInstantiated() {
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 
188 class 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 
208 }
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:124
DummyAction\execute
execute()
Definition: ActionTest.php:197
ActionTest\dummyActionCallback
dummyActionCallback()
Definition: ActionTest.php:181
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
ActionTest\testActionFactory
testActionFactory( $requestedAction, $expected)
actionProvider
Definition: ActionTest.php:134
$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:2675
DummyAction\getName
getName()
Return the name of the action this object responds to.
Definition: ActionTest.php:190
ActionTest\getPage
getPage()
Definition: ActionTest.php:32
ActionTest\testGetActionName_historysubmitWorkaround
testGetActionName_historysubmitWorkaround()
Definition: ActionTest.php:103
InstantiatedDummyAction
Definition: ActionTest.php:207
ActionTest\testDisabledAction_factoryReturnsFalse
testDisabledAction_factoryReturnsFalse()
Definition: ActionTest.php:174
ActionTest\testGetActionName
testGetActionName( $requestedAction, $expected)
actionProvider
Definition: ActionTest.php:88
ActionTest\testNull_doesNotExist
testNull_doesNotExist()
Definition: ActionTest.php:141
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:95
CalledDummyAction
Definition: ActionTest.php:204
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:127
ActionTest\actionProvider
actionProvider()
Definition: ActionTest.php:46
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:706
ActionTest\testActionExists_doesNotRequireInstantiation
testActionExists_doesNotRequireInstantiation()
Definition: ActionTest.php:76
MediaWikiTestCase
Definition: MediaWikiTestCase.php:16
ActionTest\testNull_canNotBeInstantiated
testNull_canNotBeInstantiated()
Definition: ActionTest.php:154
ActionTest\testActionExists
testActionExists( $requestedAction, $expected)
actionProvider
Definition: ActionTest.php:70
ActionTest\testGetActionName_whenCanNotUseWikiPage_defaultsToView
testGetActionName_whenCanNotUseWikiPage_defaultsToView()
Definition: ActionTest.php:120
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:545
ActionTest\testGetActionName_revisiondeleteWorkaround
testGetActionName_revisiondeleteWorkaround()
Definition: ActionTest.php:111
$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:2675
ActionTest\setUp
setUp()
Definition: ActionTest.php:14
ActionTest\getContext
getContext( $requestedAction=null)
Definition: ActionTest.php:36
ActionTest\testDisabledAction_exists
testDisabledAction_exists()
Definition: ActionTest.php:161
NamedDummyAction
Definition: ActionTest.php:201
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:432
ActionTest\testDisabledAction_isNotResolved
testDisabledAction_isNotResolved()
Definition: ActionTest.php:167
Action\exists
static exists( $name)
Check if a given action is recognised, even if it's disabled.
Definition: Action.php:171
DummyAction
Definition: ActionTest.php:188
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:194
MediaWikiTestCase\assertType
assertType( $type, $actual, $message='')
Asserts the type of the provided value.
Definition: MediaWikiTestCase.php:2143
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:147
ActionTest
Action.
Definition: ActionTest.php:12