Merge "Restore the original behaviour of Reference Previews"

This commit is contained in:
jenkins-bot 2024-06-24 07:07:44 +00:00 committed by Gerrit Code Review
commit 058c772d52
3 changed files with 43 additions and 15 deletions

View file

@ -22,6 +22,7 @@
"BeforePageDisplay": "PopupsHooks",
"ResourceLoaderGetConfigVars": "PopupsHooks",
"GetPreferences": "PopupsHooks",
"UserGetDefaultOptions": "PopupsHooks",
"MakeGlobalVariablesScript": "PopupsHooks"
},
"HookHandlers": {
@ -201,21 +202,6 @@
"named-user"
]
]
],
"popups-reference-previews": [
[
"1",
[
"registered-after",
"20170816000000"
]
],
[
"0",
[
"named-user"
]
]
]
},
"manifest_version": 2

View file

@ -28,6 +28,7 @@ use MediaWiki\Output\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;
@ -42,6 +43,7 @@ class PopupsHooks implements
GetPreferencesHook,
BeforePageDisplayHook,
ResourceLoaderGetConfigVarsHook,
UserGetDefaultOptionsHook,
MakeGlobalVariablesScriptHook
{
@ -242,4 +244,18 @@ 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';
}
}
}

View file

@ -315,6 +315,32 @@ 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 ] );
}
}
public static function provideReferencePreviewsFlag() {
return [
[ false ],