Aude has uploaded a new change for review.

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

Change subject: Add test case for statements on property in ChangeOps
......................................................................

Add test case for statements on property in ChangeOps

ChangeOpClaim and ChangeOpClaimTest are a terrible mess
that needs further refactoring.

this just adds a test case for bug 72671, which is
fatal error when trying to add statements to properties.

Change-Id: Idfe2bd9a329b473610919ca2f3ac3e1ea9873988
---
M repo/tests/phpunit/includes/ChangeOp/ChangeOpClaimTest.php
1 file changed, 60 insertions(+), 29 deletions(-)


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

diff --git a/repo/tests/phpunit/includes/ChangeOp/ChangeOpClaimTest.php 
b/repo/tests/phpunit/includes/ChangeOp/ChangeOpClaimTest.php
index 7d6d8e8..c8e71fe 100644
--- a/repo/tests/phpunit/includes/ChangeOp/ChangeOpClaimTest.php
+++ b/repo/tests/phpunit/includes/ChangeOp/ChangeOpClaimTest.php
@@ -14,12 +14,14 @@
 use Wikibase\DataModel\Entity\EntityId;
 use Wikibase\DataModel\Entity\Item;
 use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Entity\Property;
 use Wikibase\DataModel\Entity\PropertyId;
 use Wikibase\DataModel\Reference;
 use Wikibase\DataModel\ReferenceList;
 use Wikibase\DataModel\Snak\PropertyNoValueSnak;
 use Wikibase\DataModel\Snak\PropertySomeValueSnak;
 use Wikibase\DataModel\Snak\PropertyValueSnak;
+use Wikibase\DataModel\Snak\Snak;
 use Wikibase\DataModel\Snak\SnakList;
 use Wikibase\DataModel\Statement\Statement;
 use Wikibase\Lib\ClaimGuidGenerator;
@@ -81,9 +83,10 @@
 
        public function provideTestApply() {
                $itemEmpty = Item::newEmpty();
-               $itemEmpty->setId( new ItemId( 'q888' ) );
-               $item777 = self::makeNewItemWithStatement( 'Q777', new 
PropertyNoValueSnak( 45 ) );
-               $item666 = self::makeNewItemWithStatement( 'Q666', new 
PropertySomeValueSnak( 44 ) );
+               $itemEmpty->setId( new ItemId( 'Q888' ) );
+
+               $item777 = $this->makeNewItemWithStatement( 'Q777', new 
PropertyNoValueSnak( 45 ) );
+               $item666 = $this->makeNewItemWithStatement( 'Q666', new 
PropertySomeValueSnak( 44 ) );
 
                $item777Statements = $item777->getClaims();
                $item666Statements = $item666->getClaims();
@@ -91,16 +94,18 @@
                $statement777 = reset( $item777Statements );
                $statement666 = reset( $item666Statements );
 
-               //claims that exist on the given entities
+               // claims that exist on the given entities
                $statements[0] = new Statement( new Claim( new 
PropertyNoValueSnak( 43 ) ) );
                $statements[777] = clone $statement777;
                $statements[666] = clone $statement666;
-               //claims with a null guid
+
+               // claims with a null guid
                $statements[7770] = clone $statement777;
                $statements[7770]->setGuid( null );
                $statements[6660] = clone $statement666;
                $statements[6660]->setGuid( null );
-               //new claims not yet on the entity
+
+               // new claims not yet on the entity
                $statements[7777] = clone $statement777;
                $statements[7777]->setGuid( 
'Q777$D8404CDA-25E4-4334-AF13-A3290BC77777' );
                $statements[6666] = clone $statement666;
@@ -114,29 +119,49 @@
                $statements[13]->setGuid( 
'Q666$D8404CDA-25E4-4334-AF13-A3290BC66613' );
 
                $args = array();
-               //test adding claims with guids from other items(these 
shouldn't be added)
+
+               // test adding claims with guids from other items(these 
shouldn't be added)
                $args[] = array( $itemEmpty, $statements[666], false );
                $args[] = array( $itemEmpty, $statements[777], false );
                $args[] = array( $item666, $statements[777], false );
                $args[] = array( $item777, $statements[666], false );
-               //test adding the same claims with a null guid (a guid should 
be created)
+
+               // test adding the same claims with a null guid (a guid should 
be created)
                $args[] = array( $item777, $statements[7770], array( 
$statements[777], $statements[7770] ) );
                $args[] = array( $item666, $statements[6660], array( 
$statements[666], $statements[6660] ) );
-               //test adding the same claims with a correct but different guid 
(these should be added)
+
+               // test adding the same claims with a correct but different 
guid (these should be added)
                $args[] = array( $item777, $statements[7777], array( 
$statements[777], $statements[7770], $statements[7777] ) );
                $args[] = array( $item666, $statements[6666], array( 
$statements[666], $statements[6660], $statements[6666] ) );
-               //test adding the same claims with and id that already exists 
(these shouldn't be added)
+
+               // test adding the same claims with and id that already exists 
(these shouldn't be added)
                $args[] = array( $item777, $statements[7777], array( 
$statements[777], $statements[7770], $statements[7777] ) );
                $args[] = array( $item666, $statements[6666], array( 
$statements[666], $statements[6660], $statements[6666] ) );
+
                // test adding a claim at a specific index
                $args[] = array( $item777, $statements[0], array( 
$statements[0], $statements[777], $statements[7770], $statements[7777] ), 0 );
+
                // test moving a claim
                $args[] = array( $item666, $statements[6666], array( 
$statements[666], $statements[6666], $statements[6660] ), 1 );
+
                // test adding a claim featuring another property id within the 
boundaries of claims the
                // same property
                $args[] = array( $item666, $statements[11], array( 
$statements[666], $statements[6666], $statements[6660], $statements[11] ), 1 );
+
                // test moving a subset of claims featuring the same property
                $args[] = array( $item666, $statements[12], array( 
$statements[12], $statements[11], $statements[666], $statements[6666], 
$statements[6660] ), 0 );
+
+               // test statements on properties
+               $property = Property::newEmpty();
+               $property->setId( new PropertyId( 'P73923' ) );
+
+               $newProperty = clone $property;
+               $propertySnak = new PropertyNoValueSnak( 45 );
+               $propertyStatement = new Statement( $this->makeClaim( 
$newProperty, $propertySnak ) );
+
+               $newProperty->getStatements()->addStatement( $propertyStatement 
);
+
+               $args[] = array( $newProperty, $propertyStatement, 1 );
 
                return $args;
        }
@@ -210,20 +235,11 @@
                        $this->mockProvider->getMockSnakValidator()
                ) );
 
