10 class ApiOptionsTest
extends MediaWikiLangTestCase
{
12 /** @var PHPUnit_Framework_MockObject_MockObject */
14 /** @var ApiOptions */
17 /** @var DerivativeContext */
20 private $mOldGetPreferencesHooks = false;
22 private static $Success = array( 'options' => 'success' );
24 protected function setUp() {
27 $this->mUserMock
= $this->getMockBuilder( 'User' )
28 ->disableOriginalConstructor()
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 ) );
37 // Set up callback for User::getOptionKinds
38 $this->mUserMock
->expects( $this->any() )
39 ->method( 'getOptionKinds' )->will( $this->returnCallback( array( $this, 'getOptionKinds' ) ) );
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
);
46 $main = new ApiMain( $this->mContext
);
49 $this->mSession
= array();
51 $this->mTested
= new ApiOptions( $main, 'options' );
54 if ( !isset( $wgHooks['GetPreferences'] ) ) {
55 $wgHooks['GetPreferences'] = array();
57 $this->mOldGetPreferencesHooks
= $wgHooks['GetPreferences'];
58 $wgHooks['GetPreferences'][] = array( $this, 'hookGetPreferences' );
61 protected function tearDown() {
64 if ( $this->mOldGetPreferencesHooks
!== false ) {
65 $wgHooks['GetPreferences'] = $this->mOldGetPreferencesHooks
;
66 $this->mOldGetPreferencesHooks
= false;
72 public function hookGetPreferences( $user, &$preferences ) {
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-',
103 * @param IContextSource $context
104 * @param array|null $options
108 public function getOptionKinds( IContextSource
$context, $options = null ) {
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',
122 if ( $options === null ) {
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';
133 $mapping[$key] = 'unused';
140 private function getSampleRequest( $custom = array() ) {
144 'optionname' => null,
145 'optionvalue' => null,
148 return array_merge( $request, $custom );
151 private function executeQuery( $request ) {
152 $this->mContext
->setRequest( new FauxRequest( $request, true, $this->mSession
) );
153 $this->mTested
->execute();
155 return $this->mTested
->getResult()->getData();
159 * @expectedException UsageException
161 public function testNoToken() {
162 $request = $this->getSampleRequest( array( 'token' => null ) );
164 $this->executeQuery( $request );
167 public function testAnon() {
168 $this->mUserMock
->expects( $this->once() )
170 ->will( $this->returnValue( true ) );
173 $request = $this->getSampleRequest();
175 $this->executeQuery( $request );
176 } catch ( UsageException
$e ) {
177 $this->assertEquals( 'notloggedin', $e->getCodeString() );
178 $this->assertEquals( 'Anonymous users cannot change preferences', $e->getMessage() );
182 $this->fail( "UsageException was not thrown" );
185 public function testNoOptionname() {
187 $request = $this->getSampleRequest( array( 'optionvalue' => '1' ) );
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() );
196 $this->fail( "UsageException was not thrown" );
199 public function testNoChanges() {
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' );
210 $request = $this->getSampleRequest();
212 $this->executeQuery( $request );
213 } catch ( UsageException
$e ) {
214 $this->assertEquals( 'nochanges', $e->getCodeString() );
215 $this->assertEquals( 'No changes were requested', $e->getMessage() );
219 $this->fail( "UsageException was not thrown" );
222 public function testReset() {
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' );
233 $request = $this->getSampleRequest( array( 'reset' => '' ) );
235 $response = $this->executeQuery( $request );
237 $this->assertEquals( self
::$Success, $response );
240 public function testResetKinds() {
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' );
251 $request = $this->getSampleRequest( array( 'reset' => '', 'resetkinds' => 'registered' ) );
253 $response = $this->executeQuery( $request );
255 $this->assertEquals( self
::$Success, $response );
258 public function testOptionWithValue() {
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' );
269 $request = $this->getSampleRequest( array( 'optionname' => 'name', 'optionvalue' => 'value' ) );
271 $response = $this->executeQuery( $request );
273 $this->assertEquals( self
::$Success, $response );
276 public function testOptionResetValue() {
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' );
287 $request = $this->getSampleRequest( array( 'optionname' => 'name' ) );
288 $response = $this->executeQuery( $request );
290 $this->assertEquals( self
::$Success, $response );
293 public function testChange() {
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' ) );
323 $response = $this->executeQuery( $request );
325 $this->assertEquals( self
::$Success, $response );
328 public function testResetChangeOption() {
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'
356 $response = $this->executeQuery( $this->getSampleRequest( $args ) );
358 $this->assertEquals( self
::$Success, $response );
361 public function testMultiSelect() {
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' );
384 $request = $this->getSampleRequest( array(
385 'change' => 'testmultiselect-opt1=1|testmultiselect-opt2|testmultiselect-opt3=|testmultiselect-opt4=0'
388 $response = $this->executeQuery( $request );
390 $this->assertEquals( self
::$Success, $response );
393 public function testSpecialOption() {
394 $this->mUserMock
->expects( $this->never() )
395 ->method( 'resetOptions' );
397 $this->mUserMock
->expects( $this->never() )
398 ->method( 'saveSettings' );
400 $request = $this->getSampleRequest( array(
401 'change' => 'special=1'
404 $response = $this->executeQuery( $request );
406 $this->assertEquals( array(
407 'options' => 'success',
410 '*' => "Validation error for 'special': cannot be set by this module"
416 public function testUnknownOption() {
417 $this->mUserMock
->expects( $this->never() )
418 ->method( 'resetOptions' );
420 $this->mUserMock
->expects( $this->never() )
421 ->method( 'saveSettings' );
423 $request = $this->getSampleRequest( array(
424 'change' => 'unknownOption=1'
427 $response = $this->executeQuery( $request );
429 $this->assertEquals( array(
430 'options' => 'success',
433 '*' => "Validation error for 'unknownOption': not a valid preference"
439 public function testUserjsOption() {
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' );
450 $request = $this->getSampleRequest( array(
451 'change' => 'userjs-option=1'
454 $response = $this->executeQuery( $request );
456 $this->assertEquals( self
::$Success, $response );