24 protected function setUp() {
27 $this->mUserMock = $this->getMockBuilder(
'User' )
28 ->disableOriginalConstructor()
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 ) );
38 $this->mUserMock->expects( $this->any() )
39 ->method(
'getOptionKinds' )->will( $this->returnCallback(
array( $this,
'getOptionKinds' ) ) );
44 $this->mContext->setUser( $this->mUserMock );
46 $main =
new ApiMain( $this->mContext );
49 $this->mSession =
array();
51 $this->mTested =
new ApiOptions( $main,
'options' );
54 if ( !isset(
$wgHooks[
'GetPreferences'] ) ) {
57 $this->mOldGetPreferencesHooks =
$wgHooks[
'GetPreferences'];
64 if ( $this->mOldGetPreferencesHooks !==
false ) {
66 $this->mOldGetPreferencesHooks =
false;
73 $preferences =
array();
75 foreach (
array(
'name',
'willBeNull',
'willBeEmpty',
'willBeHappy' )
as $k ) {
76 $preferences[$k] =
array(
83 $preferences[
'testmultiselect'] =
array(
84 'type' =>
'multiselect',
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',
95 'prefix' =>
'testmultiselect-',
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',
128 if ( isset( $kinds[$key] ) ) {
129 $mapping[$key] = $kinds[$key];
130 } elseif ( substr( $key, 0, 7 ) ===
'userjs-' ) {
131 $mapping[$key] =
'userjs';
133 $mapping[$key] =
'unused';
144 'optionname' =>
null,
145 'optionvalue' =>
null,
148 return array_merge( $request, $custom );
152 $this->mContext->setRequest(
new FauxRequest( $request,
true, $this->mSession ) );
153 $this->mTested->execute();
155 return $this->mTested->getResult()->getData();
168 $this->mUserMock->expects( $this->once() )
170 ->will( $this->returnValue(
true ) );
177 $this->assertEquals(
'notloggedin',
$e->getCodeString() );
178 $this->assertEquals(
'Anonymous users cannot change preferences',
$e->getMessage() );
182 $this->
fail(
"UsageException was not thrown" );
191 $this->assertEquals(
'nooptionname',
$e->getCodeString() );
192 $this->assertEquals(
'The optionname parameter must be set',
$e->getMessage() );
196 $this->
fail(
"UsageException was not thrown" );
200 $this->mUserMock->expects( $this->never() )
201 ->method(
'resetOptions' );
203 $this->mUserMock->expects( $this->never() )
204 ->method(
'setOption' );
206 $this->mUserMock->expects( $this->never() )
207 ->method(
'saveSettings' );
214 $this->assertEquals(
'nochanges',
$e->getCodeString() );
215 $this->assertEquals(
'No changes were requested',
$e->getMessage() );
219 $this->
fail(
"UsageException was not thrown" );
223 $this->mUserMock->expects( $this->once() )
224 ->method(
'resetOptions' )
225 ->with( $this->equalTo(
array(
'all' ) ) );
227 $this->mUserMock->expects( $this->never() )
228 ->method(
'setOption' );
230 $this->mUserMock->expects( $this->once() )
231 ->method(
'saveSettings' );
241 $this->mUserMock->expects( $this->once() )
242 ->method(
'resetOptions' )
243 ->with( $this->equalTo(
array(
'registered' ) ) );
245 $this->mUserMock->expects( $this->never() )
246 ->method(
'setOption' );
248 $this->mUserMock->expects( $this->once() )
249 ->method(
'saveSettings' );
259 $this->mUserMock->expects( $this->never() )
260 ->method(
'resetOptions' );
262 $this->mUserMock->expects( $this->once() )
263 ->method(
'setOption' )
264 ->with( $this->equalTo(
'name' ), $this->equalTo(
'value' ) );
266 $this->mUserMock->expects( $this->once() )
267 ->method(
'saveSettings' );
277 $this->mUserMock->expects( $this->never() )
278 ->method(
'resetOptions' );
280 $this->mUserMock->expects( $this->once() )
281 ->method(
'setOption' )
282 ->with( $this->equalTo(
'name' ), $this->identicalTo(
null ) );
284 $this->mUserMock->expects( $this->once() )
285 ->method(
'saveSettings' );
294 $this->mUserMock->expects( $this->never() )
295 ->method(
'resetOptions' );
297 $this->mUserMock->expects( $this->at( 2 ) )
298 ->method(
'getOptions' );
300 $this->mUserMock->expects( $this->at( 4 ) )
301 ->method(
'setOption' )
302 ->with( $this->equalTo(
'willBeNull' ), $this->identicalTo(
null ) );
304 $this->mUserMock->expects( $this->at( 5 ) )
305 ->method(
'getOptions' );
307 $this->mUserMock->expects( $this->at( 6 ) )
308 ->method(
'setOption' )
309 ->with( $this->equalTo(
'willBeEmpty' ), $this->equalTo(
'' ) );
311 $this->mUserMock->expects( $this->at( 7 ) )
312 ->method(
'getOptions' );
314 $this->mUserMock->expects( $this->at( 8 ) )
315 ->method(
'setOption' )
316 ->with( $this->equalTo(
'willBeHappy' ), $this->equalTo(
'Happy' ) );
318 $this->mUserMock->expects( $this->once() )
319 ->method(
'saveSettings' );
321 $request = $this->
getSampleRequest(
array(
'change' =>
'willBeNull|willBeEmpty=|willBeHappy=Happy' ) );
329 $this->mUserMock->expects( $this->once() )
330 ->method(
'resetOptions' );
332 $this->mUserMock->expects( $this->at( 4 ) )
333 ->method(
'getOptions' );
335 $this->mUserMock->expects( $this->at( 5 ) )
336 ->method(
'setOption' )
337 ->with( $this->equalTo(
'willBeHappy' ), $this->equalTo(
'Happy' ) );
339 $this->mUserMock->expects( $this->at( 6 ) )
340 ->method(
'getOptions' );
342 $this->mUserMock->expects( $this->at( 7 ) )
343 ->method(
'setOption' )
344 ->with( $this->equalTo(
'name' ), $this->equalTo(
'value' ) );
346 $this->mUserMock->expects( $this->once() )
347 ->method(
'saveSettings' );
351 'change' =>
'willBeHappy=Happy',
352 'optionname' =>
'name',
353 'optionvalue' =>
'value'
362 $this->mUserMock->expects( $this->never() )
363 ->method(
'resetOptions' );
365 $this->mUserMock->expects( $this->at( 3 ) )
366 ->method(
'setOption' )
367 ->with( $this->equalTo(
'testmultiselect-opt1' ), $this->identicalTo(
true ) );
369 $this->mUserMock->expects( $this->at( 4 ) )
370 ->method(
'setOption' )
371 ->with( $this->equalTo(
'testmultiselect-opt2' ), $this->identicalTo(
null ) );
373 $this->mUserMock->expects( $this->at( 5 ) )
374 ->method(
'setOption' )
375 ->with( $this->equalTo(
'testmultiselect-opt3' ), $this->identicalTo(
false ) );
377 $this->mUserMock->expects( $this->at( 6 ) )
378 ->method(
'setOption' )
379 ->with( $this->equalTo(
'testmultiselect-opt4' ), $this->identicalTo(
false ) );
381 $this->mUserMock->expects( $this->once() )
382 ->method(
'saveSettings' );
385 'change' =>
'testmultiselect-opt1=1|testmultiselect-opt2|testmultiselect-opt3=|testmultiselect-opt4=0'
394 $this->mUserMock->expects( $this->never() )
395 ->method(
'resetOptions' );
397 $this->mUserMock->expects( $this->never() )
398 ->method(
'saveSettings' );
401 'change' =>
'special=1'
406 $this->assertEquals(
array(
407 'options' =>
'success',
410 '*' =>
"Validation error for 'special': cannot be set by this module"
417 $this->mUserMock->expects( $this->never() )
418 ->method(
'resetOptions' );
420 $this->mUserMock->expects( $this->never() )
421 ->method(
'saveSettings' );
424 'change' =>
'unknownOption=1'
429 $this->assertEquals(
array(
430 'options' =>
'success',
433 '*' =>
"Validation error for 'unknownOption': not a valid preference"
440 $this->mUserMock->expects( $this->never() )
441 ->method(
'resetOptions' );
443 $this->mUserMock->expects( $this->at( 3 ) )
444 ->method(
'setOption' )
445 ->with( $this->equalTo(
'userjs-option' ), $this->equalTo(
'1' ) );
447 $this->mUserMock->expects( $this->once() )
448 ->method(
'saveSettings' );
451 'change' =>
'userjs-option=1'
456 $this->assertEquals( self::$Success,
$response );