Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / tests / phpunit / includes / api / ApiOptionsTest.php
blobbd34018e3f589bceaa619dce9fe0663256d857fe
1 <?php
3 /**
4 * @group API
5 * @group Database
6 * @group medium
8 * @covers ApiOptions
9 */
10 class ApiOptionsTest extends MediaWikiLangTestCase {
12 /** @var PHPUnit_Framework_MockObject_MockObject */
13 private $mUserMock;
14 /** @var ApiOptions */
15 private $mTested;
16 private $mSession;
17 /** @var DerivativeContext */
18 private $mContext;
20 private static $Success = array( 'options' => 'success' );
22 protected function setUp() {
23 parent::setUp();
25 $this->mUserMock = $this->getMockBuilder( 'User' )
26 ->disableOriginalConstructor()
27 ->getMock();
29 // Set up groups and rights
30 $this->mUserMock->expects( $this->any() )
31 ->method( 'getEffectiveGroups' )->will( $this->returnValue( array( '*', 'user' ) ) );
32 $this->mUserMock->expects( $this->any() )
33 ->method( 'isAllowed' )->will( $this->returnValue( true ) );
35 // Set up callback for User::getOptionKinds
36 $this->mUserMock->expects( $this->any() )
37 ->method( 'getOptionKinds' )->will( $this->returnCallback( array( $this, 'getOptionKinds' ) ) );
39 // Create a new context
40 $this->mContext = new DerivativeContext( new RequestContext() );
41 $this->mContext->getContext()->setTitle( Title::newFromText( 'Test' ) );
42 $this->mContext->setUser( $this->mUserMock );
44 $main = new ApiMain( $this->mContext );
46 // Empty session
47 $this->mSession = array();
49 $this->mTested = new ApiOptions( $main, 'options' );
51 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
52 'GetPreferences' => array(
53 array( $this, 'hookGetPreferences' )
55 ) );
58 public function hookGetPreferences( $user, &$preferences ) {
59 $preferences = array();
61 foreach ( array( 'name', 'willBeNull', 'willBeEmpty', 'willBeHappy' ) as $k ) {
62 $preferences[$k] = array(
63 'type' => 'text',
64 'section' => 'test',
65 'label' => '&#160;',
69 $preferences['testmultiselect'] = array(
70 'type' => 'multiselect',
71 'options' => array(
72 'Test' => array(
73 '<span dir="auto">Some HTML here for option 1</span>' => 'opt1',
74 '<span dir="auto">Some HTML here for option 2</span>' => 'opt2',
75 '<span dir="auto">Some HTML here for option 3</span>' => 'opt3',
76 '<span dir="auto">Some HTML here for option 4</span>' => 'opt4',
79 'section' => 'test',
80 'label' => '&#160;',
81 'prefix' => 'testmultiselect-',
82 'default' => array(),
85 return true;
88 /**
89 * @param IContextSource $context
90 * @param array|null $options
92 * @return array
94 public function getOptionKinds( IContextSource $context, $options = null ) {
95 // Match with above.
96 $kinds = array(
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 ) {
109 return $kinds;
112 $mapping = array();
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';
118 } else {
119 $mapping[$key] = 'unused';
123 return $mapping;
126 private function getSampleRequest( $custom = array() ) {
127 $request = array(
128 'token' => '123ABC',
129 'change' => null,
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() )
155 ->method( 'isAnon' )
156 ->will( $this->returnValue( true ) );
158 try {
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() );
166 return;
168 $this->fail( "UsageException was not thrown" );
171 public function testNoOptionname() {
172 try {
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() );
180 return;
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' );
195 try {
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() );
203 return;
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(
308 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy'
309 ) );
311 $response = $this->executeQuery( $request );
313 $this->assertEquals( self::$Success, $response );
316 public function testResetChangeOption() {
317 $this->mUserMock->expects( $this->once() )
318 ->method( 'resetOptions' );
320 $this->mUserMock->expects( $this->at( 4 ) )
321 ->method( 'getOptions' );
323 $this->mUserMock->expects( $this->at( 5 ) )
324 ->method( 'setOption' )
325 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
327 $this->mUserMock->expects( $this->at( 6 ) )
328 ->method( 'getOptions' );
330 $this->mUserMock->expects( $this->at( 7 ) )
331 ->method( 'setOption' )
332 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
334 $this->mUserMock->expects( $this->once() )
335 ->method( 'saveSettings' );
337 $args = array(
338 'reset' => '',
339 'change' => 'willBeHappy=Happy',
340 'optionname' => 'name',
341 'optionvalue' => 'value'
344 $response = $this->executeQuery( $this->getSampleRequest( $args ) );
346 $this->assertEquals( self::$Success, $response );
349 public function testMultiSelect() {
350 $this->mUserMock->expects( $this->never() )
351 ->method( 'resetOptions' );
353 $this->mUserMock->expects( $this->at( 3 ) )
354 ->method( 'setOption' )
355 ->with( $this->equalTo( 'testmultiselect-opt1' ), $this->identicalTo( true ) );
357 $this->mUserMock->expects( $this->at( 4 ) )
358 ->method( 'setOption' )
359 ->with( $this->equalTo( 'testmultiselect-opt2' ), $this->identicalTo( null ) );
361 $this->mUserMock->expects( $this->at( 5 ) )
362 ->method( 'setOption' )
363 ->with( $this->equalTo( 'testmultiselect-opt3' ), $this->identicalTo( false ) );
365 $this->mUserMock->expects( $this->at( 6 ) )
366 ->method( 'setOption' )
367 ->with( $this->equalTo( 'testmultiselect-opt4' ), $this->identicalTo( false ) );
369 $this->mUserMock->expects( $this->once() )
370 ->method( 'saveSettings' );
372 $request = $this->getSampleRequest( array(
373 'change' => 'testmultiselect-opt1=1|testmultiselect-opt2|'
374 . 'testmultiselect-opt3=|testmultiselect-opt4=0'
375 ) );
377 $response = $this->executeQuery( $request );
379 $this->assertEquals( self::$Success, $response );
382 public function testSpecialOption() {
383 $this->mUserMock->expects( $this->never() )
384 ->method( 'resetOptions' );
386 $this->mUserMock->expects( $this->never() )
387 ->method( 'saveSettings' );
389 $request = $this->getSampleRequest( array(
390 'change' => 'special=1'
391 ) );
393 $response = $this->executeQuery( $request );
395 $this->assertEquals( array(
396 'options' => 'success',
397 'warnings' => array(
398 'options' => array(
399 '*' => "Validation error for 'special': cannot be set by this module"
402 ), $response );
405 public function testUnknownOption() {
406 $this->mUserMock->expects( $this->never() )
407 ->method( 'resetOptions' );
409 $this->mUserMock->expects( $this->never() )
410 ->method( 'saveSettings' );
412 $request = $this->getSampleRequest( array(
413 'change' => 'unknownOption=1'
414 ) );
416 $response = $this->executeQuery( $request );
418 $this->assertEquals( array(
419 'options' => 'success',
420 'warnings' => array(
421 'options' => array(
422 '*' => "Validation error for 'unknownOption': not a valid preference"
425 ), $response );
428 public function testUserjsOption() {
429 $this->mUserMock->expects( $this->never() )
430 ->method( 'resetOptions' );
432 $this->mUserMock->expects( $this->at( 3 ) )
433 ->method( 'setOption' )
434 ->with( $this->equalTo( 'userjs-option' ), $this->equalTo( '1' ) );
436 $this->mUserMock->expects( $this->once() )
437 ->method( 'saveSettings' );
439 $request = $this->getSampleRequest( array(
440 'change' => 'userjs-option=1'
441 ) );
443 $response = $this->executeQuery( $request );
445 $this->assertEquals( self::$Success, $response );