Avoid using User::getBoolOption()

User::getBoolOption() is deprecated and should be replaced with
UserOptionsLookup::getBoolOption()

Bug: T277600
Change-Id: I9a2118a6342bd5f145174428dcfb518cba4e439b
This commit is contained in:
Alexander Vorwerk 2021-06-06 15:07:08 +02:00
parent e0e37b76cd
commit 35462639c3
6 changed files with 81 additions and 22 deletions

View file

@ -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;

View file

@ -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()
);
}
];

View file

@ -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();

View file

@ -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
);
}
/**

View file

@ -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
);
}
/**

View file

@ -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() {