Move ResultWrapper subclasses to Rdbms
[mediawiki.git] / tests / phpunit / includes / api / ApiOptionsTest.php
blobef70626120fe3e1793f46cc9b60b742681f69b62
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 = [ '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( [ '*', 'user' ] ) );
32 $this->mUserMock->expects( $this->any() )
33 ->method( 'isAllowedAny' )->will( $this->returnValue( true ) );
35 // Set up callback for User::getOptionKinds
36 $this->mUserMock->expects( $this->any() )
37 ->method( 'getOptionKinds' )->will( $this->returnCallback( [ $this, 'getOptionKinds' ] ) );
39 // No actual DB data
40 $this->mUserMock->expects( $this->any() )
41 ->method( 'getInstanceForUpdate' )->will( $this->returnValue( $this->mUserMock ) );
43 // Create a new context
44 $this->mContext = new DerivativeContext( new RequestContext() );
45 $this->mContext->getContext()->setTitle( Title::newFromText( 'Test' ) );
46 $this->mContext->setUser( $this->mUserMock );
48 $main = new ApiMain( $this->mContext );
50 // Empty session
51 $this->mSession = [];
53 $this->mTested = new ApiOptions( $main, 'options' );
55 $this->mergeMwGlobalArrayValue( 'wgHooks', [
56 'GetPreferences' => [
57 [ $this, 'hookGetPreferences' ]
59 ] );
62 public function hookGetPreferences( $user, &$preferences ) {
63 $preferences = [];
65 foreach ( [ 'name', 'willBeNull', 'willBeEmpty', 'willBeHappy' ] as $k ) {
66 $preferences[$k] = [
67 'type' => 'text',
68 'section' => 'test',
69 'label' => '&#160;',
73 $preferences['testmultiselect'] = [
74 'type' => 'multiselect',
75 'options' => [
76 'Test' => [
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' => [],
89 return true;
92 /**
93 * @param IContextSource $context
94 * @param array|null $options
96 * @return array
98 public function getOptionKinds( IContextSource $context, $options = null ) {
99 // Match with above.
100 $kinds = [
101 'name' => 'registered',
102 'willBeNull' => 'registered',
103 'willBeEmpty' => 'registered',
104 'willBeHappy' => 'registered',
105 'testmultiselect-opt1' => 'registered-multiselect',
106 'testmultiselect-opt2' => 'registered-multiselect',
107 'testmultiselect-opt3' => 'registered-multiselect',
108 'testmultiselect-opt4' => 'registered-multiselect',
109 'special' => 'special',
112 if ( $options === null ) {
113 return $kinds;
116 $mapping = [];
117 foreach ( $options as $key => $value ) {
118 if ( isset( $kinds[$key] ) ) {
119 $mapping[$key] = $kinds[$key];
120 } elseif ( substr( $key, 0, 7 ) === 'userjs-' ) {
121 $mapping[$key] = 'userjs';
122 } else {
123 $mapping[$key] = 'unused';
127 return $mapping;
130 private function getSampleRequest( $custom = [] ) {
131 $request = [
132 'token' => '123ABC',
133 'change' => null,
134 'optionname' => null,
135 'optionvalue' => null,
138 return array_merge( $request, $custom );
141 private function executeQuery( $request ) {
142 $this->mContext->setRequest( new FauxRequest( $request, true, $this->mSession ) );
143 $this->mTested->execute();
145 return $this->mTested->getResult()->getResultData( null, [ 'Strip' => 'all' ] );
149 * @expectedException ApiUsageException
151 public function testNoToken() {
152 $request = $this->getSampleRequest( [ 'token' => null ] );
154 $this->executeQuery( $request );
157 public function testAnon() {
158 $this->mUserMock->expects( $this->once() )
159 ->method( 'isAnon' )
160 ->will( $this->returnValue( true ) );
162 try {
163 $request = $this->getSampleRequest();
165 $this->executeQuery( $request );
166 } catch ( ApiUsageException $e ) {
167 $this->assertTrue( ApiTestCase::apiExceptionHasCode( $e, 'notloggedin' ) );
168 return;
170 $this->fail( "ApiUsageException was not thrown" );
173 public function testNoOptionname() {
174 try {
175 $request = $this->getSampleRequest( [ 'optionvalue' => '1' ] );
177 $this->executeQuery( $request );
178 } catch ( ApiUsageException $e ) {
179 $this->assertTrue( ApiTestCase::apiExceptionHasCode( $e, 'nooptionname' ) );
180 return;
182 $this->fail( "ApiUsageException 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 ( ApiUsageException $e ) {
200 $this->assertTrue( ApiTestCase::apiExceptionHasCode( $e, 'nochanges' ) );
201 return;
203 $this->fail( "ApiUsageException was not thrown" );
206 public function testReset() {
207 $this->mUserMock->expects( $this->once() )
208 ->method( 'resetOptions' )
209 ->with( $this->equalTo( [ 'all' ] ) );
211 $this->mUserMock->expects( $this->never() )
212 ->method( 'setOption' );
214 $this->mUserMock->expects( $this->once() )
215 ->method( 'saveSettings' );
217 $request = $this->getSampleRequest( [ 'reset' => '' ] );
219 $response = $this->executeQuery( $request );
221 $this->assertEquals( self::$Success, $response );
224 public function testResetKinds() {
225 $this->mUserMock->expects( $this->once() )
226 ->method( 'resetOptions' )
227 ->with( $this->equalTo( [ 'registered' ] ) );
229 $this->mUserMock->expects( $this->never() )
230 ->method( 'setOption' );
232 $this->mUserMock->expects( $this->once() )
233 ->method( 'saveSettings' );
235 $request = $this->getSampleRequest( [ 'reset' => '', 'resetkinds' => 'registered' ] );
237 $response = $this->executeQuery( $request );
239 $this->assertEquals( self::$Success, $response );
242 public function testOptionWithValue() {
243 $this->mUserMock->expects( $this->never() )
244 ->method( 'resetOptions' );
246 $this->mUserMock->expects( $this->once() )
247 ->method( 'setOption' )
248 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
250 $this->mUserMock->expects( $this->once() )
251 ->method( 'saveSettings' );
253 $request = $this->getSampleRequest( [ 'optionname' => 'name', 'optionvalue' => 'value' ] );
255 $response = $this->executeQuery( $request );
257 $this->assertEquals( self::$Success, $response );
260 public function testOptionResetValue() {
261 $this->mUserMock->expects( $this->never() )
262 ->method( 'resetOptions' );
264 $this->mUserMock->expects( $this->once() )
265 ->method( 'setOption' )
266 ->with( $this->equalTo( 'name' ), $this->identicalTo( null ) );
268 $this->mUserMock->expects( $this->once() )
269 ->method( 'saveSettings' );
271 $request = $this->getSampleRequest( [ 'optionname' => 'name' ] );
272 $response = $this->executeQuery( $request );
274 $this->assertEquals( self::$Success, $response );
277 public function testChange() {
278 $this->mUserMock->expects( $this->never() )
279 ->method( 'resetOptions' );
281 $this->mUserMock->expects( $this->at( 2 ) )
282 ->method( 'getOptions' );
284 $this->mUserMock->expects( $this->at( 5 ) )
285 ->method( 'setOption' )
286 ->with( $this->equalTo( 'willBeNull' ), $this->identicalTo( null ) );
288 $this->mUserMock->expects( $this->at( 6 ) )
289 ->method( 'getOptions' );
291 $this->mUserMock->expects( $this->at( 7 ) )
292 ->method( 'setOption' )
293 ->with( $this->equalTo( 'willBeEmpty' ), $this->equalTo( '' ) );
295 $this->mUserMock->expects( $this->at( 8 ) )
296 ->method( 'getOptions' );
298 $this->mUserMock->expects( $this->at( 9 ) )
299 ->method( 'setOption' )
300 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
302 $this->mUserMock->expects( $this->once() )
303 ->method( 'saveSettings' );
305 $request = $this->getSampleRequest( [
306 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy'
307 ] );
309 $response = $this->executeQuery( $request );
311 $this->assertEquals( self::$Success, $response );
314 public function testResetChangeOption() {
315 $this->mUserMock->expects( $this->once() )
316 ->method( 'resetOptions' );
318 $this->mUserMock->expects( $this->at( 5 ) )
319 ->method( 'getOptions' );
321 $this->mUserMock->expects( $this->at( 6 ) )
322 ->method( 'setOption' )
323 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
325 $this->mUserMock->expects( $this->at( 7 ) )
326 ->method( 'getOptions' );
328 $this->mUserMock->expects( $this->at( 8 ) )
329 ->method( 'setOption' )
330 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
332 $this->mUserMock->expects( $this->once() )
333 ->method( 'saveSettings' );
335 $args = [
336 'reset' => '',
337 'change' => 'willBeHappy=Happy',
338 'optionname' => 'name',
339 'optionvalue' => 'value'
342 $response = $this->executeQuery( $this->getSampleRequest( $args ) );
344 $this->assertEquals( self::$Success, $response );
347 public function testMultiSelect() {
348 $this->mUserMock->expects( $this->never() )
349 ->method( 'resetOptions' );
351 $this->mUserMock->expects( $this->at( 4 ) )
352 ->method( 'setOption' )
353 ->with( $this->equalTo( 'testmultiselect-opt1' ), $this->identicalTo( true ) );
355 $this->mUserMock->expects( $this->at( 5 ) )
356 ->method( 'setOption' )
357 ->with( $this->equalTo( 'testmultiselect-opt2' ), $this->identicalTo( null ) );
359 $this->mUserMock->expects( $this->at( 6 ) )
360 ->method( 'setOption' )
361 ->with( $this->equalTo( 'testmultiselect-opt3' ), $this->identicalTo( false ) );
363 $this->mUserMock->expects( $this->at( 7 ) )
364 ->method( 'setOption' )
365 ->with( $this->equalTo( 'testmultiselect-opt4' ), $this->identicalTo( false ) );
367 $this->mUserMock->expects( $this->once() )
368 ->method( 'saveSettings' );
370 $request = $this->getSampleRequest( [
371 'change' => 'testmultiselect-opt1=1|testmultiselect-opt2|'
372 . 'testmultiselect-opt3=|testmultiselect-opt4=0'
373 ] );
375 $response = $this->executeQuery( $request );
377 $this->assertEquals( self::$Success, $response );
380 public function testSpecialOption() {
381 $this->mUserMock->expects( $this->never() )
382 ->method( 'resetOptions' );
384 $this->mUserMock->expects( $this->never() )
385 ->method( 'saveSettings' );
387 $request = $this->getSampleRequest( [
388 'change' => 'special=1'
389 ] );
391 $response = $this->executeQuery( $request );
393 $this->assertEquals( [
394 'options' => 'success',
395 'warnings' => [
396 'options' => [
397 'warnings' => "Validation error for \"special\": cannot be set by this module."
400 ], $response );
403 public function testUnknownOption() {
404 $this->mUserMock->expects( $this->never() )
405 ->method( 'resetOptions' );
407 $this->mUserMock->expects( $this->never() )
408 ->method( 'saveSettings' );
410 $request = $this->getSampleRequest( [
411 'change' => 'unknownOption=1'
412 ] );
414 $response = $this->executeQuery( $request );
416 $this->assertEquals( [
417 'options' => 'success',
418 'warnings' => [
419 'options' => [
420 'warnings' => "Validation error for \"unknownOption\": not a valid preference."
423 ], $response );
426 public function testUserjsOption() {
427 $this->mUserMock->expects( $this->never() )
428 ->method( 'resetOptions' );
430 $this->mUserMock->expects( $this->once() )
431 ->method( 'setOption' )
432 ->with( $this->equalTo( 'userjs-option' ), $this->equalTo( '1' ) );
434 $this->mUserMock->expects( $this->once() )
435 ->method( 'saveSettings' );
437 $request = $this->getSampleRequest( [
438 'change' => 'userjs-option=1'
439 ] );
441 $response = $this->executeQuery( $request );
443 $this->assertEquals( self::$Success, $response );