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
(cherry picked from commit 5752c2538e)
This commit is contained in:
Thiemo Kreuz 2019-12-17 11:58:36 +01:00 committed by Thiemo Kreuz (WMDE)
parent 219c0c208b
commit 24f2f88452
2 changed files with 39 additions and 22 deletions

View file

@ -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,32 +172,41 @@ 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 );
if ( $config->get( 'PopupsReferencePreviews' ) &&
!$config->get( 'PopupsReferencePreviewsBetaFeature' )
) {
$user->setOption( PopupsContext::REFERENCE_PREVIEWS_PREFERENCE_NAME, $default );
}
}
/**
* Register preferences that enable experimental features.
@ -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' );

View file

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