Aude has uploaded a new change for review.

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


Change subject: (bug 48497) Check for property id exists in ByPropertyIdArray
......................................................................

(bug 48497) Check for property id exists in ByPropertyIdArray

Change-Id: I7308c74e7de70b9c43da6f232270d8379ad4704b
---
M DataModel/DataModel/Claim/Claims.php
M DataModel/tests/phpunit/Claim/ClaimsTest.php
M lib/includes/ByPropertyIdArray.php
M lib/tests/phpunit/ByPropertyIdArrayTest.php
4 files changed, 21 insertions(+), 1 deletion(-)


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

diff --git a/DataModel/DataModel/Claim/Claims.php 
b/DataModel/DataModel/Claim/Claims.php
index 8dbe718..0842a6d 100644
--- a/DataModel/DataModel/Claim/Claims.php
+++ b/DataModel/DataModel/Claim/Claims.php
@@ -179,6 +179,10 @@
                $claimsByProp = new ByPropertyIdArray( $this );
                $claimsByProp->buildIndex();
 
+               if ( !( in_array( $propertyId, $claimsByProp->getPropertyIds() 
) ) ) {
+                       return new Claims();
+               }
+
                $claimsForProperty = new Claims( 
$claimsByProp->getByPropertyId( $propertyId ) );
                return $claimsForProperty;
        }
diff --git a/DataModel/tests/phpunit/Claim/ClaimsTest.php 
b/DataModel/tests/phpunit/Claim/ClaimsTest.php
index 1c285c0..091030a 100644
--- a/DataModel/tests/phpunit/Claim/ClaimsTest.php
+++ b/DataModel/tests/phpunit/Claim/ClaimsTest.php
@@ -167,6 +167,10 @@
                $claims = $array->getClaimsForProperty( 23 );
                $this->assertInstanceOf( 'Wikibase\Claims', $claims );
                $this->assertCount( 1, $claims );
+
+               $claims = $array->getClaimsForProperty( 9000 );
+               $this->assertInstanceOf( 'Wikibase\Claims', $claims );
+               $this->assertCount( 0, $claims );
        }
 
        public function testDuplicateClaims() {
diff --git a/lib/includes/ByPropertyIdArray.php 
b/lib/includes/ByPropertyIdArray.php
index b5df765..cc81803 100644
--- a/lib/includes/ByPropertyIdArray.php
+++ b/lib/includes/ByPropertyIdArray.php
@@ -92,13 +92,17 @@
         * @param integer $propertyId
         *
         * @return object[]
-        * @throws MWException
+        * @throws MWException|OutOfBoundsException
         */
        public function getByPropertyId( $propertyId ) {
                if ( $this->byId === null ) {
                        throw new MWException( 'Index not build, call 
buildIndex first' );
                }
 
+               if ( !( array_key_exists( $propertyId, $this->byId ) ) ) {
+                       throw new \OutOfBoundsException( 'Property id array key 
does not exist.' );
+               }
+
                return $this->byId[$propertyId];
        }
 
diff --git a/lib/tests/phpunit/ByPropertyIdArrayTest.php 
b/lib/tests/phpunit/ByPropertyIdArrayTest.php
index ce18473..73a3d3f 100644
--- a/lib/tests/phpunit/ByPropertyIdArrayTest.php
+++ b/lib/tests/phpunit/ByPropertyIdArrayTest.php
@@ -115,4 +115,12 @@
                $this->assertArrayEquals( $objects, $allObtainedObjects );
        }
 
+       public function testGetByNotSetIdThrowsException() {
+               $indexedArray = new ByPropertyIdArray();
+               $indexedArray->buildIndex();
+
+               $this->setExpectedException( 'OutOfBoundsException' );
+
+               $indexedArray->getByPropertyId( 9000 );
+       }
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7308c74e7de70b9c43da6f232270d8379ad4704b
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: mw1.22-wmf4
Gerrit-Owner: Aude <[email protected]>

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

Reply via email to