From: Timo Tijhof Date: Tue, 10 Feb 2015 06:12:15 +0000 (+0000) Subject: mediawiki.cookie: Implement expiry option as time from now X-Git-Tag: 1.31.0-rc.0~12415^2 X-Git-Url: https://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/comptes/ajouter.php?a=commitdiff_plain;h=b7796c608cb1b80b7f242a8914a3bd66fb4f8f2e;p=lhc%2Fweb%2Fwiklou.git mediawiki.cookie: Implement expiry option as time from now The current option (Date object) isn't user-friendly as it requires users to construct a Date and set the time offset accordingly. They likely do that using an offset, not an exact date. Optimise for the common case by supporting this directly (similar to the server-side cookie expiration logic, as well as our ObjectCache classes and other systems). Support for exact dates is kept as this is still useful for the native transport layer where timestamps are used. But the API surface now supports durations as well. Note that there was an undocumented fall-through of options.expires as number of days. This is implemented by upstream jquery.cookie and mw.cookie previously didn't recognise or reject this value so it fell through. Change-Id: Ia2e51d3a3e4d19484367cb74003c4f756c87345d --- diff --git a/resources/src/mediawiki/mediawiki.cookie.js b/resources/src/mediawiki/mediawiki.cookie.js index 73e7f6e7fc..8f091e4dd0 100644 --- a/resources/src/mediawiki/mediawiki.cookie.js +++ b/resources/src/mediawiki/mediawiki.cookie.js @@ -27,7 +27,7 @@ * @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. * @@ -70,16 +70,20 @@ 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; } diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.cookie.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.cookie.test.js index c6aa3b71aa..f5f199ea9c 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.cookie.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.cookie.test.js @@ -53,7 +53,7 @@ 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(); @@ -61,15 +61,22 @@ 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 @@ -81,7 +88,7 @@ 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 ) {