Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.cookie.test.js
index 4606cbd..b3f04b7 100644 (file)
@@ -2,23 +2,31 @@
 
        var NOW = 9012, // miliseconds
                DEFAULT_DURATION = 5678, // seconds
+               jqcookie,
+               defaults = {
+                       prefix: 'mywiki',
+                       domain: 'example.org',
+                       path: '/path',
+                       expires: DEFAULT_DURATION,
+                       secure: false
+               },
+               setDefaults = require( 'mediawiki.cookie' ).setDefaults,
                expiryDate = new Date();
 
        expiryDate.setTime( NOW + ( DEFAULT_DURATION * 1000 ) );
 
-       QUnit.module( 'mediawiki.cookie', QUnit.newMwEnvironment( {
-               setup: function () {
-                       this.stub( $, 'cookie' ).returns( null );
-
-                       this.sandbox.useFakeTimers( NOW );
+       QUnit.module( 'mediawiki.cookie', {
+               beforeEach: function () {
+                       jqcookie = sinon.stub( $, 'cookie' ).returns( null );
+                       this.clock = sinon.useFakeTimers( NOW );
+                       this.savedDefaults = setDefaults( defaults );
                },
-               config: {
-                       wgCookiePrefix: 'mywiki',
-                       wgCookieDomain: 'example.org',
-                       wgCookiePath: '/path',
-                       wgCookieExpiration: DEFAULT_DURATION
+               afterEach: function () {
+                       jqcookie.restore();
+                       this.clock.restore();
+                       setDefaults( this.savedDefaults );
                }
-       } ) );
+       } );
 
        QUnit.test( 'set( key, value )', function ( assert ) {
                var call;
@@ -26,7 +34,7 @@
                // Simple case
                mw.cookie.set( 'foo', 'bar' );
 
-               call = $.cookie.lastCall.args;
+               call = jqcookie.lastCall.args;
                assert.strictEqual( call[ 0 ], 'mywikifoo' );
                assert.strictEqual( call[ 1 ], 'bar' );
                assert.deepEqual( call[ 2 ], {
                } );
 
                mw.cookie.set( 'foo', null );
-               call = $.cookie.lastCall.args;
+               call = jqcookie.lastCall.args;
                assert.strictEqual( call[ 1 ], null, 'null removes cookie' );
 
                mw.cookie.set( 'foo', undefined );
-               call = $.cookie.lastCall.args;
+               call = jqcookie.lastCall.args;
                assert.strictEqual( call[ 1 ], 'undefined', 'undefined is value' );
 
                mw.cookie.set( 'foo', false );
-               call = $.cookie.lastCall.args;
+               call = jqcookie.lastCall.args;
                assert.strictEqual( call[ 1 ], 'false', 'false is a value' );
 
                mw.cookie.set( 'foo', 0 );
-               call = $.cookie.lastCall.args;
+               call = jqcookie.lastCall.args;
                assert.strictEqual( call[ 1 ], '0', '0 is value' );
        } );
 
                date.setTime( 1234 );
 
                mw.cookie.set( 'foo', 'bar' );
-               options = $.cookie.lastCall.args[ 2 ];
+               options = jqcookie.lastCall.args[ 2 ];
                assert.deepEqual( options.expires, expiryDate, 'default expiration' );
 
                mw.cookie.set( 'foo', 'bar', date );
-               options = $.cookie.lastCall.args[ 2 ];
+               options = jqcookie.lastCall.args[ 2 ];
                assert.strictEqual( options.expires, date, 'custom expiration as Date' );
 
                date = new Date();
                date.setDate( date.getDate() + 1 );
 
                mw.cookie.set( 'foo', 'bar', 86400 );
-               options = $.cookie.lastCall.args[ 2 ];
+               options = jqcookie.lastCall.args[ 2 ];
                assert.deepEqual( options.expires, date, 'custom expiration as lifetime in seconds' );
 
                mw.cookie.set( 'foo', 'bar', null );
-               options = $.cookie.lastCall.args[ 2 ];
+               options = jqcookie.lastCall.args[ 2 ];
                assert.strictEqual( options.expires, undefined, 'null forces session cookie' );
 
-               // Per DefaultSettings.php, when wgCookieExpiration is 0, the default should
-               // be session cookies
-               mw.config.set( 'wgCookieExpiration', 0 );
+               // Per DefaultSettings.php, if wgCookieExpiration is 0,
+               // then the default should be session cookies
+               setDefaults( $.extend( {}, defaults, { expires: 0 } ) );
 
                mw.cookie.set( 'foo', 'bar' );
-               options = $.cookie.lastCall.args[ 2 ];
+               options = jqcookie.lastCall.args[ 2 ];
                assert.strictEqual( options.expires, undefined, 'wgCookieExpiration=0 results in session cookies by default' );
 
                mw.cookie.set( 'foo', 'bar', date );
-               options = $.cookie.lastCall.args[ 2 ];
+               options = jqcookie.lastCall.args[ 2 ];
                assert.strictEqual( options.expires, date, 'custom expiration (with wgCookieExpiration=0)' );
        } );
 
                        secure: true
                } );
 
-               call = $.cookie.lastCall.args;
+               call = jqcookie.lastCall.args;
                assert.strictEqual( call[ 0 ], 'myPrefixfoo' );
                assert.deepEqual( call[ 2 ], {
                        expires: expiryDate,
                        secure: true
                } );
 
-               call = $.cookie.lastCall.args;
+               call = jqcookie.lastCall.args;
                assert.strictEqual( call[ 0 ], 'myPrefixfoo' );
                assert.deepEqual( call[ 2 ], {
                        expires: date,
 
                mw.cookie.get( 'foo' );
 
-               key = $.cookie.lastCall.args[ 0 ];
+               key = jqcookie.lastCall.args[ 0 ];
                assert.strictEqual( key, 'mywikifoo', 'Default prefix' );
 
                mw.cookie.get( 'foo', undefined );
-               key = $.cookie.lastCall.args[ 0 ];
+               key = jqcookie.lastCall.args[ 0 ];
                assert.strictEqual( key, 'mywikifoo', 'Use default prefix for undefined' );
 
                mw.cookie.get( 'foo', null );
-               key = $.cookie.lastCall.args[ 0 ];
+               key = jqcookie.lastCall.args[ 0 ];
                assert.strictEqual( key, 'mywikifoo', 'Use default prefix for null' );
 
                mw.cookie.get( 'foo', '' );
-               key = $.cookie.lastCall.args[ 0 ];
+               key = jqcookie.lastCall.args[ 0 ];
                assert.strictEqual( key, 'foo', 'Don\'t use default prefix for empty string' );
 
                value = mw.cookie.get( 'foo' );
        QUnit.test( 'get( key ) - with value', function ( assert ) {
                var value;
 
-               $.cookie.returns( 'bar' );
+               jqcookie.returns( 'bar' );
 
                value = mw.cookie.get( 'foo' );
                assert.strictEqual( value, 'bar', 'Return value of cookie' );
 
                mw.cookie.get( 'foo', 'bar' );
 
-               key = $.cookie.lastCall.args[ 0 ];
+               key = jqcookie.lastCall.args[ 0 ];
                assert.strictEqual( key, 'barfoo' );
        } );