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()
24 $this->mUserMock
->expects( $this->any() )
25 ->method( 'getEffectiveGroups' )->will( $this->returnValue( array( '*', 'user' ) ) );
27 // Set up callback for User::getOptionKinds
28 $this->mUserMock
->expects( $this->any() )
29 ->method( 'getOptionKinds' )->will( $this->returnCallback( array( $this, 'getOptionKinds' ) ) );
31 // Create a new context
32 $this->mContext
= new DerivativeContext( new RequestContext() );
33 $this->mContext
->getContext()->setTitle( Title
::newFromText( 'Test' ) );
34 $this->mContext
->setUser( $this->mUserMock
);
36 $main = new ApiMain( $this->mContext
);
39 $this->mSession
= array();
41 $this->mTested
= new ApiOptions( $main, 'options' );
44 if ( !isset( $wgHooks['GetPreferences'] ) ) {
45 $wgHooks['GetPreferences'] = array();
47 $this->mOldGetPreferencesHooks
= $wgHooks['GetPreferences'];
48 $wgHooks['GetPreferences'][] = array( $this, 'hookGetPreferences' );
51 protected function tearDown() {
54 if ( $this->mOldGetPreferencesHooks
!== false ) {
55 $wgHooks['GetPreferences'] = $this->mOldGetPreferencesHooks
;
56 $this->mOldGetPreferencesHooks
= false;
62 public function hookGetPreferences( $user, &$preferences ) {
63 $preferences = array();
65 foreach ( array( 'name', 'willBeNull', 'willBeEmpty', 'willBeHappy' ) as $k ) {
66 $preferences[$k] = array(
73 $preferences['testmultiselect'] = array(
74 'type' => 'multiselect',
77 '<span dir="auto">Some HTML here for option 1</span>' => 'opt1',
78 '<span dir="auto">Some HTML here for option 2</span>' => 'opt2',
79 '<span dir="auto">Some HTML here for option 3</span>' => 'opt3',
80 '<span dir="auto">Some HTML here for option 4</span>' => 'opt4',
85 'prefix' => 'testmultiselect-',
92 public function getOptionKinds( IContextSource
$context, $options = null ) {
95 'name' => 'registered',
96 'willBeNull' => 'registered',
97 'willBeEmpty' => 'registered',
98 'willBeHappy' => 'registered',
99 'testmultiselect-opt1' => 'registered-multiselect',
100 'testmultiselect-opt2' => 'registered-multiselect',
101 'testmultiselect-opt3' => 'registered-multiselect',
102 'testmultiselect-opt4' => 'registered-multiselect',
105 if ( $options === null ) {
110 foreach ( $options as $key => $value ) {
111 if ( isset( $kinds[$key] ) ) {
112 $mapping[$key] = $kinds[$key];
113 } elseif ( substr( $key, 0, 7 ) === 'userjs-' ) {
114 $mapping[$key] = 'userjs';
116 $mapping[$key] = 'unused';
122 private function getSampleRequest( $custom = array() ) {
126 'optionname' => null,
127 'optionvalue' => null,
129 return array_merge( $request, $custom );
132 private function executeQuery( $request ) {
133 $this->mContext
->setRequest( new FauxRequest( $request, true, $this->mSession
) );
134 $this->mTested
->execute();
135 return $this->mTested
->getResult()->getData();
139 * @expectedException UsageException
141 public function testNoToken() {
142 $request = $this->getSampleRequest( array( 'token' => null ) );
144 $this->executeQuery( $request );
147 public function testAnon() {
148 $this->mUserMock
->expects( $this->once() )
150 ->will( $this->returnValue( true ) );
153 $request = $this->getSampleRequest();
155 $this->executeQuery( $request );
156 } catch ( UsageException
$e ) {
157 $this->assertEquals( 'notloggedin', $e->getCodeString() );
158 $this->assertEquals( 'Anonymous users cannot change preferences', $e->getMessage() );
161 $this->fail( "UsageException was not thrown" );
164 public function testNoOptionname() {
166 $request = $this->getSampleRequest( array( 'optionvalue' => '1' ) );
168 $this->executeQuery( $request );
169 } catch ( UsageException
$e ) {
170 $this->assertEquals( 'nooptionname', $e->getCodeString() );
171 $this->assertEquals( 'The optionname parameter must be set', $e->getMessage() );
174 $this->fail( "UsageException was not thrown" );
177 public function testNoChanges() {
178 $this->mUserMock
->expects( $this->never() )
179 ->method( 'resetOptions' );
181 $this->mUserMock
->expects( $this->never() )
182 ->method( 'setOption' );
184 $this->mUserMock
->expects( $this->never() )
185 ->method( 'saveSettings' );
188 $request = $this->getSampleRequest();
190 $this->executeQuery( $request );
191 } catch ( UsageException
$e ) {
192 $this->assertEquals( 'nochanges', $e->getCodeString() );
193 $this->assertEquals( 'No changes were requested', $e->getMessage() );
196 $this->fail( "UsageException was not thrown" );
199 public function testReset() {
200 $this->mUserMock
->expects( $this->once() )
201 ->method( 'resetOptions' );
203 $this->mUserMock
->expects( $this->never() )
204 ->method( 'setOption' );
206 $this->mUserMock
->expects( $this->once() )
207 ->method( 'saveSettings' );
209 $request = $this->getSampleRequest( array( 'reset' => '' ) );
211 $response = $this->executeQuery( $request );
213 $this->assertEquals( self
::$Success, $response );
216 public function testOptionWithValue() {
217 $this->mUserMock
->expects( $this->never() )
218 ->method( 'resetOptions' );
220 $this->mUserMock
->expects( $this->once() )
221 ->method( 'setOption' )
222 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
224 $this->mUserMock
->expects( $this->once() )
225 ->method( 'saveSettings' );
227 $request = $this->getSampleRequest( array( 'optionname' => 'name', 'optionvalue' => 'value' ) );
229 $response = $this->executeQuery( $request );
231 $this->assertEquals( self
::$Success, $response );
234 public function testOptionResetValue() {
235 $this->mUserMock
->expects( $this->never() )
236 ->method( 'resetOptions' );
238 $this->mUserMock
->expects( $this->once() )
239 ->method( 'setOption' )
240 ->with( $this->equalTo( 'name' ), $this->equalTo( null ) );
242 $this->mUserMock
->expects( $this->once() )
243 ->method( 'saveSettings' );
245 $request = $this->getSampleRequest( array( 'optionname' => 'name' ) );
246 $response = $this->executeQuery( $request );
248 $this->assertEquals( self
::$Success, $response );
251 public function testChange() {
252 $this->mUserMock
->expects( $this->never() )
253 ->method( 'resetOptions' );
255 $this->mUserMock
->expects( $this->at( 2 ) )
256 ->method( 'getOptions' );
258 $this->mUserMock
->expects( $this->at( 3 ) )
259 ->method( 'setOption' )
260 ->with( $this->equalTo( 'willBeNull' ), $this->equalTo( null ) );
262 $this->mUserMock
->expects( $this->at( 4 ) )
263 ->method( 'getOptions' );
265 $this->mUserMock
->expects( $this->at( 5 ) )
266 ->method( 'setOption' )
267 ->with( $this->equalTo( 'willBeEmpty' ), $this->equalTo( '' ) );
269 $this->mUserMock
->expects( $this->at( 6 ) )
270 ->method( 'getOptions' );
272 $this->mUserMock
->expects( $this->at( 7 ) )
273 ->method( 'setOption' )
274 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
276 $this->mUserMock
->expects( $this->once() )
277 ->method( 'saveSettings' );
279 $request = $this->getSampleRequest( array( 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy' ) );
281 $response = $this->executeQuery( $request );
283 $this->assertEquals( self
::$Success, $response );
286 public function testResetChangeOption() {
287 $this->mUserMock
->expects( $this->once() )
288 ->method( 'resetOptions' );
290 $this->mUserMock
->expects( $this->at( 3 ) )
291 ->method( 'getOptions' );
293 $this->mUserMock
->expects( $this->at( 4 ) )
294 ->method( 'setOption' )
295 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
297 $this->mUserMock
->expects( $this->at( 5 ) )
298 ->method( 'getOptions' );
300 $this->mUserMock
->expects( $this->at( 6 ) )
301 ->method( 'setOption' )
302 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
304 $this->mUserMock
->expects( $this->once() )
305 ->method( 'saveSettings' );
309 'change' => 'willBeHappy=Happy',
310 'optionname' => 'name',
311 'optionvalue' => 'value'
314 $response = $this->executeQuery( $this->getSampleRequest( $args ) );
316 $this->assertEquals( self
::$Success, $response );
319 public function testMultiSelect() {
320 $this->mUserMock
->expects( $this->never() )
321 ->method( 'resetOptions' );
323 $this->mUserMock
->expects( $this->at( 2 ) )
324 ->method( 'setOption' )
325 ->with( $this->equalTo( 'testmultiselect-opt1' ), $this->equalTo( true ) );
327 $this->mUserMock
->expects( $this->at( 3 ) )
328 ->method( 'setOption' )
329 ->with( $this->equalTo( 'testmultiselect-opt2' ), $this->equalTo( false ) );
331 $this->mUserMock
->expects( $this->at( 4 ) )
332 ->method( 'setOption' )
333 ->with( $this->equalTo( 'testmultiselect-opt3' ), $this->equalTo( false ) );
335 $this->mUserMock
->expects( $this->at( 5 ) )
336 ->method( 'setOption' )
337 ->with( $this->equalTo( 'testmultiselect-opt4' ), $this->equalTo( false ) );
339 $this->mUserMock
->expects( $this->once() )
340 ->method( 'saveSettings' );
342 $request = $this->getSampleRequest( array(
343 'change' => 'testmultiselect-opt1=1|testmultiselect-opt2|testmultiselect-opt3=|testmultiselect-opt4=0'
346 $response = $this->executeQuery( $request );
348 $this->assertEquals( self
::$Success, $response );
351 public function testUnknownOption() {
352 $this->mUserMock
->expects( $this->never() )
353 ->method( 'resetOptions' );
355 $this->mUserMock
->expects( $this->never() )
356 ->method( 'saveSettings' );
358 $request = $this->getSampleRequest( array(
359 'change' => 'unknownOption=1'
362 $response = $this->executeQuery( $request );
364 $this->assertEquals( array(
365 'options' => 'success',
368 '*' => "Validation error for 'unknownOption': not a valid preference"
374 public function testUserjsOption() {
375 $this->mUserMock
->expects( $this->never() )
376 ->method( 'resetOptions' );
378 $this->mUserMock
->expects( $this->at( 2 ) )
379 ->method( 'setOption' )
380 ->with( $this->equalTo( 'userjs-option' ), $this->equalTo( '1' ) );
382 $this->mUserMock
->expects( $this->once() )
383 ->method( 'saveSettings' );
385 $request = $this->getSampleRequest( array(
386 'change' => 'userjs-option=1'
389 $response = $this->executeQuery( $request );
391 $this->assertEquals( self
::$Success, $response );