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 )', 5, function ( assert ) {
62 mw.cookie.set( 'foo', 'bar' );
63 options = $.cookie.lastCall.args[ 2 ];
64 assert.deepEqual( options.expires, expiryDate, 'Default cookie expiration is used' );
66 mw.cookie.set( 'foo', 'bar', date );
67 options = $.cookie.lastCall.args[ 2 ];
68 assert.strictEqual( options.expires, date, 'Custom expiration date' );
70 mw.cookie.set( 'foo', 'bar', null );
71 options = $.cookie.lastCall.args[ 2 ];
72 assert.strictEqual( options.expires, undefined, 'Expiry null forces session cookie' );
74 // Per DefaultSettings.php, when wgCookieExpiration is 0, the default should
76 mw.config.set( 'wgCookieExpiration', 0 );
78 mw.cookie.set( 'foo', 'bar' );
79 options = $.cookie.lastCall.args[ 2 ];
80 assert.strictEqual( options.expires, undefined, 'wgCookieExpiration=0 results in session cookies by default' );
82 mw.cookie.set( 'foo', 'bar', date );
83 options = $.cookie.lastCall.args[ 2 ];
84 assert.strictEqual( options.expires, date, 'Custom expiration when default is session cookies' );
87 QUnit.test( 'set( key, value, options )', 4, function ( assert ) {
90 mw.cookie.set( 'foo', 'bar', {
97 call = $.cookie.lastCall.args;
98 assert.strictEqual( call[0], 'myPrefixfoo' );
99 assert.deepEqual( call[ 2 ], {
104 }, 'Options (without expires)' );
107 date.setTime( 1234 );
109 mw.cookie.set( 'foo', 'bar', {
117 call = $.cookie.lastCall.args;
118 assert.strictEqual( call[0], 'myPrefixfoo' );
119 assert.deepEqual( call[ 2 ], {
124 }, 'Options (incl. expires)' );
127 QUnit.test( 'get( key ) - no values', 6, function ( assert ) {
130 mw.cookie.get( 'foo' );
132 key = $.cookie.lastCall.args[ 0 ];
133 assert.strictEqual( key, 'mywikifoo', 'Default prefix' );
135 mw.cookie.get( 'foo', undefined );
136 key = $.cookie.lastCall.args[ 0 ];
137 assert.strictEqual( key, 'mywikifoo', 'Use default prefix for undefined' );
139 mw.cookie.get( 'foo', null );
140 key = $.cookie.lastCall.args[ 0 ];
141 assert.strictEqual( key, 'mywikifoo', 'Use default prefix for null' );
143 mw.cookie.get( 'foo', '' );
144 key = $.cookie.lastCall.args[ 0 ];
145 assert.strictEqual( key, 'foo', 'Don\'t use default prefix for empty string' );
147 value = mw.cookie.get( 'foo' );
148 assert.strictEqual( value, null, 'Return null by default' );
150 value = mw.cookie.get( 'foo', null, 'bar' );
151 assert.strictEqual( value, 'bar', 'Custom default value' );
154 QUnit.test( 'get( key ) - with value', 1, function ( assert ) {
157 $.cookie.returns( 'bar' );
159 value = mw.cookie.get( 'foo' );
160 assert.strictEqual( value, 'bar', 'Return value of cookie' );
163 QUnit.test( 'get( key, prefix )', 1, function ( assert ) {
166 mw.cookie.get( 'foo', 'bar' );
168 key = $.cookie.lastCall.args[ 0 ];
169 assert.strictEqual( key, 'barfoo' );
172 } ( mediaWiki, jQuery ) );