Add test for canUseWikiPage special case to ActionTest
[lhc/web/wiklou.git] / tests / phpunit / includes / actions / ActionTest.php
1 <?php
2
3 /**
4 * @covers Action
5 *
6 * @licence GNU GPL v2+
7 * @author Thiemo Mättig
8 *
9 * @group Action
10 * @group Database
11 */
12 class ActionTest extends MediaWikiTestCase {
13
14 protected function setUp() {
15 parent::setUp();
16
17 $context = $this->getContext();
18 $this->setMwGlobals( 'wgActions', array(
19 'null' => null,
20 'disabled' => false,
21 'view' => true,
22 'edit' => true,
23 'revisiondelete' => true,
24 'dummy' => true,
25 'string' => 'NamedDummyAction',
26 'declared' => 'NonExistingClassName',
27 'callable' => array( $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( array( '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 array(
48 array( 'dummy', 'DummyAction' ),
49 array( 'string', 'NamedDummyAction' ),
50 array( 'callable', 'CalledDummyAction' ),
51 array( 'object', 'InstantiatedDummyAction' ),
52
53 // Capitalization is ignored
54 array( 'DUMMY', 'DummyAction' ),
55 array( 'STRING', 'NamedDummyAction' ),
56
57 // Null and non-existing values
58 array( 'null', null ),
59 array( 'undeclared', null ),
60 );
61 }
62
63 /**
64 * @dataProvider actionProvider
65 * @param string $requestedAction
66 * @param string|null $expected
67 */
68 public function testActionExists( $requestedAction, $expected ) {
69 $exists = Action::exists( $requestedAction );
70
71 $this->assertSame( $expected !== null, $exists );
72 }
73
74 public function testActionExists_doesNotRequireInstantiation() {
75 // The method is not supposed to check if the action can be instantiated.
76 $exists = Action::exists( 'declared' );
77
78 $this->assertTrue( $exists );
79 }
80
81 /**
82 * @dataProvider actionProvider
83 * @param string $requestedAction
84 * @param string|null $expected
85 */
86 public function testGetActionName( $requestedAction, $expected ) {
87 $context = $this->getContext( $requestedAction );
88 $actionName = Action::getActionName( $context );
89
90 $this->assertEquals( $expected ?: 'nosuchaction', $actionName );
91 }
92
93 public function testGetActionName_editredlinkWorkaround() {
94 // See https://bugzilla.wikimedia.org/show_bug.cgi?id=20966
95 $context = $this->getContext( 'editredlink' );
96 $actionName = Action::getActionName( $context );
97
98 $this->assertEquals( 'edit', $actionName );
99 }
100
101 public function testGetActionName_historysubmitWorkaround() {
102 // See https://bugzilla.wikimedia.org/show_bug.cgi?id=20966
103 $context = $this->getContext( 'historysubmit' );
104 $actionName = Action::getActionName( $context );
105
106 $this->assertEquals( 'view', $actionName );
107 }
108
109 public function testGetActionName_revisiondeleteWorkaround() {
110 // See https://bugzilla.wikimedia.org/show_bug.cgi?id=20966
111 $context = $this->getContext( 'historysubmit' );
112 $context->getRequest()->setVal( 'revisiondelete', true );
113 $actionName = Action::getActionName( $context );
114
115 $this->assertEquals( 'revisiondelete', $actionName );
116 }
117
118 public function testGetActionName_whenCanNotUseWikiPage_defaultsToView() {
119 $request = new FauxRequest( array( 'action' => 'edit' ) );
120 $context = new DerivativeContext( RequestContext::getMain() );
121 $context->setRequest( $request );
122 $actionName = Action::getActionName( $context );
123
124 $this->assertEquals( 'view', $actionName );
125 }
126
127 /**
128 * @dataProvider actionProvider
129 * @param string $requestedAction
130 * @param string|null $expected
131 */
132 public function testActionFactory( $requestedAction, $expected ) {
133 $context = $this->getContext();
134 $action = Action::factory( $requestedAction, $context->getWikiPage(), $context );
135
136 $this->assertType( $expected ?: 'null', $action );
137 }
138
139 public function emptyActionProvider() {
140 return array(
141 array( null ),
142 array( false ),
143 array( '' ),
144 );
145 }
146
147 /**
148 * @dataProvider emptyActionProvider
149 */
150 public function testEmptyAction_doesNotExist( $requestedAction ) {
151 $exists = Action::exists( $requestedAction );
152
153 $this->assertFalse( $exists );
154 }
155
156 /**
157 * @dataProvider emptyActionProvider
158 */
159 public function testEmptyAction_defaultsToView() {
160 $context = $this->getContext( null );
161 $actionName = Action::getActionName( $context );
162
163 $this->assertEquals( 'view', $actionName );
164 }
165
166 /**
167 * @dataProvider emptyActionProvider
168 */
169 public function testEmptyAction_canNotBeInstantiated() {
170 $page = $this->getPage();
171 $action = Action::factory( null, $page );
172
173 $this->assertNull( $action );
174 }
175
176 public function testDisabledAction_exists() {
177 $exists = Action::exists( 'disabled' );
178
179 $this->assertTrue( $exists );
180 }
181
182 public function testDisabledAction_isNotResolved() {
183 $context = $this->getContext( 'disabled' );
184 $actionName = Action::getActionName( $context );
185
186 $this->assertEquals( 'nosuchaction', $actionName );
187 }
188
189 public function testDisabledAction_factoryReturnsFalse() {
190 $page = $this->getPage();
191 $action = Action::factory( 'disabled', $page );
192
193 $this->assertFalse( $action );
194 }
195
196 public function dummyActionCallback() {
197 $context = $this->getContext();
198 return new CalledDummyAction( $context->getWikiPage(), $context );
199 }
200
201 }
202
203 class DummyAction extends Action {
204
205 public function getName() {
206 return get_called_class();
207 }
208
209 public function show() {
210 }
211
212 public function execute() {
213 }
214 }
215
216 class NamedDummyAction extends DummyAction {
217 }
218
219 class CalledDummyAction extends DummyAction {
220 }
221
222 class InstantiatedDummyAction extends DummyAction {
223 }