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