api: Replace deprecated User::getOptionKinds/resetOptions in ApiOptions

Bug: T277600
Change-Id: I66320d906f735fa3edadfa29a14d6699d159d400
This commit is contained in:
Umherirrender 2021-06-28 21:57:01 +02:00
parent 10d4adf6e1
commit 44a2f83f37
2 changed files with 36 additions and 29 deletions

View file

@ -112,7 +112,7 @@ class ApiOptions extends ApiBase {
}
$prefs = $this->getPreferences();
$prefsKinds = $user->getOptionKinds( $this->getContext(), $changes );
$prefsKinds = $this->userOptionsManager->getOptionKinds( $user, $this->getContext(), $changes );
$htmlForm = null;
foreach ( $changes as $key => $value ) {
@ -211,7 +211,7 @@ class ApiOptions extends ApiBase {
* @param string[] $kinds One or more types returned by UserOptionsManager::listOptionKinds() or 'all'
*/
protected function resetPreferences( array $kinds ) {
$this->getUserForUpdates()->resetOptions( $kinds, $this->getContext() );
$this->userOptionsManager->resetOptions( $this->getUserForUpdates(), $this->getContext(), $kinds );
}
/**
@ -221,7 +221,7 @@ class ApiOptions extends ApiBase {
* @param mixed $value
*/
protected function setPreference( $preference, $value ) {
$this->getUserForUpdates()->setOption( $preference, $value );
$this->userOptionsManager->setOption( $this->getUserForUpdates(), $preference, $value );
}
/**

View file

@ -17,6 +17,8 @@ class ApiOptionsTest extends MediaWikiLangTestCase {
/** @var MockObject */
private $mUserMock;
/** @var MockObject */
private $userOptionsManagerMock;
/** @var ApiOptions */
private $mTested;
private $mSession;
@ -35,9 +37,6 @@ class ApiOptionsTest extends MediaWikiLangTestCase {
// Set up groups and rights
$this->mUserMock->method( 'getEffectiveGroups' )->willReturn( [ '*', 'user' ] );
// Set up callback for User::getOptionKinds
$this->mUserMock->method( 'getOptionKinds' )->will( $this->returnCallback( [ $this, 'getOptionKinds' ] ) );
// No actual DB data
$this->mUserMock->method( 'getInstanceForUpdate' )->willReturn( $this->mUserMock );
@ -55,14 +54,18 @@ class ApiOptionsTest extends MediaWikiLangTestCase {
// Empty session
$this->mSession = [];
$userOptionsManagerMock = $this->createNoOpMock(
$this->userOptionsManagerMock = $this->createNoOpMock(
UserOptionsManager::class,
[ 'getOptions', 'listOptionKinds' ]
[ 'getOptions', 'listOptionKinds', 'getOptionKinds', 'resetOptions', 'setOption' ]
);
// Needs to return something
$userOptionsManagerMock->method( 'getOptions' )->willReturn( [] );
$this->userOptionsManagerMock->method( 'getOptions' )->willReturn( [] );
$userOptionsManagerMock->method( 'listOptionKinds' )->willReturn(
// Set up callback for UserOptionsManager::getOptionKinds
$this->userOptionsManagerMock->method( 'getOptionKinds' )
->willReturnCallback( [ $this, 'getOptionKinds' ] );
$this->userOptionsManagerMock->method( 'listOptionKinds' )->willReturn(
[
'registered',
'registered-multiselect',
@ -80,7 +83,7 @@ class ApiOptionsTest extends MediaWikiLangTestCase {
$preferencesFactory->method( 'getFormDescriptor' )
->willReturnCallback( [ $this, 'getPreferencesFormDescription' ] );
$this->mTested = new ApiOptions( $main, 'options', $userOptionsManagerMock, $preferencesFactory );
$this->mTested = new ApiOptions( $main, 'options', $this->userOptionsManagerMock, $preferencesFactory );
$this->mergeMwGlobalArrayValue( 'wgDefaultUserOptions', [
'testradio' => 'option1',
@ -124,12 +127,13 @@ class ApiOptionsTest extends MediaWikiLangTestCase {
}
/**
* @param mixed $unused
* @param IContextSource $context
* @param array|null $options
*
* @return array
*/
public function getOptionKinds( IContextSource $context, $options = null ) {
public function getOptionKinds( $unused, IContextSource $context, $options = null ) {
// Match with above.
$kinds = [
'name' => 'registered',
@ -222,10 +226,10 @@ class ApiOptionsTest extends MediaWikiLangTestCase {
public function testNoChanges() {
$this->mUserMock->method( 'isRegistered' )->willReturn( true );
$this->mUserMock->expects( $this->never() )
$this->userOptionsManagerMock->expects( $this->never() )
->method( 'resetOptions' );
$this->mUserMock->expects( $this->never() )
$this->userOptionsManagerMock->expects( $this->never() )
->method( 'setOption' );
$this->mUserMock->expects( $this->never() )
@ -245,11 +249,10 @@ class ApiOptionsTest extends MediaWikiLangTestCase {
public function testReset() {
$this->mUserMock->method( 'isRegistered' )->willReturn( true );
$this->mUserMock->expects( $this->once() )
->method( 'resetOptions' )
->with( [ 'all' ] );
$this->userOptionsManagerMock->expects( $this->once() )
->method( 'resetOptions' );
$this->mUserMock->expects( $this->never() )
$this->userOptionsManagerMock->expects( $this->never() )
->method( 'setOption' );
$this->mUserMock->expects( $this->once() )
@ -265,11 +268,10 @@ class ApiOptionsTest extends MediaWikiLangTestCase {
public function testResetKinds() {
$this->mUserMock->method( 'isRegistered' )->willReturn( true );
$this->mUserMock->expects( $this->once() )
->method( 'resetOptions' )
->with( [ 'registered' ] );
$this->userOptionsManagerMock->expects( $this->once() )
->method( 'resetOptions' );
$this->mUserMock->expects( $this->never() )
$this->userOptionsManagerMock->expects( $this->never() )
->method( 'setOption' );
$this->mUserMock->expects( $this->once() )
@ -285,14 +287,14 @@ class ApiOptionsTest extends MediaWikiLangTestCase {
public function testResetChangeOption() {
$this->mUserMock->method( 'isRegistered' )->willReturn( true );
$this->mUserMock->expects( $this->once() )
$this->userOptionsManagerMock->expects( $this->once() )
->method( 'resetOptions' );
$this->mUserMock->expects( $this->exactly( 2 ) )
$this->userOptionsManagerMock->expects( $this->exactly( 2 ) )
->method( 'setOption' )
->withConsecutive(
[ 'willBeHappy', 'Happy' ],
[ 'name', 'value' ]
[ $this->mUserMock, 'willBeHappy', 'Happy' ],
[ $this->mUserMock, 'name', 'value' ]
);
$this->mUserMock->expects( $this->once() )
@ -318,12 +320,17 @@ class ApiOptionsTest extends MediaWikiLangTestCase {
) {
$this->mUserMock->method( 'isRegistered' )->willReturn( true );
$this->mUserMock->expects( $this->never() )
$this->userOptionsManagerMock->expects( $this->never() )
->method( 'resetOptions' );
$this->mUserMock->expects( $this->exactly( count( $setOptions ) ) )
$args = [];
foreach ( $setOptions as $setOption ) {
$args[] = array_merge( [ $this->mUserMock ], $setOption );
}
$this->userOptionsManagerMock->expects( $this->exactly( count( $setOptions ) ) )
->method( 'setOption' )
->withConsecutive( ...$setOptions );
->withConsecutive( ...$args );
if ( $setOptions ) {
$this->mUserMock->expects( $this->once() )