public function testSnaksOrderSerialization()
 {
     $snakListSerializerMock = $this->getMock('\\Serializers\\Serializer');
     $snakListSerializerMock->expects($this->any())->method('serialize')->with($this->equalTo(new SnakList(array(new PropertyNoValueSnak(new PropertyId('P42')), new PropertySomeValueSnak(new PropertyId('P24')), new PropertyNoValueSnak(new PropertyId('P24'))))))->will($this->returnValue(array()));
     $referenceSerializer = new ReferenceSerializer($snakListSerializerMock);
     $reference = new Reference(new SnakList(array(new PropertyNoValueSnak(new PropertyId('P42')), new PropertySomeValueSnak(new PropertyId('P24')), new PropertyNoValueSnak(new PropertyId('P24')))));
     $this->assertEquals(array('hash' => $reference->getHash(), 'snaks' => array(), 'snaks-order' => array('P42', 'P24')), $referenceSerializer->serialize($reference));
 }
示例#2
0
 public function testCanConstructWithReferenceListObject()
 {
     $reference = new Reference(array(new PropertyNoValueSnak(1)));
     $original = new ReferenceList(array($reference));
     $copy = new ReferenceList($original);
     $this->assertSame(1, $copy->count());
     $this->assertNotNull($copy->getReference($reference->getHash()));
 }
 public function testSnaksOrderDeserialization()
 {
     $snaksDeserializerMock = $this->getMock('\\Deserializers\\Deserializer');
     $snaksDeserializerMock->expects($this->any())->method('deserialize')->with($this->equalTo(array('P24' => array(array('snaktype' => 'novalue', 'property' => 'P24')), 'P42' => array(array('snaktype' => 'somevalue', 'property' => 'P42'), array('snaktype' => 'novalue', 'property' => 'P42')))))->will($this->returnValue(new SnakList(array(new PropertyNoValueSnak(new PropertyId('P24')), new PropertySomeValueSnak(new PropertyId('P42')), new PropertyNoValueSnak(new PropertyId('P42'))))));
     $referenceDeserializer = new ReferenceDeserializer($snaksDeserializerMock);
     $reference = new Reference(new SnakList(array(new PropertySomeValueSnak(new PropertyId('P42')), new PropertyNoValueSnak(new PropertyId('P42')), new PropertyNoValueSnak(new PropertyId('P24')))));
     $serialization = array('hash' => '20726a1e99eab73834c0f4a25f3c5c2561993e6e', 'snaks' => array('P24' => array(array('snaktype' => 'novalue', 'property' => 'P24')), 'P42' => array(array('snaktype' => 'somevalue', 'property' => 'P42'), array('snaktype' => 'novalue', 'property' => 'P42'))), 'snaks-order' => array('P42', 'P24'));
     $this->assertTrue($reference->equals($referenceDeserializer->deserialize($serialization)));
 }
 /**
  * @since 0.4
  *
  * @param ReferenceList $references
  * @param Summary|null $summary
  *
  * @throws ChangeOpException
  */
 protected function setReference(ReferenceList $references, Summary $summary = null)
 {
     if (!$references->hasReferenceHash($this->referenceHash)) {
         throw new ChangeOpException("Reference with hash {$this->referenceHash} does not exist");
     }
     $currentIndex = $references->indexOf($this->reference);
     if ($this->index === null && $currentIndex !== false) {
         // Set index to current index to not have the reference removed and appended but
         // retain its position within the list of references.
         $this->index = $currentIndex;
     }
     if ($references->hasReference($this->reference) && $this->index === $currentIndex) {
         throw new ChangeOpException('The statement has already a reference with hash ' . $this->reference->getHash() . ' and index (' . $currentIndex . ') is not changed');
     }
     $references->removeReferenceHash($this->referenceHash);
     $references->addReference($this->reference, $this->index);
     $this->updateSummary($summary, 'set');
 }
 public function provideValidate()
 {
     $p11 = new PropertyId('P11');
     $q17 = new ItemId('Q17');
     $item = new Item($q17);
     $claimGuid = $this->mockProvider->getGuidGenerator()->newGuid($q17);
     $oldSnak = new PropertyValueSnak($p11, new StringValue("old reference"));
     $oldReference = new Reference(new SnakList(array($oldSnak)));
     $snak = new PropertyNoValueSnak($p11);
     $qualifiers = new SnakList(array($oldSnak));
     $item->getStatements()->addNewStatement($snak, $qualifiers, null, $claimGuid);
     //NOTE: the mock validator will consider the string "INVALID" to be invalid.
     $badSnak = new PropertyValueSnak($p11, new StringValue('INVALID'));
     $brokenSnak = new PropertyValueSnak($p11, new NumberValue(23));
     $badReference = new Reference(new SnakList(array($badSnak)));
     $brokenReference = new Reference(new SnakList(array($brokenSnak)));
     $refHash = $oldReference->getHash();
     $cases = array();
     $cases['invalid snak value'] = array($item, $claimGuid, $badReference, '');
     $cases['invalid snak value type'] = array($item, $claimGuid, $brokenReference, $refHash);
     return $cases;
 }
 /**
  * Generates the HTML for a Reference object.
  *
  * @param Reference $reference
  *
  * @return string HTML
  */
 private function getHtmlForReference(Reference $reference)
 {
     $snaks = $reference->getSnaks();
     $referenceSnaksByProperty = new ByPropertyIdGrouper($snaks);
     $snaklistviewsHtml = '';
     foreach ($referenceSnaksByProperty->getPropertyIds() as $propertyId) {
         $snaklistviewsHtml .= $this->getSnaklistviewHtml($referenceSnaksByProperty->getByPropertyId($propertyId));
     }
     return $this->templateFactory->render('wikibase-referenceview', 'wikibase-referenceview-' . $reference->getHash(), $snaklistviewsHtml);
 }
