3 var NOW
= 9012, // miliseconds
4 DEFAULT_DURATION
= 5678, // seconds
5 expiryDate
= new Date();
7 expiryDate
.setTime( NOW
+ ( DEFAULT_DURATION
* 1000 ) );
9 QUnit
.module( 'mediawiki.cookie', QUnit
.newMwEnvironment( {
11 this.stub( $, 'cookie' ).returns( null );
13 this.sandbox
.useFakeTimers( NOW
);
16 wgCookiePrefix
: 'mywiki',
17 wgCookieDomain
: 'example.org',
18 wgCookiePath
: '/path',
19 wgCookieExpiration
: DEFAULT_DURATION
23 QUnit
.test( 'set( key, value )', 7, function ( assert
) {
27 mw
.cookie
.set( 'foo', 'bar' );
29 call
= $.cookie
.lastCall
.args
;
30 assert
.strictEqual( call
[ 0 ], 'mywikifoo' );
31 assert
.strictEqual( call
[ 1 ], 'bar' );
32 assert
.deepEqual( call
[ 2 ], {
34 domain
: 'example.org',
39 mw
.cookie
.set( 'foo', null );
40 call
= $.cookie
.lastCall
.args
;
41 assert
.strictEqual( call
[ 1 ], null, 'null removes cookie' );
43 mw
.cookie
.set( 'foo', undefined );
44 call
= $.cookie
.lastCall
.args
;
45 assert
.strictEqual( call
[ 1 ], 'undefined', 'undefined is value' );
47 mw
.cookie
.set( 'foo', false );
48 call
= $.cookie
.lastCall
.args
;
49 assert
.strictEqual( call
[ 1 ], 'false', 'false is a value' );
51 mw
.cookie
.set( 'foo', 0 );
52 call
= $.cookie
.lastCall
.args
;
53 assert
.strictEqual( call
[ 1 ], '0', '0 is value' );
56 QUnit
.test( 'set( key, value, expires )', 6, function ( assert
) {
62 mw
.cookie
.set( 'foo', 'bar' );
63 options
= $.cookie
.lastCall
.args
[ 2 ];
64 assert
.deepEqual( options
.expires
, expiryDate
, 'default expiration' );
66 mw
.cookie
.set( 'foo', 'bar', date
);
67 options
= $.cookie
.lastCall
.args
[ 2 ];
68 assert
.strictEqual( options
.expires
, date
, 'custom expiration as Date' );
71 date
.setDate( date
.getDate() + 1 );
73 mw
.cookie
.set( 'foo', 'bar', 86400 );
74 options
= $.cookie
.lastCall
.args
[ 2 ];
75 assert
.deepEqual( options
.expires
, date
, 'custom expiration as lifetime in seconds' );
77 mw
.cookie
.set( 'foo', 'bar', null );
78 options
= $.cookie
.lastCall
.args
[ 2 ];
79 assert
.strictEqual( options
.expires
, undefined, 'null forces session cookie' );
81 // Per DefaultSettings.php, when wgCookieExpiration is 0, the default should
83 mw
.config
.set( 'wgCookieExpiration', 0 );
85 mw
.cookie
.set( 'foo', 'bar' );
86 options
= $.cookie
.lastCall
.args
[ 2 ];
87 assert
.strictEqual( options
.expires
, undefined, 'wgCookieExpiration=0 results in session cookies by default' );
89 mw
.cookie
.set( 'foo', 'bar', date
);
90 options
= $.cookie
.lastCall
.args
[ 2 ];
91 assert
.strictEqual( options
.expires
, date
, 'custom expiration (with wgCookieExpiration=0)' );
94 QUnit
.test( 'set( key, value, options )', 4, function ( assert
) {
97 mw
.cookie
.set( 'foo', 'bar', {
104 call
= $.cookie
.lastCall
.args
;
105 assert
.strictEqual( call
[ 0 ], 'myPrefixfoo' );
106 assert
.deepEqual( call
[ 2 ], {
111 }, 'Options (without expires)' );
114 date
.setTime( 1234 );
116 mw
.cookie
.set( 'foo', 'bar', {
124 call
= $.cookie
.lastCall
.args
;
125 assert
.strictEqual( call
[ 0 ], 'myPrefixfoo' );
126 assert
.deepEqual( call
[ 2 ], {
131 }, 'Options (incl. expires)' );
134 QUnit
.test( 'get( key ) - no values', 6, function ( assert
) {
137 mw
.cookie
.get( 'foo' );
139 key
= $.cookie
.lastCall
.args
[ 0 ];
140 assert
.strictEqual( key
, 'mywikifoo', 'Default prefix' );
142 mw
.cookie
.get( 'foo', undefined );
143 key
= $.cookie
.lastCall
.args
[ 0 ];
144 assert
.strictEqual( key
, 'mywikifoo', 'Use default prefix for undefined' );
146 mw
.cookie
.get( 'foo', null );
147 key
= $.cookie
.lastCall
.args
[ 0 ];
148 assert
.strictEqual( key
, 'mywikifoo', 'Use default prefix for null' );
150 mw
.cookie
.get( 'foo', '' );
151 key
= $.cookie
.lastCall
.args
[ 0 ];
152 assert
.strictEqual( key
, 'foo', 'Don\'t use default prefix for empty string' );
154 value
= mw
.cookie
.get( 'foo' );
155 assert
.strictEqual( value
, null, 'Return null by default' );
157 value
= mw
.cookie
.get( 'foo', null, 'bar' );
158 assert
.strictEqual( value
, 'bar', 'Custom default value' );
161 QUnit
.test( 'get( key ) - with value', 1, function ( assert
) {
164 $.cookie
.returns( 'bar' );
166 value
= mw
.cookie
.get( 'foo' );
167 assert
.strictEqual( value
, 'bar', 'Return value of cookie' );
170 QUnit
.test( 'get( key, prefix )', 1, function ( assert
) {
173 mw
.cookie
.get( 'foo', 'bar' );
175 key
= $.cookie
.lastCall
.args
[ 0 ];
176 assert
.strictEqual( key
, 'barfoo' );
179 }( mediaWiki
, jQuery
) );