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-',
102 public function getOptionKinds( IContextSource
$context, $options = null ) {
105 'name' => 'registered',
106 'willBeNull' => 'registered',
107 'willBeEmpty' => 'registered',
108 'willBeHappy' => 'registered',
109 'testmultiselect-opt1' => 'registered-multiselect',
110 'testmultiselect-opt2' => 'registered-multiselect',
111 'testmultiselect-opt3' => 'registered-multiselect',
112 'testmultiselect-opt4' => 'registered-multiselect',
115 if ( $options === null ) {
120 foreach ( $options as $key => $value ) {
121 if ( isset( $kinds[$key] ) ) {
122 $mapping[$key] = $kinds[$key];
123 } elseif ( substr( $key, 0, 7 ) === 'userjs-' ) {
124 $mapping[$key] = 'userjs';
126 $mapping[$key] = 'unused';
133 private function getSampleRequest( $custom = array() ) {
137 'optionname' => null,
138 'optionvalue' => null,
141 return array_merge( $request, $custom );
144 private function executeQuery( $request ) {
145 $this->mContext
->setRequest( new FauxRequest( $request, true, $this->mSession
) );
146 $this->mTested
->execute();
148 return $this->mTested
->getResult()->getData();
152 * @expectedException UsageException
154 public function testNoToken() {
155 $request = $this->getSampleRequest( array( 'token' => null ) );
157 $this->executeQuery( $request );
160 public function testAnon() {
161 $this->mUserMock
->expects( $this->once() )
163 ->will( $this->returnValue( true ) );
166 $request = $this->getSampleRequest();
168 $this->executeQuery( $request );
169 } catch ( UsageException
$e ) {
170 $this->assertEquals( 'notloggedin', $e->getCodeString() );
171 $this->assertEquals( 'Anonymous users cannot change preferences', $e->getMessage() );
175 $this->fail( "UsageException was not thrown" );
178 public function testNoOptionname() {
180 $request = $this->getSampleRequest( array( 'optionvalue' => '1' ) );
182 $this->executeQuery( $request );
183 } catch ( UsageException
$e ) {
184 $this->assertEquals( 'nooptionname', $e->getCodeString() );
185 $this->assertEquals( 'The optionname parameter must be set', $e->getMessage() );
189 $this->fail( "UsageException was not thrown" );
192 public function testNoChanges() {
193 $this->mUserMock
->expects( $this->never() )
194 ->method( 'resetOptions' );
196 $this->mUserMock
->expects( $this->never() )
197 ->method( 'setOption' );
199 $this->mUserMock
->expects( $this->never() )
200 ->method( 'saveSettings' );
203 $request = $this->getSampleRequest();
205 $this->executeQuery( $request );
206 } catch ( UsageException
$e ) {
207 $this->assertEquals( 'nochanges', $e->getCodeString() );
208 $this->assertEquals( 'No changes were requested', $e->getMessage() );
212 $this->fail( "UsageException was not thrown" );
215 public function testReset() {
216 $this->mUserMock
->expects( $this->once() )
217 ->method( 'resetOptions' )
218 ->with( $this->equalTo( array( 'all' ) ) );
220 $this->mUserMock
->expects( $this->never() )
221 ->method( 'setOption' );
223 $this->mUserMock
->expects( $this->once() )
224 ->method( 'saveSettings' );
226 $request = $this->getSampleRequest( array( 'reset' => '' ) );
228 $response = $this->executeQuery( $request );
230 $this->assertEquals( self
::$Success, $response );
233 public function testResetKinds() {
234 $this->mUserMock
->expects( $this->once() )
235 ->method( 'resetOptions' )
236 ->with( $this->equalTo( array( 'registered' ) ) );
238 $this->mUserMock
->expects( $this->never() )
239 ->method( 'setOption' );
241 $this->mUserMock
->expects( $this->once() )
242 ->method( 'saveSettings' );
244 $request = $this->getSampleRequest( array( 'reset' => '', 'resetkinds' => 'registered' ) );
246 $response = $this->executeQuery( $request );
248 $this->assertEquals( self
::$Success, $response );
251 public function testOptionWithValue() {
252 $this->mUserMock
->expects( $this->never() )
253 ->method( 'resetOptions' );
255 $this->mUserMock
->expects( $this->once() )
256 ->method( 'setOption' )
257 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
259 $this->mUserMock
->expects( $this->once() )
260 ->method( 'saveSettings' );
262 $request = $this->getSampleRequest( array( 'optionname' => 'name', 'optionvalue' => 'value' ) );
264 $response = $this->executeQuery( $request );
266 $this->assertEquals( self
::$Success, $response );
269 public function testOptionResetValue() {
270 $this->mUserMock
->expects( $this->never() )
271 ->method( 'resetOptions' );
273 $this->mUserMock
->expects( $this->once() )
274 ->method( 'setOption' )
275 ->with( $this->equalTo( 'name' ), $this->identicalTo( null ) );
277 $this->mUserMock
->expects( $this->once() )
278 ->method( 'saveSettings' );
280 $request = $this->getSampleRequest( array( 'optionname' => 'name' ) );
281 $response = $this->executeQuery( $request );
283 $this->assertEquals( self
::$Success, $response );
286 public function testChange() {
287 $this->mUserMock
->expects( $this->never() )
288 ->method( 'resetOptions' );
290 $this->mUserMock
->expects( $this->at( 2 ) )
291 ->method( 'getOptions' );
293 $this->mUserMock
->expects( $this->at( 4 ) )
294 ->method( 'setOption' )
295 ->with( $this->equalTo( 'willBeNull' ), $this->identicalTo( null ) );
297 $this->mUserMock
->expects( $this->at( 5 ) )
298 ->method( 'getOptions' );
300 $this->mUserMock
->expects( $this->at( 6 ) )
301 ->method( 'setOption' )
302 ->with( $this->equalTo( 'willBeEmpty' ), $this->equalTo( '' ) );
304 $this->mUserMock
->expects( $this->at( 7 ) )
305 ->method( 'getOptions' );
307 $this->mUserMock
->expects( $this->at( 8 ) )
308 ->method( 'setOption' )
309 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
311 $this->mUserMock
->expects( $this->once() )
312 ->method( 'saveSettings' );
314 $request = $this->getSampleRequest( array( 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy' ) );
316 $response = $this->executeQuery( $request );
318 $this->assertEquals( self
::$Success, $response );
321 public function testResetChangeOption() {
322 $this->mUserMock
->expects( $this->once() )
323 ->method( 'resetOptions' );
325 $this->mUserMock
->expects( $this->at( 4 ) )
326 ->method( 'getOptions' );
328 $this->mUserMock
->expects( $this->at( 5 ) )
329 ->method( 'setOption' )
330 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
332 $this->mUserMock
->expects( $this->at( 6 ) )
333 ->method( 'getOptions' );
335 $this->mUserMock
->expects( $this->at( 7 ) )
336 ->method( 'setOption' )
337 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
339 $this->mUserMock
->expects( $this->once() )
340 ->method( 'saveSettings' );
344 'change' => 'willBeHappy=Happy',
345 'optionname' => 'name',
346 'optionvalue' => 'value'
349 $response = $this->executeQuery( $this->getSampleRequest( $args ) );
351 $this->assertEquals( self
::$Success, $response );
354 public function testMultiSelect() {
355 $this->mUserMock
->expects( $this->never() )
356 ->method( 'resetOptions' );
358 $this->mUserMock
->expects( $this->at( 3 ) )
359 ->method( 'setOption' )
360 ->with( $this->equalTo( 'testmultiselect-opt1' ), $this->identicalTo( true ) );
362 $this->mUserMock
->expects( $this->at( 4 ) )
363 ->method( 'setOption' )
364 ->with( $this->equalTo( 'testmultiselect-opt2' ), $this->identicalTo( null ) );
366 $this->mUserMock
->expects( $this->at( 5 ) )
367 ->method( 'setOption' )
368 ->with( $this->equalTo( 'testmultiselect-opt3' ), $this->identicalTo( false ) );
370 $this->mUserMock
->expects( $this->at( 6 ) )
371 ->method( 'setOption' )
372 ->with( $this->equalTo( 'testmultiselect-opt4' ), $this->identicalTo( false ) );
374 $this->mUserMock
->expects( $this->once() )
375 ->method( 'saveSettings' );
377 $request = $this->getSampleRequest( array(
378 'change' => 'testmultiselect-opt1=1|testmultiselect-opt2|testmultiselect-opt3=|testmultiselect-opt4=0'
381 $response = $this->executeQuery( $request );
383 $this->assertEquals( self
::$Success, $response );
386 public function testUnknownOption() {
387 $this->mUserMock
->expects( $this->never() )
388 ->method( 'resetOptions' );
390 $this->mUserMock
->expects( $this->never() )
391 ->method( 'saveSettings' );
393 $request = $this->getSampleRequest( array(
394 'change' => 'unknownOption=1'
397 $response = $this->executeQuery( $request );
399 $this->assertEquals( array(
400 'options' => 'success',
403 '*' => "Validation error for 'unknownOption': not a valid preference"
409 public function testUserjsOption() {
410 $this->mUserMock
->expects( $this->never() )
411 ->method( 'resetOptions' );
413 $this->mUserMock
->expects( $this->at( 3 ) )
414 ->method( 'setOption' )
415 ->with( $this->equalTo( 'userjs-option' ), $this->equalTo( '1' ) );
417 $this->mUserMock
->expects( $this->once() )
418 ->method( 'saveSettings' );
420 $request = $this->getSampleRequest( array(
421 'change' => 'userjs-option=1'
424 $response = $this->executeQuery( $request );
426 $this->assertEquals( self
::$Success, $response );