From 5752c2538e310b57e47ec56cf18c517a789a3f21 Mon Sep 17 00:00:00 2001 From: Thiemo Kreuz Date: Tue, 17 Dec 2019 11:58:36 +0100 Subject: [PATCH] Fix initial preferences for newly created user accounts It appears like we accidentially copy-pasted the behavior for PagePreviews and made ReferencePreviews behave the same, not taking into account that the later feature is in Beta mode. Bug: T240947 Change-Id: I4d31805ee9b2045c49c7ab4179c5a4adbcba0394 --- includes/PopupsHooks.php | 48 +++++++++++++++++++------------ tests/phpunit/PopupsHooksTest.php | 13 +++++++-- 2 files changed, 39 insertions(+), 22 deletions(-) diff --git a/includes/PopupsHooks.php b/includes/PopupsHooks.php index c20f751f6..a4d839f94 100644 --- a/includes/PopupsHooks.php +++ b/includes/PopupsHooks.php @@ -20,10 +20,11 @@ */ namespace Popups; +use Config; use MediaWiki\MediaWikiServices; -use User; use OutputPage; use Skin; +use User; /** * Hooks definitions for Popups extension @@ -81,7 +82,7 @@ class PopupsHooks { 'section' => self::PREVIEWS_PREFERENCES_SECTION, 'disabled' => $option['disabled'] ?? false, ]; - }; + } if ( $skinPosition !== false ) { $injectIntoIndex = $skinPosition + 1; @@ -131,7 +132,7 @@ class PopupsHooks { * @param string $skin */ public static function onResourceLoaderGetConfigVars( array &$vars, $skin ) { - /** @var \Config $config */ + /** @var Config $config */ $config = MediaWikiServices::getInstance()->getService( 'Popups.Config' ); $vars['wgPopupsVirtualPageViews'] = $config->get( 'PopupsVirtualPageViews' ); @@ -171,31 +172,40 @@ class PopupsHooks { } /** - * Register default preferences for popups + * Called whenever a user wants to reset their preferences. * - * @param array &$wgDefaultUserOptions Reference to default options array + * @param array &$defaultOptions */ - public static function onUserGetDefaultOptions( array &$wgDefaultUserOptions ) { - $default = MediaWikiServices::getInstance()->getService( 'Popups.Config' ) - ->get( 'PopupsOptInDefaultState' ); + public static function onUserGetDefaultOptions( array &$defaultOptions ) { + /** @var Config $config */ + $config = MediaWikiServices::getInstance()->getService( 'Popups.Config' ); + $default = $config->get( 'PopupsOptInDefaultState' ); + $defaultOptions[PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME] = $default; - $wgDefaultUserOptions[PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME] = $default; - $wgDefaultUserOptions[PopupsContext::REFERENCE_PREVIEWS_PREFERENCE_NAME] = $default; + if ( $config->get( 'PopupsReferencePreviews' ) && + !$config->get( 'PopupsReferencePreviewsBetaFeature' ) + ) { + $defaultOptions[PopupsContext::REFERENCE_PREVIEWS_PREFERENCE_NAME] = $default; + } } /** - * Change the default PagePreviews visibility state for newly created accounts + * Called one time when initializing a users preferences for a newly created account. * * @param User $user Newly created user object - * @param bool $autocreated Is user autocreated + * @param bool $isAutoCreated */ - public static function onLocalUserCreated( User $user, $autocreated ) { - // ignore the $autocreated flag, we always want to set PagePreviews visibility - $default = MediaWikiServices::getInstance()->getService( 'Popups.Config' ) - ->get( 'PopupsOptInStateForNewAccounts' ); - + public static function onLocalUserCreated( User $user, $isAutoCreated ) { + /** @var Config $config */ + $config = MediaWikiServices::getInstance()->getService( 'Popups.Config' ); + $default = $config->get( 'PopupsOptInStateForNewAccounts' ); $user->setOption( PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME, $default ); - $user->setOption( PopupsContext::REFERENCE_PREVIEWS_PREFERENCE_NAME, $default ); + + if ( $config->get( 'PopupsReferencePreviews' ) && + !$config->get( 'PopupsReferencePreviewsBetaFeature' ) + ) { + $user->setOption( PopupsContext::REFERENCE_PREVIEWS_PREFERENCE_NAME, $default ); + } } /** @@ -205,7 +215,7 @@ class PopupsHooks { * @param array[] &$prefs Array of beta features */ public static function onGetBetaFeaturePreferences( User $user, array &$prefs ) { - /** @var \Config $config */ + /** @var Config $config */ $config = MediaWikiServices::getInstance()->getService( 'Popups.Config' ); $extensionAssetsPath = $config->get( 'ExtensionAssetsPath' ); diff --git a/tests/phpunit/PopupsHooksTest.php b/tests/phpunit/PopupsHooksTest.php index 6d20a4941..cedbe6517 100644 --- a/tests/phpunit/PopupsHooksTest.php +++ b/tests/phpunit/PopupsHooksTest.php @@ -284,7 +284,9 @@ class PopupsHooksTest extends MediaWikiTestCase { ]; $this->setMwGlobals( [ - 'wgPopupsOptInDefaultState' => '1' + 'wgPopupsOptInDefaultState' => '1', + 'wgPopupsReferencePreviews' => true, + 'wgPopupsReferencePreviewsBetaFeature' => false, ] ); PopupsHooks::onUserGetDefaultOptions( $userOptions ); @@ -301,10 +303,15 @@ class PopupsHooksTest extends MediaWikiTestCase { $userMock = $this->createMock( User::class ); $userMock->expects( $this->exactly( 2 ) ) ->method( 'setOption' ) - ->with( $this->stringStartsWith( 'popups' ), $expectedState ); + ->withConsecutive( + [ 'popups', $expectedState ], + [ 'popupsreferencepreviews', $expectedState ] + ); $this->setMwGlobals( [ - 'wgPopupsOptInStateForNewAccounts' => $expectedState + 'wgPopupsOptInStateForNewAccounts' => $expectedState, + 'wgPopupsReferencePreviews' => true, + 'wgPopupsReferencePreviewsBetaFeature' => false, ] ); PopupsHooks::onLocalUserCreated( $userMock, false ); }