e718957b27d4d2d782d694295c0babd260d12f19
8 class ApiOptionsTest
extends MediaWikiLangTestCase
{
10 private $mTested, $mUserMock, $mContext, $mSession;
12 private $mOldGetPreferencesHooks = false;
14 private static $Success = array( 'options' => 'success' );
16 protected function setUp() {
19 $this->mUserMock
= $this->getMockBuilder( 'User' )
20 ->disableOriginalConstructor()
23 // Set up groups and rights
24 $this->mUserMock
->expects( $this->any() )
25 ->method( 'getEffectiveGroups' )->will( $this->returnValue( array( '*', 'user' ) ) );
26 $this->mUserMock
->expects( $this->any() )
27 ->method( 'isAllowed' )->will( $this->returnValue( true ) );
29 // Set up callback for User::getOptionKinds
30 $this->mUserMock
->expects( $this->any() )
31 ->method( 'getOptionKinds' )->will( $this->returnCallback( array( $this, 'getOptionKinds' ) ) );
33 // Create a new context
34 $this->mContext
= new DerivativeContext( new RequestContext() );
35 $this->mContext
->getContext()->setTitle( Title
::newFromText( 'Test' ) );
36 $this->mContext
->setUser( $this->mUserMock
);
38 $main = new ApiMain( $this->mContext
);
41 $this->mSession
= array();
43 $this->mTested
= new ApiOptions( $main, 'options' );
46 if ( !isset( $wgHooks['GetPreferences'] ) ) {
47 $wgHooks['GetPreferences'] = array();
49 $this->mOldGetPreferencesHooks
= $wgHooks['GetPreferences'];
50 $wgHooks['GetPreferences'][] = array( $this, 'hookGetPreferences' );
53 protected function tearDown() {
56 if ( $this->mOldGetPreferencesHooks
!== false ) {
57 $wgHooks['GetPreferences'] = $this->mOldGetPreferencesHooks
;
58 $this->mOldGetPreferencesHooks
= false;
64 public function hookGetPreferences( $user, &$preferences ) {
65 $preferences = array();
67 foreach ( array( 'name', 'willBeNull', 'willBeEmpty', 'willBeHappy' ) as $k ) {
68 $preferences[$k] = array(
75 $preferences['testmultiselect'] = array(
76 'type' => 'multiselect',
79 '<span dir="auto">Some HTML here for option 1</span>' => 'opt1',
80 '<span dir="auto">Some HTML here for option 2</span>' => 'opt2',
81 '<span dir="auto">Some HTML here for option 3</span>' => 'opt3',
82 '<span dir="auto">Some HTML here for option 4</span>' => 'opt4',
87 'prefix' => 'testmultiselect-',
94 public function getOptionKinds( IContextSource
$context, $options = null ) {
97 'name' => 'registered',
98 'willBeNull' => 'registered',
99 'willBeEmpty' => 'registered',
100 'willBeHappy' => 'registered',
101 'testmultiselect-opt1' => 'registered-multiselect',
102 'testmultiselect-opt2' => 'registered-multiselect',
103 'testmultiselect-opt3' => 'registered-multiselect',
104 'testmultiselect-opt4' => 'registered-multiselect',
105 'special' => 'special',
108 if ( $options === null ) {
113 foreach ( $options as $key => $value ) {
114 if ( isset( $kinds[$key] ) ) {
115 $mapping[$key] = $kinds[$key];
116 } elseif ( substr( $key, 0, 7 ) === 'userjs-' ) {
117 $mapping[$key] = 'userjs';
119 $mapping[$key] = 'unused';
126 private function getSampleRequest( $custom = array() ) {
130 'optionname' => null,
131 'optionvalue' => null,
134 return array_merge( $request, $custom );
137 private function executeQuery( $request ) {
138 $this->mContext
->setRequest( new FauxRequest( $request, true, $this->mSession
) );
139 $this->mTested
->execute();
141 return $this->mTested
->getResult()->getData();
145 * @expectedException UsageException
147 public function testNoToken() {
148 $request = $this->getSampleRequest( array( 'token' => null ) );
150 $this->executeQuery( $request );
153 public function testAnon() {
154 $this->mUserMock
->expects( $this->once() )
156 ->will( $this->returnValue( true ) );
159 $request = $this->getSampleRequest();
161 $this->executeQuery( $request );
162 } catch ( UsageException
$e ) {
163 $this->assertEquals( 'notloggedin', $e->getCodeString() );
164 $this->assertEquals( 'Anonymous users cannot change preferences', $e->getMessage() );
168 $this->fail( "UsageException was not thrown" );
171 public function testNoOptionname() {
173 $request = $this->getSampleRequest( array( 'optionvalue' => '1' ) );
175 $this->executeQuery( $request );
176 } catch ( UsageException
$e ) {
177 $this->assertEquals( 'nooptionname', $e->getCodeString() );
178 $this->assertEquals( 'The optionname parameter must be set', $e->getMessage() );
182 $this->fail( "UsageException was not thrown" );
185 public function testNoChanges() {
186 $this->mUserMock
->expects( $this->never() )
187 ->method( 'resetOptions' );
189 $this->mUserMock
->expects( $this->never() )
190 ->method( 'setOption' );
192 $this->mUserMock
->expects( $this->never() )
193 ->method( 'saveSettings' );
196 $request = $this->getSampleRequest();
198 $this->executeQuery( $request );
199 } catch ( UsageException
$e ) {
200 $this->assertEquals( 'nochanges', $e->getCodeString() );
201 $this->assertEquals( 'No changes were requested', $e->getMessage() );
205 $this->fail( "UsageException was not thrown" );
208 public function testReset() {
209 $this->mUserMock
->expects( $this->once() )
210 ->method( 'resetOptions' )
211 ->with( $this->equalTo( array( 'all' ) ) );
213 $this->mUserMock
->expects( $this->never() )
214 ->method( 'setOption' );
216 $this->mUserMock
->expects( $this->once() )
217 ->method( 'saveSettings' );
219 $request = $this->getSampleRequest( array( 'reset' => '' ) );
221 $response = $this->executeQuery( $request );
223 $this->assertEquals( self
::$Success, $response );
226 public function testResetKinds() {
227 $this->mUserMock
->expects( $this->once() )
228 ->method( 'resetOptions' )
229 ->with( $this->equalTo( array( 'registered' ) ) );
231 $this->mUserMock
->expects( $this->never() )
232 ->method( 'setOption' );
234 $this->mUserMock
->expects( $this->once() )
235 ->method( 'saveSettings' );
237 $request = $this->getSampleRequest( array( 'reset' => '', 'resetkinds' => 'registered' ) );
239 $response = $this->executeQuery( $request );
241 $this->assertEquals( self
::$Success, $response );
244 public function testOptionWithValue() {
245 $this->mUserMock
->expects( $this->never() )
246 ->method( 'resetOptions' );
248 $this->mUserMock
->expects( $this->once() )
249 ->method( 'setOption' )
250 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
252 $this->mUserMock
->expects( $this->once() )
253 ->method( 'saveSettings' );
255 $request = $this->getSampleRequest( array( 'optionname' => 'name', 'optionvalue' => 'value' ) );
257 $response = $this->executeQuery( $request );
259 $this->assertEquals( self
::$Success, $response );
262 public function testOptionResetValue() {
263 $this->mUserMock
->expects( $this->never() )
264 ->method( 'resetOptions' );
266 $this->mUserMock
->expects( $this->once() )
267 ->method( 'setOption' )
268 ->with( $this->equalTo( 'name' ), $this->identicalTo( null ) );
270 $this->mUserMock
->expects( $this->once() )
271 ->method( 'saveSettings' );
273 $request = $this->getSampleRequest( array( 'optionname' => 'name' ) );
274 $response = $this->executeQuery( $request );
276 $this->assertEquals( self
::$Success, $response );
279 public function testChange() {
280 $this->mUserMock
->expects( $this->never() )
281 ->method( 'resetOptions' );
283 $this->mUserMock
->expects( $this->at( 2 ) )
284 ->method( 'getOptions' );
286 $this->mUserMock
->expects( $this->at( 4 ) )
287 ->method( 'setOption' )
288 ->with( $this->equalTo( 'willBeNull' ), $this->identicalTo( null ) );
290 $this->mUserMock
->expects( $this->at( 5 ) )
291 ->method( 'getOptions' );
293 $this->mUserMock
->expects( $this->at( 6 ) )
294 ->method( 'setOption' )
295 ->with( $this->equalTo( 'willBeEmpty' ), $this->equalTo( '' ) );
297 $this->mUserMock
->expects( $this->at( 7 ) )
298 ->method( 'getOptions' );
300 $this->mUserMock
->expects( $this->at( 8 ) )
301 ->method( 'setOption' )
302 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
304 $this->mUserMock
->expects( $this->once() )
305 ->method( 'saveSettings' );
307 $request = $this->getSampleRequest( array( 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy' ) );
309 $response = $this->executeQuery( $request );
311 $this->assertEquals( self
::$Success, $response );
314 public function testResetChangeOption() {
315 $this->mUserMock
->expects( $this->once() )
316 ->method( 'resetOptions' );
318 $this->mUserMock
->expects( $this->at( 4 ) )
319 ->method( 'getOptions' );
321 $this->mUserMock
->expects( $this->at( 5 ) )
322 ->method( 'setOption' )
323 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
325 $this->mUserMock
->expects( $this->at( 6 ) )
326 ->method( 'getOptions' );
328 $this->mUserMock
->expects( $this->at( 7 ) )
329 ->method( 'setOption' )
330 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
332 $this->mUserMock
->expects( $this->once() )
333 ->method( 'saveSettings' );
337 'change' => 'willBeHappy=Happy',
338 'optionname' => 'name',
339 'optionvalue' => 'value'
342 $response = $this->executeQuery( $this->getSampleRequest( $args ) );
344 $this->assertEquals( self
::$Success, $response );
347 public function testMultiSelect() {
348 $this->mUserMock
->expects( $this->never() )
349 ->method( 'resetOptions' );
351 $this->mUserMock
->expects( $this->at( 3 ) )
352 ->method( 'setOption' )
353 ->with( $this->equalTo( 'testmultiselect-opt1' ), $this->identicalTo( true ) );
355 $this->mUserMock
->expects( $this->at( 4 ) )
356 ->method( 'setOption' )
357 ->with( $this->equalTo( 'testmultiselect-opt2' ), $this->identicalTo( null ) );
359 $this->mUserMock
->expects( $this->at( 5 ) )
360 ->method( 'setOption' )
361 ->with( $this->equalTo( 'testmultiselect-opt3' ), $this->identicalTo( false ) );
363 $this->mUserMock
->expects( $this->at( 6 ) )
364 ->method( 'setOption' )
365 ->with( $this->equalTo( 'testmultiselect-opt4' ), $this->identicalTo( false ) );
367 $this->mUserMock
->expects( $this->once() )
368 ->method( 'saveSettings' );
370 $request = $this->getSampleRequest( array(
371 'change' => 'testmultiselect-opt1=1|testmultiselect-opt2|testmultiselect-opt3=|testmultiselect-opt4=0'
374 $response = $this->executeQuery( $request );
376 $this->assertEquals( self
::$Success, $response );
379 public function testSpecialOption() {
380 $this->mUserMock
->expects( $this->never() )
381 ->method( 'resetOptions' );
383 $this->mUserMock
->expects( $this->never() )
384 ->method( 'saveSettings' );
386 $request = $this->getSampleRequest( array(
387 'change' => 'special=1'
390 $response = $this->executeQuery( $request );
392 $this->assertEquals( array(
393 'options' => 'success',
396 '*' => "Validation error for 'special': cannot be set by this module"
402 public function testUnknownOption() {
403 $this->mUserMock
->expects( $this->never() )
404 ->method( 'resetOptions' );
406 $this->mUserMock
->expects( $this->never() )
407 ->method( 'saveSettings' );
409 $request = $this->getSampleRequest( array(
410 'change' => 'unknownOption=1'
413 $response = $this->executeQuery( $request );
415 $this->assertEquals( array(
416 'options' => 'success',
419 '*' => "Validation error for 'unknownOption': not a valid preference"
425 public function testUserjsOption() {
426 $this->mUserMock
->expects( $this->never() )
427 ->method( 'resetOptions' );
429 $this->mUserMock
->expects( $this->at( 3 ) )
430 ->method( 'setOption' )
431 ->with( $this->equalTo( 'userjs-option' ), $this->equalTo( '1' ) );
433 $this->mUserMock
->expects( $this->once() )
434 ->method( 'saveSettings' );
436 $request = $this->getSampleRequest( array(
437 'change' => 'userjs-option=1'
440 $response = $this->executeQuery( $request );
442 $this->assertEquals( self
::$Success, $response );