Revert "feature(Popups): Conditional User Defaults Implementation"

This reverts commit 62e1729536.

Reason for revert: Jdlrobson: reported bad test on debug host

Change-Id: Ia38e89141188e54937fbd56dd0b74d2cdcff9f8d
This commit is contained in:
Jsn.sherman 2024-05-29 21:06:19 +00:00 committed by Gerrit Code Review
parent 62e1729536
commit 59ad787895
3 changed files with 111 additions and 35 deletions

View file

@ -22,7 +22,9 @@
"BeforePageDisplay": "PopupsHooks",
"ResourceLoaderGetConfigVars": "PopupsHooks",
"GetPreferences": "PopupsHooks",
"MakeGlobalVariablesScript": "PopupsHooks"
"UserGetDefaultOptions": "PopupsHooks",
"MakeGlobalVariablesScript": "PopupsHooks",
"LocalUserCreated": "PopupsHooks"
},
"HookHandlers": {
"PopupsHooks": {
@ -61,6 +63,10 @@
"description": "@var string:['1'|'0'] Default Page Previews visibility for old accounts. Has to be a string as a compatibility with beta feature settings. For more info see @T191888",
"value": "1"
},
"PopupsOptInStateForNewAccounts": {
"description": "@var string:['1'|'0'] Default Page Previews visibility for newly created accounts (from Q2 2018). For more info see @T191888",
"value": "1"
},
"PopupsConflictingNavPopupsGadgetName": {
"description": "@var string: Navigation popups gadget name",
"value": "Navigation_popups"
@ -208,41 +214,8 @@
"includes/ServiceWiring.php"
],
"DefaultUserOptions": {
"popups": 1,
"popupsreferencepreviews": 0,
"popups-reference-previews": 0
},
"ConditionalUserOptions": {
"popups": [
[
1,
[
"registered-after",
"20170816000000"
]
],
[
0,
[
"named-user"
]
]
],
"popups-reference-previews": [
[
1,
[
"registered-after",
"20170816000000"
]
],
[
0,
[
"named-user"
]
]
]
},
"manifest_version": 2
}

View file

@ -21,12 +21,14 @@
namespace Popups;
use ExtensionRegistry;
use MediaWiki\Auth\Hook\LocalUserCreatedHook;
use MediaWiki\Config\Config;
use MediaWiki\Hook\BeforePageDisplayHook;
use MediaWiki\Hook\MakeGlobalVariablesScriptHook;
use MediaWiki\Output\OutputPage;
use MediaWiki\Preferences\Hook\GetPreferencesHook;
use MediaWiki\ResourceLoader\Hook\ResourceLoaderGetConfigVarsHook;
use MediaWiki\User\Hook\UserGetDefaultOptionsHook;
use MediaWiki\User\Options\UserOptionsManager;
use MediaWiki\User\User;
use Psr\Log\LoggerInterface;
@ -41,7 +43,9 @@ class PopupsHooks implements
GetPreferencesHook,
BeforePageDisplayHook,
ResourceLoaderGetConfigVarsHook,
MakeGlobalVariablesScriptHook
MakeGlobalVariablesScriptHook,
UserGetDefaultOptionsHook,
LocalUserCreatedHook
{
private const PREVIEWS_PREFERENCES_SECTION = 'rendering/reading';
@ -241,4 +245,41 @@ class PopupsHooks implements
public function onMakeGlobalVariablesScript( &$vars, $out ): void {
$vars['wgPopupsFlags'] = $this->popupsContext->getConfigBitmaskFromUser( $out->getUser() );
}
/**
* Called whenever a user wants to reset their preferences.
*
* @param array &$defaultOptions
*/
public function onUserGetDefaultOptions( &$defaultOptions ) {
$default = $this->config->get( 'PopupsOptInDefaultState' );
$defaultOptions[PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME] = $default;
if ( $this->config->get( 'PopupsReferencePreviews' ) ) {
$defaultOptions[PopupsContext::REFERENCE_PREVIEWS_PREFERENCE_NAME] = '1';
}
}
/**
* Called one time when initializing a users preferences for a newly created account.
*
* @param User $user Newly created user object
* @param bool $isAutoCreated
*/
public function onLocalUserCreated( $user, $isAutoCreated ) {
$default = $this->config->get( 'PopupsOptInStateForNewAccounts' );
$this->userOptionsManager->setOption(
$user,
PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME,
$default
);
if ( $this->config->get( 'PopupsReferencePreviews' ) ) {
$this->userOptionsManager->setOption(
$user,
PopupsContext::REFERENCE_PREVIEWS_PREFERENCE_NAME,
$default
);
}
}
}

View file

@ -22,6 +22,7 @@
use MediaWiki\Config\HashConfig;
use MediaWiki\Config\MultiConfig;
use MediaWiki\Output\OutputPage;
use MediaWiki\User\Options\UserOptionsManager;
use MediaWiki\User\User;
use Popups\PopupsContext;
use Popups\PopupsHooks;
@ -315,6 +316,67 @@ class PopupsHooksTest extends MediaWikiIntegrationTestCase {
'The wgPopupsFlags global is present and 0.' );
}
/**
* @covers ::onUserGetDefaultOptions
* @dataProvider provideReferencePreviewsFlag
*/
public function testOnUserGetDefaultOptions( bool $enabled ) {
$userOptions = [
'test' => 'not_empty'
];
( new PopupsHooks(
new HashConfig( [
'PopupsOptInDefaultState' => '1',
'PopupsReferencePreviews' => $enabled,
] ),
$this->getServiceContainer()->getService( 'Popups.Context' ),
$this->getServiceContainer()->getService( 'Popups.Logger' ),
$this->getServiceContainer()->getUserOptionsManager()
) )
->onUserGetDefaultOptions( $userOptions );
$this->assertCount( $enabled ? 3 : 2, $userOptions );
$this->assertSame( '1', $userOptions[ PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME ] );
if ( $enabled ) {
$this->assertSame( '1', $userOptions[ PopupsContext::REFERENCE_PREVIEWS_PREFERENCE_NAME ] );
}
}
/**
* @covers ::onUserGetDefaultOptions
* @dataProvider provideReferencePreviewsFlag
*/
public function testOnLocalUserCreatedForNewlyCreatedUser( bool $enabled ) {
$expectedState = '1';
$userMock = $this->createMock( User::class );
$userOptionsManagerMock = $this->createMock( UserOptionsManager::class );
$expectedOptions = [
'popups' => $expectedState,
'popups-reference-previews' => $expectedState
];
$userOptionsManagerMock->expects( $this->exactly( $enabled ? 2 : 1 ) )
->method( 'setOption' )
->willReturnCallback( function ( $user, $option, $val ) use ( &$expectedOptions, $userMock ) {
$this->assertSame( $userMock, $user );
$this->assertArrayHasKey( $option, $expectedOptions );
$this->assertSame( $expectedOptions[$option], $val );
unset( $expectedOptions[$option] );
} );
( new PopupsHooks(
new HashConfig( [
'PopupsOptInStateForNewAccounts' => $expectedState,
'PopupsReferencePreviews' => $enabled,
] ),
$this->getServiceContainer()->getService( 'Popups.Context' ),
$this->getServiceContainer()->getService( 'Popups.Logger' ),
$userOptionsManagerMock
) )
->onLocalUserCreated( $userMock, false );
}
public static function provideReferencePreviewsFlag() {
return [
[ false ],