10 class ApiOptionsTest
extends MediaWikiLangTestCase
{
12 /** @var PHPUnit_Framework_MockObject_MockObject */
14 /** @var ApiOptions */
17 /** @var DerivativeContext */
20 private static $Success = [ 'options' => 'success' ];
22 protected function setUp() {
25 $this->mUserMock
= $this->getMockBuilder( User
::class )
26 ->disableOriginalConstructor()
29 // Set up groups and rights
30 $this->mUserMock
->expects( $this->any() )
31 ->method( 'getEffectiveGroups' )->will( $this->returnValue( [ '*', 'user' ] ) );
33 // Set up callback for User::getOptionKinds
34 $this->mUserMock
->expects( $this->any() )
35 ->method( 'getOptionKinds' )->will( $this->returnCallback( [ $this, 'getOptionKinds' ] ) );
38 $this->mUserMock
->expects( $this->any() )
39 ->method( 'getInstanceForUpdate' )->will( $this->returnValue( $this->mUserMock
) );
41 // Needs to return something
42 $this->mUserMock
->method( 'getOptions' )
45 // Create a new context
46 $this->mContext
= new DerivativeContext( new RequestContext() );
47 $this->mContext
->getContext()->setTitle( Title
::newFromText( 'Test' ) );
48 $this->mContext
->setUser( $this->mUserMock
);
50 $this->overrideUserPermissions( $this->mUserMock
, [ 'editmyoptions' ] );
51 $main = new ApiMain( $this->mContext
);
56 $this->mTested
= new ApiOptions( $main, 'options' );
58 $this->mergeMwGlobalArrayValue( 'wgHooks', [
60 [ $this, 'hookGetPreferences' ]
63 $this->mergeMwGlobalArrayValue( 'wgDefaultUserOptions', [
64 'testradio' => 'option1',
66 // Workaround for static caching in User::getDefaultOptions()
67 $this->setContentLang( Language
::factory( 'qqq' ) );
70 public function hookGetPreferences( $user, &$preferences ) {
73 foreach ( [ 'name', 'willBeNull', 'willBeEmpty', 'willBeHappy' ] as $k ) {
77 'label' => "\u{00A0}",
81 $preferences['testmultiselect'] = [
82 'type' => 'multiselect',
85 '<span dir="auto">Some HTML here for option 1</span>' => 'opt1',
86 '<span dir="auto">Some HTML here for option 2</span>' => 'opt2',
87 '<span dir="auto">Some HTML here for option 3</span>' => 'opt3',
88 '<span dir="auto">Some HTML here for option 4</span>' => 'opt4',
92 'label' => "\u{00A0}",
93 'prefix' => 'testmultiselect-',
97 $preferences['testradio'] = [
99 'options' => [ 'Option 1' => 'option1', 'Option 2' => 'option2' ],
105 * @param IContextSource $context
106 * @param array|null $options
110 public function getOptionKinds( IContextSource
$context, $options = null ) {
113 'name' => 'registered',
114 'willBeNull' => 'registered',
115 'willBeEmpty' => 'registered',
116 'willBeHappy' => 'registered',
117 'testradio' => 'registered',
118 'testmultiselect-opt1' => 'registered-multiselect',
119 'testmultiselect-opt2' => 'registered-multiselect',
120 'testmultiselect-opt3' => 'registered-multiselect',
121 'testmultiselect-opt4' => 'registered-multiselect',
122 'special' => 'special',
125 if ( $options === null ) {
130 foreach ( $options as $key => $value ) {
131 if ( isset( $kinds[$key] ) ) {
132 $mapping[$key] = $kinds[$key];
133 } elseif ( substr( $key, 0, 7 ) === 'userjs-' ) {
134 $mapping[$key] = 'userjs';
136 $mapping[$key] = 'unused';
143 private function getSampleRequest( $custom = [] ) {
147 'optionname' => null,
148 'optionvalue' => null,
151 return array_merge( $request, $custom );
154 private function executeQuery( $request ) {
155 $this->mContext
->setRequest( new FauxRequest( $request, true, $this->mSession
) );
156 $this->mTested
->execute();
158 return $this->mTested
->getResult()->getResultData( null, [ 'Strip' => 'all' ] );
162 * @expectedException ApiUsageException
164 public function testNoToken() {
165 $request = $this->getSampleRequest( [ 'token' => null ] );
167 $this->executeQuery( $request );
170 public function testAnon() {
171 $this->mUserMock
->expects( $this->once() )
173 ->will( $this->returnValue( true ) );
176 $request = $this->getSampleRequest();
178 $this->executeQuery( $request );
179 } catch ( ApiUsageException
$e ) {
180 $this->assertTrue( ApiTestCase
::apiExceptionHasCode( $e, 'notloggedin' ) );
183 $this->fail( "ApiUsageException was not thrown" );
186 public function testNoOptionname() {
188 $request = $this->getSampleRequest( [ 'optionvalue' => '1' ] );
190 $this->executeQuery( $request );
191 } catch ( ApiUsageException
$e ) {
192 $this->assertTrue( ApiTestCase
::apiExceptionHasCode( $e, 'nooptionname' ) );
195 $this->fail( "ApiUsageException was not thrown" );
198 public function testNoChanges() {
199 $this->mUserMock
->expects( $this->never() )
200 ->method( 'resetOptions' );
202 $this->mUserMock
->expects( $this->never() )
203 ->method( 'setOption' );
205 $this->mUserMock
->expects( $this->never() )
206 ->method( 'saveSettings' );
209 $request = $this->getSampleRequest();
211 $this->executeQuery( $request );
212 } catch ( ApiUsageException
$e ) {
213 $this->assertTrue( ApiTestCase
::apiExceptionHasCode( $e, 'nochanges' ) );
216 $this->fail( "ApiUsageException was not thrown" );
219 public function testReset() {
220 $this->mUserMock
->expects( $this->once() )
221 ->method( 'resetOptions' )
222 ->with( $this->equalTo( [ 'all' ] ) );
224 $this->mUserMock
->expects( $this->never() )
225 ->method( 'setOption' );
227 $this->mUserMock
->expects( $this->once() )
228 ->method( 'saveSettings' );
230 $request = $this->getSampleRequest( [ 'reset' => '' ] );
232 $response = $this->executeQuery( $request );
234 $this->assertEquals( self
::$Success, $response );
237 public function testResetKinds() {
238 $this->mUserMock
->expects( $this->once() )
239 ->method( 'resetOptions' )
240 ->with( $this->equalTo( [ 'registered' ] ) );
242 $this->mUserMock
->expects( $this->never() )
243 ->method( 'setOption' );
245 $this->mUserMock
->expects( $this->once() )
246 ->method( 'saveSettings' );
248 $request = $this->getSampleRequest( [ 'reset' => '', 'resetkinds' => 'registered' ] );
250 $response = $this->executeQuery( $request );
252 $this->assertEquals( self
::$Success, $response );
255 public function testResetChangeOption() {
256 $this->mUserMock
->expects( $this->once() )
257 ->method( 'resetOptions' );
259 $this->mUserMock
->expects( $this->exactly( 2 ) )
260 ->method( 'setOption' )
262 [ $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) ],
263 [ $this->equalTo( 'name' ), $this->equalTo( 'value' ) ]
266 $this->mUserMock
->expects( $this->once() )
267 ->method( 'saveSettings' );
271 'change' => 'willBeHappy=Happy',
272 'optionname' => 'name',
273 'optionvalue' => 'value'
276 $response = $this->executeQuery( $this->getSampleRequest( $args ) );
278 $this->assertEquals( self
::$Success, $response );
282 * @dataProvider provideOptionManupulation
283 * @param array $params
284 * @param array $setOptions
285 * @param array|null $result
287 public function testOptionManupulation( array $params, array $setOptions, array $result = null,
290 $this->mUserMock
->expects( $this->never() )
291 ->method( 'resetOptions' );
293 $this->mUserMock
->expects( $this->exactly( count( $setOptions ) ) )
294 ->method( 'setOption' )
295 ->withConsecutive( ...$setOptions );
298 $this->mUserMock
->expects( $this->once() )
299 ->method( 'saveSettings' );
301 $this->mUserMock
->expects( $this->never() )
302 ->method( 'saveSettings' );
305 $request = $this->getSampleRequest( $params );
306 $response = $this->executeQuery( $request );
309 $result = self
::$Success;
311 $this->assertEquals( $result, $response, $message );
314 public function provideOptionManupulation() {
317 [ 'change' => 'userjs-option=1' ],
318 [ [ 'userjs-option', '1' ] ],
320 'Setting userjs options',
323 [ 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy' ],
325 [ 'willBeNull', null ],
326 [ 'willBeEmpty', '' ],
327 [ 'willBeHappy', 'Happy' ],
330 'Basic option setting',
333 [ 'change' => 'testradio=option2' ],
334 [ [ 'testradio', 'option2' ] ],
336 'Changing radio options',
339 [ 'change' => 'testradio' ],
340 [ [ 'testradio', null ] ],
342 'Resetting radio options',
345 [ 'change' => 'unknownOption=1' ],
348 'options' => 'success',
351 'warnings' => "Validation error for \"unknownOption\": not a valid preference."
355 'Unrecognized options should be rejected',
358 [ 'change' => 'special=1' ],
361 'options' => 'success',
364 'warnings' => "Validation error for \"special\": cannot be set by this module."
368 'Refuse setting special options',
372 'change' => 'testmultiselect-opt1=1|testmultiselect-opt2|'
373 . 'testmultiselect-opt3=|testmultiselect-opt4=0'
376 [ 'testmultiselect-opt1', true ],
377 [ 'testmultiselect-opt2', null ],
378 [ 'testmultiselect-opt3', false ],
379 [ 'testmultiselect-opt4', false ],
382 'Setting multiselect options',
385 [ 'optionname' => 'name', 'optionvalue' => 'value' ],
386 [ [ 'name', 'value' ] ],
388 'Setting options via optionname/optionvalue'
391 [ 'optionname' => 'name' ],
392 [ [ 'name', null ] ],
394 'Resetting options via optionname without optionvalue',