示例#7
0
 /**
  * Validate a list of references.
  * This is done by validating all snaks in all of the references.
  *
  * @param Reference $reference
  *
  * @return Result
  */
 public function validateReference(Reference $reference)
 {
     foreach ($reference->getSnaks() as $snak) {
         $result = $this->validate($snak);
         if (!$result->isValid()) {
             return $result;
         }
     }
     return Result::newSuccess();
 }
 public function testRequests()
 {
     $reference = new Reference(array(new PropertySomeValueSnak(100)));
     $referenceHash = $reference->getHash();
     $statement = $this->getNewItemWithStatementAndReference(array($reference));
     $guid = $statement->getGuid();
     // Replace the reference with this new one
     $newReference = new Reference(new SnakList(array(new PropertyNoValueSnak(self::$propertyIds[1]))));
     $serializedReferenceResult = $this->makeValidRequest($guid, $referenceHash, $newReference);
     // Since the reference got modified, the hash should no longer match
     $propertyIdString = self::$propertyIds[0]->getSerialization();
     $this->makeInvalidRequest($guid, $referenceHash, '{"' . $propertyIdString . '":[{"snaktype":"novalue","property":"' . $propertyIdString . '"}]}', '["' . $propertyIdString . '"]', 'no-such-reference');
     // Replace the previous reference with a reference with 2 snaks
     $aditionalReference = new Reference(new SnakList(array(new PropertyNoValueSnak(self::$propertyIds[0]), new PropertyNoValueSnak(self::$propertyIds[1]))));
     $serializedReferenceResult = $this->makeValidRequest($guid, $serializedReferenceResult['hash'], $aditionalReference);
     // Reorder reference snaks by moving the last property id to the front:
     $firstPropertyId = array_shift($serializedReferenceResult['snaks-order']);
     array_push($serializedReferenceResult['snaks-order'], $firstPropertyId);
     $this->makeValidRequest($guid, $serializedReferenceResult['hash'], $serializedReferenceResult);
 }
示例#9
0
 public function testIsEmpty()
 {
     $emptyReference = new Reference();
     $this->assertTrue($emptyReference->isEmpty());
     $referenceWithSnak = new Reference(array(new PropertyNoValueSnak(1)));
     $this->assertFalse($referenceWithSnak->isEmpty());
     $referenceWithSnaks = new Reference(array(new PropertyNoValueSnak(1), new PropertyNoValueSnak(2)));
     $this->assertFalse($referenceWithSnaks->isEmpty());
 }
示例#10
0
 private function getSerialized(Reference $reference)
 {
     return array('hash' => $reference->getHash(), 'snaks' => $this->snaksSerializer->serialize($reference->getSnaks()), 'snaks-order' => $this->buildSnakListOrderList($reference->getSnaks()));
 }
示例#11
0
 /**
  * Removes the reference with the same hash as the provided reference if such a reference exists in the list.
  *
  * @since 0.1
  *
  * @param Reference $reference
  */
 public function removeReference(Reference $reference)
 {
     $this->removeReferenceHash($reference->getHash());
 }