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