Phuedx has uploaded a new change for review. https://gerrit.wikimedia.org/r/325286
Change subject: Include user's preview count in BOOT action ...................................................................... Include user's preview count in BOOT action Action changes: * Include user's preview count in the user property of the action. Changes: * Extract mw.popups.UserSettings#getPreviewCount and #setPreviewCount and associated tests from the ext.popups.core module. Bug: T152223 Change-Id: I6b7afef31311be8fede685deb536f577845cb9cf --- M resources/ext.popups/actions.js M resources/ext.popups/boot.js M resources/ext.popups/counts.js M resources/ext.popups/userSettings.js M tests/qunit/ext.popups/actions.test.js M tests/qunit/ext.popups/counts.test.js M tests/qunit/ext.popups/reducers.eventLogging.test.js M tests/qunit/ext.popups/userSettings.test.js 8 files changed, 195 insertions(+), 42 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Popups refs/changes/86/325286/1 diff --git a/resources/ext.popups/actions.js b/resources/ext.popups/actions.js index 450e023..838f901 100644 --- a/resources/ext.popups/actions.js +++ b/resources/ext.popups/actions.js @@ -38,12 +38,20 @@ * * @param {Function} isUserInCondition See `mw.popups.createExperiment` * @param {mw.user} user + * @param {ext.popups.UserSettings} userSettings * @param {Function} generateToken * @param {mw.Map} config The config of the MediaWiki client-side application, * i.e. `mw.config` */ - actions.boot = function ( isUserInCondition, user, generateToken, config ) { - var editCount = config.get( 'wgUserEditCount' ); + actions.boot = function ( + isUserInCondition, + user, + userSettings, + generateToken, + config + ) { + var editCount = config.get( 'wgUserEditCount' ), + previewCount = userSettings.getPreviewCount(); return { type: types.BOOT, @@ -57,7 +65,8 @@ user: { isInCondition: isUserInCondition(), isAnon: user.isAnon(), - editCount: editCount + editCount: editCount, + previewCount: previewCount } }; }; diff --git a/resources/ext.popups/boot.js b/resources/ext.popups/boot.js index a2d9208..05c077e 100644 --- a/resources/ext.popups/boot.js +++ b/resources/ext.popups/boot.js @@ -10,23 +10,6 @@ ]; /** - * Creates an experiment with sensible values for the depenencies. - * - * See `mw.popups.createExperiment`. - * - * @return {Function} - */ - function isUserInCondition() { - var userSettings = mw.popups.createUserSettings( mw.storage, mw.user ); - - return mw.popups.createExperiment( - mw.config, - mw.user, - userSettings - ); - } - - /** * Creates a gateway with sensible values for the dependencies. * * See `mw.popups.createGateway`. @@ -80,8 +63,14 @@ var compose = Redux.compose, store, actions, + + // So-called "services" generateToken = mw.user.generateRandomSessionId, - gateway = createGateway(); + gateway = createGateway(), + userSettings = mw.popups.createUserSettings( mw.storage, mw.user ), + isUserInCondition; + + isUserInCondition = mw.popups.createExperiment( mw.config, mw.user, userSettings ); // If debug mode is enabled, then enable Redux DevTools. if ( mw.config.get( 'debug' ) === true ) { @@ -98,8 +87,9 @@ registerChangeListeners( store, actions ); actions.boot( - isUserInCondition(), + isUserInCondition, mw.user, + userSettings, generateToken, mw.config ); diff --git a/resources/ext.popups/counts.js b/resources/ext.popups/counts.js index 47fed15..0c19281 100644 --- a/resources/ext.popups/counts.js +++ b/resources/ext.popups/counts.js @@ -31,4 +31,38 @@ return bucket + ' edits'; }; + /** + * Return count bucket for the number of previews a user has seen. + * + * If local storage isn't available - because the user has disabled it + * or the browser doesn't support it - then then "unknown" is returned. + * + * The buckets are defined as part of + * [the Popups schema](https://meta.wikimedia.org/wiki/Schema:Popups). + * + * Extracted from `mw.popups.getPreviewCountBucket`. + * + * @param {Number} count + * @return {String} + */ + mw.popups.counts.getPreviewCountBucket = function ( count ) { + var bucket; + + if ( count === -1 ) { + return 'unknown'; + } + + if ( count === 0 ) { + bucket = '0'; + } else if ( count >= 1 && count <= 4 ) { + bucket = '1-4'; + } else if ( count >= 5 && count <= 20 ) { + bucket = '5-20'; + } else if ( count >= 21 ) { + bucket = '21+'; + } + + return bucket + ' previews'; + }; + }( mediaWiki ) ); diff --git a/resources/ext.popups/userSettings.js b/resources/ext.popups/userSettings.js index ca46b94..47c4aea 100644 --- a/resources/ext.popups/userSettings.js +++ b/resources/ext.popups/userSettings.js @@ -1,10 +1,11 @@ ( function ( mw ) { /** - * @typedef {Object} mw.popups.UserSettings + * @typedef {Object} ext.popups.UserSettings */ - var IS_ENABLED_KEY = 'mwe-popups-enabled'; + var IS_ENABLED_KEY = 'mwe-popups-enabled', + PREVIEW_COUNT_KEY = 'ext.popups.core.previewCount'; /** * Given the global state of the application, creates an object whose methods @@ -13,7 +14,7 @@ * @param {mw.storage} storage The `mw.storage` singleton instance * @param {mw.user} user The `mw.user` singleton instance * - * @return {mw.popups.UserSettings} + * @return {ext.popups.UserSettings} */ mw.popups.createUserSettings = function ( storage, user ) { return { @@ -69,6 +70,34 @@ } return id; + }, + + /** + * Gets the number of Page Previews that the user has seen. + * + * If the storage isn't available, then -1 is returned. + * + * @return {Number} + */ + getPreviewCount: function () { + var result = storage.get( PREVIEW_COUNT_KEY ); + + if ( result === false ) { + return -1; + } else if ( result === null ) { + return 0; + } + + return parseInt( result, 10 ); + }, + + /** + * Sets the number of Page Previews that the user has seen. + * + * @param {Number} count + */ + setPreviewCount: function ( count ) { + storage.set( PREVIEW_COUNT_KEY, count.toString() ); } }; }; diff --git a/tests/qunit/ext.popups/actions.test.js b/tests/qunit/ext.popups/actions.test.js index b603764..ff4cc1f 100644 --- a/tests/qunit/ext.popups/actions.test.js +++ b/tests/qunit/ext.popups/actions.test.js @@ -10,7 +10,9 @@ return '9876543210'; }, config = new mw.Map(), - stubUser = mw.popups.tests.stubs.createStubUser( /* isAnon = */ true ); + stubUser = mw.popups.tests.stubs.createStubUser( /* isAnon = */ true ), + stubUserSettings, + action; config.set( { wgTitle: 'Foo', @@ -19,10 +21,24 @@ wgUserEditCount: 3 } ); + stubUserSettings = { + getPreviewCount: function () { + return 22; + } + }; + assert.expect( 1 ); + action = mw.popups.actions.boot( + isUserInCondition, + stubUser, + stubUserSettings, + generateToken, + config + ); + assert.deepEqual( - mw.popups.actions.boot( isUserInCondition, stubUser, generateToken, config ), + action, { type: 'BOOT', sessionToken: '0123456789', @@ -35,7 +51,8 @@ user: { isInCondition: false, isAnon: true, - editCount: 3 + editCount: 3, + previewCount: 22 } } ); diff --git a/tests/qunit/ext.popups/counts.test.js b/tests/qunit/ext.popups/counts.test.js index 642ccab..5001ec8 100644 --- a/tests/qunit/ext.popups/counts.test.js +++ b/tests/qunit/ext.popups/counts.test.js @@ -34,4 +34,33 @@ } } ); + QUnit.test( '#getPreviewCountBucket', function ( assert ) { + var i, count, bucket, + cases = [ + [ -1, 'unknown' ], + [ 0, '0 previews' ], + [ 1, '1-4 previews' ], + [ 2, '1-4 previews' ], + [ 4, '1-4 previews' ], + [ 5, '5-20 previews' ], + [ 10, '5-20 previews' ], + [ 20, '5-20 previews' ], + [ 21, '21+ previews' ], + [ 100, '21+ previews' ], + [ 1000, '21+ previews' ] + ]; + + QUnit.expect( cases.length ); + + for ( i = 0; i < cases.length; i++ ) { + count = cases[ i ][ 0 ]; + bucket = mw.popups.counts.getPreviewCountBucket( count ); + assert.equal( + bucket, + cases[ i ][ 1 ], + 'Preview count bucket is "' + bucket + '" when preview count is ' + count + '.' + ); + } + } ); + }( mediaWiki ) ); diff --git a/tests/qunit/ext.popups/reducers.eventLogging.test.js b/tests/qunit/ext.popups/reducers.eventLogging.test.js index 0678723..28b7047 100644 --- a/tests/qunit/ext.popups/reducers.eventLogging.test.js +++ b/tests/qunit/ext.popups/reducers.eventLogging.test.js @@ -12,20 +12,26 @@ QUnit.test( 'BOOT', function ( assert ) { var action = { - type: 'BOOT', - sessionToken: '0123456789', - pageToken: '9876543210', - page: { - title: 'Foo', - namespaceID: 1, - id: 2 + type: 'BOOT', + sessionToken: '0123456789', + pageToken: '9876543210', + page: { + title: 'Foo', + namespaceID: 1, + id: 2 + }, + user: { + isInCondition: true, + isAnon: false, + editCount: 11, + previewCount: 22 + } }, - user: { - isInCondition: true, - isAnon: false, - editCount: 11 - } - }; + expectedEditCountBucket, + expectedPreviewCountBucket; + + expectedEditCountBucket = counts.getEditCountBucket( action.user.editCount ); + expectedPreviewCountBucket = counts.getPreviewCountBucket( action.user.previewCount ); assert.deepEqual( mw.popups.reducers.eventLogging( this.initialState, action ), @@ -38,7 +44,9 @@ popupEnabled: action.isUserInCondition, pageToken: action.pageToken, sessionToken: action.sessionToken, - editCountBucket: counts.getEditCountBucket( action.user.editCount ) + editCountBucket: expectedEditCountBucket, + previewCount: action.user.previewCount, + previewCountBucket: expectedPreviewCountBucket }, queue: [ { diff --git a/tests/qunit/ext.popups/userSettings.test.js b/tests/qunit/ext.popups/userSettings.test.js index 7ef0de8..0e2eb77 100644 --- a/tests/qunit/ext.popups/userSettings.test.js +++ b/tests/qunit/ext.popups/userSettings.test.js @@ -60,4 +60,41 @@ assert.equal( this.userSettings.getToken(), this.userSettings.getToken() ); } ); + QUnit.test( '#getPreviewCount should return the count as a number', function ( assert ) { + assert.expect( 3 ); + + assert.strictEqual( + this.userSettings.getPreviewCount(), + 0, + '#getPreviewCount returns 0 when the storage is empty.' + ); + + // --- + + this.storage.set( 'ext.popups.core.previewCount', false ); + + assert.strictEqual( + this.userSettings.getPreviewCount(), + -1, + '#getPreviewCount returns -1 when the storage isn\'t available.' + ); + + // --- + + this.storage.set( 'ext.popups.core.previewCount', '111' ); + + assert.strictEqual( + this.userSettings.getPreviewCount(), + 111 + ); + } ); + + QUnit.test( '#setPreviewCount should store the count as a string', function ( assert ) { + assert.expect( 1 ); + + this.userSettings.setPreviewCount( 222 ); + + assert.strictEqual( this.storage.get( 'ext.popups.core.previewCount' ), '222' ); + } ); + }( mediaWiki ) ); -- To view, visit https://gerrit.wikimedia.org/r/325286 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I6b7afef31311be8fede685deb536f577845cb9cf Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Popups Gerrit-Branch: mpga Gerrit-Owner: Phuedx <samsm...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits