mediawiki-extensions-Multim.../tests/qunit/mmv/mmv.Config.test.js
Bartosz Dziewoński 900ce9d74c Update localStorage mocks for changes in mw.SafeStorage
Since MediaWiki change 6f9d4829b2fefc98a1969bb8f7ab3d04ef0268fe
mw.SafeStorage sometimes calls removeItem() before calling setItem(),
so we need to mock it as well.

Bug: T340069
Change-Id: Ife6c4cc032689bb75e572d7936ce33afa76cf396
2023-06-21 22:09:11 +00:00

207 lines
10 KiB
JavaScript

/*
* This file is part of the MediaWiki extension MediaViewer.
*
* MediaViewer is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* MediaViewer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MediaViewer. If not, see <http://www.gnu.org/licenses/>.
*/
const { Config } = require( 'mmv.bootstrap' );
const { createLocalStorage, getDisabledLocalStorage, getFakeLocalStorage, getUnsupportedLocalStorage } = require( './mmv.testhelpers.js' );
( function () {
QUnit.module( 'mmv.Config', QUnit.newMwEnvironment() );
QUnit.test( 'Constructor sense test', function ( assert ) {
var config = new Config( {}, {}, {}, {}, null );
assert.true( config instanceof Config );
} );
QUnit.test( 'Localstorage get', function ( assert ) {
var localStorage, config;
localStorage = getUnsupportedLocalStorage(); // no browser support
config = new Config( {}, {}, {}, {}, localStorage );
assert.strictEqual( config.getFromLocalStorage( 'foo' ), null, 'Returns null when not supported' );
assert.strictEqual( config.getFromLocalStorage( 'foo', 'bar' ), 'bar', 'Returns fallback when not supported' );
localStorage = getDisabledLocalStorage(); // browser supports it but disabled
config = new Config( {}, {}, {}, {}, localStorage );
assert.strictEqual( config.getFromLocalStorage( 'foo' ), null, 'Returns null when disabled' );
assert.strictEqual( config.getFromLocalStorage( 'foo', 'bar' ), 'bar', 'Returns fallback when disabled' );
localStorage = createLocalStorage( { getItem: this.sandbox.stub() } );
config = new Config( {}, {}, {}, {}, localStorage );
localStorage.store.getItem.withArgs( 'foo' ).returns( null );
assert.strictEqual( config.getFromLocalStorage( 'foo' ), null, 'Returns null when key not set' );
assert.strictEqual( config.getFromLocalStorage( 'foo', 'bar' ), 'bar', 'Returns fallback when key not set' );
localStorage.store.getItem.reset();
localStorage.store.getItem.withArgs( 'foo' ).returns( 'boom' );
assert.strictEqual( config.getFromLocalStorage( 'foo' ), 'boom', 'Returns correct value' );
assert.strictEqual( config.getFromLocalStorage( 'foo', 'bar' ), 'boom', 'Returns correct value ignoring fallback' );
} );
QUnit.test( 'Localstorage set', function ( assert ) {
var localStorage, config;
localStorage = getUnsupportedLocalStorage(); // no browser support
config = new Config( {}, {}, {}, {}, localStorage );
assert.strictEqual( config.setInLocalStorage( 'foo', 'bar' ), false, 'Returns false when not supported' );
localStorage = getDisabledLocalStorage(); // browser supports it but disabled
config = new Config( {}, {}, {}, {}, localStorage );
assert.strictEqual( config.setInLocalStorage( 'foo', 'bar' ), false, 'Returns false when disabled' );
localStorage = createLocalStorage( { setItem: this.sandbox.stub(), removeItem: this.sandbox.stub() } );
config = new Config( {}, {}, {}, {}, localStorage );
assert.strictEqual( config.setInLocalStorage( 'foo', 'bar' ), true, 'Returns true when works' );
localStorage.store.setItem.throwsException( 'localStorage full!' );
assert.strictEqual( config.setInLocalStorage( 'foo', 'bar' ), false, 'Returns false on error' );
} );
QUnit.test( 'Localstorage remove', function ( assert ) {
var localStorage, config;
localStorage = getUnsupportedLocalStorage(); // no browser support
config = new Config( {}, {}, {}, {}, localStorage );
assert.strictEqual( config.removeFromLocalStorage( 'foo' ), true, 'Returns true when not supported' );
localStorage = getDisabledLocalStorage(); // browser supports it but disabled
config = new Config( {}, {}, {}, {}, localStorage );
assert.strictEqual( config.removeFromLocalStorage( 'foo' ), true, 'Returns true when disabled' );
localStorage = createLocalStorage( { removeItem: this.sandbox.stub() } );
config = new Config( {}, {}, {}, {}, localStorage );
assert.strictEqual( config.removeFromLocalStorage( 'foo' ), true, 'Returns true when works' );
} );
QUnit.test( 'isMediaViewerEnabledOnClick', function ( assert ) {
var localStorage = createLocalStorage( { getItem: this.sandbox.stub() } ),
mwConfig = { get: this.sandbox.stub() },
mwUser = { isAnon: this.sandbox.stub() },
config = new Config( {}, mwConfig, mwUser, {}, localStorage );
mwUser.isAnon.returns( false );
mwConfig.get.withArgs( 'wgMediaViewer' ).returns( true );
mwConfig.get.withArgs( 'wgMediaViewerOnClick' ).returns( true );
assert.strictEqual( config.isMediaViewerEnabledOnClick(), true, 'Returns true for logged-in with standard settings' );
mwUser.isAnon.returns( false );
mwConfig.get.withArgs( 'wgMediaViewer' ).returns( false );
mwConfig.get.withArgs( 'wgMediaViewerOnClick' ).returns( true );
assert.strictEqual( config.isMediaViewerEnabledOnClick(), false, 'Returns false if opted out via user JS flag' );
mwUser.isAnon.returns( false );
mwConfig.get.withArgs( 'wgMediaViewer' ).returns( true );
mwConfig.get.withArgs( 'wgMediaViewerOnClick' ).returns( false );
assert.strictEqual( config.isMediaViewerEnabledOnClick(), false, 'Returns false if opted out via preferences' );
mwUser.isAnon.returns( true );
mwConfig.get.withArgs( 'wgMediaViewer' ).returns( false );
mwConfig.get.withArgs( 'wgMediaViewerOnClick' ).returns( true );
assert.strictEqual( config.isMediaViewerEnabledOnClick(), false, 'Returns false if anon user opted out via user JS flag' );
mwUser.isAnon.returns( true );
mwConfig.get.withArgs( 'wgMediaViewer' ).returns( true );
mwConfig.get.withArgs( 'wgMediaViewerOnClick' ).returns( false );
assert.strictEqual( config.isMediaViewerEnabledOnClick(), false, 'Returns false if anon user opted out in some weird way' ); // apparently someone created a browser extension to do this
mwUser.isAnon.returns( true );
mwConfig.get.withArgs( 'wgMediaViewer' ).returns( true );
mwConfig.get.withArgs( 'wgMediaViewerOnClick' ).returns( true );
localStorage.store.getItem.withArgs( 'wgMediaViewerOnClick' ).returns( null );
assert.strictEqual( config.isMediaViewerEnabledOnClick(), true, 'Returns true for anon with standard settings' );
mwUser.isAnon.returns( true );
mwConfig.get.withArgs( 'wgMediaViewer' ).returns( true );
mwConfig.get.withArgs( 'wgMediaViewerOnClick' ).returns( true );
localStorage.store.getItem.withArgs( 'wgMediaViewerOnClick' ).returns( '0' );
assert.strictEqual( config.isMediaViewerEnabledOnClick(), false, 'Returns true for anon opted out via localSettings' );
} );
QUnit.test( 'setMediaViewerEnabledOnClick sense check', function ( assert ) {
var localStorage = createLocalStorage( {
getItem: this.sandbox.stub(),
setItem: this.sandbox.stub(),
removeItem: this.sandbox.stub()
} ),
mwUser = { isAnon: this.sandbox.stub() },
mwConfig = new mw.Map(),
api = { saveOption: this.sandbox.stub().returns( $.Deferred().resolve() ) },
config = new Config( {}, mwConfig, mwUser, api, localStorage );
mwConfig.set( 'wgMediaViewerEnabledByDefault', false );
mwUser.isAnon.returns( false );
api.saveOption.returns( $.Deferred().resolve() );
config.setMediaViewerEnabledOnClick( false );
assert.true( api.saveOption.called, 'For logged-in users, pref change is via API' );
mwUser.isAnon.returns( true );
config.setMediaViewerEnabledOnClick( false );
assert.true( localStorage.store.setItem.called, 'For anons, opt-out is set in localStorage' );
mwUser.isAnon.returns( true );
config.setMediaViewerEnabledOnClick( true );
assert.true( localStorage.store.removeItem.called, 'For anons, opt-in means clearing localStorage' );
} );
QUnit.test( 'shouldShowStatusInfo', function ( assert ) {
var config,
mwConfig = new mw.Map(),
fakeLocalStorage = getFakeLocalStorage(),
mwUser = { isAnon: this.sandbox.stub() },
api = { saveOption: this.sandbox.stub().returns( $.Deferred().resolve() ) };
mwConfig.set( {
wgMediaViewer: true,
wgMediaViewerOnClick: true,
wgMediaViewerEnabledByDefault: true
} );
config = new Config( {}, mwConfig, mwUser, api, fakeLocalStorage );
mwUser.isAnon.returns( false );
assert.strictEqual( config.shouldShowStatusInfo(), false, 'Status info is not shown by default' );
config.setMediaViewerEnabledOnClick( false );
assert.strictEqual( config.shouldShowStatusInfo(), true, 'Status info is shown after MMV is disabled the first time' );
config.setMediaViewerEnabledOnClick( true );
assert.strictEqual( config.shouldShowStatusInfo(), false, 'Status info is not shown when MMV is enabled' );
config.setMediaViewerEnabledOnClick( false );
assert.strictEqual( config.shouldShowStatusInfo(), true, 'Status info is shown after MMV is disabled the first time #2' );
config.disableStatusInfo();
assert.strictEqual( config.shouldShowStatusInfo(), false, 'Status info is not shown when already displayed once' );
config.setMediaViewerEnabledOnClick( true );
assert.strictEqual( config.shouldShowStatusInfo(), false, 'Further status changes have no effect' );
config.setMediaViewerEnabledOnClick( false );
assert.strictEqual( config.shouldShowStatusInfo(), false, 'Further status changes have no effect #2' );
// make sure disabling calls maybeEnableStatusInfo() for logged-in as well
config.localStorage = getFakeLocalStorage();
mwUser.isAnon.returns( true );
assert.strictEqual( config.shouldShowStatusInfo(), false, 'Status info is not shown by default for logged-in users' );
config.setMediaViewerEnabledOnClick( false );
assert.strictEqual( config.shouldShowStatusInfo(), true, 'Status info is shown after MMV is disabled the first time for logged-in users' );
// make sure popup is not shown immediately on disabled-by-default sites, but still works otherwise
config.localStorage = getFakeLocalStorage();
mwConfig.set( 'wgMediaViewerEnabledByDefault', false );
assert.strictEqual( config.shouldShowStatusInfo(), false, 'Status info is not shown by default #2' );
config.setMediaViewerEnabledOnClick( true );
assert.strictEqual( config.shouldShowStatusInfo(), false, 'Status info is not shown when MMV is enabled #2' );
config.setMediaViewerEnabledOnClick( false );
assert.strictEqual( config.shouldShowStatusInfo(), true, 'Status info is shown after MMV is disabled the first time #2' );
} );
}() );