| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Api; |
| 4 |
|
| 5 |
namespace MediaWiki\Tests\Api; |
| 6 |
|
| 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; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
class ApiOptionsTest extends ApiTestCase { |
| 29 |
use MockAuthorityTrait; |
| 30 |
|
| 31 |
|
| 32 |
private $mUserMock; |
| 33 |
|
| 34 |
private $userOptionsManagerMock; |
| 35 |
|
| 36 |
private $mTested; |
| 37 |
|
| 38 |
private $mSession; |
| 39 |
|
| 40 |
private $mContext; |
| 41 |
|
| 42 |
private const SUCCESS = [ 'options' => 'success' ]; |
| 43 |
|
| 44 |
protected function setUp(): void { |
| 45 |
parent::setUp(); |
| 46 |
|
| 47 |
$this->mUserMock = $this->createMock( User::class ); |
| 48 |
|
| 49 |
|
| 50 |
$this->mUserMock->method( 'getInstanceForUpdate' )->willReturn( $this->mUserMock ); |
| 51 |
|
| 52 |
$this->mUserMock->method( 'isAllowedAny' )->willReturn( true ); |
| 53 |
|
| 54 |
|
| 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' ] ) |
| 59 |
); |
| 60 |
|
| 61 |
$main = new ApiMain( $this->mContext ); |
| 62 |
|
| 63 |
|
| 64 |
$this->mSession = []; |
| 65 |
|
| 66 |
$this->userOptionsManagerMock = $this->createNoOpMock( |
| 67 |
UserOptionsManager::class, |
| 68 |
[ 'getOptions', 'resetOptionsByName', 'setOption', 'isOptionGlobal' ] |
| 69 |
); |
| 70 |
|
| 71 |
$this->userOptionsManagerMock->method( 'getOptions' )->willReturn( [] ); |
| 72 |
|
| 73 |
$preferencesFactory = $this->createNoOpMock( |
| 74 |
DefaultPreferencesFactory::class, |
| 75 |
[ 'getFormDescriptor', 'listResetKinds', 'getResetKinds', 'getOptionNamesForReset' ] |
| 76 |
); |
| 77 |
$preferencesFactory->method( 'getFormDescriptor' ) |
| 78 |
->willReturnCallback( [ $this, 'getPreferencesFormDescription' ] ); |
| 79 |
$preferencesFactory->method( 'listResetKinds' )->willReturn( |
| 80 |
[ |
| 81 |
'registered', |
| 82 |
'registered-multiselect', |
| 83 |
'registered-checkmatrix', |
| 84 |
'userjs', |
| 85 |
'special', |
| 86 |
'unused' |
| 87 |
] |
| 88 |
); |
| 89 |
$preferencesFactory->method( 'getResetKinds' ) |
| 90 |
->willReturnCallback( [ $this, 'getResetKinds' ] ); |
| 91 |
$preferencesFactory->method( 'getOptionNamesForReset' ) |
| 92 |
->willReturn( [] ); |
| 93 |
|
| 94 |
$this->mTested = new ApiOptions( $main, 'options', $this->userOptionsManagerMock, $preferencesFactory ); |
| 95 |
|
| 96 |
$this->mergeMwGlobalArrayValue( 'wgDefaultUserOptions', [ |
| 97 |
'testradio' => 'option1', |
| 98 |
] ); |
| 99 |
} |
| 100 |
|
| 101 |
public function getPreferencesFormDescription() { |
| 102 |
$preferences = []; |
| 103 |
|
| 104 |
foreach ( [ 'name', 'willBeNull', 'willBeEmpty', 'willBeHappy' ] as $k ) { |
| 105 |
$preferences[$k] = [ |
| 106 |
'type' => 'text', |
| 107 |
'section' => 'test', |
| 108 |
'label' => "\u{00A0}", |
| 109 |
]; |
| 110 |
} |
| 111 |
|
| 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', |
| 120 |
], |
| 121 |
], |
| 122 |
'section' => 'test', |
| 123 |
'label' => "\u{00A0}", |
| 124 |
'prefix' => 'testmultiselect-', |
| 125 |
'default' => [], |
| 126 |
]; |
| 127 |
|
| 128 |
$preferences['testradio'] = [ |
| 129 |
'type' => 'radio', |
| 130 |
'options' => [ 'Option 1' => 'option1', 'Option 2' => 'option2' ], |
| 131 |
'section' => 'test', |
| 132 |
]; |
| 133 |
|
| 134 |
return $preferences; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public function getResetKinds( $unused, IContextSource $context, $options = null ) { |
| 145 |
|
| 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', |
| 157 |
]; |
| 158 |
|
| 159 |
if ( $options === null ) { |
| 160 |
return $kinds; |
| 161 |
} |
| 162 |
|
| 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'; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
return $mapping; |
| 175 |
} |
| 176 |
|
| 177 |
private function getSampleRequest( $custom = [] ) { |
| 178 |
$request = [ |
| 179 |
'token' => '123ABC', |
| 180 |
'change' => null, |
| 181 |
'optionname' => null, |
| 182 |
'optionvalue' => null, |
| 183 |
]; |
| 184 |
|
| 185 |
return array_merge( $request, $custom ); |
| 186 |
} |
| 187 |
|
| 188 |
private function executeQuery( $request ) { |
| 189 |
$this->mContext->setRequest( new FauxRequest( $request, true, $this->mSession ) ); |
| 190 |
$this->mUserMock->method( 'getRequest' )->willReturn( $this->mContext->getRequest() ); |
| 191 |
|
| 192 |
$this->mTested->execute(); |
| 193 |
|
| 194 |
return $this->mTested->getResult()->getResultData( null, [ 'Strip' => 'all' ] ); |
| 195 |
} |
| 196 |
|
| 197 |
public function testNoToken() { |
| 198 |
$request = $this->getSampleRequest( [ 'token' => null ] ); |
| 199 |
|
| 200 |
$this->expectException( ApiUsageException::class ); |
| 201 |
$this->executeQuery( $request ); |
| 202 |
} |
| 203 |
|
| 204 |
public function testAnon() { |
| 205 |
$this->mUserMock |
| 206 |
->method( 'isRegistered' ) |
| 207 |
->willReturn( false ); |
| 208 |
|
| 209 |
try { |
| 210 |
$request = $this->getSampleRequest(); |
| 211 |
|
| 212 |
$this->executeQuery( $request ); |
| 213 |
} catch ( ApiUsageException $e ) { |
| 214 |
$this->assertApiErrorCode( 'notloggedin', $e ); |
| 215 |
return; |
| 216 |
} |
| 217 |
$this->fail( "ApiUsageException was not thrown" ); |
| 218 |
} |
| 219 |
|
| 220 |
public function testNoOptionname() { |
| 221 |
$this->mUserMock->method( 'isRegistered' )->willReturn( true ); |
| 222 |
$this->mUserMock->method( 'isNamed' )->willReturn( true ); |
| 223 |
|
| 224 |
try { |
| 225 |
$request = $this->getSampleRequest( [ 'optionvalue' => '1' ] ); |
| 226 |
|
| 227 |
$this->executeQuery( $request ); |
| 228 |
} catch ( ApiUsageException $e ) { |
| 229 |
$this->assertApiErrorCode( 'nooptionname', $e ); |
| 230 |
return; |
| 231 |
} |
| 232 |
$this->fail( "ApiUsageException was not thrown" ); |
| 233 |
} |
| 234 |
|
| 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' ); |
| 240 |
|
| 241 |
$this->userOptionsManagerMock->expects( $this->never() ) |
| 242 |
->method( 'setOption' ); |
| 243 |
|
| 244 |
$this->mUserMock->expects( $this->never() ) |
| 245 |
->method( 'saveSettings' ); |
| 246 |
|
| 247 |
try { |
| 248 |
$request = $this->getSampleRequest(); |
| 249 |
|
| 250 |
$this->executeQuery( $request ); |
| 251 |
} catch ( ApiUsageException $e ) { |
| 252 |
$this->assertApiErrorCode( 'nochanges', $e ); |
| 253 |
return; |
| 254 |
} |
| 255 |
$this->fail( "ApiUsageException was not thrown" ); |
| 256 |
} |
| 257 |
|
| 258 |
public function userScenarios() { |
| 259 |
return [ |
| 260 |
[ true, true, false ], |
| 261 |
[ true, false, true ], |
| 262 |
]; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
public function testReset( $isRegistered, $isNamed, $expectException ) { |
| 269 |
$this->mUserMock->method( 'isRegistered' )->willReturn( $isRegistered ); |
| 270 |
$this->mUserMock->method( 'isNamed' )->willReturn( $isNamed ); |
| 271 |
|
| 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' ); |
| 280 |
} |
| 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 ); |
| 288 |
} |
| 289 |
} catch ( ApiUsageException $e ) { |
| 290 |
if ( !$expectException ) { |
| 291 |
$this->fail( 'Unexpected "notloggedin" error.' ); |
| 292 |
} else { |
| 293 |
$this->assertApiErrorCode( 'notloggedin', $e ); |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 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' ); |
| 312 |
} |
| 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 ); |
| 320 |
} |
| 321 |
} catch ( ApiUsageException $e ) { |
| 322 |
if ( !$expectException ) { |
| 323 |
throw $e; |
| 324 |
} |
| 325 |
$this->assertNotNull( $e->getMessageObject() ); |
| 326 |
$this->assertApiErrorCode( 'notloggedin', $e ); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
public function testResetChangeOption( $isRegistered, $isNamed, $expectException ) { |
| 334 |
$this->mUserMock->method( 'isRegistered' )->willReturn( $isRegistered ); |
| 335 |
$this->mUserMock->method( 'isNamed' )->willReturn( $isNamed ); |
| 336 |
|
| 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', |
| 346 |
]; |
| 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' ); |
| 356 |
} |
| 357 |
|
| 358 |
$args = [ |
| 359 |
'reset' => '', |
| 360 |
'change' => 'willBeHappy=Happy', |
| 361 |
'optionname' => 'name', |
| 362 |
'optionvalue' => 'value' |
| 363 |
]; |
| 364 |
|
| 365 |
try { |
| 366 |
$response = $this->executeQuery( $this->getSampleRequest( $args ) ); |
| 367 |
|
| 368 |
if ( $expectException ) { |
| 369 |
$this->fail( "Expected an ApiUsageException" ); |
| 370 |
} else { |
| 371 |
$this->assertEquals( self::SUCCESS, $response ); |
| 372 |
} |
| 373 |
} catch ( ApiUsageException $e ) { |
| 374 |
if ( !$expectException ) { |
| 375 |
throw $e; |
| 376 |
} |
| 377 |
$this->assertNotNull( $e->getMessageObject() ); |
| 378 |
$this->assertApiErrorCode( 'notloggedin', $e ); |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
public function testOptionManupulation( array $params, array $setOptions, ?array $result = null, |
| 386 |
$message = '' |
| 387 |
) { |
| 388 |
$this->mUserMock->method( 'isRegistered' )->willReturn( true ); |
| 389 |
$this->mUserMock->method( 'isNamed' )->willReturn( true ); |
| 390 |
$this->userOptionsManagerMock->expects( $this->never() ) |
| 391 |
->method( 'resetOptionsByName' ); |
| 392 |
|
| 393 |
$expectedOptions = []; |
| 394 |
foreach ( $setOptions as [ $opt, $val ] ) { |
| 395 |
$expectedOptions[$opt] = $val; |
| 396 |
} |
| 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 |
} ); |
| 405 |
|
| 406 |
if ( $setOptions ) { |
| 407 |
$this->mUserMock->expects( $this->once() ) |
| 408 |
->method( 'saveSettings' ); |
| 409 |
} else { |
| 410 |
$this->mUserMock->expects( $this->never() ) |
| 411 |
->method( 'saveSettings' ); |
| 412 |
} |
| 413 |
|
| 414 |
$request = $this->getSampleRequest( $params ); |
| 415 |
$response = $this->executeQuery( $request ); |
| 416 |
|
| 417 |
if ( !$result ) { |
| 418 |
$result = self::SUCCESS; |
| 419 |
} |
| 420 |
$this->assertEquals( $result, $response, $message ); |
| 421 |
} |
| 422 |
|
| 423 |
public static function provideOptionManupulation() { |
| 424 |
return [ |
| 425 |
[ |
| 426 |
[ 'change' => 'userjs-option=1' ], |
| 427 |
[ [ 'userjs-option', '1' ] ], |
| 428 |
null, |
| 429 |
'Setting userjs options', |
| 430 |
], |
| 431 |
[ |
| 432 |
[ 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy' ], |
| 433 |
[ |
| 434 |
[ 'willBeNull', null ], |
| 435 |
[ 'willBeEmpty', '' ], |
| 436 |
[ 'willBeHappy', 'Happy' ], |
| 437 |
], |
| 438 |
null, |
| 439 |
'Basic option setting', |
| 440 |
], |
| 441 |
[ |
| 442 |
[ 'change' => 'testradio=option2' ], |
| 443 |
[ [ 'testradio', 'option2' ] ], |
| 444 |
null, |
| 445 |
'Changing radio options', |
| 446 |
], |
| 447 |
[ |
| 448 |
[ 'change' => 'testradio' ], |
| 449 |
[ [ 'testradio', null ] ], |
| 450 |
null, |
| 451 |
'Resetting radio options', |
| 452 |
], |
| 453 |
[ |
| 454 |
[ 'change' => 'unknownOption=1' ], |
| 455 |
[], |
| 456 |
[ |
| 457 |
'options' => 'success', |
| 458 |
'warnings' => [ |
| 459 |
'options' => [ |
| 460 |
'warnings' => "Validation error for \"unknownOption\": not a valid preference." |
| 461 |
], |
| 462 |
], |
| 463 |
], |
| 464 |
'Unrecognized options should be rejected', |
| 465 |
], |
| 466 |
[ |
| 467 |
[ 'change' => 'special=1' ], |
| 468 |
[], |
| 469 |
[ |
| 470 |
'options' => 'success', |
| 471 |
'warnings' => [ |
| 472 |
'options' => [ |
| 473 |
'warnings' => "Validation error for \"special\": cannot be set by this module." |
| 474 |
] |
| 475 |
] |
| 476 |
], |
| 477 |
'Refuse setting special options', |
| 478 |
], |
| 479 |
[ |
| 480 |
[ |
| 481 |
'change' => 'testmultiselect-opt1=1|testmultiselect-opt2|' |
| 482 |
. 'testmultiselect-opt3=|testmultiselect-opt4=0' |
| 483 |
], |
| 484 |
[ |
| 485 |
[ 'testmultiselect-opt1', true ], |
| 486 |
[ 'testmultiselect-opt2', null ], |
| 487 |
[ 'testmultiselect-opt3', false ], |
| 488 |
[ 'testmultiselect-opt4', false ], |
| 489 |
], |
| 490 |
null, |
| 491 |
'Setting multiselect options', |
| 492 |
], |
| 493 |
[ |
| 494 |
[ 'optionname' => 'name', 'optionvalue' => 'value' ], |
| 495 |
[ [ 'name', 'value' ] ], |
| 496 |
null, |
| 497 |
'Setting options via optionname/optionvalue' |
| 498 |
], |
| 499 |
[ |
| 500 |
[ 'optionname' => 'name' ], |
| 501 |
[ [ 'name', null ] ], |
| 502 |
null, |
| 503 |
'Resetting options via optionname without optionvalue', |
| 504 |
], |
| 505 |
[ |
| 506 |
[ 'optionname' => 'name', 'optionvalue' => str_repeat( '测试', 16383 ) ], |
| 507 |
[], |
| 508 |
[ |
| 509 |
'options' => 'success', |
| 510 |
'warnings' => [ |
| 511 |
'options' => [ |
| 512 |
'warnings' => 'Validation error for "name": value too long (no more than 65,530 bytes allowed).' |
| 513 |
], |
| 514 |
], |
| 515 |
], |
| 516 |
'Options with too long value should be rejected', |
| 517 |
], |
| 518 |
]; |
| 519 |
} |
| 520 |
} |