Phuedx has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/120806

Change subject: [WIP] Add the mw.cookie module
......................................................................

[WIP] Add the mw.cookie module

Wrap $.cookie so that setting a cookie is syntactically and functionally
equivalent to WebResponse#setcookie.

Bug: 61979
Change-Id: I217ef258aecf1acd335e2cea56ae08b22541c7d4
---
M resources/Resources.php
A resources/mediawiki.cookie/mediawiki.cookie.js
M tests/qunit/QUnitTestResources.php
A tests/qunit/suites/resources/mediawiki.cookie/mediawiki.cookie.test.js
4 files changed, 153 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/06/120806/1

diff --git a/resources/Resources.php b/resources/Resources.php
index 49b8fda..e3eb669 100644
--- a/resources/Resources.php
+++ b/resources/Resources.php
@@ -1392,4 +1392,11 @@
                ),
                'targets' => array( 'desktop', 'mobile' ),
        ),
+       'mediawiki.cookie' => array(
+               'scripts' => 'resources/mediawiki.cookie/mediawiki.cookie.js',
+               'dependencies' => array(
+                       'jquery',
+                       'mediawiki',
+               ),
+       ),
 );
diff --git a/resources/mediawiki.cookie/mediawiki.cookie.js 
b/resources/mediawiki.cookie/mediawiki.cookie.js
new file mode 100644
index 0000000..32de018
--- /dev/null
+++ b/resources/mediawiki.cookie/mediawiki.cookie.js
@@ -0,0 +1,38 @@
+( function( mw, $ ) {
+
+       'use strict';
+
+       mw.cookie = {
+               set: function( key, value, expire, options ) {
+                       var config = mw.config.get( [ 'wgCookiePrefix', 
'wgCookieDomain',
+                               'wgCookiePath', 'wgCookieSecure', 
'wgCookieExpiration' ] ),
+                               defaultOptions = {
+                                       prefix: config.wgCookiePrefix,
+                                       domain: config.wgCookieDomain,
+                                       path: config.wgCookiePath,
+                                       secure: config.wgCookieSecure
+                               },
+                               seconds;
+
+                       options = options || {};
+
+                       if ( expire === undefined || expire === null ) {
+                               expire = 0;
+                       } else if ( expire === 0 && config.wgCookieExpiration ) 
{
+                               expire = config.wgCookieExpiration;
+                       }
+
+                       if ( expire > 0 ) {
+                               seconds = expire;
+                               expire = new Date();
+                               expire.setTime( +expire + seconds );
+                       }
+
+                       options.expires = expire;
+                       options = $.extend( {}, defaultOptions, options );
+
+                       $.cookie( key, value, options );
+               }
+       };
+
+} )( mediaWiki, jQuery );
diff --git a/tests/qunit/QUnitTestResources.php 
b/tests/qunit/QUnitTestResources.php
index 31470e8..844217a 100644
--- a/tests/qunit/QUnitTestResources.php
+++ b/tests/qunit/QUnitTestResources.php
@@ -75,6 +75,7 @@
                        
'tests/qunit/suites/resources/mediawiki.special/mediawiki.special.recentchanges.test.js',
                        
'tests/qunit/suites/resources/mediawiki/mediawiki.language.test.js',
                        
'tests/qunit/suites/resources/mediawiki/mediawiki.cldr.test.js',
+                       
'tests/qunit/suites/resources/mediawiki.cookie/mediawiki.cookie.test.js',
                ),
                'dependencies' => array(
                        'jquery.autoEllipsis',
@@ -106,6 +107,7 @@
                        'mediawiki.special.recentchanges',
                        'mediawiki.language',
                        'mediawiki.cldr',
+                       'mediawiki.cookie',
                        'test.mediawiki.qunit.testrunner',
                ),
        )
