Make Vector feature flags compatible with beta features

- Added Key 'beta' to features config in skin.json. As usual this also supported by global configs.
- Added feature management logic to check for beta feature enabled status and the user's selection of utilising the vector beta features.
- Added VECTOR_BETA_FEATURES to Constants.php which is an array that holds features names that would be enabled.
- Removed final from class declaration of OverridableConfigRequirement class, as it does interfere with the mocking in the current setup

Remaining Work:
- Missing images to be added in the hook to show in beta features under Special:Preferences -- this will be added in a followup

Bug: T347772
Change-Id: I7bf8284e328c66c019c392f57207cab21ce0d4f6
This commit is contained in:
Moh'd Khier Abualruz 2023-10-18 19:32:16 +02:00
parent ce5c5dba93
commit 173c91a0f5
7 changed files with 188 additions and 15 deletions

View file

@ -83,5 +83,7 @@
"vector-readability-survey-optout": "Press continue to send us your design and publish your feedback on the next page. [[#close-vector-survey|Remove this tool]].",
"vector-readability-survey-randomize": "randomize",
"vector-readability-survey-reset": "reset",
"vector-readability-survey-share": "continue"
"vector-readability-survey-share": "continue",
"vector-2022-beta-preview-label": "Vector 2022 beta",
"vector-2022-beta-preview-description": "Get early previews of new Vector 2022 features."
}

View file

@ -100,5 +100,7 @@
"vector-readability-survey-optout": "Text to provide feedback or opt-out of typography survey",
"vector-readability-survey-randomize": "label for readability survey randomize button",
"vector-readability-survey-reset": "label for readability survey reset button",
"vector-readability-survey-share": "label for readability survey share button"
"vector-readability-survey-share": "label for readability survey share button",
"vector-2022-beta-preview-label": "label for beta feature preview link in beta features under special preferences",
"vector-2022-beta-preview-description": "label for beta feature preview description in beta features under special preferences"
}

View file

