* @param {string|null} value Value of cookie. If `value` is `null` then this method will
* instead remove a cookie by name of `key`.
* @param {Object|Date} [options] Options object, or expiry date
- * @param {Date|null} [options.expires] The expiry date of the cookie.
+ * @param {Date|number|null} [options.expires] The expiry date of the cookie, or lifetime in seconds.
*
* If `options.expires` is null, then a session cookie is set.
*
options = $.extend( defaultOptions, options );
}
- // $.cookie makes session cookies when expiry is omitted,
- // however our default is to expire wgCookieExpiration seconds from now.
- // Note: If wgCookieExpiration is 0, that is considered a special value indicating
+ // Default to using wgCookieExpiration (lifetime in seconds).
+ // If wgCookieExpiration is 0, that is considered a special value indicating
// all cookies should be session cookies by default.
if ( options.expires === undefined && config.wgCookieExpiration !== 0 ) {
date = new Date();
date.setTime( Number( date ) + ( config.wgCookieExpiration * 1000 ) );
options.expires = date;
+ } else if ( typeof options.expires === 'number' ) {
+ // Lifetime in seconds
+ date = new Date();
+ date.setTime( Number( date ) + ( options.expires * 1000 ) );
+ options.expires = date;
} else if ( options.expires === null ) {
- // $.cookie makes a session cookie when expires is omitted
+ // $.cookie makes a session cookie when options.expires is omitted
delete options.expires;
}
assert.strictEqual( call[ 1 ], '0', '0 is value' );
} );
- QUnit.test( 'set( key, value, expires )', 5, function ( assert ) {
+ QUnit.test( 'set( key, value, expires )', 6, function ( assert ) {
var date, options;
date = new Date();
mw.cookie.set( 'foo', 'bar' );
options = $.cookie.lastCall.args[ 2 ];
- assert.deepEqual( options.expires, expiryDate, 'Default cookie expiration is used' );
+ assert.deepEqual( options.expires, expiryDate, 'default expiration' );
mw.cookie.set( 'foo', 'bar', date );
options = $.cookie.lastCall.args[ 2 ];
- assert.strictEqual( options.expires, date, 'Custom expiration date' );
+ 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 ];
+ assert.deepEqual( options.expires, date, 'custom expiration as lifetime in seconds' );
mw.cookie.set( 'foo', 'bar', null );
options = $.cookie.lastCall.args[ 2 ];
- assert.strictEqual( options.expires, undefined, 'Expiry null forces session cookie' );
+ assert.strictEqual( options.expires, undefined, 'null forces session cookie' );
// Per DefaultSettings.php, when wgCookieExpiration is 0, the default should
// be session cookies
mw.cookie.set( 'foo', 'bar', date );
options = $.cookie.lastCall.args[ 2 ];
- assert.strictEqual( options.expires, date, 'Custom expiration when default is session cookies' );
+ assert.strictEqual( options.expires, date, 'custom expiration (with wgCookieExpiration=0)' );
} );
QUnit.test( 'set( key, value, options )', 4, function ( assert ) {