Daniel Werner has uploaded a new change for review.

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


Change subject: Added JS wikibase.getSitesOfGroup
......................................................................

Added JS wikibase.getSitesOfGroup

Change-Id: Ia197107b99c30e77cfe69d5331cd12664ed0dfe9
---
M lib/resources/wikibase.js
M lib/tests/qunit/wikibase.tests.js
2 files changed, 64 insertions(+), 12 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/93/67393/1

diff --git a/lib/resources/wikibase.js b/lib/resources/wikibase.js
index 5533eec..ec1164b 100644
--- a/lib/resources/wikibase.js
+++ b/lib/resources/wikibase.js
@@ -120,7 +120,7 @@
        /**
         * Returns an array with all the known sites.
         *
-        * @return wikibase.Site[]
+        * @return {Object} site IDs as field names, related wb.Site objects as 
value
         */
        this.getSites = function() {
                if( this._siteList !== null ) {
@@ -144,6 +144,27 @@
        };
 
        /**
+        * Returns an array with all the known sites of one group.
+        *
+        * @since 0.4
+        *
+        * @return {Object} site IDs as field names, related wb.Site objects as 
value
+        */
+       this.getSitesOfGroup = function( groupId ) {
+               var sitesOfGroup = {},
+                       sites = this.getSites();
+
+               for( var siteId in sites ) {
+                       var site = sites[ siteId ];
+
+                       if( site.getGroup() === groupId ) {
+                               sitesOfGroup[ siteId ] = site;
+                       }
+               }
+               return sitesOfGroup;
+       };
+
+       /**
         * Returns an array with all known site groups.
         *
         * @since 0.4
diff --git a/lib/tests/qunit/wikibase.tests.js 
b/lib/tests/qunit/wikibase.tests.js
index a906ed8..c2be0d1 100644
--- a/lib/tests/qunit/wikibase.tests.js
+++ b/lib/tests/qunit/wikibase.tests.js
@@ -65,18 +65,21 @@
                );
        } );
 
-       QUnit.test( 'wikibase.getSites()', 3, function( assert ) {
-               var sites = wb.getSites();
-
+       /**
+        * Generic test for testing an object of sites as supposed to be 
returned by getSites() and
+        * others.
+        *
+        * TODO: Another reason why there should be a SiteList object, much 
easier to verify that the
+        *  returned value of different functions returning a list of sites is 
valid.
+        *
+        * @param {QUnit.assert} assert
+        * @param {string} fnName
+        * @param {Object} sites
+        */
+       function siteSetTest( assert, fnName, sites ) {
                assert.ok(
                        $.isPlainObject( sites ),
-                       'getSites() returns a plain object'
-               );
-
-               assert.strictEqual(
-                       sites.length,
-                       TEST_SITE_DEFINITIONS.length,
-                       'getSites() returns expected number of sites'
+                       fnName + '() returns a plain object'
                );
 
                var allSiteInstances = true;
@@ -86,8 +89,36 @@
 
                assert.ok(
                        allSiteInstances,
-                       'getSites() returned object fields only hold site 
objects'
+                       fnName + '() returned object fields only hold site 
objects'
                );
+       }
+
+       QUnit.test( 'wikibase.getSites()', function( assert ) {
+               var sites = wb.getSites();
+               siteSetTest( assert, 'getSites', sites );
+
+               assert.strictEqual(
+                       sites.length,
+                       TEST_SITE_DEFINITIONS.length,
+                       'getSites() returns expected number of sites'
+               );
+       } );
+
+       QUnit.test( 'wikibase.getSitesOfGroup()', function( assert ) {
+               $.each( TEST_SITE_GROUPS, function( i, group ) {
+                       var sites = wb.getSitesOfGroup( group );
+                       siteSetTest( assert, 'getSitesOfGroup', sites );
+
+                       var allFromRightGroup = true;
+                       $.each( sites, function( i, site ) {
+                               allFromRightGroup = allFromRightGroup && ( 
site.getGroup() === group );
+                       } );
+
+                       assert.ok(
+                               allFromRightGroup,
+                               'getSitesOfGroup( "' + group + '" ) only 
returned sites of group "' + group + '"'
+                       );
+               } );
        } );
 
        QUnit.test( 'wikibase.getSite()', 2, function( assert ) {

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia197107b99c30e77cfe69d5331cd12664ed0dfe9
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Werner <[email protected]>

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

Reply via email to