Avoid using User::getOption

Remove using of User:getOption since this method
will be hard-deprecated. Now it is soft-deprecated.

Bug: T296083
Change-Id: I3194a9c1c5c70592f88bc4dbedc78846d1141768
This commit is contained in:
vladshapik 2021-11-23 15:40:35 +02:00 committed by Jdlrobson
parent 8c19f565d1
commit 262a520a2c
5 changed files with 56 additions and 29 deletions

View File

@ -55,7 +55,8 @@ return [
new SkinVersionLookup(
$context->getRequest(),
$context->getUser(),
$services->getService( Constants::SERVICE_CONFIG )
$services->getService( Constants::SERVICE_CONFIG ),
$services->getUserOptionsLookup()
)
)
);

View File

@ -585,7 +585,9 @@ class SkinVector extends SkinMustache {
private function isSidebarVisible() {
$skin = $this->getSkin();
if ( $skin->getUser()->isRegistered() ) {
$userPrefSidebarState = $skin->getUser()->getOption(
$userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
$userPrefSidebarState = $userOptionsLookup->getOption(
$skin->getUser(),
Constants::PREF_KEY_SIDEBAR_VISIBLE
);

View File

@ -23,6 +23,7 @@
namespace Vector;
use Config;
use MediaWiki\User\UserOptionsLookup;
use User;
use WebRequest;
@ -61,6 +62,10 @@ final class SkinVersionLookup {
* @var Config
*/
private $config;
/**
* @var UserOptionsLookup
*/
private $userOptionsLookup;
/**
* This constructor accepts all dependencies needed to obtain the skin version. The dependencies
@ -69,11 +74,18 @@ final class SkinVersionLookup {
* @param WebRequest $request
* @param User $user
* @param Config $config
* @param UserOptionsLookup $userOptionsLookup
*/
public function __construct( WebRequest $request, User $user, Config $config ) {
public function __construct(
WebRequest $request,
User $user,
Config $config,
UserOptionsLookup $userOptionsLookup
) {
$this->request = $request;
$this->user = $user;
$this->config = $config;
$this->userOptionsLookup = $userOptionsLookup;
}
/**
@ -102,11 +114,12 @@ final class SkinVersionLookup {
// sessions are unavailable at that time so it's not possible to determine whether the
// preference is for a logged in user or an anonymous user. Since new users are known to have
// had their user preferences initialized in `Hooks::onLocalUserCreated()`, that means all
// subsequent requests to `User->getOption()` that do not have a preference set are either
// existing accounts or anonymous users. Login state makes the distinction.
// subsequent requests to `UserOptionsLookup->getOption()` that do not have a preference set
//are either existing accounts or anonymous users. Login state makes the distinction.
return (string)$this->request->getVal(
Constants::QUERY_PARAM_SKIN_VERSION,
$this->user->getOption(
$this->userOptionsLookup->getOption(
$this->user,
Constants::PREF_KEY_SKIN_VERSION,
$this->config->get(
$this->user->isRegistered()

View File

@ -19,6 +19,7 @@
* @since 1.35
*/
use MediaWiki\User\UserOptionsLookup;
use Vector\SkinVersionLookup;
/**
@ -41,17 +42,15 @@ class SkinVersionLookupTest extends \MediaWikiIntegrationTestCase {
$user
->method( 'isRegistered' )
->willReturn( false );
$user
->method( 'getOption' )
->with( $this->anything(), '2' )
->willReturn( 'beta' );
$config = new HashConfig( [
'VectorDefaultSkinVersion' => '2',
'VectorDefaultSkinVersionForExistingAccounts' => '1'
] );
$skinVersionLookup = new SkinVersionLookup( $request, $user, $config );
$userOptionsLookup = $this->getUserOptionsLookupMock( $user, '2', 'beta' );
$skinVersionLookup = new SkinVersionLookup( $request, $user, $config, $userOptionsLookup );
$this->assertSame(
'alpha',
@ -80,17 +79,15 @@ class SkinVersionLookupTest extends \MediaWikiIntegrationTestCase {
$user
->method( 'isRegistered' )
->willReturn( false );
$user
->method( 'getOption' )
->with( $this->anything(), '2' )
->willReturn( 'beta' );
$config = new HashConfig( [
'VectorDefaultSkinVersion' => '2',
'VectorDefaultSkinVersionForExistingAccounts' => '1'
] );
$skinVersionLookup = new SkinVersionLookup( $request, $user, $config );
$userOptionsLookup = $this->getUserOptionsLookupMock( $user, '2', 'beta' );
$skinVersionLookup = new SkinVersionLookup( $request, $user, $config, $userOptionsLookup );
$this->assertSame(
'beta',
@ -119,17 +116,15 @@ class SkinVersionLookupTest extends \MediaWikiIntegrationTestCase {
$user
->method( 'isRegistered' )
->willReturn( true );
$user
->method( 'getOption' )
->with( $this->anything(), '1' )
->willReturn( '1' );
$config = new HashConfig( [
'VectorDefaultSkinVersion' => '2',
'VectorDefaultSkinVersionForExistingAccounts' => '1'
] );
$skinVersionLookup = new SkinVersionLookup( $request, $user, $config );
$userOptionsLookup = $this->getUserOptionsLookupMock( $user, '1', '1' );
$skinVersionLookup = new SkinVersionLookup( $request, $user, $config, $userOptionsLookup );
$this->assertSame(
'1',
@ -158,17 +153,15 @@ class SkinVersionLookupTest extends \MediaWikiIntegrationTestCase {
$user
->method( 'isRegistered' )
->willReturn( false );
$user
->method( 'getOption' )
->with( $this->anything(), '2' )
->willReturn( '2' );
$config = new HashConfig( [
'VectorDefaultSkinVersion' => '2',
'VectorDefaultSkinVersionForExistingAccounts' => '1'
] );
$skinVersionLookup = new SkinVersionLookup( $request, $user, $config );
$userOptionsLookup = $this->getUserOptionsLookupMock( $user, '2', '2' );
$skinVersionLookup = new SkinVersionLookup( $request, $user, $config, $userOptionsLookup );
$this->assertSame(
'2',
@ -181,4 +174,18 @@ class SkinVersionLookupTest extends \MediaWikiIntegrationTestCase {
'Version is non-Legacy.'
);
}
/**
* @param User $user
* @param mixed|null $defaultOverride
* @param mixed|null $returnVal
* @return UserOptionsLookup
*/
private function getUserOptionsLookupMock( $user, $defaultOverride, $returnVal ) {
$mock = $this->createMock( UserOptionsLookup::class );
$mock->method( 'getOption' )
->with( $user, $this->anything(), $defaultOverride )
->willReturn( $returnVal );
return $mock;
}
}

View File

@ -21,6 +21,7 @@
namespace Vector\FeatureManagement\Tests;
use HashConfig;
use MediaWiki\User\UserOptionsLookup;
use User;
use Vector\FeatureManagement\Requirements\LatestSkinVersionRequirement;
use Vector\SkinVersionLookup;
@ -48,8 +49,10 @@ class LatestSkinVersionRequirementTest extends \MediaWikiUnitTestCase {
$user = $this->createMock( User::class );
$user->method( 'isRegistered' )->willReturn( true );
$user->method( 'getOption' )
->will( $this->returnArgument( 1 ) );
$userOptionsLookup = $this->createMock( UserOptionsLookup::class );
$userOptionsLookup->method( 'getOption' )
->will( $this->returnArgument( 2 ) );
$request = $this->createMock( WebRequest::class );
$request->method( 'getVal' )
@ -59,7 +62,8 @@ class LatestSkinVersionRequirementTest extends \MediaWikiUnitTestCase {
new SkinVersionLookup(
$request,
$user,
$config
$config,
$userOptionsLookup
)
);