Merge "mw.loader: Guard against odd setTimeout behaviour in old Firefox"
[mediawiki.git] / tests / phpunit / includes / api / ApiOptionsTest.php
blob902b7b8550815a192494a1cbf277108e09a4440f
1 <?php
3 /**
4 * @group API
5 * @group Database
6 * @group medium
7 */
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() {
17 parent::setUp();
19 $this->mUserMock = $this->getMockBuilder( 'User' )
20 ->disableOriginalConstructor()
21 ->getMock();
23 // Set up groups
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 );
38 // Empty session
39 $this->mSession = array();
41 $this->mTested = new ApiOptions( $main, 'options' );
43 global $wgHooks;
44 if ( !isset( $wgHooks['GetPreferences'] ) ) {
45 $wgHooks['GetPreferences'] = array();
47 $this->mOldGetPreferencesHooks = $wgHooks['GetPreferences'];
48 $wgHooks['GetPreferences'][] = array( $this, 'hookGetPreferences' );
51 protected function tearDown() {
52 global $wgHooks;
54 if ( $this->mOldGetPreferencesHooks !== false ) {
55 $wgHooks['GetPreferences'] = $this->mOldGetPreferencesHooks;
56 $this->mOldGetPreferencesHooks = false;
59 parent::tearDown();
62 public function hookGetPreferences( $user, &$preferences ) {
63 $preferences = array();
65 foreach ( array( 'name', 'willBeNull', 'willBeEmpty', 'willBeHappy' ) as $k ) {
66 $preferences[$k] = array(
67 'type' => 'text',
68 'section' => 'test',
69 'label' => '&#160;',
73 $preferences['testmultiselect'] = array(
74 'type' => 'multiselect',
75 'options' => array(
76 'Test' => array(
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',
83 'section' => 'test',
84 'label' => '&#160;',
85 'prefix' => 'testmultiselect-',
86 'default' => array(),
89 return true;
92 public function getOptionKinds( IContextSource $context, $options = null ) {
93 // Match with above.
94 $kinds = array(
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 ) {
106 return $kinds;
109 $mapping = array();
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';
115 } else {
116 $mapping[$key] = 'unused';
119 return $mapping;
122 private function getSampleRequest( $custom = array() ) {
123 $request = array(
124 'token' => '123ABC',
125 'change' => null,
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() )
149 ->method( 'isAnon' )
150 ->will( $this->returnValue( true ) );
152 try {
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() );
159 return;
161 $this->fail( "UsageException was not thrown" );
164 public function testNoOptionname() {
165 try {
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() );
172 return;
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' );
187 try {
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() );
194 return;
196 $this->fail( "UsageException was not thrown" );
199 public function testReset() {
200 $this->mUserMock->expects( $this->once() )
201 ->method( 'resetOptions' )
202 ->with( $this->equalTo( array( 'all' ) ) );
204 $this->mUserMock->expects( $this->never() )
205 ->method( 'setOption' );
207 $this->mUserMock->expects( $this->once() )
208 ->method( 'saveSettings' );
210 $request = $this->getSampleRequest( array( 'reset' => '' ) );
212 $response = $this->executeQuery( $request );
214 $this->assertEquals( self::$Success, $response );
217 public function testResetKinds() {
218 $this->mUserMock->expects( $this->once() )
219 ->method( 'resetOptions' )
220 ->with( $this->equalTo( array( 'registered' ) ) );
222 $this->mUserMock->expects( $this->never() )
223 ->method( 'setOption' );
225 $this->mUserMock->expects( $this->once() )
226 ->method( 'saveSettings' );
228 $request = $this->getSampleRequest( array( 'reset' => '', 'resetkinds' => 'registered' ) );
230 $response = $this->executeQuery( $request );
232 $this->assertEquals( self::$Success, $response );
235 public function testOptionWithValue() {
236 $this->mUserMock->expects( $this->never() )
237 ->method( 'resetOptions' );
239 $this->mUserMock->expects( $this->once() )
240 ->method( 'setOption' )
241 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
243 $this->mUserMock->expects( $this->once() )
244 ->method( 'saveSettings' );
246 $request = $this->getSampleRequest( array( 'optionname' => 'name', 'optionvalue' => 'value' ) );
248 $response = $this->executeQuery( $request );
250 $this->assertEquals( self::$Success, $response );
253 public function testOptionResetValue() {
254 $this->mUserMock->expects( $this->never() )
255 ->method( 'resetOptions' );
257 $this->mUserMock->expects( $this->once() )
258 ->method( 'setOption' )
259 ->with( $this->equalTo( 'name' ), $this->identicalTo( null ) );
261 $this->mUserMock->expects( $this->once() )
262 ->method( 'saveSettings' );
264 $request = $this->getSampleRequest( array( 'optionname' => 'name' ) );
265 $response = $this->executeQuery( $request );
267 $this->assertEquals( self::$Success, $response );
270 public function testChange() {
271 $this->mUserMock->expects( $this->never() )
272 ->method( 'resetOptions' );
274 $this->mUserMock->expects( $this->at( 2 ) )
275 ->method( 'getOptions' );
277 $this->mUserMock->expects( $this->at( 3 ) )
278 ->method( 'setOption' )
279 ->with( $this->equalTo( 'willBeNull' ), $this->identicalTo( null ) );
281 $this->mUserMock->expects( $this->at( 4 ) )
282 ->method( 'getOptions' );
284 $this->mUserMock->expects( $this->at( 5 ) )
285 ->method( 'setOption' )
286 ->with( $this->equalTo( 'willBeEmpty' ), $this->equalTo( '' ) );
288 $this->mUserMock->expects( $this->at( 6 ) )
289 ->method( 'getOptions' );
291 $this->mUserMock->expects( $this->at( 7 ) )
292 ->method( 'setOption' )
293 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
295 $this->mUserMock->expects( $this->once() )
296 ->method( 'saveSettings' );
298 $request = $this->getSampleRequest( array( 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy' ) );
300 $response = $this->executeQuery( $request );
302 $this->assertEquals( self::$Success, $response );
305 public function testResetChangeOption() {
306 $this->mUserMock->expects( $this->once() )
307 ->method( 'resetOptions' );
309 $this->mUserMock->expects( $this->at( 3 ) )
310 ->method( 'getOptions' );
312 $this->mUserMock->expects( $this->at( 4 ) )
313 ->method( 'setOption' )
314 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
316 $this->mUserMock->expects( $this->at( 5 ) )
317 ->method( 'getOptions' );
319 $this->mUserMock->expects( $this->at( 6 ) )
320 ->method( 'setOption' )
321 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
323 $this->mUserMock->expects( $this->once() )
324 ->method( 'saveSettings' );
326 $args = array(
327 'reset' => '',
328 'change' => 'willBeHappy=Happy',
329 'optionname' => 'name',
330 'optionvalue' => 'value'
333 $response = $this->executeQuery( $this->getSampleRequest( $args ) );
335 $this->assertEquals( self::$Success, $response );
338 public function testMultiSelect() {
339 $this->mUserMock->expects( $this->never() )
340 ->method( 'resetOptions' );
342 $this->mUserMock->expects( $this->at( 2 ) )
343 ->method( 'setOption' )
344 ->with( $this->equalTo( 'testmultiselect-opt1' ), $this->identicalTo( true ) );
346 $this->mUserMock->expects( $this->at( 3 ) )
347 ->method( 'setOption' )
348 ->with( $this->equalTo( 'testmultiselect-opt2' ), $this->identicalTo( null ) );
350 $this->mUserMock->expects( $this->at( 4 ) )
351 ->method( 'setOption' )
352 ->with( $this->equalTo( 'testmultiselect-opt3' ), $this->identicalTo( false ) );
354 $this->mUserMock->expects( $this->at( 5 ) )
355 ->method( 'setOption' )
356 ->with( $this->equalTo( 'testmultiselect-opt4' ), $this->identicalTo( false ) );
358 $this->mUserMock->expects( $this->once() )
359 ->method( 'saveSettings' );
361 $request = $this->getSampleRequest( array(
362 'change' => 'testmultiselect-opt1=1|testmultiselect-opt2|testmultiselect-opt3=|testmultiselect-opt4=0'
363 ) );
365 $response = $this->executeQuery( $request );
367 $this->assertEquals( self::$Success, $response );
370 public function testUnknownOption() {
371 $this->mUserMock->expects( $this->never() )
372 ->method( 'resetOptions' );
374 $this->mUserMock->expects( $this->never() )
375 ->method( 'saveSettings' );
377 $request = $this->getSampleRequest( array(
378 'change' => 'unknownOption=1'
379 ) );
381 $response = $this->executeQuery( $request );
383 $this->assertEquals( array(
384 'options' => 'success',
385 'warnings' => array(
386 'options' => array(
387 '*' => "Validation error for 'unknownOption': not a valid preference"
390 ), $response );
393 public function testUserjsOption() {
394 $this->mUserMock->expects( $this->never() )
395 ->method( 'resetOptions' );
397 $this->mUserMock->expects( $this->at( 2 ) )
398 ->method( 'setOption' )
399 ->with( $this->equalTo( 'userjs-option' ), $this->equalTo( '1' ) );
401 $this->mUserMock->expects( $this->once() )
402 ->method( 'saveSettings' );
404 $request = $this->getSampleRequest( array(
405 'change' => 'userjs-option=1'
406 ) );
408 $response = $this->executeQuery( $request );
410 $this->assertEquals( self::$Success, $response );