Kaldari has submitted this change and it was merged.

Change subject: WikiGrok: use local WikiGrok campaign suggestions
......................................................................


WikiGrok: use local WikiGrok campaign suggestions

* Works for author, actor, and album questions;
* Remove tools lab related stuff.

Change-Id: I2bf8c94703f7e5d49c3a60cf8885f7a1194e7726
---
M includes/Resources.php
A javascripts/modules/wikigrok/WikiGrokCampaigns.js
M javascripts/modules/wikigrok/WikiGrokDialog.js
M javascripts/modules/wikigrok/WikiGrokDialogB.js
D javascripts/modules/wikigrok/WikiGrokSuggestionApi.js
M tests/qunit/modules/wikigrok/test_WikiGrokDialog.js
M tests/qunit/modules/wikigrok/test_WikiGrokDialogB.js
A tests/qunit/modules/wikigrok/test_wikiGrokCampaigns.js
8 files changed, 274 insertions(+), 265 deletions(-)

Approvals:
  Kaldari: Verified; Looks good to me, approved



diff --git a/includes/Resources.php b/includes/Resources.php
index c4208c7..5dc50b4 100644
--- a/includes/Resources.php
+++ b/includes/Resources.php
@@ -940,8 +940,8 @@
                ),
                'scripts' => array(
                        'javascripts/modules/wikigrok/WikiDataApi.js',
-                       'javascripts/modules/wikigrok/WikiGrokSuggestionApi.js',
                        'javascripts/modules/wikigrok/WikiGrokResponseApi.js',
+                       'javascripts/modules/wikigrok/wikiGrokCampaigns.js',
                ),
        ),
 
diff --git a/javascripts/modules/wikigrok/WikiGrokCampaigns.js 
b/javascripts/modules/wikigrok/WikiGrokCampaigns.js
new file mode 100644
index 0000000..190dae7
--- /dev/null
+++ b/javascripts/modules/wikigrok/WikiGrokCampaigns.js
@@ -0,0 +1,68 @@
+( function ( M, $ ) {
+       /**
+        * Gets campaigns, claims, and labels from mw.config
+        * @class wikiGrokCampaigns
+        */
+       var wikiGrokCampaigns = {
+               /**
+                * Randomly pick a campaign for the current page
+                * @returns {Object|null} Object with campaign data. Includes 
these properties:
+                *     name: Name of the campaign chosen, e.g. 'album'
+                *     property: Wikidata ID for the relevant property, e.g. 
'P31'
+                *     questions: object with item IDs and labels for claim 
suggestions
+                *     suggestions: array of Wikidata item IDs corresponding to 
claim suggestions
+                *     randomClaimId: Wikidata item ID of a randomly chosen 
suggestion
+                */
+               getRandomCampaign: function () {
+                       var campaignName,
+                               campaigns = getCampaigns(),
+                               campaign = null;
+
+                       if ( campaigns ) {
+                               campaignName = getRandomProperty( campaigns );
+                               campaign = campaigns[campaignName];
+                               campaign.name = campaignName;
+                               campaign.randomClaimId = getRandomProperty( 
campaign.questions );
+
+                               // make suggestions
+                               // FIXME: Refactor dialog code to use questions 
param instead
+                               campaign.suggestions =  $.map( 
campaign.questions, function ( value, key ) {
+                                       return key;
+                               } );
+                       }
+                       return campaign;
+               }
+       };
+
+       /*
+        * Get WikiGrok campaigns that are present on the page
+        * This is a method rather than a variable because we need it in tests
+        * @returns {Object|null} campaigns
+        */
+       function getCampaigns() {
+               return mw.config.get( 'wgWikiGrokCampaigns' );
+       }
+
+       /*
+        * Randomly pick a property
+        * @param {Object} object
+        * @returns {String} object's property
+        */
+       function getRandomProperty( object ) {
+               var result,
+                       property,
+                       count = 0;
+
+               for ( property in object ) {
+                       if ( object.hasOwnProperty( property ) ) {
+                               if ( Math.random() <= 1 / ++count ) {
+                                       result = property;
+                               }
+                       }
+               }
+               return result;
+       }
+
+       M.define( 'modules/wikigrok/wikiGrokCampaigns', wikiGrokCampaigns );
+
+} ( mw.mobileFrontend, jQuery ) );
diff --git a/javascripts/modules/wikigrok/WikiGrokDialog.js 
b/javascripts/modules/wikigrok/WikiGrokDialog.js
index b082f29..6dbcd99 100644
--- a/javascripts/modules/wikigrok/WikiGrokDialog.js
+++ b/javascripts/modules/wikigrok/WikiGrokDialog.js
@@ -3,9 +3,9 @@
 
        var Panel = M.require( 'Panel' ),
                settings = M.require( 'settings' ),
-               WikiGrokSuggestionApi = M.require( 
'modules/wikigrok/WikiGrokSuggestionApi' ),
                WikiGrokResponseApi = M.require( 
'modules/wikigrok/WikiGrokResponseApi' ),
                WikiDataApi = M.require( 'modules/wikigrok/WikiDataApi' ),
