Aleksey Bekh-Ivanov (WMDE) has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/376044 )

Change subject: Add form API module
......................................................................

Add form API module

Change-Id: Icf04fc9aaa4256f76ff54f89aed7410333361c5a
---
A src/Api/CreateForm.php
A src/ChangeOp/ChangeOpAddForm.php
M src/DataModel/Lexeme.php
A tests/phpunit/mediawiki/ChangeOp/ChangeOpAddFormTest.php
4 files changed, 158 insertions(+), 0 deletions(-)


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

diff --git a/src/Api/CreateForm.php b/src/Api/CreateForm.php
new file mode 100644
index 0000000..fdaa855
--- /dev/null
+++ b/src/Api/CreateForm.php
@@ -0,0 +1,55 @@
+<?php
+
+namespace Wikibase\Lexeme\Api;
+
+use ApiBase;
+
+class CreateForm extends ApiBase  {
+
+       /**
+        * @see ApiBase::execute()
+        */
+       public function execute() {
+               [
+                       'lexemeId' => 'L1',
+                       'representations'=>[
+                               ['representation' => '', 'language' => ''],
+                               ['representation' => '', 'language' => ''],
+                       ],
+                       'grammaticalFeatures'=>[
+                               'Q1','Q2'
+                       ]
+               ];
+
+
+       }
+
+       /**
+        * @see ApiBase::getAllowedParams
+        */
+       protected function getAllowedParams() {
+               return array_merge(
+                       [
+                               'lexemeId' => [
+                                       self::PARAM_TYPE => 'string',
+                                       self::PARAM_REQUIRED => true,
+                               ],
+                               'data' => [
+                                       self::PARAM_TYPE => 'text',
+                                       self::PARAM_REQUIRED => true,
+                               ],
+                               'token' => [
+                                       self::PARAM_TYPE => 'string',
+                                       self::PARAM_REQUIRED => true,
+                               ],
+                               'baserevid' => [
+                                       self::PARAM_TYPE => 'integer',
+                               ],
+                               'bot' => [
+                                       self::PARAM_TYPE => 'boolean',
+                                       self::PARAM_DFLT => false,
+                               ]
+                       ]
+               );
+       }
+}
diff --git a/src/ChangeOp/ChangeOpAddForm.php b/src/ChangeOp/ChangeOpAddForm.php
new file mode 100644
index 0000000..0abeabd
--- /dev/null
+++ b/src/ChangeOp/ChangeOpAddForm.php
@@ -0,0 +1,50 @@
+<?php
+
+namespace Wikibase\Lexeme\ChangeOp;
+
+use ValueValidators\Result;
+use Wikibase\DataModel\Entity\EntityDocument;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Term\TermList;
+use Wikibase\Lexeme\DataModel\Lexeme;
+use Wikibase\Repo\ChangeOp\ChangeOp;
+use Wikibase\Repo\ChangeOp\ChangeOpBase;
+use Wikibase\Repo\Store\EntityPermissionChecker;
+use Wikibase\Summary;
+use Wikimedia\Assert\Assert;
+
+class ChangeOpAddForm extends ChangeOpBase  {
+
+       /**
+        * @var TermList
+        */
+       private $representations;
+       /**
+        * @var ItemId[]
+        */
+       private $grammaticalFeatures;
+
+       /**
+        * @param TermList $representations
+        * @param ItemId[] $grammaticalFeatures
+        */
+       public function __construct( TermList $representations, array 
$grammaticalFeatures  ) {
+
+               $this->representations = $representations;
+               $this->grammaticalFeatures = $grammaticalFeatures;
+       }
+
+       public function validate( EntityDocument $lexeme ) {
+               Assert::parameterType( Lexeme::class, $lexeme, '$entity' );
+
+               return Result::newSuccess();
+       }
+
+       public function apply( EntityDocument $lexeme, Summary $summary = null 
) {
+               Assert::parameterType( Lexeme::class, $lexeme, '$entity' );
+               /** @var Lexeme $lexeme */
+               $lexeme->addForm($this->representations, 
$this->grammaticalFeatures);
+
+//             $this->updateSummary($summary, 'add')
+       }
+}
diff --git a/src/DataModel/Lexeme.php b/src/DataModel/Lexeme.php
index 836aca2..f72bac4 100644
--- a/src/DataModel/Lexeme.php
+++ b/src/DataModel/Lexeme.php
@@ -270,4 +270,10 @@
                        && $this->lexicalCategory !== null;
        }
 
+       public function addForm(TermList $representataions, array 
$grammaticalFeatures) {
+               //FIXME: Test it
+               $formId = new FormId('F1');
+               $this->forms[] = new Form( $formId, $representataions, 
$grammaticalFeatures );
+       }
+
 }
diff --git a/tests/phpunit/mediawiki/ChangeOp/ChangeOpAddFormTest.php 
b/tests/phpunit/mediawiki/ChangeOp/ChangeOpAddFormTest.php
new file mode 100644
index 0000000..dfc3e90
--- /dev/null
+++ b/tests/phpunit/mediawiki/ChangeOp/ChangeOpAddFormTest.php
@@ -0,0 +1,47 @@
+<?php
+
+namespace Wikibase\Lexeme\Tests\MediaWiki\ChangeOp;
+
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Term\Term;
+use Wikibase\DataModel\Term\TermList;
+use Wikibase\Lexeme\ChangeOp\ChangeOpAddForm;
+use Wikibase\Lexeme\Tests\DataModel\NewLexeme;
+use Wikibase\Repo\Tests\NewItem;
+
+class ChangeOpAddFormTest extends \PHPUnit_Framework_TestCase {
+
+       public function test_validateFailsIfProvidedEntityIsNotALexeme() {
+               $changeOpAddForm = new ChangeOpAddForm(new TermList(), []);
+
+               $this->setExpectedException(\InvalidArgumentException::class);
+               $changeOpAddForm->validate( NewItem::withId( 'Q1' )->build() );
+       }
+
+       public function test_validatePassesIfProvidedEntityIsALexeme() {
+               $changeOpAddForm = new ChangeOpAddForm(new TermList(), []);
+
+               $result = $changeOpAddForm->validate( 
NewLexeme::create()->build() );
+
+               $this->assertTrue( $result->isValid() );
+       }
+
+       public function test_applyFailsIfProvidedEntityIsNotALexeme() {
+               $changeOpAddForm = new ChangeOpAddForm(new TermList(), []);
+
+               $this->setExpectedException(\InvalidArgumentException::class);
+               $changeOpAddForm->apply( NewItem::withId( 'Q1' )->build() );
+       }
+
+       public function test_applyAddsFormIfGivenALexeme() {
+               $representations = new TermList([new Term('en', 'goat')]);
+               $changeOp = new ChangeOpAddForm($representations, [ new ItemId( 
'Q1')]);
+               $lexeme = NewLexeme::create()->build();
+               $changeOp->apply( $lexeme );
+
+               $this->assertEquals($representations, 
$lexeme->getForms()[0]->getRepresentations());
+               $this->assertEquals([ new ItemId('Q1')], 
$lexeme->getForms()[0]->getGrammaticalFeatures());
+       }
+
+
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Icf04fc9aaa4256f76ff54f89aed7410333361c5a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseLexeme
Gerrit-Branch: master
Gerrit-Owner: Aleksey Bekh-Ivanov (WMDE) <aleksey.bekh-iva...@wikimedia.de>

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

Reply via email to