Thiemo Mättig (WMDE) has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/391057 )

Change subject: Add test for wbsetclaim API on a Form and make it pass
......................................................................

Add test for wbsetclaim API on a Form and make it pass

At the moment this patch re-submits elements from the previously
submitted experiment I13f232b. I could as well reuse that experiment.

This also depends on
https://github.com/wmde/WikibaseDataModel/pull/760

Bug: T163724
Change-Id: Ibad2da2bf0201b3d6b0d106350292189d7ca5035
Depends-On: I581a2be249c105a971da2c719d605a812fd48013
---
M WikibaseLexeme.entitytypes.php
A src/Content/FormHandler.php
M src/DataModel/Form.php
M src/DataModel/FormId.php
M src/DataModel/Lexeme.php
M src/WikibaseLexeme.hooks.php
A tests/phpunit/mediawiki/Api/FormSetClaimTest.php
M tests/phpunit/mediawiki/Api/LexemeEditEntityTest.php
M tests/phpunit/mediawiki/Api/LexemeGetEntitiesTest.php
9 files changed, 209 insertions(+), 19 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseLexeme 
refs/changes/57/391057/1

diff --git a/WikibaseLexeme.entitytypes.php b/WikibaseLexeme.entitytypes.php
index d830937..fb1b419 100644
--- a/WikibaseLexeme.entitytypes.php
+++ b/WikibaseLexeme.entitytypes.php
@@ -22,8 +22,10 @@
 use Wikibase\Lexeme\ChangeOp\Deserialization\LemmaChangeOpDeserializer;
 use Wikibase\Lexeme\ChangeOp\Deserialization\LexemeChangeOpDeserializer;
 use 
Wikibase\Lexeme\ChangeOp\Deserialization\LexicalCategoryChangeOpDeserializer;
+use Wikibase\Lexeme\Content\FormHandler;
 use Wikibase\Lexeme\Content\LexemeContent;
 use Wikibase\Lexeme\Content\LexemeHandler;
+use Wikibase\Lexeme\DataModel\FormId;
 use Wikibase\Lexeme\DataModel\Lexeme;
 use Wikibase\Lexeme\DataModel\LexemeId;
 use Wikibase\Lexeme\DataModel\Serialization\ExternalLexemeSerializer;
@@ -187,5 +189,14 @@
                                $basicEntityDiffVisualizer
                        );
                }
-       ]
+       ],
+       'form' => [
+               'content-handler-factory-callback' => function () {
+                       return new FormHandler();
+               },
+               'entity-id-pattern' => '/^L[1-9]\d*-F[1-9]\d*\z/',
+               'entity-id-builder' => function ( $serialization ) {
+                       return new FormId( $serialization );
+               },
+       ],
 ];
diff --git a/src/Content/FormHandler.php b/src/Content/FormHandler.php
new file mode 100644
index 0000000..de9ab14
--- /dev/null
+++ b/src/Content/FormHandler.php
@@ -0,0 +1,33 @@
+<?php
+
+namespace Wikibase\Lexeme\Content;
+
+use LogicException;
+use Wikibase\Content\EntityHolder;
+use Wikibase\Repo\Content\EntityHandler;
+
+/**
+ * @license GPL-2.0+
+ */
+class FormHandler extends EntityHandler {
+
+       public function __construct() {
+       }
+
+       public function getEntityType() {
+               return 'form';
+       }
+
+       public function makeEntityId( $id ) {
+               throw new LogicException( 'TBD' );
+       }
+
+       public function makeEmptyEntity() {
+               throw new LogicException( 'TBD' );
+       }
+
+       protected function newEntityContent( EntityHolder $entityHolder = null 
) {
+               throw new LogicException( 'TBD' );
+       }
+
+}
diff --git a/src/DataModel/Form.php b/src/DataModel/Form.php
index 2ba1464..1e6d90f 100644
--- a/src/DataModel/Form.php
+++ b/src/DataModel/Form.php
@@ -3,6 +3,8 @@
 namespace Wikibase\Lexeme\DataModel;
 
 use InvalidArgumentException;
+use Wikibase\DataModel\Entity\EntityDocument;
+use Wikibase\DataModel\Entity\EntityId;
 use Wikibase\DataModel\Statement\StatementList;
 use Wikibase\DataModel\Statement\StatementListProvider;
 use Wikibase\DataModel\Entity\ItemId;
@@ -12,7 +14,7 @@
 /**
  * @license GPL-2.0+
  */