+               wikiGrokCampaigns = M.require( 
'modules/wikigrok/wikiGrokCampaigns' ),
                schema = M.require( 'loggingSchemas/mobileWebWikiGrok' ),
                errorSchema = M.require( 
'loggingSchemas/mobileWebWikiGrokError' ),
                WikiGrokDialog,
@@ -50,13 +50,10 @@
                initialize: function ( options ) {
                        var self = this;
 
+                       options.campaign = 
wikiGrokCampaigns.getRandomCampaign();
                        // Remove any disambiguation parentheticals from the 
title.
                        options.name = options.title.replace( / \(.+\)$/, '' );
-                       this.apiWikiGrokSuggestion = new WikiGrokSuggestionApi( 
{
-                               itemId: options.itemId,
-                               subject: options.name,
-                               version: this.version
-                       } );
+
                        this.apiWikiGrokResponse = new WikiGrokResponseApi( {
                                itemId: options.itemId,
                                subject: options.name,
@@ -155,72 +152,59 @@
                },
 
                /**
-                * Creates a question with a yes, no and not sure answer
-                * Makes API request to Wikidata to retrieve labels.
+                * Creates a question with a yes, no and not sure answer.
+                * Makes API request to Wikidata to retrieve labels and uses 
campaigns for that.
                 * FIXME: No i18n
                 * @method
                 * @param {Object} options needed to render.
                 */
                askWikidataQuestion: function ( options ) {
                        var self = this,
-                               vowels = [ 'a', 'e', 'i', 'o', 'u' ],
-                               theCountries = [ 'United States', 'United 
Kingdom', 'Philippines',
-                                       'Marshall Islands', 'Central African 
Republic' ];
+                               vowels = [ 'a', 'e', 'i', 'o', 'u' ];
 
-                       if ( options.suggestions.length ) {
-                               // choose a suggestion category (dob, dod, 
occupation, or nationality) randomly
-                               options.suggestion = 
this.chooseRandomItemsFromArray( options.suggestions, 1 )[0];
-                               // pick a claim randomly
-                               options.claimId = 
this.chooseRandomItemsFromArray( options.suggestion.list, 1 )[0];
+                       options.claimId = options.campaign.randomClaimId;
 
-                               // Get the name of the claim from Wikidata.
-                               self.apiWikiData.getLabels( [ options.claimId ] 
).done( function ( labels ) {
-                                       options.claimLabel = labels[ 
options.claimId ];
-                                       if ( options.claimLabel ) {
-                                               // ask if it is a correct 
occupation for the person.
-                                               // FIXME: add support for DOB 
and DOD
-                                               if ( options.suggestion.name 
=== 'occupations' ) {
-                                                       // Hack for English 
prototype
-                                                       if ( $.inArray( 
options.claimLabel.charAt( 0 ), vowels ) === -1 ) {
-                                                               
options.contentMsg = 'Was ' + options.name + ' a ' + options.claimLabel + '?';
-                                                       } else {
-                                                               
options.contentMsg = 'Was ' + options.name + ' an ' + options.claimLabel + '?';
-                                                       }
-                                               } else if ( 
options.suggestion.name === 'nationality' ) {
-                                                       if ( $.inArray( 
options.claimLabel, theCountries ) === -1 ) {
-                                                               
options.contentMsg = 'Was ' + options.name + ' a citizen of ' + 
options.claimLabel + '?';
-                                                       } else {
-                                                               
options.contentMsg = 'Was ' + options.name + ' a citizen of the ' + 
options.claimLabel + '?';
-                                                       }
-                                               } else if ( 
options.suggestion.name === 'schools' ) {
-                                                       options.contentMsg = 
'Did ' + options.name + ' attend ' + options.claimLabel + '?';
+                       self.apiWikiData.getLabels( [ options.claimId ] ).done( 
function ( labels ) {
+                               options.claimLabel = labels[ options.claimId ];
+                               if ( options.claimLabel ) {
+                                       if ( options.campaign.name === 'author' 
) {
+                                               // Hack for English prototype
+                                               if ( $.inArray( 
options.claimLabel.charAt( 0 ), vowels ) === -1 ) {
+                                                       options.contentMsg = 
'Is ' + options.name + ' a ' + options.claimLabel + '?';
+                                               } else {
+                                                       options.contentMsg = 
'Is ' + options.name + ' an ' + options.claimLabel + '?';
                                                }
-
-                                               // Re-render with new content 
for 'Question' step
-                                               options.beginQuestions = true;
-                                               options.buttons = [
-                                                       {
-                                                               classes: 'yes 
inline mw-ui-button mw-ui-progressive',
-                                                               label: 'Yes'
-                                                       },
-                                                       {
-                                                               classes: 
'not-sure inline mw-ui-button',
-                                                               label: 'Not 
Sure'
-                                                       },
-                                                       {
-                                                               classes: 'no 
inline mw-ui-button mw-ui-progressive',
-                                                               label: 'No'
-                                                       }
-                                               ];
-                                               options.noticeMsg = 'All 
submissions are <a class="wg-notice-link" href="#/wikigrok/about">released 
freely</a>';
-                                               self.render( options );
-                                       } else {
-                                               self.showError( options, 'There 
was an error retrieving tag labels.' );
+                                       } else if ( options.campaign.name === 
'actor' ) {
+                                               options.contentMsg = 'Is ' + 
options.name + ' a ' + options.claimLabel + '?';
+                                       } else if ( options.campaign.name === 
'album' ) {
+                                               options.contentMsg = 'Is this a 
' + options.claimLabel + '?';
                                        }
-                               } ).fail( function () {
-                                       self.logError( 
'no-impression-cannot-fetch-labels' );
-                               } );
-                       }
+
+                                       // Re-render with new content for 
'Question' step
+                                       options.beginQuestions = true;
+                                       options.buttons = [
+                                               {
+                                                       classes: 'yes inline 
mw-ui-button mw-ui-progressive',
+                                                       label: 'Yes'
+                                               },
+                                               {
+                                                       classes: 'not-sure 
inline mw-ui-button',
+                                                       label: 'Not Sure'
+                                               },
+                                               {
+                                                       classes: 'no inline 
mw-ui-button mw-ui-progressive',
+                                                       label: 'No'
+                                               }
+                                       ];
+                                       options.noticeMsg = 'All submissions 
are <a class="wg-notice-link" ' +
+                                               
'href="#/wikigrok/about">released freely</a>';
+                                       self.render( options );
+                               } else {
+                                       self.showError( options, 'There was an 
error retrieving tag labels.' );
+                               }
+                       } ).fail( function () {
+                               self.logError( 
'no-impression-cannot-fetch-labels' );
+                       } );
                },
 
                showError: function ( options, errorMsg ) {
@@ -242,16 +226,16 @@
                                        valueid: options.claimId,
                                        value: options.claimLabel,
                                        correct: options.claimIsCorrect,
-                                       propid: options.suggestion.id
+                                       propid: options.campaign.property
                                };
 
-                       // FIXME: add support for DOB and DOD
-                       if ( options.suggestion.name === 'occupations' ) {
+                       if (
+                               options.campaign.name === 'author' ||
+                               options.campaign.name === 'actor'
+                       ) {
                                claim.prop = 'occupation';
-                       } else if ( options.suggestion.name === 'nationality' ) 
{
-                               claim.prop = 'nationality';
-                       } else if ( options.suggestion.name === 'schools' ) {
-                               claim.prop = 'alma mater';
+                       } else if ( options.campaign.name === 'album' ) {
+                               claim.prop = 'instance of';
                        }
 
                        this.apiWikiGrokResponse.recordClaims( [ claim ] 
).always( function () {
@@ -428,38 +412,13 @@
                        this.reveal( options );
                },
 
+               /**
+                * Show WikiGrok dialog
+                * @param {Object} options
+                */
                reveal: function ( options ) {
-                       var self = this;
-
-                       // fetch suggestions only if we didn't try loading 
suggestions before
-                       if ( options.suggestions ) {
-                               self.show();
-                       } else {
-                               options.suggestions = [];
-                               self.apiWikiData.getClaims().done( function ( 
claims ) {
-                                       if ( claims.isHuman ) {
-                                               
self.apiWikiGrokSuggestion.getSuggestions().fail( function () {
-                                                       self.logError( 
'no-impression-cannot-fetch-suggestions' );
-                                               } ).done( function ( 
suggestions ) {
-                                                       // FIXME: add support 
for DOB and DOD
-                                                       if ( 
suggestions.occupations && suggestions.occupations.list.length ) {
-                                                               
options.suggestions.push( suggestions.occupations );
-                                                       }
-                                                       if ( 
suggestions.nationalities && suggestions.nationalities.list.length ) {
-                                                               
options.suggestions.push( suggestions.nationalities );
-                                                       }
-                                                       if ( 
suggestions.schools && suggestions.schools.list.length ) {
-                                                               
options.suggestions.push( suggestions.schools );
-                                                       }
-                                                       if ( 
options.suggestions.length ) {
-                                                               self.show();
-                                                       } else {
-                                                               // FIXME: 
remove this before deploying to stable
-                                                               self.logError( 
'no-impression-not-enough-suggestions' );
-                                                       }
-                                               } );
-                                       }
-                               } );
+                       if ( options.campaign ) {
+                               this.show();
                        }
                }
        } );
diff --git a/javascripts/modules/wikigrok/WikiGrokDialogB.js 
b/javascripts/modules/wikigrok/WikiGrokDialogB.js
index 5d89802..7b10779 100644
--- a/javascripts/modules/wikigrok/WikiGrokDialogB.js
+++ b/javascripts/modules/wikigrok/WikiGrokDialogB.js
@@ -31,43 +31,22 @@
                 * Renders a set of buttons to the panel
                 * Shows panel to user when there are suggestions.
                 * @method
-                * @param {Array} suggestions as returned by 
WikiGrokApi.getSuggestions
                 */
-               _renderSuggestions: function ( suggestions ) {
-                       var
+               _renderSuggestions: function ( campaign ) {
+                       var suggestions,
                                self = this,
-                               allSuggestions = [],
-                               suggestionsList = [],
-                               // Maps item ids to a key in i18n file
-                               lookupProp = {},
                                i18n = {
-                                       dob: 'Born on',
-                                       dod: 'Died on',
-                                       nationalities: 'Home country',
-                                       occupations: 'Profession',
-                                       schools: 'School'
+                                       actor: 'Profession',
+                                       author: 'Profession',
+                                       album: 'Album type'
                                };
-
-                       $.each( suggestions, function ( type, data ) {
-                               var prop = {
-                                       type: type,
-                                       name: data.name,
-                                       id: data.id
-                               };
-
-                               allSuggestions = allSuggestions.concat( 
data.list );
-                               // Make sure it's easy to look up the property 
later.
-                               $.each( data.list, function ( i, itemId ) {
-                                       lookupProp[itemId] = prop;
-                               } );
-                       } );
 
                        // randomly pick 4 suggestions
-                       suggestionsList = self.chooseRandomItemsFromArray( 
allSuggestions, 4 );
+                       suggestions = self.chooseRandomItemsFromArray( 
campaign.suggestions, 4 );
 
                        // Now work out the labels if we have some suggestions
-                       if ( suggestionsList.length ) {
-                               self.apiWikiData.getLabels( suggestionsList 
).done( function ( labels ) {
+                       if ( suggestions.length ) {
+                               self.apiWikiData.getLabels( suggestions ).done( 
function ( labels ) {
                                        var $next = self.$( '.footer .next' ),
                                                $none = self.$( '.footer .none' 
);
 
@@ -80,7 +59,6 @@
                                        self.$( '.tags' ).show();
                                        $.each( labels, function ( itemId, 
label ) {
                                                var $tag,
-                                                       prop = 
lookupProp[itemId],
                                                        id = 'tag-' + itemId;
 
                                                if ( label ) {
@@ -101,14 +79,14 @@
 
                                                        // FIXME: Use a 
template for this magic.
                                                        $tag.attr( 'id', id )
-                                                               .data( 
'propName', prop.name )
-                                                               .data( 
'propId', prop.id )
+                                                               .data( 
'propName', campaign.name )
+                                                               .data( 
'propId', campaign.property )
                                                                .data( 
'itemId', itemId )
                                                                .data( 
'readable', label );
 
                                                        // Add the property 
label
                                                        $( '<label>' )
-                                                               .text( 
i18n[prop.type] ).appendTo( $tag );
+                                                               .text( 
i18n[campaign.name] ).appendTo( $tag );
 
                                                        // Add the value label
                                                        $( '<label>' )
@@ -142,7 +120,7 @@
                        self.$( '.wg-content' ).text( 'Select tags that 
correctly describe ' + options.title );
                        self.$( '.footer' ).show();
 
-                       self._renderSuggestions( options.suggestions );
+                       self._renderSuggestions( options.campaign );
 
                        this.$save = this.$( '.save' );
                        this.$save.on( 'click', function () {
@@ -208,28 +186,13 @@
                        }
                },
 
+               /**
+                * @inheritdoc
+                */
                reveal: function ( options ) {
-                       var self = this;
-
-                       options.suggestions = {};
-                       self.apiWikiData.getClaims().done( function ( claims ) {
-                               if ( claims.isHuman ) {
-                                       
self.apiWikiGrokSuggestion.getSuggestions().done( function ( suggestions ) {
-                                               if ( ( suggestions.occupations 
&& suggestions.occupations.list.length ) ||
-                                                       ( 
suggestions.nationalities && suggestions.nationalities.list.length ) ||
-                                                       ( suggestions.schools 
&& suggestions.schools.list.length )
-                                               ) {
-                                                       options.suggestions = 
suggestions;
-                                                       self.show();
-                                               } else {
-                                                       // FIXME: remove this 
before deploying to stable
-                                                       self.logError( 
'no-impression-not-enough-suggestions' );
-                                               }
-                                       } ).fail( function () {
-                                               self.logError( 
'no-impression-cannot-fetch-suggestions' );
-                                       } );
-                               }
-                       } );
+                       if ( options.campaign ) {
+                               this.show();
+                       }
                }
        } );
 
diff --git a/javascripts/modules/wikigrok/WikiGrokSuggestionApi.js 
b/javascripts/modules/wikigrok/WikiGrokSuggestionApi.js
deleted file mode 100644
index d76b99a..0000000
--- a/javascripts/modules/wikigrok/WikiGrokSuggestionApi.js
+++ /dev/null
@@ -1,56 +0,0 @@
-// jscs:disable requireCamelCaseOrUpperCaseIdentifiers
-( function ( M ) {
-       var WikiGrokSuggestionApi,
-               Api = M.require( 'api' ).Api;
-
-       /**
-        * Gets suggestions, nationalities, occupations from the API
-        * @class WikiGrokSuggestionApi
-        * @extends Api
-        */
-       WikiGrokSuggestionApi = Api.extend( {
-               apiUrl: 'https://tools.wmflabs.org/wikigrok/api2.php',
-               useJsonp: true,
-
-               initialize: function ( options ) {
-                       this.subjectId = options.itemId;
-                       this.subject = options.subject;
-                       this.version = options.version;
-                       Api.prototype.initialize.apply( this, arguments );
-               },
-               /**
-                * Get suggestions for the current person.
-                * Currently 50% of time returns occupations, 50% of time 
nationalities
-                * FIXME: In future it should look for both.
-                * @method
-                * @return {jQuery.Deferred} where parameter is a set of key 
value pairs
-                */
-               getSuggestions: function () {
-                       return this.action( 'get_suggestions', 'suggestions' );
-               },
-               /**
-                * Performs an api action on wikigrok
-                * @method
-                * @param {string} action A valid action as documented on 
https://github.com/kaldari/WikiGrokAPI/blob/master/README.md
-                * @param {string} key of data to return
-                * @return {jQuery.Deferred} where parameter of callback is a 
list of wikidata ids;
-                */
-               action: function ( action, key ) {
-                       return this.ajax( {
-                                       action: action,
-                                       item: this.subjectId.replace( 'Q', '' )
-                               } ).then( function ( data ) {
-                                       if ( key ) {
-                                               if ( data[key] !== undefined ) {
-                                                       return data[key];
-                                               } else {
-                                                       return [];
-                                               }
-                                       }
-                               } );
-               }
-       } );
-
-       M.define( 'modules/wikigrok/WikiGrokSuggestionApi', 
WikiGrokSuggestionApi );
-
-}( mw.mobileFrontend ) );
diff --git a/tests/qunit/modules/wikigrok/test_WikiGrokDialog.js 
b/tests/qunit/modules/wikigrok/test_WikiGrokDialog.js
index 5e7417f..d241a0e 100644
--- a/tests/qunit/modules/wikigrok/test_WikiGrokDialog.js
+++ b/tests/qunit/modules/wikigrok/test_WikiGrokDialog.js
@@ -4,13 +4,25 @@
                WikiDataApi = M.require( 'modules/wikigrok/WikiDataApi' ),
                WikiGrokResponseApi = M.require( 
'modules/wikigrok/WikiGrokResponseApi' ),
                settings = M.require( 'settings'),
-               suggestions = [ {
-                       "id": "P106",
-                       "name": "occupations",
-                       "list": [ "Q285759" ]
-               } ],
+               campaigns = {
+                       album: {
+                               property: "P31",
+                               questions: {
+                                       Q208569: "studio album",
+                                       Q209939: "live album"
+                               }
+                       }
+               },
+               suggestions = {
+                       album: {
+                               id: 'P31',
+                               list: ['Q208569', 'Q209939'],
+                               name: 'album'
+                       }
+               },
                labels = {
-                       Q285759: "insurance broker"
+                       Q208569: 'studio album',
+                       Q209939: 'live album'
                },
                pageTitle = M.getCurrentPage().title || 'Some guy';
 
@@ -26,6 +38,16 @@
                },
                setup: function () {
                        settings.remove( 'pagesWithWikiGrokContributions', 
false );
+
+                       // don't run eventLogging
+                       this.stub( WikiGrokDialog.prototype, 'log' );
+                       this.stub( WikiGrokDialog.prototype, 'logError' );
+
+                       this.sandbox.stub( mw.config, 'get').withArgs( 
'wgWikiGrokCampaigns' )
+                               .returns( campaigns );
+                       this.sandbox.stub( WikiDataApi.prototype, 'getLabels' )
+                               .returns( $.Deferred().resolve( labels ) );
+
                        this.$el = $( '<div id="test">' );
                        this.wk = new WikiGrokDialog( {
                                el: this.$el,
@@ -36,9 +58,6 @@
                                // Set suggestions to go to the second screen.
                                suggestions: suggestions
                        } );
-                       // don't run eventLogging
-                       this.stub( WikiGrokDialog.prototype, 'log' );
-                       this.stub( WikiGrokDialog.prototype, 'logError' );
                }
        } );
 
@@ -112,17 +131,10 @@
                assert.ok( spy.called );
        } );
 
-       function getToQuestion() {
-               this.sandbox.stub( WikiDataApi.prototype, 'getLabels' )
-                       .returns( $.Deferred().resolve( labels ) );
-
-               this.$el.find('.proceed').click();
-       }
-
        QUnit.test( '#UI clicking OK, takes you to the question dialog', 1, 
function ( assert ) {
-               getToQuestion.apply(this);
-               // The name of the page is on the question
-               assert.ok( this.$el.text().indexOf(pageTitle) !== -1 );
+               this.$el.find( '.proceed' ).click();
+               // the question title is visible
+               assert.notEqual( this.$el.text().indexOf('Is this a'), -1, 
'Question is visible' );
        } );
 
        function answerQuestion( sel ) {
@@ -132,7 +144,7 @@
        }
 
        QUnit.test( '#UI - Question - Click Yes', 4, function ( assert ) {
-               getToQuestion.apply( this );
+               this.$el.find( '.proceed' ).click();
 
                assert.equal(
                        getPagesWithWikiGrokContributions()[pageTitle],
@@ -154,7 +166,7 @@
        } );
 
        QUnit.test( '#UI - Question - Click No', 4, function ( assert ) {
-               getToQuestion.apply( this );
+               this.$el.find( '.proceed' ).click();
 
                assert.equal(
                        getPagesWithWikiGrokContributions()[pageTitle],
@@ -176,7 +188,7 @@
        } );
 
        QUnit.test( '#UI - Question - Click Not sure', 4, function ( assert ) {
-               getToQuestion.apply( this );
+               this.$el.find( '.proceed' ).click();
 
                assert.equal(
                        getPagesWithWikiGrokContributions()[pageTitle],
diff --git a/tests/qunit/modules/wikigrok/test_WikiGrokDialogB.js 
b/tests/qunit/modules/wikigrok/test_WikiGrokDialogB.js
index aec4190..704ea64 100644
--- a/tests/qunit/modules/wikigrok/test_WikiGrokDialogB.js
+++ b/tests/qunit/modules/wikigrok/test_WikiGrokDialogB.js
@@ -2,12 +2,27 @@
 
        var WikiGrokDialogB = M.require( 'modules/wikigrok/WikiGrokDialogB' ),
                WikiDataApi = M.require( 'modules/wikigrok/WikiDataApi' ),
-               WikiGrokSuggestionApi = M.require( 
'modules/wikigrok/WikiGrokSuggestionApi' ),
                WikiGrokResponseApi = M.require( 
'modules/wikigrok/WikiGrokResponseApi' ),
-               claims = JSON.parse( 
'{"isHuman":true,"hasOccupation":false,"hasCountryOfCitizenship":true,"hasDateOfBirth":true,"hasDateOfDeath":true,"entities":{"P18":[{"id":"q3784220$8E93773C-9D9F-40E7-B570-941D49157250","mainsnak":{"snaktype":"value","property":"P18","datatype":"commonsMedia","datavalue":{"value":"Anne
 Dallas Dudley 
LOC.jpg","type":"string"}},"type":"statement","rank":"normal","references":[{"hash":"7eb64cf9621d34c54fd4bd040ed4b61a88c4a1a0","snaks":{"P143":[{"snaktype":"value","property":"P143","datatype":"wikibase-item","datavalue":{"value":{"entity-type":"item","numeric-id":328},"type":"wikibase-entityid"}}]},"snaks-order":["P143"]}]}],"P21":[{"id":"q3784220$3bbdabca-47f4-023a-56da-cc5faeceee76","mainsnak":{"snaktype":"value","property":"P21","datatype":"wikibase-item","datavalue":{"value":{"entity-type":"item","numeric-id":6581072},"type":"wikibase-entityid"}},"type":"statement","rank":"normal"}],"P373":[{"id":"q3784220$6F08ECAF-748D-4844-ADDB-B8F3B0CEFFE8","mainsnak":{"snaktype":"value","property":"P373","datatype":"string","datavalue":{"value":"Anne
 Dallas 
Dudley","type":"string"}},"type":"statement","rank":"normal","references":[{"hash":"3f12e959c196a5a4adc7f7d1ba480c2629b550f8","snaks":{"P143":[{"snaktype":"value","property":"P143","datatype":"wikibase-item","datavalue":{"value":{"entity-type":"item","numeric-id":565},"type":"wikibase-entityid"}}]},"snaks-order":["P143"]}]}],"P31":[{"id":"Q3784220$145FD1EB-53A0-47CE-B964-2D6C9F380738","mainsnak":{"snaktype":"value","property":"P31","datatype":"wikibase-item","datavalue":{"value":{"entity-type":"item","numeric-id":5},"type":"wikibase-entityid"}},"type":"statement","rank":"normal","references":[{"hash":"7eb64cf9621d34c54fd4bd040ed4b61a88c4a1a0","snaks":{"P143":[{"snaktype":"value","property":"P143","datatype":"wikibase-item","datavalue":{"value":{"entity-type":"item","numeric-id":328},"type":"wikibase-entityid"}}]},"snaks-order":["P143"]}]}],"P19":[{"id":"Q3784220$34CF719D-DC0C-4115-AB38-077F2E903068","mainsnak":{"snaktype":"value","property":"P19","datatype":"wikibase-item","datavalue":{"value":{"entity-type":"item","numeric-id":23197},"type":"wikibase-entityid"}},"type":"statement","rank":"normal","references":[{"hash":"7eb64cf9621d34c54fd4bd040ed4b61a88c4a1a0","snaks":{"P143":[{"snaktype":"value","property":"P143","datatype":"wikibase-item","datavalue":{"value":{"entity-type":"item","numeric-id":328},"type":"wikibase-entityid"}}]},"snaks-order":["P143"]}]}],"P20":[{"id":"Q3784220$A7563BBC-8FE1-41E1-B2F0-384C28A2CEFF","mainsnak":{"snaktype":"value","property":"P20","datatype":"wikibase-item","datavalue":{"value":{"entity-type":"item","numeric-id":2195766},"type":"wikibase-entityid"}},"type":"statement","rank":"normal","references":[{"hash":"7eb64cf9621d34c54fd4bd040ed4b61a88c4a1a0","snaks":{"P143":[{"snaktype":"value","property":"P143","datatype":"wikibase-item","datavalue":{"value":{"entity-type":"item","numeric-id":328},"type":"wikibase-entityid"}}]},"snaks-order":["P143"]}]}],"P570":[{"id":"Q3784220$57C383D1-6936-4DB3-A920-38EBF90344A0","mainsnak":{"snaktype":"value","property":"P570","datatype":"time","datavalue":{"value":{"time":"+00000001955-09-13T00:00:00Z","timezone":0,"before":0,"after":0,"precision":11,"calendarmodel":"http://www.wikidata.org/entity/Q1985727"},"type":"time"}},"type":"statement","rank":"normal","references":[{"hash":"7eb64cf9621d34c54fd4bd040ed4b61a88c4a1a0","snaks":{"P143":[{"snaktype":"value","property":"P143","datatype":"wikibase-item","datavalue":{"value":{"entity-type":"item","numeric-id":328},"type":"wikibase-entityid"}}]},"snaks-order":["P143"]}]}],"P569":[{"id":"Q3784220$4FFA323F-9D0F-4F6D-8076-15DFCBE2BFCB","mainsnak":{"snaktype":"value","property":"P569","datatype":"time","datavalue":{"value":{"time":"+00000001876-11-13T00:00:00Z","timezone":0,"before":0,"after":0,"precision":11,"calendarmodel":"http://www.wikidata.org/entity/Q1985727"},"type":"time"}},"type":"statement","rank":"normal","references":[{"hash":"7eb64cf9621d34c54fd4bd040ed4b61a88c4a1a0","snaks":{"P143":[{"snaktype":"value","property":"P143","datatype":"wikibase-item","datavalue":{"value":{"entity-type":"item","numeric-id":328},"type":"wikibase-entityid"}}]},"snaks-order":["P143"]}]}],"P735":[{"id":"Q3784220$22758370-00AA-4378-BA0F-384CF1CC0480","mainsnak":{"snaktype":"value","property":"P735","datatype":"wikibase-item","datavalue":{"value":{"entity-type":"item","numeric-id":564684},"type":"wikibase-entityid"}},"type":"statement","rank":"normal","references":[{"hash":"50f57a3dbac4708ce4ae4a827c0afac7fcdb4a5c","snaks":{"P143":[{"snaktype":"value","property":"P143","datatype":"wikibase-item","datavalue":{"value":{"entity-type":"item","numeric-id":11920},"type":"wikibase-entityid"}}]},"snaks-order":["P143"]}]}],"P27":[{"id":"Q3784220$8EC66D5F-07EE-4951-86D7-A508E777C733","mainsnak":{"snaktype":"value","property":"P27","datatype":"wikibase-item","datavalue":{"value":{"entity-type":"item","numeric-id":30},"type":"wikibase-entityid"}},"type":"statement","rank":"normal","references":[{"hash":"50f57a3dbac4708ce4ae4a827c0afac7fcdb4a5c","snaks":{"P143":[{"snaktype":"value","property":"P143","datatype":"wikibase-item","datavalue":{"value":{"entity-type":"item","numeric-id":11920},"type":"wikibase-entityid"}}]},"snaks-order":["P143"]}]}]},"description":"American
 women\'s suffrage activist"}' ),
-               suggestions = JSON.parse( 
'{"occupations":{"id":"P106","name":"occupations","list":["Q285759"]},"nationalities":{"id":"P27","name":"nationality","list":[]},"schools":{"id":"P69","name":"schools","list":[]}}'
 ),
+               campaigns = {
+                       actor: {
+                               property: "P106",
+                               questions: {
+                                       Q10798782: "television actor",
+                                       Q10800557: "film actor"
+                               }
+                       }
+               },
+               suggestions = {
+                       actor: {
+                               id: 'P106',
+                               list: ['Q10798782', 'Q10800557'],
+                               name: 'actor'
+                       }
+               },
                labels = {
-                       Q285759: 'insurance broker'
+                       Q10798782: 'television actor',
+                       Q10800557: 'film actor'
+
                },
                pageTitle = 'Some guy';
 
@@ -30,17 +45,17 @@
                        this.wk.remove();
                },
                setup: function () {
-                       this.sandbox.stub( WikiDataApi.prototype, 'getClaims' )
-                               .returns( $.Deferred().resolve( claims ) );
-                       this.sandbox.stub( WikiGrokSuggestionApi.prototype, 
'getSuggestions' )
-                               .returns( $.Deferred().resolve( suggestions ) );
                        // don't run eventLogging
                        this.sandbox.stub( WikiGrokDialogB.prototype, 'log' );
                        this.sandbox.stub( WikiGrokDialogB.prototype, 
'logError' );
+
+                       this.sandbox.stub( mw.config, 'get').withArgs( 
'wgWikiGrokCampaigns' )
+                               .returns( campaigns );
                        this.sandbox.stub( WikiDataApi.prototype, 'getLabels' )
                                .returns( $.Deferred().resolve( labels ) );
                        this.sandbox.stub( WikiGrokResponseApi.prototype, 
'recordClaims' )
                                .returns( $.Deferred().resolve() );
+
                        this.$el = $( '<div id="test">' );
                        this.wk = new WikiGrokDialogB( {
                                el: this.$el,
@@ -69,7 +84,7 @@
        } );
 
        QUnit.asyncTest( '#UI clicking OK, takes you to the question dialog', 
function ( assert ) {
-               QUnit.expect( 4 );
+               QUnit.expect( 3 );
                this.wk.$el.find( '.proceed' ).trigger( 'click' );
                // The name of the page is on the question
                assert.ok( this.wk.$el.text().indexOf( pageTitle ) !== -1 );
@@ -78,9 +93,9 @@
                        // The question is there
                        var tags = this.wk.$el.find( '.tags .ui-tag-button' ),
                                labels = tags.find( 'label' );
-                       assert.strictEqual( tags.length, 1 );
-                       assert.strictEqual( labels.first().text(), 'Profession' 
);
-                       assert.strictEqual( labels.last().text(), 'insurance 
broker' );
+                       //console.log(JSON.stringify(tags));
+                       assert.strictEqual( tags.length, 2, 'Correct number of 
tags' );
+                       assert.strictEqual( labels.first().text(), 
'Profession', 'Correct label text' );
                        QUnit.start();
                }, this ), 0 );
        } );
@@ -112,17 +127,6 @@
 
        QUnit.module( 'MobileFrontend: WikiGrokDialogB', {
                setup: function () {
-                       var noSuggestionResponse = $.Deferred().resolve( {
-                               suggestions: {
-                                       occupations: {
-                                               id: 'P106', list: []
-                                       },
-                                       nationalities: {
-                                               id: 'P27', list: []
-                                       }
-                               }
-                       } );
-
                        this.$el = $( '<div id="test">' );
                        this.wk = new WikiGrokDialogB( {
                                el: this.$el,
@@ -133,20 +137,13 @@
                                // Set suggestions to go to the second screen.
                                suggestions: suggestions
                        } );
-                       // don't run eventLogging
-                       this.sandbox.stub( WikiDataApi.prototype, 'getClaims' )
-                               .returns( $.Deferred().resolve( { isHuman: true 
} ) );
-                       this.logError = this.sandbox.stub( 
WikiGrokDialogB.prototype, 'logError' );
-                       this.sandbox.stub( WikiGrokSuggestionApi.prototype, 
'getSuggestions' )
-                               .returns( noSuggestionResponse );
                }
        } );
 
-       QUnit.test( '#UI should not display when there are no suggestions', 2, 
function ( assert ) {
+       QUnit.test( '#UI should not display when there are no suggestions', 1, 
function ( assert ) {
                var spy = this.sandbox.stub( WikiGrokDialogB.prototype, 'show' 
);
                this.wk.reveal( {} );
                assert.ok( spy.notCalled, 'We do not call if the response 
provides no suggestions.' );
-               assert.ok( this.logError.called, 'Make sure we log an error.' );
        } );
 
 }( jQuery, mw.mobileFrontend ) );
diff --git a/tests/qunit/modules/wikigrok/test_wikiGrokCampaigns.js 
b/tests/qunit/modules/wikigrok/test_wikiGrokCampaigns.js
new file mode 100644
index 0000000..ea95160
--- /dev/null
+++ b/tests/qunit/modules/wikigrok/test_wikiGrokCampaigns.js
@@ -0,0 +1,66 @@
+( function ( $, M, mw ) {
+
+       var wikiGrokCampaigns = M.require( 
'modules/wikigrok/wikiGrokCampaigns'),
+               campaigns = {
+                       author: {
+                               property: "P106",
+                               questions: {
+                                       Q482980: "author"
+                               }
+                       },
+                       actor: {
+                               property: "P106",
+                               questions: {
+                                       Q10798782: "television actor",
+                                       Q10800557: "film actor"
+                               }
+                       },
+                       album: {
+                               property: "P31",
+                               questions: {
+                                       Q208569: "studio album",
+                                       Q209939: "live album"
+                               }
+                       }
+               };
+               // this can be used in new tests
+               //suggestions = {
+               //      author: {
+               //              id: 'P106',
+               //              list: ['Q482980'],
+               //              name: 'author'
+               //      },
+               //      actor: {
+               //              id: 'P106',
+               //              list: ['Q10798782', 'Q10800557'],
+               //              name: 'actor'
+               //      },
+               //      album: {
+               //              id: 'P31',
+               //              list: ['Q208569', 'Q209939'],
+               //              name: 'album'
+               //      }
+               //};
+
+       QUnit.module( 'MobileFrontend: WikiGrokCampaigns', {
+               teardown: function () {
+               },
+               setup: function () {
+               }
+       } );
+
+       QUnit.test( 'campaigns', 4, function ( assert ) {
+               var campaign;
+
+               assert.equal( wikiGrokCampaigns.getRandomCampaign(), null, 'no 
campaigns');
+
+               this.sandbox.stub( mw.config, 'get').withArgs( 
'wgWikiGrokCampaigns' )
+                       .returns( campaigns );
+
+               campaign = wikiGrokCampaigns.getRandomCampaign();
+               assert.equal( typeof campaign.name, 'string', 'campaign name 
exists' );
+               assert.equal( typeof campaign.suggestions, 'object', 
'suggestions exist' );
+               assert.equal( typeof campaign.randomClaimId, 'string', 'random 
claim id exist' );
+       } );
+
+}( jQuery, mw.mobileFrontend, mw ) );

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I2bf8c94703f7e5d49c3a60cf8885f7a1194e7726
Gerrit-PatchSet: 11
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Bmansurov <[email protected]>
Gerrit-Reviewer: Awjrichards <[email protected]>
Gerrit-Reviewer: Bmansurov <[email protected]>
Gerrit-Reviewer: Kaldari <[email protected]>
Gerrit-Reviewer: Phuedx <[email protected]>

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

Reply via email to