6 * @author Thiemo Mättig
11 class ActionTest
extends MediaWikiTestCase
{
13 protected function setUp() {
16 $context = $this->getContext();
17 $this->setMwGlobals( 'wgActions', [
22 'revisiondelete' => 'SpecialPageAction',
24 'string' => 'NamedDummyAction',
25 'declared' => 'NonExistingClassName',
26 'callable' => [ $this, 'dummyActionCallback' ],
27 'object' => new InstantiatedDummyAction( $context->getWikiPage(), $context ),
31 private function getPage() {
32 return WikiPage
::factory( Title
::makeTitle( 0, 'Title' ) );
35 private function getContext( $requestedAction = null ) {
36 $request = new FauxRequest( [ 'action' => $requestedAction ] );
38 $context = new DerivativeContext( RequestContext
::getMain() );
39 $context->setRequest( $request );
40 $context->setWikiPage( $this->getPage() );
45 public function actionProvider() {
47 [ 'dummy', 'DummyAction' ],
48 [ 'string', 'NamedDummyAction' ],
49 [ 'callable', 'CalledDummyAction' ],
50 [ 'object', 'InstantiatedDummyAction' ],
52 // Capitalization is ignored
53 [ 'DUMMY', 'DummyAction' ],
54 [ 'STRING', 'NamedDummyAction' ],
56 // Null and non-existing values
58 [ 'undeclared', null ],
65 * @dataProvider actionProvider
66 * @param string $requestedAction
67 * @param string|null $expected
69 public function testActionExists( $requestedAction, $expected ) {
70 $exists = Action
::exists( $requestedAction );
72 $this->assertSame( $expected !== null, $exists );
75 public function testActionExists_doesNotRequireInstantiation() {
76 // The method is not supposed to check if the action can be instantiated.
77 $exists = Action
::exists( 'declared' );
79 $this->assertTrue( $exists );
83 * @dataProvider actionProvider
84 * @param string $requestedAction
85 * @param string|null $expected
87 public function testGetActionName( $requestedAction, $expected ) {
88 $context = $this->getContext( $requestedAction );
89 $actionName = Action
::getActionName( $context );
91 $this->assertEquals( $expected ?
: 'nosuchaction', $actionName );
94 public function testGetActionName_editredlinkWorkaround() {
95 // See https://phabricator.wikimedia.org/T22966
96 $context = $this->getContext( 'editredlink' );
97 $actionName = Action
::getActionName( $context );
99 $this->assertEquals( 'edit', $actionName );
102 public function testGetActionName_historysubmitWorkaround() {
103 // See https://phabricator.wikimedia.org/T22966
104 $context = $this->getContext( 'historysubmit' );
105 $actionName = Action
::getActionName( $context );
107 $this->assertEquals( 'view', $actionName );
110 public function testGetActionName_revisiondeleteWorkaround() {
111 // See https://phabricator.wikimedia.org/T22966
112 $context = $this->getContext( 'historysubmit' );
113 $context->getRequest()->setVal( 'revisiondelete', true );
114 $actionName = Action
::getActionName( $context );
116 $this->assertEquals( 'revisiondelete', $actionName );
119 public function testGetActionName_whenCanNotUseWikiPage_defaultsToView() {
120 $request = new FauxRequest( [ 'action' => 'edit' ] );
121 $context = new DerivativeContext( RequestContext
::getMain() );
122 $context->setRequest( $request );
123 $actionName = Action
::getActionName( $context );
125 $this->assertEquals( 'view', $actionName );
129 * @dataProvider actionProvider
130 * @param string $requestedAction
131 * @param string|null $expected
133 public function testActionFactory( $requestedAction, $expected ) {
134 $context = $this->getContext();
135 $action = Action
::factory( $requestedAction, $context->getWikiPage(), $context );
137 $this->assertType( $expected ?
: 'null', $action );
140 public function testNull_doesNotExist() {
141 $exists = Action
::exists( null );
143 $this->assertFalse( $exists );
146 public function testNull_defaultsToView() {
147 $context = $this->getContext( null );
148 $actionName = Action
::getActionName( $context );
150 $this->assertEquals( 'view', $actionName );
153 public function testNull_canNotBeInstantiated() {
154 $page = $this->getPage();
155 $action = Action
::factory( null, $page );
157 $this->assertNull( $action );
160 public function testDisabledAction_exists() {
161 $exists = Action
::exists( 'disabled' );
163 $this->assertTrue( $exists );
166 public function testDisabledAction_isNotResolved() {
167 $context = $this->getContext( 'disabled' );
168 $actionName = Action
::getActionName( $context );
170 $this->assertEquals( 'nosuchaction', $actionName );
173 public function testDisabledAction_factoryReturnsFalse() {
174 $page = $this->getPage();
175 $action = Action
::factory( 'disabled', $page );
177 $this->assertFalse( $action );
180 public function dummyActionCallback() {
181 $context = $this->getContext();
182 return new CalledDummyAction( $context->getWikiPage(), $context );
187 class DummyAction
extends Action
{
189 public function getName() {
190 return static::class;
193 public function show() {
196 public function execute() {
200 class NamedDummyAction
extends DummyAction
{
203 class CalledDummyAction
extends DummyAction
{
206 class InstantiatedDummyAction
extends DummyAction
{