Merge "Update wikimedia/normalized-exception to 2.1.1"
[mediawiki.git] / tests / phpunit / includes / api / ApiOptionsTest.php
blob3ca600c0d5add74bddae05f8b39e9207845d4c29
1 <?php
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;
21 /**
22 * @group API
23 * @group Database
24 * @group medium
26 * @covers \MediaWiki\Api\ApiOptions
28 class ApiOptionsTest extends ApiTestCase {
29 use MockAuthorityTrait;
31 /** @var MockObject */
32 private $mUserMock;
33 /** @var MockObject */
34 private $userOptionsManagerMock;
35 /** @var ApiOptions */
36 private $mTested;
37 /** @var array */
38 private $mSession;
39 /** @var DerivativeContext */
40 private $mContext;
42 private const SUCCESS = [ 'options' => 'success' ];
44 protected function setUp(): void {
45 parent::setUp();
47 $this->mUserMock = $this->createMock( User::class );
49 // No actual DB data
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 );
63 // Empty session
64 $this->mSession = [];
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(
81 'registered',
82 'registered-multiselect',
83 'registered-checkmatrix',
84 'userjs',
85 'special',
86 'unused'
89 $preferencesFactory->method( 'getResetKinds' )
90 ->willReturnCallback( [ $this, 'getResetKinds' ] );
91 $preferencesFactory->method( 'getOptionNamesForReset' )
92 ->willReturn( [] );
94 $this->mTested = new ApiOptions( $main, 'options', $this->userOptionsManagerMock, $preferencesFactory );
96 $this->mergeMwGlobalArrayValue( 'wgDefaultUserOptions', [
97 'testradio' => 'option1',
98 ] );
101 public function getPreferencesFormDescription() {
102 $preferences = [];
104 foreach ( [ 'name', 'willBeNull', 'willBeEmpty', 'willBeHappy' ] as $k ) {
105 $preferences[$k] = [
106 'type' => 'text',
107 'section' => 'test',
108 'label' => "\u{00A0}",
112 $preferences['testmultiselect'] = [
113 'type' => 'multiselect',
114 'options' => [
115 'Test' => [
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',
122 'section' => 'test',
123 'label' => "\u{00A0}",
124 'prefix' => 'testmultiselect-',
125 'default' => [],
128 $preferences['testradio'] = [
129 'type' => 'radio',
130 'options' => [ 'Option 1' => 'option1', 'Option 2' => 'option2' ],
131 'section' => 'test',
134 return $preferences;
138 * @param mixed $unused
139 * @param IContextSource $context
140 * @param array|null $options
142 * @return array
144 public function getResetKinds( $unused, IContextSource $context, $options = null ) {
145 // Match with above.
146 $kinds = [
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 ) {
160 return $kinds;
163 $mapping = [];
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';
169 } else {
170 $mapping[$key] = 'unused';
174 return $mapping;
177 private function getSampleRequest( $custom = [] ) {
178 $request = [
179 'token' => '123ABC',
180 'change' => null,
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() {
205 $this->mUserMock
206 ->method( 'isRegistered' )
207 ->willReturn( false );
209 try {
210 $request = $this->getSampleRequest();
212 $this->executeQuery( $request );
213 } catch ( ApiUsageException $e ) {
214 $this->assertApiErrorCode( 'notloggedin', $e );
215 return;
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 );
224 try {
225 $request = $this->getSampleRequest( [ 'optionvalue' => '1' ] );
227 $this->executeQuery( $request );
228 } catch ( ApiUsageException $e ) {
229 $this->assertApiErrorCode( 'nooptionname', $e );
230 return;
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' );
247 try {
248 $request = $this->getSampleRequest();
250 $this->executeQuery( $request );
251 } catch ( ApiUsageException $e ) {
252 $this->assertApiErrorCode( 'nochanges', $e );
253 return;
255 $this->fail( "ApiUsageException was not thrown" );
258 public function userScenarios() {
259 return [
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' );
276 } else {
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' => '' ] );
282 try {
283 $response = $this->executeQuery( $request );
284 if ( $expectException ) {
285 $this->fail( 'Expected a "notloggedin" error.' );
286 } else {
287 $this->assertEquals( self::SUCCESS, $response );
289 } catch ( ApiUsageException $e ) {
290 if ( !$expectException ) {
291 $this->fail( 'Unexpected "notloggedin" error.' );
292 } else {
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' );
308 } else {
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' ] );
314 try {
315 $response = $this->executeQuery( $request );
316 if ( $expectException ) {
317 $this->fail( "Expected an ApiUsageException" );
318 } else {
319 $this->assertEquals( self::SUCCESS, $response );
321 } catch ( ApiUsageException $e ) {
322 if ( !$expectException ) {
323 throw $e;
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' );
341 } else {
342 $this->userOptionsManagerMock->expects( $this->once() )->method( 'resetOptionsByName' );
343 $expectedOptions = [
344 'willBeHappy' => 'Happy',
345 'name' => 'value',
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] );
354 } );
355 $this->mUserMock->expects( $this->once() )->method( 'saveSettings' );
358 $args = [
359 'reset' => '',
360 'change' => 'willBeHappy=Happy',
361 'optionname' => 'name',
362 'optionvalue' => 'value'
365 try {
366 $response = $this->executeQuery( $this->getSampleRequest( $args ) );
368 if ( $expectException ) {
369 $this->fail( "Expected an ApiUsageException" );
370 } else {
371 $this->assertEquals( self::SUCCESS, $response );
373 } catch ( ApiUsageException $e ) {
374 if ( !$expectException ) {
375 throw $e;
377 $this->assertNotNull( $e->getMessageObject() );
378 $this->assertApiErrorCode( 'notloggedin', $e );
383 * @dataProvider provideOptionManupulation
385 public function testOptionManupulation( array $params, array $setOptions, ?array $result = null,
386 $message = ''
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] );
404 } );
406 if ( $setOptions ) {
407 $this->mUserMock->expects( $this->once() )
408 ->method( 'saveSettings' );
409 } else {
410 $this->mUserMock->expects( $this->never() )
411 ->method( 'saveSettings' );
414 $request = $this->getSampleRequest( $params );
415 $response = $this->executeQuery( $request );
417 if ( !$result ) {
418 $result = self::SUCCESS;
420 $this->assertEquals( $result, $response, $message );
423 public static function provideOptionManupulation() {
424 return [
426 [ 'change' => 'userjs-option=1' ],
427 [ [ 'userjs-option', '1' ] ],
428 null,
429 'Setting userjs options',
432 [ 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy' ],
434 [ 'willBeNull', null ],
435 [ 'willBeEmpty', '' ],
436 [ 'willBeHappy', 'Happy' ],
438 null,
439 'Basic option setting',
442 [ 'change' => 'testradio=option2' ],
443 [ [ 'testradio', 'option2' ] ],
444 null,
445 'Changing radio options',
448 [ 'change' => 'testradio' ],
449 [ [ 'testradio', null ] ],
450 null,
451 'Resetting radio options',
454 [ 'change' => 'unknownOption=1' ],
457 'options' => 'success',
458 'warnings' => [
459 'options' => [
460 'warnings' => "Validation error for \"unknownOption\": not a valid preference."
464 'Unrecognized options should be rejected',
467 [ 'change' => 'special=1' ],
470 'options' => 'success',
471 'warnings' => [
472 'options' => [
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 ],
490 null,
491 'Setting multiselect options',
494 [ 'optionname' => 'name', 'optionvalue' => 'value' ],
495 [ [ 'name', 'value' ] ],
496 null,
497 'Setting options via optionname/optionvalue'
500 [ 'optionname' => 'name' ],
501 [ [ 'name', null ] ],
502 null,
503 'Resetting options via optionname without optionvalue',
506 [ 'optionname' => 'name', 'optionvalue' => str_repeat( '测试', 16383 ) ],
509 'options' => 'success',
510 'warnings' => [
511 'options' => [
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',