From 262a520a2cf8c0cfe2625a8eb25ef4e562bf37ae Mon Sep 17 00:00:00 2001 From: vladshapik Date: Tue, 23 Nov 2021 15:40:35 +0200 Subject: [PATCH] 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 --- includes/ServiceWiring.php | 3 +- includes/SkinVector.php | 4 +- includes/SkinVersionLookup.php | 21 +++++++-- .../integration/SkinVersionLookupTest.php | 47 +++++++++++-------- .../LatestSkinVersionRequirementTest.php | 10 ++-- 5 files changed, 56 insertions(+), 29 deletions(-) diff --git a/includes/ServiceWiring.php b/includes/ServiceWiring.php index 6d944b0..6193538 100644 --- a/includes/ServiceWiring.php +++ b/includes/ServiceWiring.php @@ -55,7 +55,8 @@ return [ new SkinVersionLookup( $context->getRequest(), $context->getUser(), - $services->getService( Constants::SERVICE_CONFIG ) + $services->getService( Constants::SERVICE_CONFIG ), + $services->getUserOptionsLookup() ) ) ); diff --git a/includes/SkinVector.php b/includes/SkinVector.php index 1ed00f8..1e1bc42 100644 --- a/includes/SkinVector.php +++ b/includes/SkinVector.php @@ -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 ); diff --git a/includes/SkinVersionLookup.php b/includes/SkinVersionLookup.php index a5df4c0..5b7082f 100644 --- a/includes/SkinVersionLookup.php +++ b/includes/SkinVersionLookup.php @@ -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() diff --git a/tests/phpunit/integration/SkinVersionLookupTest.php b/tests/phpunit/integration/SkinVersionLookupTest.php index fef64ea..b37fa24 100644 --- a/tests/phpunit/integration/SkinVersionLookupTest.php +++ b/tests/phpunit/integration/SkinVersionLookupTest.php @@ -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; + } } diff --git a/tests/phpunit/unit/FeatureManagement/Requirements/LatestSkinVersionRequirementTest.php b/tests/phpunit/unit/FeatureManagement/Requirements/LatestSkinVersionRequirementTest.php index c835e48..eb5c148 100644 --- a/tests/phpunit/unit/FeatureManagement/Requirements/LatestSkinVersionRequirementTest.php +++ b/tests/phpunit/unit/FeatureManagement/Requirements/LatestSkinVersionRequirementTest.php @@ -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 ) );