mediawiki.cookie: Implement expiry option as time from now
authorTimo Tijhof <krinklemail@gmail.com>
Tue, 10 Feb 2015 06:12:15 +0000 (06:12 +0000)
committerMattflaschen <mflaschen@wikimedia.org>
Sat, 14 Feb 2015 00:55:45 +0000 (00:55 +0000)
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

resources/src/mediawiki/mediawiki.cookie.js
tests/qunit/suites/resources/mediawiki/mediawiki.cookie.test.js

index 73e7f6e..8f091e4 100644 (file)
@@ -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.
                 *
                                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;
                        }
 
index c6aa3b7..f5f199e 100644 (file)
@@ -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();
 
                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 ) {