diff --git 
a/tests/qunit/suites/resources/mediawiki.cookie/mediawiki.cookie.test.js 
b/tests/qunit/suites/resources/mediawiki.cookie/mediawiki.cookie.test.js
new file mode 100644
index 0000000..5c0709b
--- /dev/null
+++ b/tests/qunit/suites/resources/mediawiki.cookie/mediawiki.cookie.test.js
@@ -0,0 +1,106 @@
+/* global: sinon */
+( function( QUnit, sinon, mw, $ ) {
+
+       var config = {
+                       wgCookiePrefix: 'prefix',
+                       wgCookieDomain: 'domain',
+                       wgCookiePath: 'path',
+                       wgCookieSecure: true,
+                       wgCookieExpiration: 5678
+               },
+               lifecycle = QUnit.newMwEnvironment( {
+                       setup: function() {
+                               sinon.spy( $, 'cookie' );
+                       },
+                       teardown: function() {
+                               $.cookie.restore();
+                       },
+                       config: config
+       } );
+
+       QUnit.module( 'mediawiki.cookie', lifecycle );
+
+       QUnit.test( 'should set the cookie with options from the config by 
default', 6, function( assert ) {
+               var args, options;
+
+               mw.cookie.set( 'foo', 'bar' );
+
+               args = $.cookie.getCall( 0 ).args;
+               options = args[ 2 ];
+               assert.equal( args[ 0 ], 'foo' );
+               assert.equal( args[ 1 ], 'bar' );
+               assert.equal( options.prefix, 'prefix' );
+               assert.equal( options.domain, 'domain' );
+               assert.equal( options.path, 'path' );
+               assert.equal( options.secure, true );
+       } );
+
+       QUnit.test( 'should set the cookie with the provided options', 4, 
function( assert ) {
+               var options;
+
+               mw.cookie.set( 'foo', 'bar', null, {
+                       prefix: 'myPrefix',
+                       domain: 'myDomain',
+                       path: 'myPath',
+                       secure: false
+               } );
+
+               options = $.cookie.getCall( 0 ).args[ 2 ];
+               assert.equal( options.prefix, 'myPrefix' );
+               assert.equal( options.domain, 'myDomain' );
+               assert.equal( options.path, 'myPath' );
+               assert.equal( options.secure, false );
+       } );
+
+       QUnit.test( 'should set the cookie to expire immediately when expire is 
undefined', 1, function( assert ) {
+               var options;
+
+               mw.cookie.set( 'foo', 'bar' );
+
+               options = $.cookie.getCall( 0 ).args[ 2 ];
+               assert.equal( options.expires, 0 );
+       } );
+
+       QUnit.test( 'should set the cookie to expire immediately when expire is 
null', 1, function( assert ) {
+               var options;
+
+               mw.cookie.set( 'foo', 'bar', null );
+
+               options = $.cookie.getCall( 0 ).args[ 2 ];
+               assert.equal( options.expires, 0 );
+       } );
+
+       QUnit.test( 'should set the cookie to expire when expire isn\'t null', 
1, function( assert ) {
+               var options;
+
+               mw.cookie.set( 'foo', 'bar', 1234 );
+
+               options = $.cookie.getCall( 0 ).args[ 2 ];
+               assert.equal( $.type( options.expires ), 'date' );
+               // TODO (phuedx, 2014-03-21) Assert that options.expires is
+               // now-ish plus 1234 seconds.
+       } );
+
+       QUnit.test( 'should set the cookie to expire when expire is 0 and 
wgCookieExpiration isn\'t null', 1, function( assert ) {
+               var options;
+
+               mw.cookie.set( 'foo', 'bar', 0 );
+
+               options = $.cookie.getCall( 0 ).args[ 2 ];
+               assert.equal( $.type( options.expires ), 'date' );
+               // TODO (phuedx, 2014-03-21) Assert that options.expires is
+               // now-ish plus 5678 seconds.
+       } );
+
+       QUnit.test( 'shouldn\'t set the cookie to expire immediately when 
expire is 0 and wgCookieExpiration is null', 1, function( assert ) {
+               var options;
+
+               mw.config.set( 'wgCookieExpiration', null );
+
+               mw.cookie.set( 'foo', 'bar', 0 );
+
+               options = $.cookie.getCall( 0 ).args[ 2 ];
+               assert.equal( options.expires, 0 );
+       } );
+
+} )( QUnit, sinon, mediaWiki, jQuery );

-- 
To view, visit https://gerrit.wikimedia.org/r/120806
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I217ef258aecf1acd335e2cea56ae08b22541c7d4
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Phuedx <g...@samsmith.io>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to