diff --git a/includes/PopupsContext.php b/includes/PopupsContext.php index e861a4690..508edc5f2 100644 --- a/includes/PopupsContext.php +++ b/includes/PopupsContext.php @@ -24,6 +24,7 @@ use BetaFeatures; use Config; use ExtensionRegistry; use MediaWiki\MediaWikiServices; +use MediaWiki\User\UserOptionsLookup; use Popups\EventLogging\EventLogger; use Title; @@ -44,12 +45,12 @@ class PopupsContext { /** * User preference value for enabled Page Previews */ - public const PREVIEWS_ENABLED = '1'; + public const PREVIEWS_ENABLED = true; /** * User preference value for disabled Page Previews */ - public const PREVIEWS_DISABLED = '0'; + public const PREVIEWS_DISABLED = false; /** * User preference key to enable/disable Page Previews @@ -100,18 +101,30 @@ class PopupsContext { */ private $eventLogger; + /** + * @var UserOptionsLookup + */ + private $userOptionsLookup; + /** * @param Config $config Mediawiki configuration * @param ExtensionRegistry $extensionRegistry MediaWiki extension registry * @param PopupsGadgetsIntegration $gadgetsIntegration Gadgets integration helper * @param EventLogger $eventLogger A logger capable of logging EventLogging + * @param UserOptionsLookup $userOptionsLookup * events */ - public function __construct( Config $config, ExtensionRegistry $extensionRegistry, - PopupsGadgetsIntegration $gadgetsIntegration, EventLogger $eventLogger ) { + public function __construct( + Config $config, + ExtensionRegistry $extensionRegistry, + PopupsGadgetsIntegration $gadgetsIntegration, + EventLogger $eventLogger, + UserOptionsLookup $userOptionsLookup + ) { $this->extensionRegistry = $extensionRegistry; $this->gadgetsIntegration = $gadgetsIntegration; $this->eventLogger = $eventLogger; + $this->userOptionsLookup = $userOptionsLookup; $this->config = $config; } @@ -159,8 +172,9 @@ class PopupsContext { ); } - return !$user->isRegistered() || - $user->getBoolOption( self::REFERENCE_PREVIEWS_PREFERENCE_NAME_AFTER_BETA ); + return !$user->isRegistered() || $this->userOptionsLookup->getBoolOption( + $user, self::REFERENCE_PREVIEWS_PREFERENCE_NAME_AFTER_BETA + ); } /** @@ -192,7 +206,10 @@ class PopupsContext { return true; } - $shouldLoadPagePreviews = $user->getBoolOption( self::PREVIEWS_OPTIN_PREFERENCE_NAME ); + $shouldLoadPagePreviews = $this->userOptionsLookup->getBoolOption( + $user, + self::PREVIEWS_OPTIN_PREFERENCE_NAME + ); $shouldLoadReferencePreviews = $this->isReferencePreviewsEnabled( $user ); return $shouldLoadPagePreviews || $shouldLoadReferencePreviews; diff --git a/includes/ServiceWiring.php b/includes/ServiceWiring.php index fa5d5e5ab..299cbac83 100644 --- a/includes/ServiceWiring.php +++ b/includes/ServiceWiring.php @@ -29,7 +29,8 @@ return [ }, 'Popups.UserPreferencesChangeHandler' => static function ( MediaWikiServices $services ) { return new UserPreferencesChangeHandler( - $services->getService( 'Popups.Context' ) + $services->getService( 'Popups.Context' ), + $services->getUserOptionsLookup() ); }, 'Popups.Logger' => static function ( MediaWikiServices $services ) { @@ -40,7 +41,8 @@ return [ $services->getService( 'Popups.Config' ), ExtensionRegistry::getInstance(), $services->getService( 'Popups.GadgetsIntegration' ), - $services->getService( 'Popups.EventLogger' ) + $services->getService( 'Popups.EventLogger' ), + $services->getUserOptionsLookup() ); } ]; diff --git a/includes/UserPreferencesChangeHandler.php b/includes/UserPreferencesChangeHandler.php index 602732c75..799f12ea9 100644 --- a/includes/UserPreferencesChangeHandler.php +++ b/includes/UserPreferencesChangeHandler.php @@ -22,6 +22,7 @@ namespace Popups; use HTMLForm; use MediaWiki\MediaWikiServices; +use MediaWiki\User\UserOptionsLookup; use User; /** @@ -37,10 +38,20 @@ class UserPreferencesChangeHandler { private $popupsContext; /** - * @param PopupsContext $context + * @var UserOptionsLookup */ - public function __construct( PopupsContext $context ) { + private $userOptionsLookup; + + /** + * @param PopupsContext $context + * @param UserOptionsLookup $userOptionsLookup + */ + public function __construct( + PopupsContext $context, + UserOptionsLookup $userOptionsLookup + ) { $this->popupsContext = $context; + $this->userOptionsLookup = $userOptionsLookup; } /** @@ -56,7 +67,10 @@ class UserPreferencesChangeHandler { } $oldSetting = (bool)$oldUserOptions[PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME]; - $newSetting = $user->getBoolOption( PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME ); + $newSetting = $this->userOptionsLookup->getBoolOption( + $user, + PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME + ); if ( $oldSetting && !$newSetting ) { $this->popupsContext->logUserDisabledPagePreviewsEvent(); diff --git a/tests/phpunit/PopupsContextTest.php b/tests/phpunit/PopupsContextTest.php index 0b4612ee5..b321bd99e 100644 --- a/tests/phpunit/PopupsContextTest.php +++ b/tests/phpunit/PopupsContextTest.php @@ -55,7 +55,14 @@ class PopupsContextTest extends MediaWikiTestCase { $integration->method( 'conflictsWithNavPopupsGadget' ) ->willReturn( false ); } - return new PopupsContext( $config, $registry, $integration, $eventLogger ); + $userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup(); + return new PopupsContext( + $config, + $registry, + $integration, + $eventLogger, + $userOptionsLookup + ); } /** diff --git a/tests/phpunit/PopupsContextTestWrapper.php b/tests/phpunit/PopupsContextTestWrapper.php index 96db1c41a..25ff4456e 100644 --- a/tests/phpunit/PopupsContextTestWrapper.php +++ b/tests/phpunit/PopupsContextTestWrapper.php @@ -44,15 +44,26 @@ class PopupsContextTestWrapper extends PopupsContext { * @param ExtensionRegistry $extensionRegistry MediaWiki extension registry * @param PopupsGadgetsIntegration|null $gadgetsIntegration Gadgets integration helper * @param EventLogger|null $eventLogger EventLogger + * @param UserOptionsLookup $userOptionsLookup */ - public function __construct( Config $config, ExtensionRegistry $extensionRegistry, - PopupsGadgetsIntegration $gadgetsIntegration = null, - EventLogger $eventLogger = null ) { + public function __construct( + Config $config, + ExtensionRegistry $extensionRegistry, + PopupsGadgetsIntegration $gadgetsIntegration, + EventLogger $eventLogger, + UserOptionsLookup $userOptionsLookup + ) { $gadgetsIntegration = $gadgetsIntegration ?: new PopupsGadgetsIntegration( $config, $extensionRegistry ); $eventLogger = $eventLogger ?: new NullLogger(); - parent::__construct( $config, $extensionRegistry, $gadgetsIntegration, $eventLogger ); + parent::__construct( + $config, + $extensionRegistry, + $gadgetsIntegration, + $eventLogger, + $userOptionsLookup + ); } /** diff --git a/tests/phpunit/unit/UserPreferencesChangeHandlerTest.php b/tests/phpunit/unit/UserPreferencesChangeHandlerTest.php index 9a9975587..e6030898a 100644 --- a/tests/phpunit/unit/UserPreferencesChangeHandlerTest.php +++ b/tests/phpunit/unit/UserPreferencesChangeHandlerTest.php @@ -18,6 +18,8 @@ * @file * @ingroup extensions */ + +use MediaWiki\User\UserOptionsLookup; use Popups\PopupsContext; use Popups\UserPreferencesChangeHandler; @@ -37,16 +39,22 @@ class UserPreferencesChangeHandlerTest extends MediaWikiUnitTestCase { $contextMock->expects( $expectedMethodCallsCount ) ->method( 'logUserDisabledPagePreviewsEvent' ); - $user = $this->createMock( User::class ); - $user->method( 'getBoolOption' ) + /** @var User $userMock */ + $userMock = $this->createMock( User::class ); + + $userOptionsLookupMock = $this->createMock( UserOptionsLookup::class ); + $userOptionsLookupMock + ->method( 'getBoolOption' ) ->willReturn( $newOption ); - /** @var User $user */ $oldOptions = [ PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME => $oldOption ]; - $listener = new UserPreferencesChangeHandler( $contextMock ); - $listener->doPreferencesFormPreSave( $user, $oldOptions ); + $listener = new UserPreferencesChangeHandler( + $contextMock, + $userOptionsLookupMock + ); + $listener->doPreferencesFormPreSave( $userMock, $oldOptions ); } public function provideDataForEventHandling() {