diff options
author | Timo Tijhof <krinklemail@gmail.com> | 2019-03-09 02:46:38 +0000 |
---|---|---|
committer | Aaron Schulz <aschulz@wikimedia.org> | 2019-03-10 00:22:41 +0000 |
commit | a0370fe3c9587f5c1ba0a366fd8045e987d61ddb (patch) | |
tree | 67fd1de539a3b166ca42cd8402923f56035d976a /resources/src/mediawiki.cookie | |
parent | f1644aa1b0dd5a51c990907d3658ea9c33ec0dce (diff) | |
download | mediawikicore-a0370fe3c9587f5c1ba0a366fd8045e987d61ddb.tar.gz mediawikicore-a0370fe3c9587f5c1ba0a366fd8045e987d61ddb.zip |
mediawiki.cookie: Export config via packageFiles
* Add a private setDefaults() method to allow mocking from
unit tests.
* Use matching keys between the data export and the API.
* Reduce duplication in the code.
* Access the Sinon stub explicitly instead of via the public path.
* Remove use of the now-redundant QUnit.newMwEnvironment().
Change-Id: I600332cdb738f0b443549948b9f070f3ccfa12aa
Diffstat (limited to 'resources/src/mediawiki.cookie')
-rw-r--r-- | resources/src/mediawiki.cookie/index.js | 100 |
1 files changed, 42 insertions, 58 deletions
diff --git a/resources/src/mediawiki.cookie/index.js b/resources/src/mediawiki.cookie/index.js index 76038f6f1138..61379aecdb03 100644 --- a/resources/src/mediawiki.cookie/index.js +++ b/resources/src/mediawiki.cookie/index.js @@ -1,14 +1,21 @@ ( function () { 'use strict'; + var config = require( './config.json' ), + defaults = { + prefix: config.prefix, + domain: config.domain, + path: config.path, + expires: config.expires, + secure: false + }; + /** - * Provides an API for getting and setting cookies that is - * syntactically and functionally similar to the server-side cookie - * API (`WebRequest#getCookie` and `WebResponse#setcookie`). + * Manage cookies in a way that is syntactically and functionally similar + * to the `WebRequest#getCookie` and `WebResponse#setcookie` methods in PHP. * * @author Sam Smith <samsmith@wikimedia.org> * @author Matthew Flaschen <mflaschen@wikimedia.org> - * @author Timo Tijhof <krinklemail@gmail.com> * * @class mw.cookie * @singleton @@ -18,23 +25,17 @@ /** * Set or delete a cookie. * - * While this is natural in JavaScript, contrary to `WebResponse#setcookie` in PHP, the - * default values for the `options` properties only apply if that property isn't set - * already in your options object (e.g. passing `{ secure: null }` or `{ secure: undefined }` - * overrides the default value for `options.secure`). + * **Note:** If explicitly passing `null` or `undefined` for an options key, + * that will override the default. This is natural in JavaScript, but noted + * here because it is contrary to MediaWiki's `WebResponse#setcookie()` method + * in PHP. * * @param {string} key * @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|number} [options] Options object, or expiry date - * @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. - * - * By default cookie expiration is based on `wgCookieExpiration`. Similar to `WebResponse` - * in PHP, we set a session cookie if `wgCookieExpiration` is 0. And for non-zero values - * it is interpreted as lifetime in seconds. - * + * @param {Date|number|null} [options.expires=wgCookieExpiration] The expiry date of the cookie, + * or lifetime in seconds. If `options.expires` is null or 0, then a session cookie is set. * @param {string} [options.prefix=wgCookiePrefix] The prefix of the key * @param {string} [options.domain=wgCookieDomain] The domain attribute of the cookie * @param {string} [options.path=wgCookiePath] The path attribute of the cookie @@ -42,61 +43,35 @@ * (Does **not** use the wgCookieSecure configuration variable) */ set: function ( key, value, options ) { - var config, defaultOptions, date; - - // wgCookieSecure is not used for now, since 'detect' could not work with - // ResourceLoaderStartUpModule, as module cache is not fragmented by protocol. - config = mw.config.get( [ - 'wgCookiePrefix', - 'wgCookieDomain', - 'wgCookiePath', - 'wgCookieExpiration' - ] ); + var date; - defaultOptions = { - prefix: config.wgCookiePrefix, - domain: config.wgCookieDomain, - path: config.wgCookiePath, - secure: false - }; - - // Options argument can also be a shortcut for the expiry - // Expiry can be a Date, number or null - if ( !options || options instanceof Date || typeof options === 'number' ) { - // Also takes care of options = undefined, in which case we also don't need $.extend() - defaultOptions.expires = options; - options = defaultOptions; - } else { - options = $.extend( defaultOptions, options ); + // The 'options' parameter may be a shortcut for the expiry. + if ( arguments.length > 2 && ( !options || options instanceof Date || typeof options === 'number' ) ) { + options = { expires: options }; } + // Apply defaults + options = $.extend( {}, defaults, options ); - // 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; + // Handle prefix + key = options.prefix + key; + // Don't pass invalid option to $.cookie + delete options.prefix; + + if ( !options.expires ) { + // Session cookie (null or zero) + // Normalize to absent (undefined) for $.cookie. + delete options.expires; } 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 options.expires is omitted - delete options.expires; } - // Process prefix - key = options.prefix + key; - delete options.prefix; - - // Process value if ( value !== null ) { value = String( value ); } - // Other options are handled by $.cookie $.cookie( key, value, options ); }, @@ -114,7 +89,7 @@ var result; if ( prefix === undefined || prefix === null ) { - prefix = mw.config.get( 'wgCookiePrefix' ); + prefix = defaults.prefix; } // Was defaultValue omitted? @@ -128,4 +103,13 @@ } }; + if ( window.QUnit ) { + module.exports = { + setDefaults: function ( value ) { + var prev = defaults; + defaults = value; + return prev; + } + }; + } }() ); |