Jeroen De Dauw has submitted this change and it was merged.

Change subject: Check for badges duplicates
......................................................................


Check for badges duplicates

This patch makes data model throw an error when
there are duplicates of badges.

Change-Id: I3004dd053628c8ad5a06011fc3953c17b0825d82
---
M DataModel/SimpleSiteLink.php
M tests/phpunit/SimpleSiteLinkTest.php
2 files changed, 34 insertions(+), 12 deletions(-)

Approvals:
  Jeroen De Dauw: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/DataModel/SimpleSiteLink.php b/DataModel/SimpleSiteLink.php
index 014d3ce..1dc0821 100644
--- a/DataModel/SimpleSiteLink.php
+++ b/DataModel/SimpleSiteLink.php
@@ -27,6 +27,28 @@
         */
        protected $badges;
 
+
+       /**
+        * @param ItemId[] $badges
+        *
+        * @throws InvalidArgumentException
+        */
+       protected function assertBadgesAreValid( $badges ) {
+               if ( !is_array( $badges ) ) {
+                       throw new InvalidArgumentException( '$badges needs to 
be an array' );
+               }
+
+               foreach( $badges as $badge ) {
+                       if ( !( $badge instanceof ItemId ) ) {
+                               throw new InvalidArgumentException( 'Each 
element in $badges needs to be an ItemId' );
+                       }
+               }
+
+               if ( count( $badges ) !== count( array_unique( $badges ) ) ) {
+                       throw new InvalidArgumentException( '$badges array 
cannot contain duplicates' );
+               }
+       }
+
        /**
         * @param string $siteId
         * @param string $pageName
@@ -43,15 +65,7 @@
                        throw new InvalidArgumentException( '$pageName needs to 
be a string' );
                }
 
-               if ( !is_array( $badges ) ) {
-                       throw new InvalidArgumentException( '$badges needs to 
be an array' );
-               }
-
-               foreach( $badges as $badge ) {
-                       if ( !( $badge instanceof ItemId ) ) {
-                               throw new InvalidArgumentException( 'Each 
element in $badges needs to be an ItemId' );
-                       }
-               }
+               $this->assertBadgesAreValid( $badges );
 
                $this->siteId = $siteId;
                $this->pageName = $pageName;
diff --git a/tests/phpunit/SimpleSiteLinkTest.php 
b/tests/phpunit/SimpleSiteLinkTest.php
index 8805200..7c18428 100644
--- a/tests/phpunit/SimpleSiteLinkTest.php
+++ b/tests/phpunit/SimpleSiteLinkTest.php
@@ -158,16 +158,17 @@
        }
 
        /**
-        * @dataProvider stuffThatIsNotArrayOfItemIdsProvider
+        * @dataProvider invalidBadgesProvider
         */
-       public function testCannotConstructWithNonArrayOfItemIdsBadges( 
$invalidBadges ) {
+       public function testCannotConstructWithInvalidBadges( $invalidBadges ) {
                $this->setExpectedException( 'InvalidArgumentException' );
                new SimpleSiteLink( 'enwiki', 'Wikidata', $invalidBadges );
        }
 
-       public function stuffThatIsNotArrayOfItemIdsProvider() {
+       public function invalidBadgesProvider() {
                $argLists = array();
 
+               // non ItemIds
                $argLists[] = array( array(
                        'nyan',
                        42
@@ -185,6 +186,13 @@
                        new PropertyId( 'P3' )
                ) );
 
+               // duplicates
+               $argLists[] = array( array(
+                       new ItemId( 'Q42' ),
+                       new ItemId( 'q149' ),
+                       new ItemId( 'q42' )
+               ) );
+
                return $argLists;
        }
 

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I3004dd053628c8ad5a06011fc3953c17b0825d82
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/WikibaseDataModel
Gerrit-Branch: master
Gerrit-Owner: Michał Łazowik <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: Michał Łazowik <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to