Merge "Remove experiment code for moduleStorage"
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiOptionsTest.php
1 <?php
2
3 /**
4 * @group API
5 * @group Database
6 * @group medium
7 *
8 * @covers ApiOptions
9 */
10 class ApiOptionsTest extends MediaWikiLangTestCase {
11
12 /** @var PHPUnit_Framework_MockObject_MockObject */
13 private $mUserMock;
14 /** @var ApiOptions */
15 private $mTested;
16 private $mSession;
17 /** @var DerivativeContext */
18 private $mContext;
19
20 private $mOldGetPreferencesHooks = false;
21
22 private static $Success = array( 'options' => 'success' );
23
24 protected function setUp() {
25 parent::setUp();
26
27 $this->mUserMock = $this->getMockBuilder( 'User' )
28 ->disableOriginalConstructor()
29 ->getMock();
30
31 // Set up groups and rights
32 $this->mUserMock->expects( $this->any() )
33 ->method( 'getEffectiveGroups' )->will( $this->returnValue( array( '*', 'user' ) ) );
34 $this->mUserMock->expects( $this->any() )
35 ->method( 'isAllowed' )->will( $this->returnValue( true ) );
36
37 // Set up callback for User::getOptionKinds
38 $this->mUserMock->expects( $this->any() )
39 ->method( 'getOptionKinds' )->will( $this->returnCallback( array( $this, 'getOptionKinds' ) ) );
40
41 // Create a new context
42 $this->mContext = new DerivativeContext( new RequestContext() );
43 $this->mContext->getContext()->setTitle( Title::newFromText( 'Test' ) );
44 $this->mContext->setUser( $this->mUserMock );
45
46 $main = new ApiMain( $this->mContext );
47
48 // Empty session
49 $this->mSession = array();
50
51 $this->mTested = new ApiOptions( $main, 'options' );
52
53 global $wgHooks;
54 if ( !isset( $wgHooks['GetPreferences'] ) ) {
55 $wgHooks['GetPreferences'] = array();
56 }
57 $this->mOldGetPreferencesHooks = $wgHooks['GetPreferences'];
58 $wgHooks['GetPreferences'][] = array( $this, 'hookGetPreferences' );
59 }
60
61 protected function tearDown() {
62 global $wgHooks;
63
64 if ( $this->mOldGetPreferencesHooks !== false ) {
65 $wgHooks['GetPreferences'] = $this->mOldGetPreferencesHooks;
66 $this->mOldGetPreferencesHooks = false;
67 }
68
69 parent::tearDown();
70 }
71
72 public function hookGetPreferences( $user, &$preferences ) {
73 $preferences = array();
74
75 foreach ( array( 'name', 'willBeNull', 'willBeEmpty', 'willBeHappy' ) as $k ) {
76 $preferences[$k] = array(
77 'type' => 'text',
78 'section' => 'test',
79 'label' => '&#160;',
80 );
81 }
82
83 $preferences['testmultiselect'] = array(
84 'type' => 'multiselect',
85 'options' => array(
86 'Test' => array(
87 '<span dir="auto">Some HTML here for option 1</span>' => 'opt1',
88 '<span dir="auto">Some HTML here for option 2</span>' => 'opt2',
89 '<span dir="auto">Some HTML here for option 3</span>' => 'opt3',
90 '<span dir="auto">Some HTML here for option 4</span>' => 'opt4',
91 ),
92 ),
93 'section' => 'test',
94 'label' => '&#160;',
95 'prefix' => 'testmultiselect-',
96 'default' => array(),
97 );
98
99 return true;
100 }
101
102 /**
103 * @param IContextSource $context
104 * @param array|null $options
105 *
106 * @return array
107 */
108 public function getOptionKinds( IContextSource $context, $options = null ) {
109 // Match with above.
110 $kinds = array(
111 'name' => 'registered',
112 'willBeNull' => 'registered',
113 'willBeEmpty' => 'registered',
114 'willBeHappy' => 'registered',
115 'testmultiselect-opt1' => 'registered-multiselect',
116 'testmultiselect-opt2' => 'registered-multiselect',
117 'testmultiselect-opt3' => 'registered-multiselect',
118 'testmultiselect-opt4' => 'registered-multiselect',
119 );
120
121 if ( $options === null ) {
122 return $kinds;
123 }
124
125 $mapping = array();
126 foreach ( $options as $key => $value ) {
127 if ( isset( $kinds[$key] ) ) {
128 $mapping[$key] = $kinds[$key];
129 } elseif ( substr( $key, 0, 7 ) === 'userjs-' ) {
130 $mapping[$key] = 'userjs';
131 } else {
132 $mapping[$key] = 'unused';
133 }
134 }
135
136 return $mapping;
137 }
138
139 private function getSampleRequest( $custom = array() ) {
140 $request = array(
141 'token' => '123ABC',
142 'change' => null,
143 'optionname' => null,
144 'optionvalue' => null,
145 );
146
147 return array_merge( $request, $custom );
148 }
149
150 private function executeQuery( $request ) {
151 $this->mContext->setRequest( new FauxRequest( $request, true, $this->mSession ) );
152 $this->mTested->execute();
153
154 return $this->mTested->getResult()->getData();
155 }
156
157 /**
158 * @expectedException UsageException
159 */
160 public function testNoToken() {
161 $request = $this->getSampleRequest( array( 'token' => null ) );
162
163 $this->executeQuery( $request );
164 }
165
166 public function testAnon() {
167 $this->mUserMock->expects( $this->once() )
168 ->method( 'isAnon' )
169 ->will( $this->returnValue( true ) );
170
171 try {
172 $request = $this->getSampleRequest();
173
174 $this->executeQuery( $request );
175 } catch ( UsageException $e ) {
176 $this->assertEquals( 'notloggedin', $e->getCodeString() );
177 $this->assertEquals( 'Anonymous users cannot change preferences', $e->getMessage() );
178
179 return;
180 }
181 $this->fail( "UsageException was not thrown" );
182 }
183
184 public function testNoOptionname() {
185 try {
186 $request = $this->getSampleRequest( array( 'optionvalue' => '1' ) );
187
188 $this->executeQuery( $request );
189 } catch ( UsageException $e ) {
190 $this->assertEquals( 'nooptionname', $e->getCodeString() );
191 $this->assertEquals( 'The optionname parameter must be set', $e->getMessage() );
192
193 return;
194 }
195 $this->fail( "UsageException was not thrown" );
196 }
197
198 public function testNoChanges() {
199 $this->mUserMock->expects( $this->never() )
200 ->method( 'resetOptions' );
201
202 $this->mUserMock->expects( $this->never() )
203 ->method( 'setOption' );
204
205 $this->mUserMock->expects( $this->never() )
206 ->method( 'saveSettings' );
207
208 try {
209 $request = $this->getSampleRequest();
210
211 $this->executeQuery( $request );
212 } catch ( UsageException $e ) {
213 $this->assertEquals( 'nochanges', $e->getCodeString() );
214 $this->assertEquals( 'No changes were requested', $e->getMessage() );
215
216 return;
217 }
218 $this->fail( "UsageException was not thrown" );
219 }
220
221 public function testReset() {
222 $this->mUserMock->expects( $this->once() )
223 ->method( 'resetOptions' )
224 ->with( $this->equalTo( array( 'all' ) ) );
225
226 $this->mUserMock->expects( $this->never() )
227 ->method( 'setOption' );
228
229 $this->mUserMock->expects( $this->once() )
230 ->method( 'saveSettings' );
231
232 $request = $this->getSampleRequest( array( 'reset' => '' ) );
233
234 $response = $this->executeQuery( $request );
235
236 $this->assertEquals( self::$Success, $response );
237 }
238
239 public function testResetKinds() {
240 $this->mUserMock->expects( $this->once() )
241 ->method( 'resetOptions' )
242 ->with( $this->equalTo( array( 'registered' ) ) );
243
244 $this->mUserMock->expects( $this->never() )
245 ->method( 'setOption' );
246
247 $this->mUserMock->expects( $this->once() )
248 ->method( 'saveSettings' );
249
250 $request = $this->getSampleRequest( array( 'reset' => '', 'resetkinds' => 'registered' ) );
251
252 $response = $this->executeQuery( $request );
253
254 $this->assertEquals( self::$Success, $response );
255 }
256
257 public function testOptionWithValue() {
258 $this->mUserMock->expects( $this->never() )
259 ->method( 'resetOptions' );
260
261 $this->mUserMock->expects( $this->once() )
262 ->method( 'setOption' )
263 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
264
265 $this->mUserMock->expects( $this->once() )
266 ->method( 'saveSettings' );
267
268 $request = $this->getSampleRequest( array( 'optionname' => 'name', 'optionvalue' => 'value' ) );
269
270 $response = $this->executeQuery( $request );
271
272 $this->assertEquals( self::$Success, $response );
273 }
274
275 public function testOptionResetValue() {
276 $this->mUserMock->expects( $this->never() )
277 ->method( 'resetOptions' );
278
279 $this->mUserMock->expects( $this->once() )
280 ->method( 'setOption' )
281 ->with( $this->equalTo( 'name' ), $this->identicalTo( null ) );
282
283 $this->mUserMock->expects( $this->once() )
284 ->method( 'saveSettings' );
285
286 $request = $this->getSampleRequest( array( 'optionname' => 'name' ) );
287 $response = $this->executeQuery( $request );
288
289 $this->assertEquals( self::$Success, $response );
290 }
291
292 public function testChange() {
293 $this->mUserMock->expects( $this->never() )
294 ->method( 'resetOptions' );
295
296 $this->mUserMock->expects( $this->at( 2 ) )
297 ->method( 'getOptions' );
298
299 $this->mUserMock->expects( $this->at( 4 ) )
300 ->method( 'setOption' )
301 ->with( $this->equalTo( 'willBeNull' ), $this->identicalTo( null ) );
302
303 $this->mUserMock->expects( $this->at( 5 ) )
304 ->method( 'getOptions' );
305
306 $this->mUserMock->expects( $this->at( 6 ) )
307 ->method( 'setOption' )
308 ->with( $this->equalTo( 'willBeEmpty' ), $this->equalTo( '' ) );
309
310 $this->mUserMock->expects( $this->at( 7 ) )
311 ->method( 'getOptions' );
312
313 $this->mUserMock->expects( $this->at( 8 ) )
314 ->method( 'setOption' )
315 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
316
317 $this->mUserMock->expects( $this->once() )
318 ->method( 'saveSettings' );
319
320 $request = $this->getSampleRequest( array( 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy' ) );
321
322 $response = $this->executeQuery( $request );
323
324 $this->assertEquals( self::$Success, $response );
325 }
326
327 public function testResetChangeOption() {
328 $this->mUserMock->expects( $this->once() )
329 ->method( 'resetOptions' );
330
331 $this->mUserMock->expects( $this->at( 4 ) )
332 ->method( 'getOptions' );
333
334 $this->mUserMock->expects( $this->at( 5 ) )
335 ->method( 'setOption' )
336 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
337
338 $this->mUserMock->expects( $this->at( 6 ) )
339 ->method( 'getOptions' );
340
341 $this->mUserMock->expects( $this->at( 7 ) )
342 ->method( 'setOption' )
343 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
344
345 $this->mUserMock->expects( $this->once() )
346 ->method( 'saveSettings' );
347
348 $args = array(
349 'reset' => '',
350 'change' => 'willBeHappy=Happy',
351 'optionname' => 'name',
352 'optionvalue' => 'value'
353 );
354
355 $response = $this->executeQuery( $this->getSampleRequest( $args ) );
356
357 $this->assertEquals( self::$Success, $response );
358 }
359
360 public function testMultiSelect() {
361 $this->mUserMock->expects( $this->never() )
362 ->method( 'resetOptions' );
363
364 $this->mUserMock->expects( $this->at( 3 ) )
365 ->method( 'setOption' )
366 ->with( $this->equalTo( 'testmultiselect-opt1' ), $this->identicalTo( true ) );
367
368 $this->mUserMock->expects( $this->at( 4 ) )
369 ->method( 'setOption' )
370 ->with( $this->equalTo( 'testmultiselect-opt2' ), $this->identicalTo( null ) );
371
372 $this->mUserMock->expects( $this->at( 5 ) )
373 ->method( 'setOption' )
374 ->with( $this->equalTo( 'testmultiselect-opt3' ), $this->identicalTo( false ) );
375
376 $this->mUserMock->expects( $this->at( 6 ) )
377 ->method( 'setOption' )
378 ->with( $this->equalTo( 'testmultiselect-opt4' ), $this->identicalTo( false ) );
379
380 $this->mUserMock->expects( $this->once() )
381 ->method( 'saveSettings' );
382
383 $request = $this->getSampleRequest( array(
384 'change' => 'testmultiselect-opt1=1|testmultiselect-opt2|testmultiselect-opt3=|testmultiselect-opt4=0'
385 ) );
386
387 $response = $this->executeQuery( $request );
388
389 $this->assertEquals( self::$Success, $response );
390 }
391
392 public function testUnknownOption() {
393 $this->mUserMock->expects( $this->never() )
394 ->method( 'resetOptions' );
395
396 $this->mUserMock->expects( $this->never() )
397 ->method( 'saveSettings' );
398
399 $request = $this->getSampleRequest( array(
400 'change' => 'unknownOption=1'
401 ) );
402
403 $response = $this->executeQuery( $request );
404
405 $this->assertEquals( array(
406 'options' => 'success',
407 'warnings' => array(
408 'options' => array(
409 '*' => "Validation error for 'unknownOption': not a valid preference"
410 )
411 )
412 ), $response );
413 }
414
415 public function testUserjsOption() {
416 $this->mUserMock->expects( $this->never() )
417 ->method( 'resetOptions' );
418
419 $this->mUserMock->expects( $this->at( 3 ) )
420 ->method( 'setOption' )
421 ->with( $this->equalTo( 'userjs-option' ), $this->equalTo( '1' ) );
422
423 $this->mUserMock->expects( $this->once() )
424 ->method( 'saveSettings' );
425
426 $request = $this->getSampleRequest( array(
427 'change' => 'userjs-option=1'
428 ) );
429
430 $response = $this->executeQuery( $request );
431
432 $this->assertEquals( self::$Success, $response );
433 }
434 }