Merge "Api request log: improve module retrieval"
[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 */
11 class ActionTest extends MediaWikiTestCase {
12
13 protected function setUp() {
14 parent::setUp();
15
16 $context = $this->getContext();
17 $this->setMwGlobals( 'wgActions', array(
18 'null' => null,
19 'disabled' => false,
20 'view' => true,
21 'edit' => true,
22 'revisiondelete' => true,
23 'dummy' => true,
24 'string' => 'NamedDummyAction',
25 'declared' => 'NonExistingClassName',
26 'callable' => array( $this, 'dummyActionCallback' ),
27 'object' => new InstantiatedDummyAction( $context->getWikiPage(), $context ),
28 ) );
29 }
30
31 private function getPage() {
32 return WikiPage::factory( Title::makeTitle( 0, 'Title' ) );
33 }
34
35 private function getContext( $requestedAction = null ) {
36 $request = new FauxRequest( array( 'action' => $requestedAction ) );
37
38 $context = new DerivativeContext( RequestContext::getMain() );
39 $context->setRequest( $request );
40 $context->setWikiPage( $this->getPage() );
41
42 return $context;
43 }
44
45 public function actionProvider() {
46 return array(
47 array( 'dummy', 'DummyAction' ),
48 array( 'string', 'NamedDummyAction' ),
49 array( 'callable', 'CalledDummyAction' ),
50 array( 'object', 'InstantiatedDummyAction' ),
51
52 // Capitalization is ignored
53 array( 'DUMMY', 'DummyAction' ),
54 array( 'STRING', 'NamedDummyAction' ),
55
56 // Null and non-existing values
57 array( 'null', null ),
58 array( 'undeclared', null ),
59 array( '', null ),
60 array( false, null ),
61 );
62 }
63
64 /**
65 * @dataProvider actionProvider
66 * @param string $requestedAction
67 * @param string|null $expected
68 */
69 public function testActionExists( $requestedAction, $expected ) {
70 $exists = Action::exists( $requestedAction );
71
72 $this->assertSame( $expected !== null, $exists );
73 }
74
75 public function testActionExists_doesNotRequireInstantiation() {
76 // The method is not supposed to check if the action can be instantiated.
77 $exists = Action::exists( 'declared' );
78
79 $this->assertTrue( $exists );
80 }
81
82 /**
83 * @dataProvider actionProvider
84 * @param string $requestedAction
85 * @param string|null $expected
86 */
87 public function testGetActionName( $requestedAction, $expected ) {
88 $context = $this->getContext( $requestedAction );
89 $actionName = Action::getActionName( $context );
90
91 $this->assertEquals( $expected ?: 'nosuchaction', $actionName );
92 }
93
94 public function testGetActionName_editredlinkWorkaround() {
95 // See https://bugzilla.wikimedia.org/show_bug.cgi?id=20966
96 $context = $this->getContext( 'editredlink' );
97 $actionName = Action::getActionName( $context );
98
99 $this->assertEquals( 'edit', $actionName );
100 }
101
102 public function testGetActionName_historysubmitWorkaround() {
103 // See https://bugzilla.wikimedia.org/show_bug.cgi?id=20966
104 $context = $this->getContext( 'historysubmit' );
105 $actionName = Action::getActionName( $context );
106
107 $this->assertEquals( 'view', $actionName );
108 }
109
110 public function testGetActionName_revisiondeleteWorkaround() {
111 // See https://bugzilla.wikimedia.org/show_bug.cgi?id=20966
112 $context = $this->getContext( 'historysubmit' );
113 $context->getRequest()->setVal( 'revisiondelete', true );
114 $actionName = Action::getActionName( $context );
115
116 $this->assertEquals( 'revisiondelete', $actionName );
117 }
118
119 /**
120 * @dataProvider actionProvider
121 * @param string $requestedAction
122 * @param string|null $expected
123 */
124 public function testActionFactory( $requestedAction, $expected ) {
125 $context = $this->getContext();
126 $action = Action::factory( $requestedAction, $context->getWikiPage(), $context );
127
128 $this->assertType( $expected ?: 'null', $action );
129 }
130
131 public function testNull_doesNotExist() {
132 $exists = Action::exists( null );
133
134 $this->assertFalse( $exists );
135 }
136
137 public function testNull_defaultsToView() {
138 $context = $this->getContext( null );
139 $actionName = Action::getActionName( $context );
140
141 $this->assertEquals( 'view', $actionName );
142 }
143
144 public function testNull_canNotBeInstantiated() {
145 $page = $this->getPage();
146 $action = Action::factory( null, $page );
147
148 $this->assertNull( $action );
149 }
150
151 public function testDisabledAction_exists() {
152 $exists = Action::exists( 'disabled' );
153
154 $this->assertTrue( $exists );
155 }
156
157 public function testDisabledAction_isNotResolved() {
158 $context = $this->getContext( 'disabled' );
159 $actionName = Action::getActionName( $context );
160
161 $this->assertEquals( 'nosuchaction', $actionName );
162 }
163
164 public function testDisabledAction_factoryReturnsFalse() {
165 $page = $this->getPage();
166 $action = Action::factory( 'disabled', $page );
167
168 $this->assertFalse( $action );
169 }
170
171 public function dummyActionCallback() {
172 $context = $this->getContext();
173 return new CalledDummyAction( $context->getWikiPage(), $context );
174 }
175
176 }
177
178 class DummyAction extends Action {
179
180 public function getName() {
181 return get_called_class();
182 }
183
184 public function show() {
185 }
186
187 public function execute() {
188 }
189 }
190
191 class NamedDummyAction extends DummyAction {
192 }
193
194 class CalledDummyAction extends DummyAction {
195 }
196
197 class InstantiatedDummyAction extends DummyAction {
198 }