Michał Łazowik has uploaded a new change for review.
https://gerrit.wikimedia.org/r/94167
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, 20 insertions(+), 4 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseDataModel
refs/changes/67/94167/1
diff --git a/DataModel/SimpleSiteLink.php b/DataModel/SimpleSiteLink.php
index 014d3ce..7f1687c 100644
--- a/DataModel/SimpleSiteLink.php
+++ b/DataModel/SimpleSiteLink.php
@@ -47,10 +47,18 @@
throw new InvalidArgumentException( '$badges needs to
be an array' );
}
- foreach( $badges as $badge ) {
+ $badgeDup = array();
+
+ foreach( $badges as $badge ) {
if ( !( $badge instanceof ItemId ) ) {
throw new InvalidArgumentException( 'Each
element in $badges needs to be an ItemId' );
}
+
+ if ( array_key_exists( (string)$badge, $badgeDup ) ) {
+ throw new InvalidArgumentException( '$badges
array cannot contain duplicates' );
+ }
+
+ $badgeDup[(string)$badge] = true;
}
$this->siteId = $siteId;
diff --git a/tests/phpunit/SimpleSiteLinkTest.php
b/tests/phpunit/SimpleSiteLinkTest.php
index 8805200..5288277 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
@@ -184,6 +185,13 @@
new PropertyId( 'P2' ),
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: newchange
Gerrit-Change-Id: I3004dd053628c8ad5a06011fc3953c17b0825d82
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseDataModel
Gerrit-Branch: master
Gerrit-Owner: Michał Łazowik <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits