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',
121 if ( $options === null ) {
126 foreach ( $options as $key => $value ) {
127 if ( isset( $kinds[$key] ) ) {
128 $mapping[$key] = $kinds[$key];
129 } elseif ( substr( $key, 0, 7 ) === 'userjs-' ) {
130 $mapping[$key] = 'userjs';
132 $mapping[$key] = 'unused';
139 private function getSampleRequest( $custom = array() ) {
143 'optionname' => null,
144 'optionvalue' => null,
147 return array_merge( $request, $custom );
150 private function executeQuery( $request ) {
151 $this->mContext
->setRequest( new FauxRequest( $request, true, $this->mSession
) );
152 $this->mTested
->execute();
154 return $this->mTested
->getResult()->getData();
158 * @expectedException UsageException
160 public function testNoToken() {
161 $request = $this->getSampleRequest( array( 'token' => null ) );
163 $this->executeQuery( $request );
166 public function testAnon() {
167 $this->mUserMock
->expects( $this->once() )
169 ->will( $this->returnValue( true ) );
172 $request = $this->getSampleRequest();
174 $this->executeQuery( $request );
175 } catch ( UsageException
$e ) {
176 $this->assertEquals( 'notloggedin', $e->getCodeString() );
177 $this->assertEquals( 'Anonymous users cannot change preferences', $e->getMessage() );
181 $this->fail( "UsageException was not thrown" );
184 public function testNoOptionname() {
186 $request = $this->getSampleRequest( array( 'optionvalue' => '1' ) );
188 $this->executeQuery( $request );
189 } catch ( UsageException
$e ) {
190 $this->assertEquals( 'nooptionname', $e->getCodeString() );
191 $this->assertEquals( 'The optionname parameter must be set', $e->getMessage() );
195 $this->fail( "UsageException was not thrown" );
198 public function testNoChanges() {
199 $this->mUserMock
->expects( $this->never() )
200 ->method( 'resetOptions' );
202 $this->mUserMock
->expects( $this->never() )
203 ->method( 'setOption' );
205 $this->mUserMock
->expects( $this->never() )
206 ->method( 'saveSettings' );
209 $request = $this->getSampleRequest();
211 $this->executeQuery( $request );
212 } catch ( UsageException
$e ) {
213 $this->assertEquals( 'nochanges', $e->getCodeString() );
214 $this->assertEquals( 'No changes were requested', $e->getMessage() );
218 $this->fail( "UsageException was not thrown" );
221 public function testReset() {
222 $this->mUserMock
->expects( $this->once() )
223 ->method( 'resetOptions' )
224 ->with( $this->equalTo( array( 'all' ) ) );
226 $this->mUserMock
->expects( $this->never() )
227 ->method( 'setOption' );
229 $this->mUserMock
->expects( $this->once() )
230 ->method( 'saveSettings' );
232 $request = $this->getSampleRequest( array( 'reset' => '' ) );
234 $response = $this->executeQuery( $request );
236 $this->assertEquals( self
::$Success, $response );
239 public function testResetKinds() {
240 $this->mUserMock
->expects( $this->once() )
241 ->method( 'resetOptions' )
242 ->with( $this->equalTo( array( 'registered' ) ) );
244 $this->mUserMock
->expects( $this->never() )
245 ->method( 'setOption' );
247 $this->mUserMock
->expects( $this->once() )
248 ->method( 'saveSettings' );
250 $request = $this->getSampleRequest( array( 'reset' => '', 'resetkinds' => 'registered' ) );
252 $response = $this->executeQuery( $request );
254 $this->assertEquals( self
::$Success, $response );
257 public function testOptionWithValue() {
258 $this->mUserMock
->expects( $this->never() )
259 ->method( 'resetOptions' );
261 $this->mUserMock
->expects( $this->once() )
262 ->method( 'setOption' )
263 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
265 $this->mUserMock
->expects( $this->once() )
266 ->method( 'saveSettings' );
268 $request = $this->getSampleRequest( array( 'optionname' => 'name', 'optionvalue' => 'value' ) );
270 $response = $this->executeQuery( $request );
272 $this->assertEquals( self
::$Success, $response );
275 public function testOptionResetValue() {
276 $this->mUserMock
->expects( $this->never() )
277 ->method( 'resetOptions' );
279 $this->mUserMock
->expects( $this->once() )
280 ->method( 'setOption' )
281 ->with( $this->equalTo( 'name' ), $this->identicalTo( null ) );
283 $this->mUserMock
->expects( $this->once() )
284 ->method( 'saveSettings' );
286 $request = $this->getSampleRequest( array( 'optionname' => 'name' ) );
287 $response = $this->executeQuery( $request );
289 $this->assertEquals( self
::$Success, $response );
292 public function testChange() {
293 $this->mUserMock
->expects( $this->never() )
294 ->method( 'resetOptions' );
296 $this->mUserMock
->expects( $this->at( 2 ) )
297 ->method( 'getOptions' );
299 $this->mUserMock
->expects( $this->at( 4 ) )
300 ->method( 'setOption' )
301 ->with( $this->equalTo( 'willBeNull' ), $this->identicalTo( null ) );
303 $this->mUserMock
->expects( $this->at( 5 ) )
304 ->method( 'getOptions' );
306 $this->mUserMock
->expects( $this->at( 6 ) )
307 ->method( 'setOption' )
308 ->with( $this->equalTo( 'willBeEmpty' ), $this->equalTo( '' ) );
310 $this->mUserMock
->expects( $this->at( 7 ) )
311 ->method( 'getOptions' );
313 $this->mUserMock
->expects( $this->at( 8 ) )
314 ->method( 'setOption' )
315 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
317 $this->mUserMock
->expects( $this->once() )
318 ->method( 'saveSettings' );
320 $request = $this->getSampleRequest( array( 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy' ) );
322 $response = $this->executeQuery( $request );
324 $this->assertEquals( self
::$Success, $response );
327 public function testResetChangeOption() {
328 $this->mUserMock
->expects( $this->once() )
329 ->method( 'resetOptions' );
331 $this->mUserMock
->expects( $this->at( 4 ) )
332 ->method( 'getOptions' );
334 $this->mUserMock
->expects( $this->at( 5 ) )
335 ->method( 'setOption' )
336 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
338 $this->mUserMock
->expects( $this->at( 6 ) )
339 ->method( 'getOptions' );
341 $this->mUserMock
->expects( $this->at( 7 ) )
342 ->method( 'setOption' )
343 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
345 $this->mUserMock
->expects( $this->once() )
346 ->method( 'saveSettings' );
350 'change' => 'willBeHappy=Happy',
351 'optionname' => 'name',
352 'optionvalue' => 'value'
355 $response = $this->executeQuery( $this->getSampleRequest( $args ) );
357 $this->assertEquals( self
::$Success, $response );
360 public function testMultiSelect() {
361 $this->mUserMock
->expects( $this->never() )
362 ->method( 'resetOptions' );
364 $this->mUserMock
->expects( $this->at( 3 ) )
365 ->method( 'setOption' )
366 ->with( $this->equalTo( 'testmultiselect-opt1' ), $this->identicalTo( true ) );
368 $this->mUserMock
->expects( $this->at( 4 ) )
369 ->method( 'setOption' )
370 ->with( $this->equalTo( 'testmultiselect-opt2' ), $this->identicalTo( null ) );
372 $this->mUserMock
->expects( $this->at( 5 ) )
373 ->method( 'setOption' )
374 ->with( $this->equalTo( 'testmultiselect-opt3' ), $this->identicalTo( false ) );
376 $this->mUserMock
->expects( $this->at( 6 ) )
377 ->method( 'setOption' )
378 ->with( $this->equalTo( 'testmultiselect-opt4' ), $this->identicalTo( false ) );
380 $this->mUserMock
->expects( $this->once() )
381 ->method( 'saveSettings' );
383 $request = $this->getSampleRequest( array(
384 'change' => 'testmultiselect-opt1=1|testmultiselect-opt2|testmultiselect-opt3=|testmultiselect-opt4=0'
387 $response = $this->executeQuery( $request );
389 $this->assertEquals( self
::$Success, $response );
392 public function testUnknownOption() {
393 $this->mUserMock
->expects( $this->never() )
394 ->method( 'resetOptions' );
396 $this->mUserMock
->expects( $this->never() )
397 ->method( 'saveSettings' );
399 $request = $this->getSampleRequest( array(
400 'change' => 'unknownOption=1'
403 $response = $this->executeQuery( $request );
405 $this->assertEquals( array(
406 'options' => 'success',
409 '*' => "Validation error for 'unknownOption': not a valid preference"
415 public function testUserjsOption() {
416 $this->mUserMock
->expects( $this->never() )
417 ->method( 'resetOptions' );
419 $this->mUserMock
->expects( $this->at( 3 ) )
420 ->method( 'setOption' )
421 ->with( $this->equalTo( 'userjs-option' ), $this->equalTo( '1' ) );
423 $this->mUserMock
->expects( $this->once() )
424 ->method( 'saveSettings' );
426 $request = $this->getSampleRequest( array(
427 'change' => 'userjs-option=1'
430 $response = $this->executeQuery( $request );
432 $this->assertEquals( self
::$Success, $response );