Fixes: exclusion logic for anonymous users

Bug: T364159
Change-Id: I7c7e9cff7b99f8e37f3f1ffb93464fd3f31c799b
This commit is contained in:
Jon Robson 2024-05-04 05:58:54 +03:00 committed by Jan Drewniak
parent 1d416d50ef
commit 55e6dfd81c
3 changed files with 11 additions and 8 deletions

View file

@ -178,7 +178,8 @@ class FeatureManager {
case CONSTANTS::PREF_NIGHT_MODE:
// if night mode is disabled for the page, add the exclude class instead and return early
if ( ConfigHelper::shouldDisable( $config->get( 'VectorNightModeOptions' ), $request, $title ) ) {
return 'skin-theme-clientpref-excluded';
// The additional "-" prefix, makes this an invalid client preference for anonymous users.
return 'skin-theme-clientpref--excluded';
} else {
$prefix = '';
}

View file

@ -30,7 +30,7 @@ function getClientPreferences() {
* @return {boolean}
*/
function isFeatureExcluded( featureName ) {
return document.documentElement.classList.contains( featureName + '-clientpref-excluded' );
return document.documentElement.classList.contains( featureName + '-clientpref--excluded' );
}
/**
@ -226,13 +226,15 @@ const getFeatureLabelMsg = ( featureName ) =>
*/
function makeControl( featureName, config ) {
const pref = config[ featureName ];
const isExcluded = isFeatureExcluded( featureName );
if ( !pref ) {
return null;
}
const currentValue = mw.user.clientPrefs.get( featureName );
// The client preference was invalid. This shouldn't happen unless a gadget
// or script has modified the documentElement.
if ( typeof currentValue === 'boolean' ) {
// or script has modified the documentElement or client preference is excluded.
if ( typeof currentValue === 'boolean' && !isExcluded ) {
return null;
}
const row = createRow( '' );
@ -241,20 +243,20 @@ function makeControl( featureName, config ) {
switch ( type ) {
case 'radio':
pref.options.forEach( ( value ) => {
appendRadioToggle( form, featureName, value, currentValue, config );
appendRadioToggle( form, featureName, value, String( currentValue ), config );
} );
break;
case 'switch': {
const labelElement = document.createElement( 'label' );
labelElement.textContent = getFeatureLabelMsg( featureName ).text();
appendToggleSwitch( form, featureName, labelElement, currentValue, config );
appendToggleSwitch( form, featureName, labelElement, String( currentValue ), config );
break;
} default:
throw new Error( 'Unknown client preference! Only switch or radio are supported.' );
}
row.appendChild( form );
if ( isFeatureExcluded( featureName ) ) {
if ( isExcluded ) {
const exclusionNotice = makeExclusionNotice( featureName );
row.appendChild( exclusionNotice );
}

View file

@ -99,7 +99,7 @@ class FeatureManagerTest extends \MediaWikiIntegrationTestCase {
$this->overrideConfigValues( [ 'VectorNightModeOptions' => [ 'exclude' => [ 'mainpage' => $disabled ] ] ] );
$this->assertEquals(
in_array( 'skin-theme-clientpref-excluded', $featureManager->getFeatureBodyClass() ),
in_array( 'skin-theme-clientpref--excluded', $featureManager->getFeatureBodyClass() ),
$disabled
);
}