Phuedx has uploaded a new change for review.

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

Change subject: WIP
......................................................................

WIP

Change-Id: Icacf84d32d24d0386e0db9775032a9dd6be6adc4
---
M Popups.hooks.php
M extension.json
M resources/ext.popups.core.js
A resources/ext.popups.experiment.js
A resources/ext.popups.index.js
A tests/qunit/ext.popups.experiment.test.js
6 files changed, 161 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Popups 
refs/changes/94/288594/1

diff --git a/Popups.hooks.php b/Popups.hooks.php
index 8663a90..aaad8a8 100644
--- a/Popups.hooks.php
+++ b/Popups.hooks.php
@@ -205,8 +205,12 @@
                                'tests/qunit/ext.popups.core.test.js',
                                'tests/qunit/ext.popups.logger.test.js',
                                'tests/qunit/ext.popups.settings.test.js',
+                               'tests/qunit/ext.popups.experiment.test.js',
                        ),
-                       'dependencies' => array( 'ext.popups.desktop' ),
+                       'dependencies' => array(
+                               'ext.popups.desktop',
+                               'ext.popups.experiment'
+                       ),
                        'localBasePath' => __DIR__,
                        'remoteExtPath' => 'Popups',
                );
diff --git a/extension.json b/extension.json
index 8706e7c..34b77f0 100644
--- a/extension.json
+++ b/extension.json
@@ -58,6 +58,7 @@
                                "resources/ext.popups.core.js"
                        ],
                        "dependencies": [
+                               "ext.popups.index",
                                "mediawiki.api",
                                "mediawiki.Title",
                                "mediawiki.Uri",
@@ -90,6 +91,23 @@
                        "dependencies": [
                                "ext.popups.core"
                        ]
+               },
+               "ext.popups.index": {
+                       "scripts": [
+                               "resources/ext.popups.index.js"
+                       ]
+               },
+               "ext.popups.experiment": {
+                       "scripts": [
+                               "resources/ext.popups.experiment.js"
+                       ],
+                       "dependencies": [
+                               "ext.popups.index",
+                               "mediawiki.user",
+                               "mediawiki.experiments"
+                       ],
+                       "targets": [ "desktop" ],
+                       "position": "top"
                }
        },
        "ResourceFileModulePaths": {
diff --git a/resources/ext.popups.core.js b/resources/ext.popups.core.js
index 46487cf..0bc9b55 100644
--- a/resources/ext.popups.core.js
+++ b/resources/ext.popups.core.js
@@ -2,12 +2,6 @@
        'use strict';
 
        /**
-        * @class mw.popups
-        * @singleton
-        */
-       mw.popups = {};
-
-       /**
         * The API object used for all this extension's requests
         * @property {Object} api
         */
diff --git a/resources/ext.popups.experiment.js 
b/resources/ext.popups.experiment.js
new file mode 100644
index 0000000..fb16e89
--- /dev/null
+++ b/resources/ext.popups.experiment.js
@@ -0,0 +1,52 @@
+( function ( mw ) {
+
+       /**
+        * @ignore
+        */
+       function getToken() {
+               var KEY = 'PopupsExperimentID',
+                       id = mw.storage.get( KEY );
+
+               if ( !id ) {
+                       id = mw.user.generateRandomSessionId();
+
+                       mw.storage.set( KEY, id );
+               }
+
+               return id;
+       }
+
+
+       /**
+        * @class
+   * @singleton
+        */
+       mw.popups.experiment = {};
+
+       /**
+        * @return {jQuery.Promise}
+        */
+       mw.popups.experiment.isUserInCondition = function isUserInCondition() {
+               var deferred = $.Deferred(),
+                               config = mw.config.get( 
'wgPopupsExperimentConfig' ),
+                               result;
+
+               if (
+                       // Users with the beta feature enabled are excluded 
from the experiment.
+                       mw.config.get( 
'wgPopupsExperimentIsBetaFeatureEnabled', false )
+               ) {
+                       deferred.resolve( false );
+               } else {
+                       mw.requestIdleCallback( function () {
+                               // FIXME: mw.experiments should expose the 
CONTROL_BUCKET constant, e.g.
+                               // `mw.experiments.CONTROL_BUCKET`.
+                               result = mw.experiments.getBucket( config, 
getToken() ) !== 'control';
+
+                               deferred.resolve( result );
+                       });
+               }
+
+               return deferred.promise();
+       };
+
+} ( mediaWiki ) );
diff --git a/resources/ext.popups.index.js b/resources/ext.popups.index.js
new file mode 100644
index 0000000..9e39095
--- /dev/null
+++ b/resources/ext.popups.index.js
@@ -0,0 +1,9 @@
+( function ( mw ) {
+       
+       /**
+        * @class mw.popups
+        * @singleton
+        */
+       mw.popups = {};
+
+} ( mediaWiki ) );
diff --git a/tests/qunit/ext.popups.experiment.test.js 
b/tests/qunit/ext.popups.experiment.test.js
new file mode 100644
index 0000000..0863f96
--- /dev/null
+++ b/tests/qunit/ext.popups.experiment.test.js
@@ -0,0 +1,77 @@
+( function ( mw ) {
+
+       QUnit.module( 'ext.popups.experiment', QUnit.newMwEnvironment( {
+               config: {
+                       wgPopupsExperimentConfig: {
+                               name: 'Popups A/B Test - May 2016',
+                               enabled: true,
+                               buckets: {
+                                       control: 0.5,
+                                       A: 0.5
+                               }
+                       }
+               },
+               teardown: function () {
+                       mw.storage.remove( 'PopupsExperimentID' );
+               }
+       } ) );
+
+       QUnit.test( '#isUserInCondition: user has beta feature enabled', 
function ( assert ) {
+               var done = assert.async();
+
+               mw.config.set( 'wgPopupsExperimentIsBetaFeatureEnabled', true );
+
+               mw.popups.experiment.isUserInCondition().then( function ( 
result ) {
+                       assert.strictEqual(
+                               result,
+                               false,
+                               'If the user has the beta feature enabled, then 
they aren\'t in the condition.'
+                       );
+
+                       done();
+               } );
+       } );
+
+       QUnit.test( '#isUserInCondition', function ( assert ) {
+               var getBucketSpy = this.sandbox.stub( mw.experiments, 
'getBucket' ).returns( 'A' ),
+                       config = mw.config.get( 'wgPopupsExperimentConfig' ),
+                       done = assert.async();
+
+               mw.popups.experiment.isUserInCondition().then( function ( 
result ) {
+                       var fistCallArgs = getBucketSpy.firstCall.args;
+
+                       assert.deepEqual(
+                               fistCallArgs[0],
+                               config,
+                               'The Popups experiment config is used when 
bucketing the user.'
+                       );
+
+                       assert.strictEqual(
+                               result,
+                               true,
+                               'If the user isn\'t in the control bucket, then 
they are in the condition.'
+                       );
+
+                       done();
+               } );
+       } );
+
+       QUnit.test( '#isUserInCondition: token is persisted', function ( assert 
) {
+               var token = '1234567890',
+                       setSpy = this.sandbox.spy( mw.storage, 'set' ),
+                       done = assert.async();
+
+               this.sandbox.stub( mw.user, 'generateRandomSessionId' 
).returns( token );
+
+               mw.popups.experiment.isUserInCondition().then( function () {
+                       assert.deepEqual(
+                               setSpy.firstCall.args[1],
+                               token,
+                               'The token is persisted transparently.'
+                       );
+
+                       done();
+               } );
+       } );
+
+} ( mediaWiki ) );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Icacf84d32d24d0386e0db9775032a9dd6be6adc4
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Popups
Gerrit-Branch: master
Gerrit-Owner: Phuedx <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to