Inject service UserOptionsLookup into FeatureManager

Change-Id: I3a056731c4a4e970cae0b974dab574bdbb0401f5
This commit is contained in:
Fomafix 2024-05-06 19:59:33 +00:00
parent 55e6dfd81c
commit 6320921bd4
4 changed files with 43 additions and 15 deletions

View file

@ -22,10 +22,10 @@
namespace MediaWiki\Skins\Vector\FeatureManagement;
use MediaWiki\MediaWikiServices;
use MediaWiki\Skins\Vector\ConfigHelper;
use MediaWiki\Skins\Vector\Constants;
use MediaWiki\Skins\Vector\FeatureManagement\Requirements\SimpleRequirement;
use MediaWiki\User\Options\UserOptionsLookup;
use RequestContext;
use Wikimedia\Assert\Assert;
@ -63,6 +63,14 @@ class FeatureManager {
*/
private $requirements = [];
private UserOptionsLookup $userOptionsLookup;
public function __construct(
UserOptionsLookup $userOptionsLookup
) {
$this->userOptionsLookup = $userOptionsLookup;
}
/**
* Register a feature and its requirements.
*
@ -143,8 +151,7 @@ class FeatureManager {
*/
public function getUserPreferenceValue( $preferenceKey ) {
$user = RequestContext::getMain()->getUser();
$userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
return $userOptionsLookup->getOption(
return $this->userOptionsLookup->getOption(
$user,
$preferenceKey
// For client preferences, this should be the same as `preferenceKey`

View file

@ -50,7 +50,7 @@ class FeatureManagerFactory {
}
public function createFeatureManager( IContextSource $context ): FeatureManager {
$featureManager = new FeatureManager();
$featureManager = new FeatureManager( $this->userOptionsLookup );
$request = $context->getRequest();
$config = $context->getConfig();

View file

@ -7,6 +7,12 @@ use MediaWiki\Skins\Vector\FeatureManagement\FeatureManager;
* @coversDefaultClass \MediaWiki\Skins\Vector\FeatureManagement\FeatureManager
*/
class FeatureManagerTest extends \MediaWikiIntegrationTestCase {
private function newFeatureManager(): FeatureManager {
return new FeatureManager(
$this->getServiceContainer()->getUserOptionsLookup()
);
}
public static function provideGetFeatureBodyClass() {
return [
[ CONSTANTS::FEATURE_LIMITED_WIDTH, false, 'vector-feature-limited-width-clientpref-0' ],
@ -23,7 +29,7 @@ class FeatureManagerTest extends \MediaWikiIntegrationTestCase {
* @covers ::getFeatureBodyClass basic usage
*/
public function testGetFeatureBodyClass( $feature, $enabled, $expected ) {
$featureManager = new FeatureManager();
$featureManager = $this->newFeatureManager();
$featureManager->registerSimpleRequirement( 'requirement', $enabled );
$featureManager->registerFeature( $feature, [ 'requirement' ] );
@ -34,7 +40,7 @@ class FeatureManagerTest extends \MediaWikiIntegrationTestCase {
* @covers ::getFeatureBodyClass returning multiple feature classes
*/
public function testGetFeatureBodyClassMultiple() {
$featureManager = new FeatureManager();
$featureManager = $this->newFeatureManager();
$featureManager->registerSimpleRequirement( 'requirement', true );
$featureManager->registerSimpleRequirement( 'disabled', false );
$featureManager->registerFeature( 'sticky-header', [ 'requirement' ] );
@ -67,7 +73,7 @@ class FeatureManagerTest extends \MediaWikiIntegrationTestCase {
* @covers ::getFeatureBodyClass pref night mode specifics - disabled pages
*/
public function testGetFeatureBodyClassNightModeQueryParam( $value, $expected ) {
$featureManager = new FeatureManager();
$featureManager = $this->newFeatureManager();
$featureManager->registerFeature( CONSTANTS::PREF_NIGHT_MODE, [] );
$context = RequestContext::getMain();
@ -91,7 +97,7 @@ class FeatureManagerTest extends \MediaWikiIntegrationTestCase {
* @covers ::getFeatureBodyClass pref night mode specifics - disabled pages
*/
public function testGetFeatureBodyClassNightModeDisabled( $disabled ) {
$featureManager = new FeatureManager();
$featureManager = $this->newFeatureManager();
$featureManager->registerFeature( CONSTANTS::PREF_NIGHT_MODE, [] );
$context = RequestContext::getMain();

View file

@ -22,6 +22,7 @@
namespace MediaWiki\Skins\Vector\Tests\Unit\FeatureManagement;
use MediaWiki\Skins\Vector\FeatureManagement\FeatureManager;
use MediaWiki\User\Options\UserOptionsLookup;
/**
* @group Vector
@ -37,7 +38,9 @@ class FeatureManagerTest extends \MediaWikiUnitTestCase {
public function testRegisterSimpleRequirementThrowsWhenRequirementIsRegisteredTwice() {
$this->expectException( \LogicException::class );
$featureManager = new FeatureManager();
$featureManager = new FeatureManager(
$this->createMock( UserOptionsLookup::class )
);
$featureManager->registerSimpleRequirement( 'requirementA', true );
$featureManager->registerSimpleRequirement( 'requirementA', true );
}
@ -68,7 +71,9 @@ class FeatureManagerTest extends \MediaWikiUnitTestCase {
public function testRegisterFeatureValidatesConfig( $expectedExceptionType, $config ) {
$this->expectException( $expectedExceptionType );
$featureManager = new FeatureManager();
$featureManager = new FeatureManager(
$this->createMock( UserOptionsLookup::class )
);
$featureManager->registerSimpleRequirement( 'requirement', true );
$featureManager->registerFeature( 'feature', $config );
}
@ -77,7 +82,9 @@ class FeatureManagerTest extends \MediaWikiUnitTestCase {
* @covers ::isRequirementMet
*/
public function testIsRequirementMet() {
$featureManager = new FeatureManager();
$featureManager = new FeatureManager(
$this->createMock( UserOptionsLookup::class )
);
$featureManager->registerSimpleRequirement( 'enabled', true );
$featureManager->registerSimpleRequirement( 'disabled', false );
@ -91,7 +98,9 @@ class FeatureManagerTest extends \MediaWikiUnitTestCase {
public function testIsRequirementMetThrowsExceptionWhenRequirementIsntRegistered() {
$this->expectException( \InvalidArgumentException::class );
$featureManager = new FeatureManager();
$featureManager = new FeatureManager(
$this->createMock( UserOptionsLookup::class )
);
$featureManager->isRequirementMet( 'foo' );
}
@ -101,7 +110,9 @@ class FeatureManagerTest extends \MediaWikiUnitTestCase {
public function testRegisterFeatureThrowsExceptionWhenFeatureIsRegisteredTwice() {
$this->expectException( \LogicException::class );
$featureManager = new FeatureManager();
$featureManager = new FeatureManager(
$this->createMock( UserOptionsLookup::class )
);
$featureManager->registerFeature( 'featureA', [] );
$featureManager->registerFeature( 'featureA', [] );
}
@ -110,7 +121,9 @@ class FeatureManagerTest extends \MediaWikiUnitTestCase {
* @covers ::isFeatureEnabled
*/
public function testIsFeatureEnabled() {
$featureManager = new FeatureManager();
$featureManager = new FeatureManager(
$this->createMock( UserOptionsLookup::class )
);
$featureManager->registerSimpleRequirement( 'foo', false );
$featureManager->registerFeature( 'requiresFoo', 'foo' );
@ -141,7 +154,9 @@ class FeatureManagerTest extends \MediaWikiUnitTestCase {
public function testIsFeatureEnabledThrowsExceptionWhenFeatureIsntRegistered() {
$this->expectException( \InvalidArgumentException::class );
$featureManager = new FeatureManager();
$featureManager = new FeatureManager(
$this->createMock( UserOptionsLookup::class )
);
$featureManager->isFeatureEnabled( 'foo' );
}
}