2024-01-12 22:30:44 +00:00
|
|
|
function createStubUserSettings( expectEnabled ) {
|
|
|
|
return {
|
|
|
|
isPreviewTypeEnabled() {
|
|
|
|
return expectEnabled !== false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function createStubUser( isAnon, options ) {
|
|
|
|
return {
|
|
|
|
isNamed() {
|
|
|
|
return !isAnon;
|
|
|
|
},
|
|
|
|
isAnon() {
|
|
|
|
return isAnon;
|
|
|
|
},
|
|
|
|
options
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const options = { get: () => '1' };
|
|
|
|
|
|
|
|
( mw.loader.getModuleNames().indexOf( 'ext.popups.main' ) !== -1 ?
|
|
|
|
QUnit.module :
|
|
|
|
QUnit.module.skip )( 'ext.cite.referencePreviews#isReferencePreviewsEnabled' );
|
|
|
|
|
2024-04-19 07:42:41 +00:00
|
|
|
QUnit.test( 'relevant combinations of anonymous flags', ( assert ) => {
|
2024-01-12 22:30:44 +00:00
|
|
|
[
|
|
|
|
{
|
|
|
|
testCase: 'enabled for an anonymous user',
|
2024-04-15 08:29:38 +00:00
|
|
|
wgCiteReferencePreviews: true,
|
2024-01-12 22:30:44 +00:00
|
|
|
isAnon: true,
|
|
|
|
enabledByAnon: true,
|
|
|
|
expected: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
testCase: 'turned off via the feature flag (anonymous user)',
|
2024-04-15 08:29:38 +00:00
|
|
|
wgCiteReferencePreviews: false,
|
2024-01-12 22:30:44 +00:00
|
|
|
isAnon: true,
|
|
|
|
enabledByAnon: true,
|
|
|
|
expected: null
|
|
|
|
},
|
|
|
|
{
|
|
|
|
testCase: 'manually disabled by the anonymous user',
|
2024-04-15 08:29:38 +00:00
|
|
|
wgCiteReferencePreviews: true,
|
2024-01-12 22:30:44 +00:00
|
|
|
isAnon: true,
|
|
|
|
enabledByAnon: false,
|
|
|
|
expected: false
|
|
|
|
}
|
|
|
|
].forEach( ( data ) => {
|
|
|
|
const user = {
|
|
|
|
isNamed: () => !data.isAnon && !data.isIPMasked,
|
|
|
|
isAnon: () => data.isAnon,
|
|
|
|
options: {
|
|
|
|
get: () => {}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
isPreviewTypeEnabled = () => {
|
|
|
|
if ( !data.isAnon ) {
|
|
|
|
assert.true( false, 'not expected to be called' );
|
|
|
|
}
|
|
|
|
return data.enabledByAnon;
|
|
|
|
},
|
2024-04-19 07:42:41 +00:00
|
|
|
config = new Map();
|
|
|
|
config.set( 'wgCiteReferencePreviews', data.wgCiteReferencePreviews );
|
2024-01-12 22:30:44 +00:00
|
|
|
|
|
|
|
if ( data.isAnon ) {
|
|
|
|
user.options.get = () => assert.true( false, 'not expected to be called 2' );
|
|
|
|
} else {
|
|
|
|
user.options.get = () => data.enabledByRegistered ? '1' : '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
require( 'ext.cite.referencePreviews' ).private.isReferencePreviewsEnabled( user, isPreviewTypeEnabled, config ),
|
|
|
|
data.expected,
|
|
|
|
data.testCase
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'it should display reference previews when conditions are fulfilled', ( assert ) => {
|
|
|
|
const user = createStubUser( false, options ),
|
|
|
|
userSettings = createStubUserSettings( false ),
|
|
|
|
config = new Map();
|
|
|
|
|
2024-04-15 08:29:38 +00:00
|
|
|
config.set( 'wgCiteReferencePreviews', true );
|
2024-01-12 22:30:44 +00:00
|
|
|
|
|
|
|
assert.true(
|
|
|
|
require( 'ext.cite.referencePreviews' ).private.isReferencePreviewsEnabled( user, userSettings, config ),
|
|
|
|
'If the user is logged in and the user is in the on group, then it\'s enabled.'
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'it should not be enabled when the global is disabling it', ( assert ) => {
|
|
|
|
const user = createStubUser( false ),
|
|
|
|
userSettings = createStubUserSettings( false ),
|
|
|
|
config = new Map();
|
|
|
|
|
2024-04-15 08:29:38 +00:00
|
|
|
config.set( 'wgCiteReferencePreviews', false );
|
2024-01-12 22:30:44 +00:00
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
require( 'ext.cite.referencePreviews' ).private.isReferencePreviewsEnabled( user, userSettings, config ),
|
|
|
|
null,
|
|
|
|
'Reference Previews is disabled.'
|
|
|
|
);
|
|
|
|
} );
|