Merge "Make MySQLi work with non-standard port"
[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 'special' => 'special',
120 );
121
122 if ( $options === null ) {
123 return $kinds;
124 }
125
126 $mapping = array();
127 foreach ( $options as $key => $value ) {
128 if ( isset( $kinds[$key] ) ) {
129 $mapping[$key] = $kinds[$key];
130 } elseif ( substr( $key, 0, 7 ) === 'userjs-' ) {
131 $mapping[$key] = 'userjs';
132 } else {
133 $mapping[$key] = 'unused';
134 }
135 }
136
137 return $mapping;
138 }
139
140 private function getSampleRequest( $custom = array() ) {
141 $request = array(
142 'token' => '123ABC',
143 'change' => null,
144 'optionname' => null,
145 'optionvalue' => null,
146 );
147
148 return array_merge( $request, $custom );
149 }
150
151 private function executeQuery( $request ) {
152 $this->mContext->setRequest( new FauxRequest( $request, true, $this->mSession ) );
153 $this->mTested->execute();
154
155 return $this->mTested->getResult()->getData();
156 }
157
158 /**
159 * @expectedException UsageException
160 */
161 public function testNoToken() {
162 $request = $this->getSampleRequest( array( 'token' => null ) );
163
164 $this->executeQuery( $request );
165 }
166
167 public function testAnon() {
168 $this->mUserMock->expects( $this->once() )
169 ->method( 'isAnon' )
170 ->will( $this->returnValue( true ) );
171
172 try {
173 $request = $this->getSampleRequest();
174
175 $this->executeQuery( $request );
176 } catch ( UsageException $e ) {
177 $this->assertEquals( 'notloggedin', $e->getCodeString() );
178 $this->assertEquals( 'Anonymous users cannot change preferences', $e->getMessage() );
179
180 return;
181 }
182 $this->fail( "UsageException was not thrown" );
183 }
184
185 public function testNoOptionname() {
186 try {
187 $request = $this->getSampleRequest( array( 'optionvalue' => '1' ) );
188
189 $this->executeQuery( $request );
190 } catch ( UsageException $e ) {
191 $this->assertEquals( 'nooptionname', $e->getCodeString() );
192 $this->assertEquals( 'The optionname parameter must be set', $e->getMessage() );
193
194 return;
195 }
196 $this->fail( "UsageException was not thrown" );
197 }
198
199 public function testNoChanges() {
200 $this->mUserMock->expects( $this->never() )
201 ->method( 'resetOptions' );
202
203 $this->mUserMock->expects( $this->never() )
204 ->method( 'setOption' );
205
206 $this->mUserMock->expects( $this->never() )
207 ->method( 'saveSettings' );
208
209 try {
210 $request = $this->getSampleRequest();
211
212 $this->executeQuery( $request );
213 } catch ( UsageException $e ) {
214 $this->assertEquals( 'nochanges', $e->getCodeString() );
215 $this->assertEquals( 'No changes were requested', $e->getMessage() );
216
217 return;
218 }
219 $this->fail( "UsageException was not thrown" );
220 }
221
222 public function testReset() {
223 $this->mUserMock->expects( $this->once() )
224 ->method( 'resetOptions' )
225 ->with( $this->equalTo( array( 'all' ) ) );
226
227 $this->mUserMock->expects( $this->never() )
228 ->method( 'setOption' );
229
230 $this->mUserMock->expects( $this->once() )
231 ->method( 'saveSettings' );
232
233 $request = $this->getSampleRequest( array( 'reset' => '' ) );
234
235 $response = $this->executeQuery( $request );
236
237 $this->assertEquals( self::$Success, $response );
238 }
239
240 public function testResetKinds() {
241 $this->mUserMock->expects( $this->once() )
242 ->method( 'resetOptions' )
243 ->with( $this->equalTo( array( 'registered' ) ) );
244
245 $this->mUserMock->expects( $this->never() )
246 ->method( 'setOption' );
247
248 $this->mUserMock->expects( $this->once() )
249 ->method( 'saveSettings' );
250
251 $request = $this->getSampleRequest( array( 'reset' => '', 'resetkinds' => 'registered' ) );
252
253 $response = $this->executeQuery( $request );
254
255 $this->assertEquals( self::$Success, $response );
256 }
257
258 public function testOptionWithValue() {
259 $this->mUserMock->expects( $this->never() )
260 ->method( 'resetOptions' );
261
262 $this->mUserMock->expects( $this->once() )
263 ->method( 'setOption' )
264 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
265
266 $this->mUserMock->expects( $this->once() )
267 ->method( 'saveSettings' );
268
269 $request = $this->getSampleRequest( array( 'optionname' => 'name', 'optionvalue' => 'value' ) );
270
271 $response = $this->executeQuery( $request );
272
273 $this->assertEquals( self::$Success, $response );
274 }
275
276 public function testOptionResetValue() {
277 $this->mUserMock->expects( $this->never() )
278 ->method( 'resetOptions' );
279
280 $this->mUserMock->expects( $this->once() )
281 ->method( 'setOption' )
282 ->with( $this->equalTo( 'name' ), $this->identicalTo( null ) );
283
284 $this->mUserMock->expects( $this->once() )
285 ->method( 'saveSettings' );
286
287 $request = $this->getSampleRequest( array( 'optionname' => 'name' ) );
288 $response = $this->executeQuery( $request );
289
290 $this->assertEquals( self::$Success, $response );
291 }
292
293 public function testChange() {
294 $this->mUserMock->expects( $this->never() )
295 ->method( 'resetOptions' );
296
297 $this->mUserMock->expects( $this->at( 2 ) )
298 ->method( 'getOptions' );
299
300 $this->mUserMock->expects( $this->at( 4 ) )
301 ->method( 'setOption' )
302 ->with( $this->equalTo( 'willBeNull' ), $this->identicalTo( null ) );
303
304 $this->mUserMock->expects( $this->at( 5 ) )
305 ->method( 'getOptions' );
306
307 $this->mUserMock->expects( $this->at( 6 ) )
308 ->method( 'setOption' )
309 ->with( $this->equalTo( 'willBeEmpty' ), $this->equalTo( '' ) );
310
311 $this->mUserMock->expects( $this->at( 7 ) )
312 ->method( 'getOptions' );
313
314 $this->mUserMock->expects( $this->at( 8 ) )
315 ->method( 'setOption' )
316 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
317
318 $this->mUserMock->expects( $this->once() )
319 ->method( 'saveSettings' );
320
321 $request = $this->getSampleRequest( array( 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy' ) );
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|testmultiselect-opt3=|testmultiselect-opt4=0'
386 ) );
387
388 $response = $this->executeQuery( $request );
389
390 $this->assertEquals( self::$Success, $response );
391 }
392
393 public function testSpecialOption() {
394 $this->mUserMock->expects( $this->never() )
395 ->method( 'resetOptions' );
396
397 $this->mUserMock->expects( $this->never() )
398 ->method( 'saveSettings' );
399
400 $request = $this->getSampleRequest( array(
401 'change' => 'special=1'
402 ) );
403
404 $response = $this->executeQuery( $request );
405
406 $this->assertEquals( array(
407 'options' => 'success',
408 'warnings' => array(
409 'options' => array(
410 '*' => "Validation error for 'special': cannot be set by this module"
411 )
412 )
413 ), $response );
414 }
415
416 public function testUnknownOption() {
417 $this->mUserMock->expects( $this->never() )
418 ->method( 'resetOptions' );
419
420 $this->mUserMock->expects( $this->never() )
421 ->method( 'saveSettings' );
422
423 $request = $this->getSampleRequest( array(
424 'change' => 'unknownOption=1'
425 ) );
426
427 $response = $this->executeQuery( $request );
428
429 $this->assertEquals( array(
430 'options' => 'success',
431 'warnings' => array(
432 'options' => array(
433 '*' => "Validation error for 'unknownOption': not a valid preference"
434 )
435 )
436 ), $response );
437 }
438
439 public function testUserjsOption() {
440 $this->mUserMock->expects( $this->never() )
441 ->method( 'resetOptions' );
442
443 $this->mUserMock->expects( $this->at( 3 ) )
444 ->method( 'setOption' )
445 ->with( $this->equalTo( 'userjs-option' ), $this->equalTo( '1' ) );
446
447 $this->mUserMock->expects( $this->once() )
448 ->method( 'saveSettings' );
449
450 $request = $this->getSampleRequest( array(
451 'change' => 'userjs-option=1'
452 ) );
453
454 $response = $this->executeQuery( $request );
455
456 $this->assertEquals( self::$Success, $response );
457 }
458 }