@ -305,6 +305,19 @@ final class Constants {
*/
public const REQUIREMENT_CLIENT_PREFERENCES = 'ClientPreferences';
/**
* @var string
*/
public const VECTOR_2022_BETA_KEY = 'vector-2022-beta-feature';
/**
* @var array
*/
public const VECTOR_BETA_FEATURES = [
self::CONFIG_ZEBRA_DESIGN,
self::CONFIG_KEY_CLIENT_PREFERENCES,
];
/**
* This class is for namespacing constants only. Forbid construction.
* @throws FatalError

View file

@ -22,6 +22,9 @@
namespace MediaWiki\Skins\Vector\FeatureManagement\Requirements;
use Config;
use ExtensionRegistry;
use MediaWiki\Extension\BetaFeatures\BetaFeatures;
use MediaWiki\Skins\Vector\Constants;
use MediaWiki\Skins\Vector\FeatureManagement\Requirement;
use User;
use WebRequest;
@ -63,7 +66,7 @@ use WebRequest;
*
* @package MediaWiki\Skins\Vector\FeatureManagement\Requirements
*/
final class OverridableConfigRequirement implements Requirement {
class OverridableConfigRequirement implements Requirement {
/**
* @var Config
@ -142,6 +145,7 @@ final class OverridableConfigRequirement implements Requirement {
$thisConfig = [
'logged_in' => $thisConfig,
'logged_out' => $thisConfig,
'beta' => $thisConfig,
];
} elseif ( array_key_exists( 'default', $thisConfig ) ) {
$thisConfig = [
@ -151,12 +155,32 @@ final class OverridableConfigRequirement implements Requirement {
$thisConfig = [
'logged_in' => $thisConfig['logged_in'] ?? false,
'logged_out' => $thisConfig['logged_out'] ?? false,
'beta' => $thisConfig['beta'] ?? false,
];
}
// Fallback to config.
return array_key_exists( 'default', $thisConfig ) ?
$userConfig = array_key_exists( 'default', $thisConfig ) ?
$thisConfig[ 'default' ] :
$thisConfig[ $this->user->isRegistered() ? 'logged_in' : 'logged_out' ];
// Check if use has enabled beta features
$betaFeatureConfig = array_key_exists( 'beta', $thisConfig ) && $thisConfig[ 'beta' ];
$betaFeatureEnabled = in_array( $this->configName, Constants::VECTOR_BETA_FEATURES ) &&
$betaFeatureConfig && $this->isVector2022BetaFeatureEnabled();
// If user has enabled beta features, use beta config
return $betaFeatureEnabled ? $betaFeatureConfig : $userConfig;
}
/**
* Check if user has enabled the Vector 2022 beta features
* @return bool
*/
public function isVector2022BetaFeatureEnabled(): bool {
return ExtensionRegistry::getInstance()->isLoaded( 'BetaFeatures' ) &&
/* @phan-suppress-next-line PhanUndeclaredClassMethod */
BetaFeatures::isFeatureEnabled(
$this->user,
Constants::VECTOR_2022_BETA_KEY
);
}
}

View file

@ -13,6 +13,7 @@ use MediaWiki\ResourceLoader\Hook\ResourceLoaderSiteStylesModulePagesHook;
use MediaWiki\Skins\Hook\SkinPageReadyConfigHook;
use MediaWiki\Skins\Vector\Hooks\HookRunner;
use OutputPage;
use RequestContext;
use RuntimeException;
use SkinTemplate;
use User;
@ -694,4 +695,41 @@ class Hooks implements
private static function isSkinVersionLegacy( $skinName ): bool {
return $skinName === Constants::SKIN_NAME_LEGACY;
}
/**
* Register Vector 2022 beta feature to the beta features list
*
* @param User $user User the preferences are for
* @param array &$betaFeatures
*/
public function onGetBetaFeaturePreferences( User $user, array &$betaFeatures ) {
$skinName = RequestContext::getMain()->getSkin()->getSkinName();
// Only Vector 2022 is supported for beta features
if ( $skinName !== Constants::SKIN_NAME_MODERN ) {
return;
}
// Only add Vector 2022 beta feature if there is at least one beta feature present in config
$config = MediaWikiServices::getInstance()->getMainConfig();
$configHasBeta = false;
foreach ( Constants::VECTOR_BETA_FEATURES as $featureName ) {
if ( $config->has( $featureName ) && $config->get( $featureName )[ 'beta' ] === true ) {
$configHasBeta = true;
break;
}
}
if ( !$configHasBeta ) {
return;
}
$betaFeatures[ Constants::VECTOR_2022_BETA_KEY ] = [
'label-message' => 'vector-2022-beta-preview-label',
'desc-message' => 'vector-2022-beta-preview-description',
'screenshot' => [
// follow up work to add images is required in T349321
'ltr' => '',
'rtl' => '',
],
'info-link' => 'https://www.mediawiki.org/wiki/Skin:Vector/2022',
'discussion-link' => 'https://www.mediawiki.org/wiki/Talk:Reading/Web/Desktop_Improvements',
];
}
}

View file

@ -190,7 +190,8 @@
"MakeGlobalVariablesScript": "VectorHooks",
"ResourceLoaderSiteModulePages": "VectorHooks",
"ResourceLoaderSiteStylesModulePages": "VectorHooks",
"SkinPageReadyConfig": "VectorHooks"
"SkinPageReadyConfig": "VectorHooks",
"GetBetaFeaturePreferences": "VectorHooks"
},
"@note": "When modifying skins.vector.styles definition, make sure the installer still works",
"ResourceModules": {
@ -556,7 +557,8 @@
"VectorClientPreferences": {
"value": {
"logged_in": false,
"logged_out": false
"logged_out": false,
"beta": false
},
"description": "@var array enables a client preferences interface."
},
@ -658,7 +660,8 @@
"VectorZebraDesign": {
"value": {
"logged_in": false,
"logged_out": false
"logged_out": false,
"beta": false
},
"description": "@var boolean Temporary feature flag that enables the zebra design for the Vector skin."
},

View file

@ -40,6 +40,7 @@ class OverridableConfigRequirementTest extends \MediaWikiUnitTestCase {
[
'logged_in' => false,
'logged_out' => false,
'beta' => false,
],
// note 0 = anon user
0,
@ -53,6 +54,7 @@ class OverridableConfigRequirementTest extends \MediaWikiUnitTestCase {
[
'logged_in' => false,
'logged_out' => true,
'beta' => false,
],
// note 0 = anon user
0,
@ -66,6 +68,7 @@ class OverridableConfigRequirementTest extends \MediaWikiUnitTestCase {
[
'logged_in' => true,
'logged_out' => false,
'beta' => false,
],
1,
// `languageinheader` query param
@ -78,6 +81,7 @@ class OverridableConfigRequirementTest extends \MediaWikiUnitTestCase {
[
'logged_in' => false,
'logged_out' => false,
'beta' => false,
],
1,
// `languageinheader` query param
@ -90,6 +94,21 @@ class OverridableConfigRequirementTest extends \MediaWikiUnitTestCase {
[
'logged_in' => false,
'logged_out' => false,
'beta' => true,
],
1,
// `languageinheader` query param
null,
false,
'Users get old treatment when beta config flag enabled and BetaFeatures extension disabled. ' .
'(BetaFeatures extension is disabled by default.)'
],
[
// Is language enabled
[
'logged_in' => false,
'logged_out' => false,
'beta' => false,
],
1,
// `languageinheader` query param
@ -108,13 +127,15 @@ class OverridableConfigRequirementTest extends \MediaWikiUnitTestCase {
* @param string|null $queryParam
* @param bool $expected
* @param string $msg
* @param bool $betaEnabled
*/
public function testLanguageInHeaderTreatmentRequirement(
$configValue,
$userId,
$queryParam,
$expected,
$msg
$msg,
$betaEnabled = false
) {
$config = new HashConfig( [
Constants::CONFIG_KEY_LANGUAGE_IN_HEADER => $configValue,
@ -128,15 +149,85 @@ class OverridableConfigRequirementTest extends \MediaWikiUnitTestCase {
$request->method( 'getCheck' )->willReturn( $queryParam !== null );
$request->method( 'getBool' )->willReturn( (bool)$queryParam );
$requirement = new OverridableConfigRequirement(
$config,
$user,
$request,
'VectorLanguageInHeader',
'LanguageInHeader'
);
if ( $betaEnabled ) {
$requirement = $this->getMockBuilder( OverridableConfigRequirement::class )
->setConstructorArgs( [
$config,
$user,
$request,
'VectorLanguageInHeader',
'LanguageInHeader'
] )->getMock();
$requirement->method( 'isVector2022BetaFeatureEnabled' )->willReturn( true );
} else {
$requirement = new OverridableConfigRequirement(
$config,
$user,
$request,
'VectorLanguageInHeader',
'LanguageInHeader'
);
}
$this->assertSame( $expected, $requirement->isMet(), $msg );
}
public static function providerLanguageInHeaderTreatmentRequirementBetaEnabled() {
return [
[
// Is language enabled
[
'logged_in' => false,
'logged_out' => false,
'beta' => true,
],
1,
// `languageinheader` query param
null,
false,
'Users get new treatment when beta config flag enabled and BetaFeatures extension enabled. ' .
'(BetaFeatures extension is disabled by default.)'
],
[
// Is language enabled
[
'logged_in' => false,
'logged_out' => false,
'beta' => false,
],
1,
// `languageinheader` query param
"0",
false,
'Users get old treatment when query param set to "0" regardless of state of config flags'
],
];
}
/**
* @covers ::isMet
* @dataProvider providerLanguageInHeaderTreatmentRequirementBetaEnabled
* @param bool $configValue
* @param int $userId
* @param string|null $queryParam
* @param bool $expected
* @param string $msg
*/
public function testLanguageInHeaderTreatmentRequirementBetaEnabled(
$configValue,
$userId,
$queryParam,
$expected,
$msg
) {
$this->testLanguageInHeaderTreatmentRequirement(
$configValue,
$userId,
$queryParam,
$expected,
$msg,
true
);
}
}