PHPUnit: Add Database tags
[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 array( '', null ),
61 array( false, null ),
62 );
63 }
64
65 /**
66 * @dataProvider actionProvider
67 * @param string $requestedAction
68 * @param string|null $expected
69 */
70 public function testActionExists( $requestedAction, $expected ) {
71 $exists = Action::exists( $requestedAction );
72
73 $this->assertSame( $expected !== null, $exists );
74 }
75
76 public function testActionExists_doesNotRequireInstantiation() {
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
83 /**
84 * @dataProvider actionProvider
85 * @param string $requestedAction
86 * @param string|null $expected
87 */
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
95 public function testGetActionName_editredlinkWorkaround() {
96 // See https://bugzilla.wikimedia.org/show_bug.cgi?id=20966
97 $context = $this->getContext( 'editredlink' );
98 $actionName = Action::getActionName( $context );
99
100 $this->assertEquals( 'edit', $actionName );
101 }
102
103 public function testGetActionName_historysubmitWorkaround() {
104 // See https://bugzilla.wikimedia.org/show_bug.cgi?id=20966
105 $context = $this->getContext( 'historysubmit' );
106 $actionName = Action::getActionName( $context );
107
108 $this->assertEquals( 'view', $actionName );
109 }
110
111 public function testGetActionName_revisiondeleteWorkaround() {
112 // See https://bugzilla.wikimedia.org/show_bug.cgi?id=20966
113 $context = $this->getContext( 'historysubmit' );
114 $context->getRequest()->setVal( 'revisiondelete', true );
115 $actionName = Action::getActionName( $context );
116
117 $this->assertEquals( 'revisiondelete', $actionName );
118 }
119
120 /**
121 * @dataProvider actionProvider
122 * @param string $requestedAction
123 * @param string|null $expected
124 */
125 public function testActionFactory( $requestedAction, $expected ) {
126 $context = $this->getContext();
127 $action = Action::factory( $requestedAction, $context->getWikiPage(), $context );
128
129 $this->assertType( $expected ?: 'null', $action );
130 }
131
132 public function testNull_doesNotExist() {
133 $exists = Action::exists( null );
134
135 $this->assertFalse( $exists );
136 }
137
138 public function testNull_defaultsToView() {
139 $context = $this->getContext( null );
140 $actionName = Action::getActionName( $context );
141
142 $this->assertEquals( 'view', $actionName );
143 }
144
145 public function testNull_canNotBeInstantiated() {
146 $page = $this->getPage();
147 $action = Action::factory( null, $page );
148
149 $this->assertNull( $action );
150 }
151
152 public function testDisabledAction_exists() {
153 $exists = Action::exists( 'disabled' );
154
155 $this->assertTrue( $exists );
156 }
157
158 public function testDisabledAction_isNotResolved() {
159 $context = $this->getContext( 'disabled' );
160 $actionName = Action::getActionName( $context );
161
162 $this->assertEquals( 'nosuchaction', $actionName );
163 }
164
165 public function testDisabledAction_factoryReturnsFalse() {
166 $page = $this->getPage();
167 $action = Action::factory( 'disabled', $page );
168
169 $this->assertFalse( $action );
170 }
171
172 public function dummyActionCallback() {
173 $context = $this->getContext();
174 return new CalledDummyAction( $context->getWikiPage(), $context );
175 }
176
177 }
178
179 class DummyAction extends Action {
180
181 public function getName() {
182 return get_called_class();
183 }
184
185 public function show() {
186 }
187
188 public function execute() {
189 }
190 }
191
192 class NamedDummyAction extends DummyAction {
193 }
194
195 class CalledDummyAction extends DummyAction {
196 }
197
198 class InstantiatedDummyAction extends DummyAction {
199 }