-class Form implements StatementListProvider {
+class Form implements EntityDocument, StatementListProvider {
 
        /**
         * @var FormId
@@ -60,11 +62,19 @@
                $this->statementList = $statementList ?: new StatementList();
        }
 
+       public function getType() {
+               return 'form';
+       }
+
        /**
         * @return FormId
         */
        public function getId() {
                return $this->id;
+       }
+
+       public function setId( $id ) {
+               throw new LogicException( 'TBD' );
        }
 
        /**
@@ -105,4 +115,16 @@
                return $this->statementList;
        }
 
+       public function isEmpty() {
+               throw new LogicException( 'TBD' );
+       }
+
+       public function equals( $target ) {
+               throw new LogicException( 'TBD' );
+       }
+
+       public function copy() {
+               throw new LogicException( 'TBD' );
+       }
+
 }
diff --git a/src/DataModel/FormId.php b/src/DataModel/FormId.php
index a54e2a0..a8a6fbd 100644
--- a/src/DataModel/FormId.php
+++ b/src/DataModel/FormId.php
@@ -2,18 +2,16 @@
 
 namespace Wikibase\Lexeme\DataModel;
 
+use Wikibase\DataModel\Entity\HierarchicalEntityId;
 use Wikimedia\Assert\Assert;
 
 /**
  * @license GPL-2.0+
  * @author Thiemo Mättig
  */
-class FormId {
+class FormId extends HierarchicalEntityId {
 
-       /**
-        * @var string
-        */
-       private $serialization;
+       const PATTERN = '/^L[1-9]\d*-F[1-9]\d*\z/';
 
        /**
         * @param string $serialization
@@ -21,19 +19,28 @@
        public function __construct( $serialization ) {
                Assert::parameterType( 'string', $serialization, 
'$serialization' );
                Assert::parameter(
-                       preg_match( '/^F[1-9]\d*\z/', $serialization ),
+                       preg_match( self::PATTERN, $serialization ),
                        '$serialization',
-                       'Form ID must match "F[1-9]\d*", given: ' . 
$serialization
+                       'must match ' . self::PATTERN
                );
 
-               $this->serialization = $serialization;
+               list ( $this->repositoryName, $this->localPart ) = 
self::extractRepositoryNameAndLocalPart( $serialization );
+
+               $parts = explode( '-', $this->localPart );
+               parent::__construct( new LexemeId( $parts[0] ), $parts[1] );
        }
 
-       /**
-        * @return string
-        */
-       public function getSerialization() {
+       public function getEntityType() {
+               return 'form';
+       }
+
+       public function serialize() {
                return $this->serialization;
        }
 
+       public function unserialize( $serialized ) {
+               $this->serialization = $serialized;
+               list ( $this->repositoryName, $this->localPart ) = 
self::extractRepositoryNameAndLocalPart( $serialized );
+       }
+
 }
diff --git a/src/DataModel/Lexeme.php b/src/DataModel/Lexeme.php
index f0462ed..b7f5f24 100644
--- a/src/DataModel/Lexeme.php
+++ b/src/DataModel/Lexeme.php
@@ -3,8 +3,11 @@
 namespace Wikibase\Lexeme\DataModel;
 
 use InvalidArgumentException;
+use LogicException;
+use OutOfBoundsException;
 use OutOfRangeException;
 use UnexpectedValueException;
+use Wikibase\DataModel\Entity\EntityContainer;
 use Wikibase\DataModel\Entity\EntityDocument;
 use Wikibase\DataModel\Entity\EntityId;
 use Wikibase\DataModel\Entity\ItemId;
@@ -15,7 +18,7 @@
 /**
  * @license GPL-2.0+
  */
-class Lexeme implements EntityDocument, StatementListProvider {
+class Lexeme implements EntityContainer, EntityDocument, StatementListProvider 
{
 
        const ENTITY_TYPE = 'lexeme';
 
@@ -407,4 +410,34 @@
                }
        }
 
+       public function getEntity( EntityId $id ) {
+               if ( $id instanceof FormId ) {
+                       return $this->getForm( $id );
+               }
+
+               throw new LogicException( 'TBD' );
+       }
+
+       public function assignFreshId( EntityDocument $entity ) {
+               throw new LogicException( 'TBD' );
+       }
+
+       public function canAddWithCustomId( EntityId $id ) {
+               throw new LogicException( 'TBD' );
+       }
+
+       public function putEntity( EntityDocument $entity ) {
+               if ( $entity instanceof Form ) {
+                       $this->forms->remove( $entity->getId() );
+                       $this->forms->add( $entity );
+                       return;
+               }
+
+               throw new LogicException( 'TBD' );
+       }
+
+       public function removeEntity( EntityId $id ) {
+               throw new LogicException( 'TBD' );
+       }
+
 }
diff --git a/src/WikibaseLexeme.hooks.php b/src/WikibaseLexeme.hooks.php
index c659b17..e4580db 100644
--- a/src/WikibaseLexeme.hooks.php
+++ b/src/WikibaseLexeme.hooks.php
@@ -24,6 +24,7 @@
 
                // Setting the namespace to false disabled automatic 
registration.
                $entityNamespacesSetting['lexeme'] = $config->get( 
'LexemeNamespace' );
+               $entityNamespacesSetting['form'] = $config->get( 
'LexemeNamespace' );
        }
 
        /**
diff --git a/tests/phpunit/mediawiki/Api/FormSetClaimTest.php 
b/tests/phpunit/mediawiki/Api/FormSetClaimTest.php
new file mode 100644
index 0000000..da1ab32
--- /dev/null
+++ b/tests/phpunit/mediawiki/Api/FormSetClaimTest.php
@@ -0,0 +1,87 @@
+<?php
+
+namespace Wikibase\Lexeme\Tests\MediaWiki\Api;
+
+use DataValues\Serializers\DataValueSerializer;
+use DataValues\StringValue;
+use FormatJson;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Entity\Property;
+use Wikibase\DataModel\Entity\PropertyId;
+use Wikibase\DataModel\SerializerFactory;
+use Wikibase\DataModel\Snak\PropertyValueSnak;
+use Wikibase\DataModel\Statement\Statement;
+use Wikibase\DataModel\Term\Term;
+use Wikibase\DataModel\Term\TermList;
+use Wikibase\Lexeme\DataModel\Form;
+use Wikibase\Lexeme\DataModel\FormId;
+use Wikibase\Lexeme\DataModel\FormSet;
+use Wikibase\Lexeme\DataModel\Lexeme;
+use Wikibase\Lexeme\DataModel\LexemeId;
+use Wikibase\Repo\Tests\Api\WikibaseApiTestCase;
+use Wikibase\Repo\WikibaseRepo;
+
+/**
+ * @group Database
+ * @group medium
+ *
+ * @license GPL-2.0+
+ */
+class FormSetClaimTest extends WikibaseApiTestCase {
+
+       public function testSetClaimApi() {
+               $store = WikibaseRepo::getDefaultInstance()->getEntityStore();
+
+               $propertyId = new PropertyId( 'P1' );
+               $property = new Property( $propertyId, null, 'string' );
+               $store->saveEntity( $property, self::class, 
$this->getTestUser()->getUser() );
+
+               $lexeme = new Lexeme(
+                       new LexemeId( 'L1' ),
+                       null,
+                       new ItemId( 'Q1' ),
+                       new ItemId( 'Q1' ),
+                       null,
+                       2,
+                       new FormSet( [
+                               new Form( new FormId( 'L1-F1' ), new TermList( 
[ new Term( 'en', 'representation' ) ] ), [] ),
+                       ] )
+               );
+               $store->saveEntity( $lexeme, self::class, 
$this->getTestUser()->getUser() );
+
+               $guid = 'L1-F1$00000000-0000-0000-0000-000000000000';
+               $statement = new Statement(
+                       new PropertyValueSnak( $propertyId, new StringValue( 
'Test me' ) ),
+                       null,
+                       null,
+                       $guid
+               );
+
+               $serializerFactory = new SerializerFactory( new 
DataValueSerializer() );
+               $statementSerializer = 
$serializerFactory->newStatementSerializer();
+
+               $params = [
+                       'action' => 'wbsetclaim',
+                       'claim' => FormatJson::encode( 
$statementSerializer->serialize( $statement ) ),
+               ];
+
+               list ( $result, ) = $this->doApiRequestWithToken( $params );
+
+               $this->assertSame( [
+                       'success' => 1,
+                       'claim' => [
+                               'mainsnak' => [
+                                       'snaktype' => 'value',
+                                       'property' => 'P1',
+                                       'hash' => 
'ee2c5102168a4119a96ef7a1713dfd14c758d931',
+                                       'datavalue' => [ 'value' => 'Test me', 
'type' => 'string' ],
+                                       'datatype' => 'string',
+                               ],
+                               'type' => 'statement',
+                               'id' => 
'L1-F1$00000000-0000-0000-0000-000000000000',
+                               'rank' => 'normal',
+                       ],
+               ], $result );
+       }
+
+}
diff --git a/tests/phpunit/mediawiki/Api/LexemeEditEntityTest.php 
b/tests/phpunit/mediawiki/Api/LexemeEditEntityTest.php
index f6405e3..761f205 100644
--- a/tests/phpunit/mediawiki/Api/LexemeEditEntityTest.php
+++ b/tests/phpunit/mediawiki/Api/LexemeEditEntityTest.php
@@ -16,8 +16,6 @@
 use Wikibase\Repo\WikibaseRepo;
 
 /**
- * @covers \Wikibase\Repo\Api\EditEntity
- *
  * @license GPL-2.0+
  *
  * @group API
diff --git a/tests/phpunit/mediawiki/Api/LexemeGetEntitiesTest.php 
b/tests/phpunit/mediawiki/Api/LexemeGetEntitiesTest.php
index 0b32af3..e14d4e8 100644
--- a/tests/phpunit/mediawiki/Api/LexemeGetEntitiesTest.php
+++ b/tests/phpunit/mediawiki/Api/LexemeGetEntitiesTest.php
@@ -12,8 +12,6 @@
 use Wikibase\Repo\WikibaseRepo;
 
 /**
- * @covers \Wikibase\Lexeme\DataModel\Serialization\ExternalLexemeSerializer
- *
  * @license GPL-2.0+
  *
  * @group Database

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ibad2da2bf0201b3d6b0d106350292189d7ca5035
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseLexeme
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <thiemo.kr...@wikimedia.de>

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

Reply via email to