2014-06-10 23:29:50 +00:00
/ *
* 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/>.
* /
( function ( mw , $ ) {
QUnit . module ( 'mmv.Config' , QUnit . newMwEnvironment ( ) ) ;
2017-07-25 23:38:21 +00:00
QUnit . test ( 'Constructor sanity test' , function ( assert ) {
2014-06-10 23:29:50 +00:00
var config = new mw . mmv . Config ( { } , { } , { } , { } , null ) ;
assert . ok ( config ) ;
} ) ;
2017-07-25 23:38:21 +00:00
QUnit . test ( 'Localstorage get' , function ( assert ) {
2014-06-10 23:29:50 +00:00
var localStorage , config ;
2017-06-02 09:29:41 +00:00
localStorage = mw . mmv . testHelpers . getUnsupportedLocalStorage ( ) ; // no browser support
2014-06-10 23:29:50 +00:00
config = new mw . mmv . 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' ) ;
2017-06-02 09:29:41 +00:00
localStorage = mw . mmv . testHelpers . getDisabledLocalStorage ( ) ; // browser supports it but disabled
2014-06-10 23:29:50 +00:00
config = new mw . mmv . Config ( { } , { } , { } , { } , localStorage ) ;
assert . strictEqual ( config . getFromLocalStorage ( 'foo' ) , null , 'Returns null when disabled' ) ;
assert . strictEqual ( config . getFromLocalStorage ( 'foo' , 'bar' ) , 'bar' , 'Returns fallback when disabled' ) ;
2017-06-02 09:29:41 +00:00
localStorage = mw . mmv . testHelpers . createLocalStorage ( { getItem : this . sandbox . stub ( ) } ) ;
2014-06-10 23:29:50 +00:00
config = new mw . mmv . Config ( { } , { } , { } , { } , localStorage ) ;
2017-06-02 09:29:41 +00:00
localStorage . store . getItem . withArgs ( 'foo' ) . returns ( null ) ;
2014-06-10 23:29:50 +00:00
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' ) ;
2017-06-02 09:29:41 +00:00
localStorage . store . getItem . reset ( ) ;
localStorage . store . getItem . withArgs ( 'foo' ) . returns ( 'boom' ) ;
2014-06-10 23:29:50 +00:00
assert . strictEqual ( config . getFromLocalStorage ( 'foo' ) , 'boom' , 'Returns correct value' ) ;
assert . strictEqual ( config . getFromLocalStorage ( 'foo' , 'bar' ) , 'boom' , 'Returns correct value ignoring fallback' ) ;
} ) ;
2017-07-25 23:38:21 +00:00
QUnit . test ( 'Localstorage set' , function ( assert ) {
2014-06-10 23:29:50 +00:00
var localStorage , config ;
2017-06-02 09:29:41 +00:00
localStorage = mw . mmv . testHelpers . getUnsupportedLocalStorage ( ) ; // no browser support
2014-06-10 23:29:50 +00:00
config = new mw . mmv . Config ( { } , { } , { } , { } , localStorage ) ;
assert . strictEqual ( config . setInLocalStorage ( 'foo' , 'bar' ) , false , 'Returns false when not supported' ) ;
2017-06-02 09:29:41 +00:00
localStorage = mw . mmv . testHelpers . getDisabledLocalStorage ( ) ; // browser supports it but disabled
2014-06-10 23:29:50 +00:00
config = new mw . mmv . Config ( { } , { } , { } , { } , localStorage ) ;
assert . strictEqual ( config . setInLocalStorage ( 'foo' , 'bar' ) , false , 'Returns false when disabled' ) ;
2017-06-02 09:29:41 +00:00
localStorage = mw . mmv . testHelpers . createLocalStorage ( { setItem : this . sandbox . stub ( ) } ) ;
2014-06-10 23:29:50 +00:00
config = new mw . mmv . Config ( { } , { } , { } , { } , localStorage ) ;
assert . strictEqual ( config . setInLocalStorage ( 'foo' , 'bar' ) , true , 'Returns true when works' ) ;
2017-06-02 09:29:41 +00:00
localStorage . store . setItem . throwsException ( 'localStorage full!' ) ;
2014-06-10 23:29:50 +00:00
assert . strictEqual ( config . setInLocalStorage ( 'foo' , 'bar' ) , false , 'Returns false on error' ) ;
} ) ;
2017-07-25 23:38:21 +00:00
QUnit . test ( 'Localstorage remove' , function ( assert ) {
2014-06-10 23:29:50 +00:00
var localStorage , config ;
2017-06-02 09:29:41 +00:00
localStorage = mw . mmv . testHelpers . getUnsupportedLocalStorage ( ) ; // no browser support
2014-06-10 23:29:50 +00:00
config = new mw . mmv . Config ( { } , { } , { } , { } , localStorage ) ;
assert . strictEqual ( config . removeFromLocalStorage ( 'foo' ) , true , 'Returns true when not supported' ) ;
2017-06-02 09:29:41 +00:00
localStorage = mw . mmv . testHelpers . getDisabledLocalStorage ( ) ; // browser supports it but disabled
2014-06-10 23:29:50 +00:00
config = new mw . mmv . Config ( { } , { } , { } , { } , localStorage ) ;
2016-07-18 13:49:27 +00:00
assert . strictEqual ( config . removeFromLocalStorage ( 'foo' ) , true , 'Returns true when disabled' ) ;
2014-06-10 23:29:50 +00:00
2017-06-02 09:29:41 +00:00
localStorage = mw . mmv . testHelpers . createLocalStorage ( { removeItem : this . sandbox . stub ( ) } ) ;
2014-06-10 23:29:50 +00:00
config = new mw . mmv . Config ( { } , { } , { } , { } , localStorage ) ;
assert . strictEqual ( config . removeFromLocalStorage ( 'foo' ) , true , 'Returns true when works' ) ;
} ) ;
2017-07-25 23:38:21 +00:00
QUnit . test ( 'isMediaViewerEnabledOnClick' , function ( assert ) {
2017-06-02 09:29:41 +00:00
var localStorage = mw . mmv . testHelpers . createLocalStorage ( { getItem : this . sandbox . stub ( ) } ) ,
2014-06-10 23:29:50 +00:00
mwConfig = { get : this . sandbox . stub ( ) } ,
mwUser = { isAnon : this . sandbox . stub ( ) } ,
config = new mw . mmv . 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 ) ;
2017-06-02 09:29:41 +00:00
localStorage . store . getItem . withArgs ( 'wgMediaViewerOnClick' ) . returns ( null ) ;
2014-06-10 23:29:50 +00:00
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 ) ;
2017-06-02 09:29:41 +00:00
localStorage . store . getItem . withArgs ( 'wgMediaViewerOnClick' ) . returns ( '0' ) ;
2014-06-10 23:29:50 +00:00
assert . strictEqual ( config . isMediaViewerEnabledOnClick ( ) , false , 'Returns true for anon opted out via localSettings' ) ;
} ) ;
2017-07-25 23:38:21 +00:00
QUnit . test ( 'setMediaViewerEnabledOnClick sanity check' , function ( assert ) {
2017-06-02 09:29:41 +00:00
var localStorage = mw . mmv . testHelpers . createLocalStorage ( {
getItem : this . sandbox . stub ( ) ,
setItem : this . sandbox . stub ( ) ,
removeItem : this . sandbox . stub ( )
} ) ,
2014-06-10 23:29:50 +00:00
mwUser = { isAnon : this . sandbox . stub ( ) } ,
2016-11-19 09:51:38 +00:00
mwConfig = new mw . Map ( ) ,
2018-02-03 13:26:17 +00:00
api = { saveOption : this . sandbox . stub ( ) . returns ( $ . Deferred ( ) . resolve ( ) ) } ,
2014-06-10 23:29:50 +00:00
config = new mw . mmv . Config ( { } , mwConfig , mwUser , api , localStorage ) ;
2016-11-02 22:29:04 +00:00
mwConfig . set ( 'wgMediaViewerEnabledByDefault' , false ) ;
2014-06-10 23:29:50 +00:00
mwUser . isAnon . returns ( false ) ;
2018-02-03 13:26:17 +00:00
api . saveOption . returns ( $ . Deferred ( ) . resolve ( ) ) ;
2014-06-10 23:29:50 +00:00
config . setMediaViewerEnabledOnClick ( false ) ;
2018-02-03 13:26:17 +00:00
assert . ok ( api . saveOption . called , 'For logged-in users, pref change is via API' ) ;
2014-06-10 23:29:50 +00:00
mwUser . isAnon . returns ( true ) ;
config . setMediaViewerEnabledOnClick ( false ) ;
2017-06-02 09:29:41 +00:00
assert . ok ( localStorage . store . setItem . called , 'For anons, opt-out is set in localStorage' ) ;
2014-06-10 23:29:50 +00:00
mwUser . isAnon . returns ( true ) ;
config . setMediaViewerEnabledOnClick ( true ) ;
2017-06-02 09:29:41 +00:00
assert . ok ( localStorage . store . removeItem . called , 'For anons, opt-in means clearing localStorage' ) ;
2014-06-10 23:29:50 +00:00
} ) ;
2014-10-03 09:15:28 +00:00
2017-07-25 23:38:21 +00:00
QUnit . test ( 'shouldShowStatusInfo' , function ( assert ) {
2014-10-03 09:15:28 +00:00
var config ,
2016-11-19 09:51:38 +00:00
mwConfig = new mw . Map ( ) ,
2014-10-03 09:15:28 +00:00
fakeLocalStorage = mw . mmv . testHelpers . getFakeLocalStorage ( ) ,
mwUser = { isAnon : this . sandbox . stub ( ) } ,
2018-02-03 13:26:17 +00:00
api = { saveOption : this . sandbox . stub ( ) . returns ( $ . Deferred ( ) . resolve ( ) ) } ;
2014-10-03 09:15:28 +00:00
2016-11-02 22:29:04 +00:00
mwConfig . set ( {
wgMediaViewer : true ,
wgMediaViewerOnClick : true ,
wgMediaViewerEnabledByDefault : true
} ) ;
2014-10-03 09:15:28 +00:00
config = new mw . mmv . 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 ) ;
2016-11-02 22:29:04 +00:00
assert . strictEqual ( config . shouldShowStatusInfo ( ) , true , 'Status info is shown after MMV is disabled the first time #2' ) ;
2014-10-03 09:15:28 +00:00
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 ) ;
2016-11-02 22:29:04 +00:00
assert . strictEqual ( config . shouldShowStatusInfo ( ) , false , 'Further status changes have no effect #2' ) ;
2014-10-03 09:15:28 +00:00
// make sure disabling calls maybeEnableStatusInfo() for logged-in as well
config . localStorage = mw . mmv . testHelpers . 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 = mw . mmv . testHelpers . getFakeLocalStorage ( ) ;
mwConfig . set ( 'wgMediaViewerEnabledByDefault' , false ) ;
2016-11-02 22:29:04 +00:00
assert . strictEqual ( config . shouldShowStatusInfo ( ) , false , 'Status info is not shown by default #2' ) ;
2014-10-03 09:15:28 +00:00
config . setMediaViewerEnabledOnClick ( true ) ;
2016-11-02 22:29:04 +00:00
assert . strictEqual ( config . shouldShowStatusInfo ( ) , false , 'Status info is not shown when MMV is enabled #2' ) ;
2014-10-03 09:15:28 +00:00
config . setMediaViewerEnabledOnClick ( false ) ;
2016-11-02 22:29:04 +00:00
assert . strictEqual ( config . shouldShowStatusInfo ( ) , true , 'Status info is shown after MMV is disabled the first time #2' ) ;
2014-10-03 09:15:28 +00:00
} ) ;
2016-07-18 13:49:27 +00:00
} ( mediaWiki , jQuery ) ) ;