10 class ApiOptionsTest
extends MediaWikiLangTestCase
{
12 /** @var PHPUnit_Framework_MockObject_MockObject */
14 /** @var ApiOptions */
17 /** @var DerivativeContext */
20 private $mOldGetPreferencesHooks;
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 $wgHooks['GetPreferences'] = $this->mOldGetPreferencesHooks
;
65 $this->mOldGetPreferencesHooks
= false;
70 public function hookGetPreferences( $user, &$preferences ) {
71 $preferences = array();
73 foreach ( array( 'name', 'willBeNull', 'willBeEmpty', 'willBeHappy' ) as $k ) {
74 $preferences[$k] = array(
81 $preferences['testmultiselect'] = array(
82 'type' => 'multiselect',
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',
93 'prefix' => 'testmultiselect-',
101 * @param IContextSource $context
102 * @param array|null $options
106 public function getOptionKinds( IContextSource
$context, $options = null ) {
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',
120 if ( $options === null ) {
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';
131 $mapping[$key] = 'unused';
138 private function getSampleRequest( $custom = array() ) {
142 'optionname' => null,
143 'optionvalue' => null,
146 return array_merge( $request, $custom );
149 private function executeQuery( $request ) {
150 $this->mContext
->setRequest( new FauxRequest( $request, true, $this->mSession
) );
151 $this->mTested
->execute();
153 return $this->mTested
->getResult()->getData();
157 * @expectedException UsageException
159 public function testNoToken() {
160 $request = $this->getSampleRequest( array( 'token' => null ) );
162 $this->executeQuery( $request );
165 public function testAnon() {
166 $this->mUserMock
->expects( $this->once() )
168 ->will( $this->returnValue( true ) );
171 $request = $this->getSampleRequest();
173 $this->executeQuery( $request );
174 } catch ( UsageException
$e ) {
175 $this->assertEquals( 'notloggedin', $e->getCodeString() );
176 $this->assertEquals( 'Anonymous users cannot change preferences', $e->getMessage() );
180 $this->fail( "UsageException was not thrown" );
183 public function testNoOptionname() {
185 $request = $this->getSampleRequest( array( 'optionvalue' => '1' ) );
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() );
194 $this->fail( "UsageException was not thrown" );
197 public function testNoChanges() {
198 $this->mUserMock
->expects( $this->never() )
199 ->method( 'resetOptions' );
201 $this->mUserMock
->expects( $this->never() )
202 ->method( 'setOption' );
204 $this->mUserMock
->expects( $this->never() )
205 ->method( 'saveSettings' );
208 $request = $this->getSampleRequest();
210 $this->executeQuery( $request );
211 } catch ( UsageException
$e ) {
212 $this->assertEquals( 'nochanges', $e->getCodeString() );
213 $this->assertEquals( 'No changes were requested', $e->getMessage() );
217 $this->fail( "UsageException was not thrown" );
220 public function testReset() {
221 $this->mUserMock
->expects( $this->once() )
222 ->method( 'resetOptions' )
223 ->with( $this->equalTo( array( 'all' ) ) );
225 $this->mUserMock
->expects( $this->never() )
226 ->method( 'setOption' );
228 $this->mUserMock
->expects( $this->once() )
229 ->method( 'saveSettings' );
231 $request = $this->getSampleRequest( array( 'reset' => '' ) );
233 $response = $this->executeQuery( $request );
235 $this->assertEquals( self
::$Success, $response );
238 public function testResetKinds() {
239 $this->mUserMock
->expects( $this->once() )
240 ->method( 'resetOptions' )
241 ->with( $this->equalTo( array( 'registered' ) ) );
243 $this->mUserMock
->expects( $this->never() )
244 ->method( 'setOption' );
246 $this->mUserMock
->expects( $this->once() )
247 ->method( 'saveSettings' );
249 $request = $this->getSampleRequest( array( 'reset' => '', 'resetkinds' => 'registered' ) );
251 $response = $this->executeQuery( $request );
253 $this->assertEquals( self
::$Success, $response );
256 public function testOptionWithValue() {
257 $this->mUserMock
->expects( $this->never() )
258 ->method( 'resetOptions' );
260 $this->mUserMock
->expects( $this->once() )
261 ->method( 'setOption' )
262 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
264 $this->mUserMock
->expects( $this->once() )
265 ->method( 'saveSettings' );
267 $request = $this->getSampleRequest( array( 'optionname' => 'name', 'optionvalue' => 'value' ) );
269 $response = $this->executeQuery( $request );
271 $this->assertEquals( self
::$Success, $response );
274 public function testOptionResetValue() {
275 $this->mUserMock
->expects( $this->never() )
276 ->method( 'resetOptions' );
278 $this->mUserMock
->expects( $this->once() )
279 ->method( 'setOption' )
280 ->with( $this->equalTo( 'name' ), $this->identicalTo( null ) );
282 $this->mUserMock
->expects( $this->once() )
283 ->method( 'saveSettings' );
285 $request = $this->getSampleRequest( array( 'optionname' => 'name' ) );
286 $response = $this->executeQuery( $request );
288 $this->assertEquals( self
::$Success, $response );
291 public function testChange() {
292 $this->mUserMock
->expects( $this->never() )
293 ->method( 'resetOptions' );
295 $this->mUserMock
->expects( $this->at( 2 ) )
296 ->method( 'getOptions' );
298 $this->mUserMock
->expects( $this->at( 4 ) )
299 ->method( 'setOption' )
300 ->with( $this->equalTo( 'willBeNull' ), $this->identicalTo( null ) );
302 $this->mUserMock
->expects( $this->at( 5 ) )
303 ->method( 'getOptions' );
305 $this->mUserMock
->expects( $this->at( 6 ) )
306 ->method( 'setOption' )
307 ->with( $this->equalTo( 'willBeEmpty' ), $this->equalTo( '' ) );
309 $this->mUserMock
->expects( $this->at( 7 ) )
310 ->method( 'getOptions' );
312 $this->mUserMock
->expects( $this->at( 8 ) )
313 ->method( 'setOption' )
314 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
316 $this->mUserMock
->expects( $this->once() )
317 ->method( 'saveSettings' );
319 $request = $this->getSampleRequest( array(
320 '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|'
386 . 'testmultiselect-opt3=|testmultiselect-opt4=0'
389 $response = $this->executeQuery( $request );
391 $this->assertEquals( self
::$Success, $response );
394 public function testSpecialOption() {
395 $this->mUserMock
->expects( $this->never() )
396 ->method( 'resetOptions' );
398 $this->mUserMock
->expects( $this->never() )
399 ->method( 'saveSettings' );
401 $request = $this->getSampleRequest( array(
402 'change' => 'special=1'
405 $response = $this->executeQuery( $request );
407 $this->assertEquals( array(
408 'options' => 'success',
411 '*' => "Validation error for 'special': cannot be set by this module"
417 public function testUnknownOption() {
418 $this->mUserMock
->expects( $this->never() )
419 ->method( 'resetOptions' );
421 $this->mUserMock
->expects( $this->never() )
422 ->method( 'saveSettings' );
424 $request = $this->getSampleRequest( array(
425 'change' => 'unknownOption=1'
428 $response = $this->executeQuery( $request );
430 $this->assertEquals( array(
431 'options' => 'success',
434 '*' => "Validation error for 'unknownOption': not a valid preference"
440 public function testUserjsOption() {
441 $this->mUserMock
->expects( $this->never() )
442 ->method( 'resetOptions' );
444 $this->mUserMock
->expects( $this->at( 3 ) )
445 ->method( 'setOption' )
446 ->with( $this->equalTo( 'userjs-option' ), $this->equalTo( '1' ) );
448 $this->mUserMock
->expects( $this->once() )
449 ->method( 'saveSettings' );
451 $request = $this->getSampleRequest( array(
452 'change' => 'userjs-option=1'
455 $response = $this->executeQuery( $request );
457 $this->assertEquals( self
::$Success, $response );