3 namespace MediaWiki\Tests\Api
;
5 namespace MediaWiki\Tests\Api
;
7 use MediaWiki\Api\ApiMain
;
8 use MediaWiki\Api\ApiOptions
;
9 use MediaWiki\Api\ApiUsageException
;
10 use MediaWiki\Context\DerivativeContext
;
11 use MediaWiki\Context\IContextSource
;
12 use MediaWiki\Context\RequestContext
;
13 use MediaWiki\Preferences\DefaultPreferencesFactory
;
14 use MediaWiki\Request\FauxRequest
;
15 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait
;
16 use MediaWiki\Title\Title
;
17 use MediaWiki\User\Options\UserOptionsManager
;
18 use MediaWiki\User\User
;
19 use PHPUnit\Framework\MockObject\MockObject
;
26 * @covers \MediaWiki\Api\ApiOptions
28 class ApiOptionsTest
extends ApiTestCase
{
29 use MockAuthorityTrait
;
31 /** @var MockObject */
33 /** @var MockObject */
34 private $userOptionsManagerMock;
35 /** @var ApiOptions */
39 /** @var DerivativeContext */
42 private const SUCCESS
= [ 'options' => 'success' ];
44 protected function setUp(): void
{
47 $this->mUserMock
= $this->createMock( User
::class );
50 $this->mUserMock
->method( 'getInstanceForUpdate' )->willReturn( $this->mUserMock
);
52 $this->mUserMock
->method( 'isAllowedAny' )->willReturn( true );
54 // Create a new context
55 $this->mContext
= new DerivativeContext( new RequestContext() );
56 $this->mContext
->getContext()->setTitle( Title
::makeTitle( NS_MAIN
, 'Test' ) );
57 $this->mContext
->setAuthority(
58 $this->mockUserAuthorityWithPermissions( $this->mUserMock
, [ 'editmyoptions' ] )
61 $main = new ApiMain( $this->mContext
);
66 $this->userOptionsManagerMock
= $this->createNoOpMock(
67 UserOptionsManager
::class,
68 [ 'getOptions', 'resetOptionsByName', 'setOption', 'isOptionGlobal' ]
70 // Needs to return something
71 $this->userOptionsManagerMock
->method( 'getOptions' )->willReturn( [] );
73 $preferencesFactory = $this->createNoOpMock(
74 DefaultPreferencesFactory
::class,
75 [ 'getFormDescriptor', 'listResetKinds', 'getResetKinds', 'getOptionNamesForReset' ]
77 $preferencesFactory->method( 'getFormDescriptor' )
78 ->willReturnCallback( [ $this, 'getPreferencesFormDescription' ] );
79 $preferencesFactory->method( 'listResetKinds' )->willReturn(
82 'registered-multiselect',
83 'registered-checkmatrix',
89 $preferencesFactory->method( 'getResetKinds' )
90 ->willReturnCallback( [ $this, 'getResetKinds' ] );
91 $preferencesFactory->method( 'getOptionNamesForReset' )
94 $this->mTested
= new ApiOptions( $main, 'options', $this->userOptionsManagerMock
, $preferencesFactory );
96 $this->mergeMwGlobalArrayValue( 'wgDefaultUserOptions', [
97 'testradio' => 'option1',
101 public function getPreferencesFormDescription() {
104 foreach ( [ 'name', 'willBeNull', 'willBeEmpty', 'willBeHappy' ] as $k ) {
108 'label' => "\u{00A0}",
112 $preferences['testmultiselect'] = [
113 'type' => 'multiselect',
116 '<span dir="auto">Some HTML here for option 1</span>' => 'opt1',
117 '<span dir="auto">Some HTML here for option 2</span>' => 'opt2',
118 '<span dir="auto">Some HTML here for option 3</span>' => 'opt3',
119 '<span dir="auto">Some HTML here for option 4</span>' => 'opt4',
123 'label' => "\u{00A0}",
124 'prefix' => 'testmultiselect-',
128 $preferences['testradio'] = [
130 'options' => [ 'Option 1' => 'option1', 'Option 2' => 'option2' ],
138 * @param mixed $unused
139 * @param IContextSource $context
140 * @param array|null $options
144 public function getResetKinds( $unused, IContextSource
$context, $options = null ) {
147 'name' => 'registered',
148 'willBeNull' => 'registered',
149 'willBeEmpty' => 'registered',
150 'willBeHappy' => 'registered',
151 'testradio' => 'registered',
152 'testmultiselect-opt1' => 'registered-multiselect',
153 'testmultiselect-opt2' => 'registered-multiselect',
154 'testmultiselect-opt3' => 'registered-multiselect',
155 'testmultiselect-opt4' => 'registered-multiselect',
156 'special' => 'special',
159 if ( $options === null ) {
164 foreach ( $options as $key => $value ) {
165 if ( isset( $kinds[$key] ) ) {
166 $mapping[$key] = $kinds[$key];
167 } elseif ( str_starts_with( $key, 'userjs-' ) ) {
168 $mapping[$key] = 'userjs';
170 $mapping[$key] = 'unused';
177 private function getSampleRequest( $custom = [] ) {
181 'optionname' => null,
182 'optionvalue' => null,
185 return array_merge( $request, $custom );
188 private function executeQuery( $request ) {
189 $this->mContext
->setRequest( new FauxRequest( $request, true, $this->mSession
) );
190 $this->mUserMock
->method( 'getRequest' )->willReturn( $this->mContext
->getRequest() );
192 $this->mTested
->execute();
194 return $this->mTested
->getResult()->getResultData( null, [ 'Strip' => 'all' ] );
197 public function testNoToken() {
198 $request = $this->getSampleRequest( [ 'token' => null ] );
200 $this->expectException( ApiUsageException
::class );
201 $this->executeQuery( $request );
204 public function testAnon() {
206 ->method( 'isRegistered' )
207 ->willReturn( false );
210 $request = $this->getSampleRequest();
212 $this->executeQuery( $request );
213 } catch ( ApiUsageException
$e ) {
214 $this->assertApiErrorCode( 'notloggedin', $e );
217 $this->fail( "ApiUsageException was not thrown" );
220 public function testNoOptionname() {
221 $this->mUserMock
->method( 'isRegistered' )->willReturn( true );
222 $this->mUserMock
->method( 'isNamed' )->willReturn( true );
225 $request = $this->getSampleRequest( [ 'optionvalue' => '1' ] );
227 $this->executeQuery( $request );
228 } catch ( ApiUsageException
$e ) {
229 $this->assertApiErrorCode( 'nooptionname', $e );
232 $this->fail( "ApiUsageException was not thrown" );
235 public function testNoChanges() {
236 $this->mUserMock
->method( 'isRegistered' )->willReturn( true );
237 $this->mUserMock
->method( 'isNamed' )->willReturn( true );
238 $this->userOptionsManagerMock
->expects( $this->never() )
239 ->method( 'resetOptionsByName' );
241 $this->userOptionsManagerMock
->expects( $this->never() )
242 ->method( 'setOption' );
244 $this->mUserMock
->expects( $this->never() )
245 ->method( 'saveSettings' );
248 $request = $this->getSampleRequest();
250 $this->executeQuery( $request );
251 } catch ( ApiUsageException
$e ) {
252 $this->assertApiErrorCode( 'nochanges', $e );
255 $this->fail( "ApiUsageException was not thrown" );
258 public function userScenarios() {
260 [ true, true, false ],
261 [ true, false, true ],
266 * @dataProvider userScenarios
268 public function testReset( $isRegistered, $isNamed, $expectException ) {
269 $this->mUserMock
->method( 'isRegistered' )->willReturn( $isRegistered );
270 $this->mUserMock
->method( 'isNamed' )->willReturn( $isNamed );
272 if ( $expectException ) {
273 $this->userOptionsManagerMock
->expects( $this->never() )->method( 'resetOptionsByName' );
274 $this->userOptionsManagerMock
->expects( $this->never() )->method( 'setOption' );
275 $this->mUserMock
->expects( $this->never() )->method( 'saveSettings' );
277 $this->userOptionsManagerMock
->expects( $this->once() )->method( 'resetOptionsByName' );
278 $this->userOptionsManagerMock
->expects( $this->never() )->method( 'setOption' );
279 $this->mUserMock
->expects( $this->once() )->method( 'saveSettings' );
281 $request = $this->getSampleRequest( [ 'reset' => '' ] );
283 $response = $this->executeQuery( $request );
284 if ( $expectException ) {
285 $this->fail( 'Expected a "notloggedin" error.' );
287 $this->assertEquals( self
::SUCCESS
, $response );
289 } catch ( ApiUsageException
$e ) {
290 if ( !$expectException ) {
291 $this->fail( 'Unexpected "notloggedin" error.' );
293 $this->assertApiErrorCode( 'notloggedin', $e );
299 * @dataProvider userScenarios
301 public function testResetKinds( $isRegistered, $isNamed, $expectException ) {
302 $this->mUserMock
->method( 'isRegistered' )->willReturn( $isRegistered );
303 $this->mUserMock
->method( 'isNamed' )->willReturn( $isNamed );
304 if ( $expectException ) {
305 $this->mUserMock
->expects( $this->never() )->method( 'saveSettings' );
306 $this->userOptionsManagerMock
->expects( $this->never() )->method( 'resetOptionsByName' );
307 $this->userOptionsManagerMock
->expects( $this->never() )->method( 'setOption' );
309 $this->userOptionsManagerMock
->expects( $this->once() )->method( 'resetOptionsByName' );
310 $this->userOptionsManagerMock
->expects( $this->never() )->method( 'setOption' );
311 $this->mUserMock
->expects( $this->once() )->method( 'saveSettings' );
313 $request = $this->getSampleRequest( [ 'reset' => '', 'resetkinds' => 'registered' ] );
315 $response = $this->executeQuery( $request );
316 if ( $expectException ) {
317 $this->fail( "Expected an ApiUsageException" );
319 $this->assertEquals( self
::SUCCESS
, $response );
321 } catch ( ApiUsageException
$e ) {
322 if ( !$expectException ) {
325 $this->assertNotNull( $e->getMessageObject() );
326 $this->assertApiErrorCode( 'notloggedin', $e );
331 * @dataProvider userScenarios
333 public function testResetChangeOption( $isRegistered, $isNamed, $expectException ) {
334 $this->mUserMock
->method( 'isRegistered' )->willReturn( $isRegistered );
335 $this->mUserMock
->method( 'isNamed' )->willReturn( $isNamed );
337 if ( $expectException ) {
338 $this->userOptionsManagerMock
->expects( $this->never() )->method( 'resetOptionsByName' );
339 $this->userOptionsManagerMock
->expects( $this->never() )->method( 'setOption' );
340 $this->mUserMock
->expects( $this->never() )->method( 'saveSettings' );
342 $this->userOptionsManagerMock
->expects( $this->once() )->method( 'resetOptionsByName' );
344 'willBeHappy' => 'Happy',
347 $this->userOptionsManagerMock
->expects( $this->exactly( count( $expectedOptions ) ) )
348 ->method( 'setOption' )
349 ->willReturnCallback( function ( $user, $oname, $val ) use ( &$expectedOptions ) {
350 $this->assertSame( $this->mUserMock
, $user );
351 $this->assertArrayHasKey( $oname, $expectedOptions );
352 $this->assertSame( $expectedOptions[$oname], $val );
353 unset( $expectedOptions[$oname] );
355 $this->mUserMock
->expects( $this->once() )->method( 'saveSettings' );
360 'change' => 'willBeHappy=Happy',
361 'optionname' => 'name',
362 'optionvalue' => 'value'
366 $response = $this->executeQuery( $this->getSampleRequest( $args ) );
368 if ( $expectException ) {
369 $this->fail( "Expected an ApiUsageException" );
371 $this->assertEquals( self
::SUCCESS
, $response );
373 } catch ( ApiUsageException
$e ) {
374 if ( !$expectException ) {
377 $this->assertNotNull( $e->getMessageObject() );
378 $this->assertApiErrorCode( 'notloggedin', $e );
383 * @dataProvider provideOptionManupulation
385 public function testOptionManupulation( array $params, array $setOptions, ?
array $result = null,
388 $this->mUserMock
->method( 'isRegistered' )->willReturn( true );
389 $this->mUserMock
->method( 'isNamed' )->willReturn( true );
390 $this->userOptionsManagerMock
->expects( $this->never() )
391 ->method( 'resetOptionsByName' );
393 $expectedOptions = [];
394 foreach ( $setOptions as [ $opt, $val ] ) {
395 $expectedOptions[$opt] = $val;
397 $this->userOptionsManagerMock
->expects( $this->exactly( count( $setOptions ) ) )
398 ->method( 'setOption' )
399 ->willReturnCallback( function ( $user, $oname, $val ) use ( &$expectedOptions ) {
400 $this->assertSame( $this->mUserMock
, $user );
401 $this->assertArrayHasKey( $oname, $expectedOptions );
402 $this->assertSame( $expectedOptions[$oname], $val );
403 unset( $expectedOptions[$oname] );
407 $this->mUserMock
->expects( $this->once() )
408 ->method( 'saveSettings' );
410 $this->mUserMock
->expects( $this->never() )
411 ->method( 'saveSettings' );
414 $request = $this->getSampleRequest( $params );
415 $response = $this->executeQuery( $request );
418 $result = self
::SUCCESS
;
420 $this->assertEquals( $result, $response, $message );
423 public static function provideOptionManupulation() {
426 [ 'change' => 'userjs-option=1' ],
427 [ [ 'userjs-option', '1' ] ],
429 'Setting userjs options',
432 [ 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy' ],
434 [ 'willBeNull', null ],
435 [ 'willBeEmpty', '' ],
436 [ 'willBeHappy', 'Happy' ],
439 'Basic option setting',
442 [ 'change' => 'testradio=option2' ],
443 [ [ 'testradio', 'option2' ] ],
445 'Changing radio options',
448 [ 'change' => 'testradio' ],
449 [ [ 'testradio', null ] ],
451 'Resetting radio options',
454 [ 'change' => 'unknownOption=1' ],
457 'options' => 'success',
460 'warnings' => "Validation error for \"unknownOption\": not a valid preference."
464 'Unrecognized options should be rejected',
467 [ 'change' => 'special=1' ],
470 'options' => 'success',
473 'warnings' => "Validation error for \"special\": cannot be set by this module."
477 'Refuse setting special options',
481 'change' => 'testmultiselect-opt1=1|testmultiselect-opt2|'
482 . 'testmultiselect-opt3=|testmultiselect-opt4=0'
485 [ 'testmultiselect-opt1', true ],
486 [ 'testmultiselect-opt2', null ],
487 [ 'testmultiselect-opt3', false ],
488 [ 'testmultiselect-opt4', false ],
491 'Setting multiselect options',
494 [ 'optionname' => 'name', 'optionvalue' => 'value' ],
495 [ [ 'name', 'value' ] ],
497 'Setting options via optionname/optionvalue'
500 [ 'optionname' => 'name' ],
501 [ [ 'name', null ] ],
503 'Resetting options via optionname without optionvalue',
506 [ 'optionname' => 'name', 'optionvalue' => str_repeat( '测试', 16383 ) ],
509 'options' => 'success',
512 'warnings' => 'Validation error for "name": value too long (no more than 65,530 bytes allowed).'
516 'Options with too long value should be rejected',