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(
322 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy'
325 $response = $this->executeQuery( $request );
327 $this->assertEquals( self
::$Success, $response );
330 public function testResetChangeOption() {
331 $this->mUserMock
->expects( $this->once() )
332 ->method( 'resetOptions' );
334 $this->mUserMock
->expects( $this->at( 4 ) )
335 ->method( 'getOptions' );
337 $this->mUserMock
->expects( $this->at( 5 ) )
338 ->method( 'setOption' )
339 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
341 $this->mUserMock
->expects( $this->at( 6 ) )
342 ->method( 'getOptions' );
344 $this->mUserMock
->expects( $this->at( 7 ) )
345 ->method( 'setOption' )
346 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
348 $this->mUserMock
->expects( $this->once() )
349 ->method( 'saveSettings' );
353 'change' => 'willBeHappy=Happy',
354 'optionname' => 'name',
355 'optionvalue' => 'value'
358 $response = $this->executeQuery( $this->getSampleRequest( $args ) );
360 $this->assertEquals( self
::$Success, $response );
363 public function testMultiSelect() {
364 $this->mUserMock
->expects( $this->never() )
365 ->method( 'resetOptions' );
367 $this->mUserMock
->expects( $this->at( 3 ) )
368 ->method( 'setOption' )
369 ->with( $this->equalTo( 'testmultiselect-opt1' ), $this->identicalTo( true ) );
371 $this->mUserMock
->expects( $this->at( 4 ) )
372 ->method( 'setOption' )
373 ->with( $this->equalTo( 'testmultiselect-opt2' ), $this->identicalTo( null ) );
375 $this->mUserMock
->expects( $this->at( 5 ) )
376 ->method( 'setOption' )
377 ->with( $this->equalTo( 'testmultiselect-opt3' ), $this->identicalTo( false ) );
379 $this->mUserMock
->expects( $this->at( 6 ) )
380 ->method( 'setOption' )
381 ->with( $this->equalTo( 'testmultiselect-opt4' ), $this->identicalTo( false ) );
383 $this->mUserMock
->expects( $this->once() )
384 ->method( 'saveSettings' );
386 $request = $this->getSampleRequest( array(
387 'change' => 'testmultiselect-opt1=1|testmultiselect-opt2|'
388 . 'testmultiselect-opt3=|testmultiselect-opt4=0'
391 $response = $this->executeQuery( $request );
393 $this->assertEquals( self
::$Success, $response );
396 public function testSpecialOption() {
397 $this->mUserMock
->expects( $this->never() )
398 ->method( 'resetOptions' );
400 $this->mUserMock
->expects( $this->never() )
401 ->method( 'saveSettings' );
403 $request = $this->getSampleRequest( array(
404 'change' => 'special=1'
407 $response = $this->executeQuery( $request );
409 $this->assertEquals( array(
410 'options' => 'success',
413 '*' => "Validation error for 'special': cannot be set by this module"
419 public function testUnknownOption() {
420 $this->mUserMock
->expects( $this->never() )
421 ->method( 'resetOptions' );
423 $this->mUserMock
->expects( $this->never() )
424 ->method( 'saveSettings' );
426 $request = $this->getSampleRequest( array(
427 'change' => 'unknownOption=1'
430 $response = $this->executeQuery( $request );
432 $this->assertEquals( array(
433 'options' => 'success',
436 '*' => "Validation error for 'unknownOption': not a valid preference"
442 public function testUserjsOption() {
443 $this->mUserMock
->expects( $this->never() )
444 ->method( 'resetOptions' );
446 $this->mUserMock
->expects( $this->at( 3 ) )
447 ->method( 'setOption' )
448 ->with( $this->equalTo( 'userjs-option' ), $this->equalTo( '1' ) );
450 $this->mUserMock
->expects( $this->once() )
451 ->method( 'saveSettings' );
453 $request = $this->getSampleRequest( array(
454 'change' => 'userjs-option=1'
457 $response = $this->executeQuery( $request );
459 $this->assertEquals( self
::$Success, $response );