-               //TODO: once we stop allowing user-generated GUIDs for new 
claims, test this below.
-               // apply change to an unknown claim
-               /*
-               $wrongClaimId = $item->getId()->getSerialization() . 
'$DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF';
-               $badClaim = clone $newClaim;
-               $badClaim->setGuid( $wrongClaimId );
-               $args['unknown claim'] = array ( $item, new ChangeOpClaim( 
$badClaim, $guidGenerator ) );
-               */
-
                // update an existing claim with wrong main snak property
                $newSnak = new PropertyNoValueSnak( 23452345 );
                $newStatement->setMainSnak( $newSnak );
 
-               $changeOp =  new ChangeOpClaim(
+               $changeOp = new ChangeOpClaim(
                        $newStatement,
                        $this->mockProvider->getGuidGenerator(),
                        $this->mockProvider->getMockGuidValidator(),
@@ -247,24 +263,39 @@
 
        /**
         * @param integer $itemId
-        * @param $snak
+        * @param Snak $snak
         * @return Item
         */
-       protected function makeNewItemWithStatement( $itemId, $snak ) {
+       private function makeNewItemWithStatement( $itemId, Snak $snak ) {
                $item = Item::newEmpty();
                $item->setId( new ItemId( $itemId ) );
 
-               $statement = $item->newClaim( $snak );
-               $guidGenerator = new ClaimGuidGenerator();
-               $statement->setGuid( $guidGenerator->newGuid( $item->getId() ) 
);
-
-               $claims = new Claims();
-               $claims->addClaim( $statement );
-               $item->setClaims( $claims );
+               $this->addClaimsToEntity( $item, $snak );
 
                return $item;
        }
 
+       /**
+        * @param Entity $entity
+        */
+       private function makeClaim( Entity $entity, Snak $snak ) {
+               $claim = $entity->newClaim( $snak );
+               $guidGenerator = new ClaimGuidGenerator();
+               $claim->setGuid( $guidGenerator->newGuid( $entity->getId() ) );
+
+               return $claim;
+       }
+
+       /**
+        * @param Entity $entity
+        * @param Snak $snak
+        */
+       private function addClaimsToEntity( Entity $entity, Snak $snak ) {
+               $claim = $this->makeClaim( $entity, $snak );
+
+               $entity->getStatements()->addStatement( new Statement( $claim ) 
);
+       }
+
        public function validateProvider() {
                $p11 = new PropertyId( 'P11' );
                $q17 = new ItemId( 'Q17' );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Idfe2bd9a329b473610919ca2f3ac3e1ea9873988
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Aude <aude.w...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to