jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/334148 )

Change subject: Add ChangeOpDeserializer for properties
......................................................................


Add ChangeOpDeserializer for properties

Bug: T154288
Change-Id: If8a58265c2a3d1dd807364a6a70408708a799fed
---
A repo/includes/ChangeOp/Deserialization/PropertyChangeOpDeserializer.php
A 
repo/tests/phpunit/includes/ChangeOp/Deserialization/PropertyChangeOpDeserializerTest.php
2 files changed, 166 insertions(+), 0 deletions(-)

Approvals:
  WMDE-leszek: Looks good to me, approved
  jenkins-bot: Verified



diff --git 
a/repo/includes/ChangeOp/Deserialization/PropertyChangeOpDeserializer.php 
b/repo/includes/ChangeOp/Deserialization/PropertyChangeOpDeserializer.php
new file mode 100644
index 0000000..9c7cf5e
--- /dev/null
+++ b/repo/includes/ChangeOp/Deserialization/PropertyChangeOpDeserializer.php
@@ -0,0 +1,68 @@
+<?php
+
+namespace Wikibase\Repo\ChangeOp\Deserialization;
+
+use Wikibase\ChangeOp\ChangeOp;
+use Wikibase\ChangeOp\ChangeOps;
+use Wikibase\Repo\ChangeOp\ChangeOpDeserializer;
+
+/**
+ * Constructs ChangeOp objects for property change requests
+ *
+ * @license GPL-2.0+
+ */
+class PropertyChangeOpDeserializer implements ChangeOpDeserializer {
+
+       /**
+        * @var ChangeOpDeserializerFactory
+        */
+       private $factory;
+
+       public function __construct( ChangeOpDeserializerFactory $factory ) {
+               $this->factory = $factory;
+       }
+
+       /**
+        * @see ChangeOpDeserializer::createEntityChangeOp
+        *
+        * @param array[] $changeRequest
+        *
+        * @return ChangeOp
+        *
+        * @throws ChangeOpDeserializationException
+        */
+       public function createEntityChangeOp( array $changeRequest ) {
+               $changeOps = new ChangeOps();
+
+               if ( array_key_exists( 'labels', $changeRequest ) ) {
+                       $changeOps->add(
+                               
$this->factory->getLabelsChangeOpDeserializer()->createEntityChangeOp( 
$changeRequest )
+                       );
+               }
+
+               if ( array_key_exists( 'descriptions', $changeRequest ) ) {
+                       $changeOps->add(
+                               
$this->factory->getDescriptionsChangeOpDeserializer()->createEntityChangeOp( 
$changeRequest )
+                       );
+               }
+
+               if ( array_key_exists( 'aliases', $changeRequest ) ) {
+                       $changeOps->add(
+                               
$this->factory->getAliasesChangeOpDeserializer()->createEntityChangeOp( 
$changeRequest )
+                       );
+               }
+
+               if ( array_key_exists( 'claims', $changeRequest ) ) {
+                       $changeOps->add(
+                               
$this->factory->getClaimsChangeOpDeserializer()->createEntityChangeOp( 
$changeRequest )
+                       );
+               }
+
+               if ( array_key_exists( 'sitelinks', $changeRequest ) ) {
+                       throw new ChangeOpDeserializationException( 'Non Items 
cannot have sitelinks', 'not-supported' );
+               }
+
+               return $changeOps;
+       }
+
+}
diff --git 
a/repo/tests/phpunit/includes/ChangeOp/Deserialization/PropertyChangeOpDeserializerTest.php
 
b/repo/tests/phpunit/includes/ChangeOp/Deserialization/PropertyChangeOpDeserializerTest.php
new file mode 100644
index 0000000..a3ca75d
--- /dev/null
+++ 
b/repo/tests/phpunit/includes/ChangeOp/Deserialization/PropertyChangeOpDeserializerTest.php
@@ -0,0 +1,98 @@
+<?php
+
+namespace Wikibase\Repo\Tests\ChangeOp\Deserialization;
+
+use HashSiteStore;
+use Wikibase\DataModel\Entity\Property;
+use Wikibase\DataModel\Entity\PropertyId;
+use Wikibase\DataModel\Snak\PropertyNoValueSnak;
+use Wikibase\DataModel\Statement\Statement;
+use Wikibase\Lib\StaticContentLanguages;
+use Wikibase\Repo\ChangeOp\Deserialization\ChangeOpDeserializationException;
+use Wikibase\Repo\ChangeOp\Deserialization\ChangeOpDeserializerFactory;
+use Wikibase\Repo\ChangeOp\Deserialization\PropertyChangeOpDeserializer;
+use 
Wikibase\Repo\ChangeOp\Deserialization\SiteLinkBadgeChangeOpSerializationValidator;
+use Wikibase\Repo\ChangeOp\Deserialization\TermChangeOpSerializationValidator;
+use Wikibase\Repo\SiteLinkTargetProvider;
+use Wikibase\Repo\WikibaseRepo;
+use Wikibase\Summary;
+
+/**
+ * @covers Wikibase\Repo\ChangeOpDeserializers\PropertyChangeOpDeserializer
+ *
+ * @group Wikibase
+ *
+ * @license GPL-2.0+
+ */
+class PropertyChangeOpDeserializerTest extends \PHPUnit_Framework_TestCase 
implements ChangeOpDeserializerTest {
+
+       use AliasChangeOpDeserializationTester;
+       use ClaimsChangeOpDeserializationTester;
+       use DescriptionsChangeOpDeserializationTester;
+       use LabelsChangeOpDeserializationTester;
+
+       public function getEntity() {
+               return new Property( new PropertyId( 'P100' ), null, 'foo' );
+       }
+
+       public function getChangeOpDeserializer() {
+               $wikibaseRepo = WikibaseRepo::getDefaultInstance();
+               $changeOpFactoryProvider = 
$wikibaseRepo->getChangeOpFactoryProvider();
+
+               return new PropertyChangeOpDeserializer( new 
ChangeOpDeserializerFactory(
+                       
$changeOpFactoryProvider->getFingerprintChangeOpFactory(),
+                       $changeOpFactoryProvider->getStatementChangeOpFactory(),
+                       $changeOpFactoryProvider->getSiteLinkChangeOpFactory(),
+                       new TermChangeOpSerializationValidator( new 
StaticContentLanguages( [ 'en' ] ) ),
+                       new SiteLinkBadgeChangeOpSerializationValidator(
+                               $wikibaseRepo->getEntityTitleLookup(),
+                               []
+                       ),
+                       $wikibaseRepo->getExternalFormatStatementDeserializer(),
+                       new SiteLinkTargetProvider( new HashSiteStore() ),
+                       $wikibaseRepo->getEntityIdParser(),
+                       $wikibaseRepo->getStringNormalizer(),
+                       []
+               ) );
+       }
+
+       public function 
testGivenAllPropertyFieldsInChangeRequest_changeOpChangesAllFieldsOnceApplied() 
{
+               $property = $this->getEntity();
+
+               $newAlias = 'test-alias';
+               $newLabel = 'test-label';
+               $newDescription = 'test-description';
+
+               $otherProperty = new PropertyId( 'P7' );
+               $statement = new Statement( new PropertyNoValueSnak( 
$otherProperty ) );
+               $statementSerialization = 
WikibaseRepo::getDefaultInstance()->getStatementSerializer()->serialize( 
$statement );
+
+               $changeRequest = [
+                       'aliases' => [ 'en' => [ 'language' => 'en', 'value' => 
$newAlias ] ],
+                       'labels' => [ 'en' => [ 'language' => 'en', 'value' => 
$newLabel ] ],
+                       'descriptions' => [ 'en' => [ 'language' => 'en', 
'value' => $newDescription ] ],
+                       'claims' => [ $statementSerialization ],
+               ];
+
+               $deserializer = $this->getChangeOpDeserializer();
+
+               $changeOp = $deserializer->createEntityChangeOp( $changeRequest 
);
+
+               $changeOp->apply( $property, new Summary() );
+
+               $this->assertSame( [ $newAlias ], 
$property->getAliasGroups()->getByLanguage( 'en' )->getAliases() );
+               $this->assertSame( $newLabel, 
$property->getLabels()->getByLanguage( 'en' )->getText() );
+               $this->assertSame( $newDescription, 
$property->getDescriptions()->getByLanguage( 'en' )->getText() );
+
+               $this->assertFalse( 
$property->getStatements()->getByPropertyId( $otherProperty )->isEmpty() );
+       }
+
+       public function 
testGivenSitelinkFieldInChangeRequest_createEntityChangeOpThrowsException() {
+               $deserializer = $this->getChangeOpDeserializer();
+
+               $this->setExpectedException( 
ChangeOpDeserializationException::class );
+
+               $deserializer->createEntityChangeOp( [ 'sitelinks' => [ 
'site-link-change-data' ] ] );
+       }
+
+}

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

Gerrit-MessageType: merged
Gerrit-Change-Id: If8a58265c2a3d1dd807364a6a70408708a799fed
Gerrit-PatchSet: 17
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: WMDE-leszek <[email protected]>
Gerrit-Reviewer: Aleksey Bekh-Ivanov (WMDE) <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Jakob <[email protected]>
Gerrit-Reviewer: Ladsgroup <[email protected]>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: WMDE-leszek <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to