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

Change subject: Make Form extend EntityDocument
......................................................................


Make Form extend EntityDocument

Form::setId is short-circuited with an exception for now to make this
patch more concise.

Bug: T180469
Change-Id: Ib6a7ca4b0e42b3fc1535a20eb48976aeb1abaf8f
---
M src/DataModel/Form.php
M tests/phpunit/composer/DataModel/FormTest.php
2 files changed, 212 insertions(+), 2 deletions(-)

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



diff --git a/src/DataModel/Form.php b/src/DataModel/Form.php
index 2ba1464..ca54bec 100644
--- a/src/DataModel/Form.php
+++ b/src/DataModel/Form.php
@@ -3,6 +3,8 @@
 namespace Wikibase\Lexeme\DataModel;
 
 use InvalidArgumentException;
+use LogicException;
+use Wikibase\DataModel\Entity\EntityDocument;
 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
@@ -61,10 +63,28 @@
        }
 
        /**
+        * @return string
+        */
+       public function getType() {
+               return 'form';
+       }
+
+       /**
         * @return FormId
         */
        public function getId() {
                return $this->id;
+       }
+
+       /**
+        * @param FormId $id
+        *
+        * @throws LogicException always
+        */
+       public function setId( $id ) {
+               throw new LogicException( 'Setting the ID of a Form is 
currently not implemented, and '
+                       . 'might not be needed any more, except when 
implementing the "clear" feature of the '
+                       . '"wbeditentity" API' );
        }
 
        /**
@@ -105,4 +125,51 @@
                return $this->statementList;
        }
 
+       /**
+        * @see EntityDocument::isEmpty
+        *
+        * @return bool
+        */
+       public function isEmpty() {
+               return $this->representations->isEmpty()
+                       && $this->grammaticalFeatures === []
+                       && $this->statementList->isEmpty();
+       }
+
+       /**
+        * @see EntityDocument::equals
+        *
+        * @param mixed $target
+        *
+        * @return bool True if the forms contents are equal. Does not consider 
the ID.
+        */
+       public function equals( $target ) {
+               if ( $this === $target ) {
+                       return true;
+               }
+
+               return $target instanceof self
+                       && $this->representations->equals( 
$target->representations )
+                       && $this->grammaticalFeatures == 
$target->grammaticalFeatures
+                       && $this->statementList->equals( $target->statementList 
);
+       }
+
+       /**
+        * @see EntityDocument::copy
+        *
+        * @return self
+        */
+       public function copy() {
+               return clone $this;
+       }
+
+       /**
+        * The forms ID and grammatical features (a set of ItemIds) are 
immutable and don't need
+        * individual cloning.
+        */
+       public function __clone() {
+               $this->representations = clone $this->representations;
+               $this->statementList = clone $this->statementList;
+       }
+
 }
diff --git a/tests/phpunit/composer/DataModel/FormTest.php 
b/tests/phpunit/composer/DataModel/FormTest.php
index 10bd239..0a80dd8 100644
--- a/tests/phpunit/composer/DataModel/FormTest.php
+++ b/tests/phpunit/composer/DataModel/FormTest.php
@@ -3,8 +3,12 @@
 namespace Wikibase\Lexeme\Tests\DataModel;
 
 use InvalidArgumentException;
+use LogicException;
 use PHPUnit_Framework_TestCase;
 use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Entity\PropertyId;
+use Wikibase\DataModel\Snak\PropertyNoValueSnak;
+use Wikibase\DataModel\Statement\Statement;
 use Wikibase\DataModel\Term\Term;
 use Wikibase\DataModel\Term\TermList;
 use Wikibase\Lexeme\DataModel\Form;
