429de4e1433f986e2ce74511849658aaea3e3f1d
[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 /**
119 * @dataProvider actionProvider
120 * @param string $requestedAction
121 * @param string|null $expected
122 */
123 public function testActionFactory( $requestedAction, $expected ) {
124 $context = $this->getContext();
125 $action = Action::factory( $requestedAction, $context->getWikiPage(), $context );
126
127 $this->assertType( $expected ?: 'null', $action );
128 }
129
130 public function emptyActionProvider() {
131 return array(
132 array( null ),
133 array( false ),
134 array( '' ),
135 );
136 }
137
138 /**
139 * @dataProvider emptyActionProvider
140 */
141 public function testEmptyAction_doesNotExist( $requestedAction ) {
142 $exists = Action::exists( $requestedAction );
143
144 $this->assertFalse( $exists );
145 }
146
147 /**
148 * @dataProvider emptyActionProvider
149 */
150 public function testEmptyAction_defaultsToView() {
151 $context = $this->getContext( null );
152 $actionName = Action::getActionName( $context );
153
154 $this->assertEquals( 'view', $actionName );
155 }
156
157 /**
158 * @dataProvider emptyActionProvider
159 */
160 public function testEmptyAction_canNotBeInstantiated() {
161 $page = $this->getPage();
162 $action = Action::factory( null, $page );
163
164 $this->assertNull( $action );
165 }
166
167 public function testDisabledAction_exists() {
168 $exists = Action::exists( 'disabled' );
169
170 $this->assertTrue( $exists );
171 }
172
173 public function testDisabledAction_isNotResolved() {
174 $context = $this->getContext( 'disabled' );
175 $actionName = Action::getActionName( $context );
176
177 $this->assertEquals( 'nosuchaction', $actionName );
178 }
179
180 public function testDisabledAction_factoryReturnsFalse() {
181 $page = $this->getPage();
182 $action = Action::factory( 'disabled', $page );
183
184 $this->assertFalse( $action );
185 }
186
187 public function dummyActionCallback() {
188 $context = $this->getContext();
189 return new CalledDummyAction( $context->getWikiPage(), $context );
190 }
191
192 }
193
194 class DummyAction extends Action {
195
196 public function getName() {
197 return get_called_class();
198 }
199
200 public function show() {
201 }
202
203 public function execute() {
204 }
205 }
206
207 class NamedDummyAction extends DummyAction {
208 }
209
210 class CalledDummyAction extends DummyAction {
211 }
212
213 class InstantiatedDummyAction extends DummyAction {
214 }