@@ -25,11 +29,12 @@
        }
 
        public function testCreateFormWithOneRepresentation_CreatesIt() {
-               new Form(
+               $form = new Form(
                        new FormId( 'L1-F1' ),
                        new TermList( [ new Term( 'en', 'representation' ) ] ),
                        []
                );
+               $this->assertCount( 1, $form->getRepresentations() );
        }
 
        public function 
testCreateForm_GrammaticalFeaturesIsNotAnArrayOfItemIds_ThrowsAnException() {
@@ -67,4 +72,142 @@
                $form->setGrammaticalFeatures( [ "Q1" ] );
        }
 
+       public function testFormIdCanNotBeChanged() {
+               $form = NewForm::havingId( 'F1' )->build();
+               $this->setExpectedException( LogicException::class );
+               $form->setId( new FormId( 'L1-F2' ) );
+       }
+
+       public function 
testGivenFormWithInitiallyRequiredRepresentation_isNotEmpty() {
+               $form = NewForm::havingRepresentation( 'en', 'one' )->build();
+               $this->assertFalse( $form->isEmpty() );
+       }
+
+       public function 
testGivenFormWithInitiallyRequiredRepresentationRemoved_isEmpty() {
+               $form = NewForm::havingRepresentation( 'en', 'one' )->build();
+               $form->getRepresentations()->removeByLanguage( 'en' );
+               $this->assertTrue( $form->isEmpty() );
+       }
+
+       public function provideNonEmptyForms() {
+               return [
+                       '2 representations' => [
+                               NewForm::havingRepresentation( 'en', 'one' )
+                                       ->andRepresentation( 'fr', 'two' )
+                                       ->build()
+                       ],
+                       '1 grammatical feature' => [
+                               NewForm::havingGrammaticalFeature( 'Q1' )
+                                       ->build()
+                       ],
+                       '1 statement' => [
+                               NewForm::havingStatement( $this->newStatement() 
)
+                                       ->build()
+                       ],
+               ];
+       }
+
+       /**
+        * @dataProvider provideNonEmptyForms
+        */
+       public function testGivenFormWithOptionalElements_isNotEmpty( Form 
$form ) {
+               $this->assertFalse( $form->isEmpty() );
+       }
+
+       public function provideEqualForms() {
+               $minimal = NewForm::havingId( 'F1' )->andRepresentation( 'en', 
'minimal' );
+               $nonEmpty = $minimal->andGrammaticalFeature( 'Q1' )
+                       ->andStatement( $this->newStatement() );
+
+               $minimalInstance = $minimal->build();
+
+               return [
+                       'same instance' => [
+                               $minimalInstance,
+                               $minimalInstance
+                       ],
+                       'minimal forms' => [
+                               $minimal->build(),
+                               $minimal->build()
+                       ],
+                       'different IDs' => [
+                               $minimal->build(),
+                               NewForm::havingId( 'F2' )->andRepresentation( 
'en', 'minimal' )->build()
+                       ],
+                       'non-empty forms' => [
+                               $nonEmpty->build(),
+                               $nonEmpty->build()
+                       ],
+                       'multiple grammatical features' => [
+                               $nonEmpty->andGrammaticalFeature( 'Q2' 
)->build(),
+                               $nonEmpty->andGrammaticalFeature( 'Q2' 
)->build()
+                       ],
+                       'grammatical features in different order' => [
+                               $minimal->andGrammaticalFeature( 'Q1' 
)->andGrammaticalFeature( 'Q2' )->build(),
+                               $minimal->andGrammaticalFeature( 'Q2' 
)->andGrammaticalFeature( 'Q1' )->build()
+                       ],
+               ];
+       }
+
+       /**
+        * @dataProvider provideEqualForms
+        */
+       public function testGivenEqualForms_areEqual( Form $form1, Form $form2 
) {
+               $this->assertTrue( $form1->equals( $form2 ) );
+       }
+
+       public function provideUnequalForms() {
+               $form = NewForm::havingId( 'F1' )->andRepresentation( 'en', 
'minimal' );
+
+               return [
+                       'different representations' => [
+                               $form->build(),
+                               NewForm::havingId( 'F1' )->andRepresentation( 
'en', 'different' )->build()
+                       ],
+                       '+1 representation' => [
+                               $form->build(),
+                               $form->andRepresentation( 'fr', 'two' )->build()
+                       ],
+                       '+1 grammatical feature' => [
+                               $form->build(),
+                               $form->andGrammaticalFeature( 'Q1' )->build()
+                       ],
+                       '+1 statement' => [
+                               $form->build(),
+                               $form->andStatement( $this->newStatement() 
)->build()
+                       ],
+               ];
+       }
+
+       /**
+        * @dataProvider provideUnequalForms
+        */
+       public function testGivenUnequalForms_areNotEqual( Form $form1, Form 
$form2 ) {
+               $this->assertFalse( $form1->equals( $form2 ) );
+       }
+
+       public function testCopyIsIndependent() {
+               $original = NewForm::havingId( 'F1' )->build();
+               $copy = $original->copy();
+
+               // Edit all mutable fields on the original
+               $original->getRepresentations()->setTextForLanguage( 'en', 
'added' );
+               $original->setGrammaticalFeatures( [ new ItemId( 'Q2' ) ] );
+               $original->getStatements()->addStatement( $this->newStatement() 
);
+
+               // Make sure the original changed
+               $this->assertTrue( 
$original->getRepresentations()->hasTermForLanguage( 'en' ) );
+               $this->assertNotEmpty( $original->getGrammaticalFeatures() );
+               $this->assertFalse( $original->getStatements()->isEmpty() );
+
+               // None of these changes should make it to the copy
+               $this->assertFalse( 
$copy->getRepresentations()->hasTermForLanguage( 'en' ) );
+               $this->assertEmpty( $copy->getGrammaticalFeatures() );
+               $this->assertTrue( $copy->getStatements()->isEmpty() );
+       }
+
+       private function newStatement() {
+               return new Statement( new PropertyNoValueSnak( new PropertyId( 
'P1' ) ) );
+       }
+
 }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ib6a7ca4b0e42b3fc1535a20eb48976aeb1abaf8f
Gerrit-PatchSet: 7
Gerrit-Project: mediawiki/extensions/WikibaseLexeme
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Jonas Kress (WMDE) <[email protected]>
Gerrit-Reviewer: Lucas Werkmeister (WMDE) <[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