Jakob has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/340114 )
Change subject: Make test methods of ChangeOpDeserializerTests reusable. ...................................................................... Make test methods of ChangeOpDeserializerTests reusable. Adds the following traits to be reused in entity ChangeOpDeserializer tests as well as in tests of each of the field's ChangeOpDeserializers respectively: * AliasChangeOpDeserializationTest * ClaimsChangeOpDeserializationTest * DescriptionsChangeOpDeserializationTest * DescriptionsChangeOpDeserializationTest Change-Id: I5b3dc0eddea5a6e236cb14885d65e63d5866a310 --- A repo/tests/phpunit/includes/ChangeOp/Deserialization/AliasChangeOpDeserializationTest.php M repo/tests/phpunit/includes/ChangeOp/Deserialization/AliasesChangeOpDeserializerTest.php A repo/tests/phpunit/includes/ChangeOp/Deserialization/ChangeOpDeserializerTest.php A repo/tests/phpunit/includes/ChangeOp/Deserialization/ClaimsChangeOpDeserializationTest.php M repo/tests/phpunit/includes/ChangeOp/Deserialization/ClaimsChangeOpDeserializerTest.php A repo/tests/phpunit/includes/ChangeOp/Deserialization/DescriptionsChangeOpDeserializationTest.php M repo/tests/phpunit/includes/ChangeOp/Deserialization/DescriptionsChangeOpDeserializerTest.php M repo/tests/phpunit/includes/ChangeOp/Deserialization/ItemChangeOpDeserializerTest.php A repo/tests/phpunit/includes/ChangeOp/Deserialization/LabelsChangeOpDeserialization.php M repo/tests/phpunit/includes/ChangeOp/Deserialization/LabelsChangeOpDeserializerTest.php 10 files changed, 380 insertions(+), 292 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/14/340114/1 diff --git a/repo/tests/phpunit/includes/ChangeOp/Deserialization/AliasChangeOpDeserializationTest.php b/repo/tests/phpunit/includes/ChangeOp/Deserialization/AliasChangeOpDeserializationTest.php new file mode 100644 index 0000000..a9807c3 --- /dev/null +++ b/repo/tests/phpunit/includes/ChangeOp/Deserialization/AliasChangeOpDeserializationTest.php @@ -0,0 +1,78 @@ +<?php + +namespace Wikibase\Repo\Tests\ChangeOp\Deserialization; + +use Wikibase\DataModel\Entity\Item; +use Wikibase\Summary; + +/** + * Set of test methods that can be reused in AliasesChangeOpDeserializerTest and tests for + * ChangeOpDeserializers of entities that have aliases + */ +trait AliasChangeOpDeserializationTest { + + public function testGivenChangeRequestSettingAliasesToItemWithNoAlias_addsAlias() { + $item = $this->getItemWithoutAliases(); + $alias = 'foo'; + $changeOp = $this->getChangeOpDeserializer()->createEntityChangeOp( [ + 'aliases' => [ 'en' => [ 'language' => 'en', 'value' => $alias ] ] + ] ); + + $changeOp->apply( $item, new Summary() ); + $this->assertSame( $item->getAliasGroups()->getByLanguage( 'en' )->getAliases(), [ $alias ] ); + } + + public function testGivenChangeRequestSettingAliases_overridesExistingAliases() { + $item = $this->getItemWithExistingAliases(); + $newAlias = 'foo'; + $changeOp = $this->getChangeOpDeserializer()->createEntityChangeOp( [ + 'aliases' => [ 'en' => [ 'language' => 'en', 'value' => $newAlias ] ] + ] ); + + $changeOp->apply( $item, new Summary() ); + $this->assertSame( $item->getAliasGroups()->getByLanguage( 'en' )->getAliases(), [ $newAlias ] ); + } + + public function testGivenChangeRequestRemovingAllExistingEnAliases_enAliasGroupDoesNotExist() { + $item = $this->getItemWithExistingAliases(); + $existingAliases = $item->getAliasGroups()->getByLanguage( 'en' )->getAliases(); + $changeOp = $this->getChangeOpDeserializer()->createEntityChangeOp( [ + 'aliases' => array_map( function( $alias ) { + return [ 'language' => 'en', 'value' => $alias, 'remove' => '' ]; + }, $existingAliases ) + ] ); + + $changeOp->apply( $item, new Summary() ); + $this->assertFalse( $item->getAliasGroups()->hasGroupForLanguage( 'en' ) ); + } + + public function testGivenChangeRequestAddingAlias_addsAlias() { + $item = $this->getItemWithExistingAliases(); + $newAlias = 'foo'; + $existingAliases = $item->getAliasGroups()->getByLanguage( 'en' )->getAliases(); + $changeOp = $this->getChangeOpDeserializer()->createEntityChangeOp( [ + 'aliases' => [ + 'en' => [ 'language' => 'en', 'value' => $newAlias, 'add' => '' ] + ] + ] ); + + $changeOp->apply( $item, new Summary() ); + $this->assertSame( + array_merge( $existingAliases, [ $newAlias ] ), + $item->getAliasGroups()->getByLanguage( 'en' )->getAliases() + ); + } + + private function getItemWithoutAliases() { + return new Item(); + } + + private function getItemWithExistingAliases() { + $existingEnAliases = [ 'en-existingAlias1', 'en-existingAlias2' ]; + $item = new Item(); + $item->setAliases( 'en', $existingEnAliases ); + + return $item; + } + +} diff --git a/repo/tests/phpunit/includes/ChangeOp/Deserialization/AliasesChangeOpDeserializerTest.php b/repo/tests/phpunit/includes/ChangeOp/Deserialization/AliasesChangeOpDeserializerTest.php index ca8759a..a53249e 100644 --- a/repo/tests/phpunit/includes/ChangeOp/Deserialization/AliasesChangeOpDeserializerTest.php +++ b/repo/tests/phpunit/includes/ChangeOp/Deserialization/AliasesChangeOpDeserializerTest.php @@ -3,14 +3,12 @@ namespace Wikibase\Repo\Tests\ChangeOp\Deserialization; use Wikibase\ChangeOp\FingerprintChangeOpFactory; -use Wikibase\DataModel\Entity\Item; use Wikibase\Lib\StaticContentLanguages; use Wikibase\Repo\ChangeOp\Deserialization\AliasesChangeOpDeserializer; use Wikibase\Repo\ChangeOp\Deserialization\ChangeOpDeserializationException; use Wikibase\Repo\ChangeOp\Deserialization\TermChangeOpSerializationValidator; use Wikibase\Repo\Tests\ChangeOp\ChangeOpTestMockProvider; use Wikibase\StringNormalizer; -use Wikibase\Summary; /** * @covers Wikibase\Repo\ChangeOp\Deserialization\AliasesChangeOpDeserializer @@ -19,7 +17,9 @@ * * @license GPL-2.0+ */ -class AliasesChangeOpDeserializerTest extends \PHPUnit_Framework_TestCase { +class AliasesChangeOpDeserializerTest extends \PHPUnit_Framework_TestCase implements ChangeOpDeserializerTest { + + use AliasChangeOpDeserializationTest; public function testGivenAliasesFieldNotAnArray_createEntityChangeOpThrowsError() { ChangeOpDeserializationAssert::assertThrowsChangeOpDeserializationException( @@ -54,70 +54,6 @@ ); } - public function testGivenChangeRequestSettingAliasesToItemWithNoAlias_addsAlias() { - $item = $this->getItemWithoutAliases(); - $alias = 'foo'; - $changeOp = $this->newAliasesChangeOpDeserializer( $this->getTermChangeOpValidator() )->createEntityChangeOp( [ - 'aliases' => [ 'en' => [ 'language' => 'en', 'value' => $alias ] ] - ] ); - - $changeOp->apply( $item, new Summary() ); - $this->assertSame( $item->getAliasGroups()->getByLanguage( 'en' )->getAliases(), [ $alias ] ); - } - - public function testGivenChangeRequestSettingAliases_overridesExistingAliases() { - $item = $this->getItemWithExistingAliases(); - $newAlias = 'foo'; - $changeOp = $this->newAliasesChangeOpDeserializer( $this->getTermChangeOpValidator() )->createEntityChangeOp( [ - 'aliases' => [ 'en' => [ 'language' => 'en', 'value' => $newAlias ] ] - ] ); - - $changeOp->apply( $item, new Summary() ); - $this->assertSame( $item->getAliasGroups()->getByLanguage( 'en' )->getAliases(), [ $newAlias ] ); - } - - public function testGivenChangeRequestRemovingAllExistingEnAliases_enAliasGroupDoesNotExist() { - $item = $this->getItemWithExistingAliases(); - $existingAliases = $item->getAliasGroups()->getByLanguage( 'en' )->getAliases(); - $changeOp = $this->newAliasesChangeOpDeserializer( $this->getTermChangeOpValidator() )->createEntityChangeOp( [ - 'aliases' => array_map( function( $alias ) { - return [ 'language' => 'en', 'value' => $alias, 'remove' => '' ]; - }, $existingAliases ) - ] ); - - $changeOp->apply( $item, new Summary() ); - $this->assertFalse( $item->getAliasGroups()->hasGroupForLanguage( 'en' ) ); - } - - public function testGivenChangeRequestAddingAlias_addsAlias() { - $item = $this->getItemWithExistingAliases(); - $newAlias = 'foo'; - $existingAliases = $item->getAliasGroups()->getByLanguage( 'en' )->getAliases(); - $changeOp = $this->newAliasesChangeOpDeserializer( $this->getTermChangeOpValidator() )->createEntityChangeOp( [ - 'aliases' => [ - 'en' => [ 'language' => 'en', 'value' => $newAlias, 'add' => '' ] - ] - ] ); - - $changeOp->apply( $item, new Summary() ); - $this->assertSame( - array_merge( $existingAliases, [ $newAlias ] ), - $item->getAliasGroups()->getByLanguage( 'en' )->getAliases() - ); - } - - private function getItemWithoutAliases() { - return new Item(); - } - - private function getItemWithExistingAliases() { - $existingEnAliases = [ 'en-existingAlias1', 'en-existingAlias2' ]; - $item = new Item(); - $item->setAliases( 'en', $existingEnAliases ); - - return $item; - } - private function newAliasesChangeOpDeserializer( TermChangeOpSerializationValidator $validator ) { return new AliasesChangeOpDeserializer( $this->getFingerPrintChangeOpFactory(), @@ -139,4 +75,7 @@ return new TermChangeOpSerializationValidator( new StaticContentLanguages( [ 'en' ] ) ); } + public function getChangeOpDeserializer() { + return $this->newAliasesChangeOpDeserializer( $this->getTermChangeOpValidator() ); + } } diff --git a/repo/tests/phpunit/includes/ChangeOp/Deserialization/ChangeOpDeserializerTest.php b/repo/tests/phpunit/includes/ChangeOp/Deserialization/ChangeOpDeserializerTest.php new file mode 100644 index 0000000..11eaa14 --- /dev/null +++ b/repo/tests/phpunit/includes/ChangeOp/Deserialization/ChangeOpDeserializerTest.php @@ -0,0 +1,7 @@ +<?php + +namespace Wikibase\Repo\Tests\ChangeOp\Deserialization; + +interface ChangeOpDeserializerTest { + public function getChangeOpDeserializer(); +} diff --git a/repo/tests/phpunit/includes/ChangeOp/Deserialization/ClaimsChangeOpDeserializationTest.php b/repo/tests/phpunit/includes/ChangeOp/Deserialization/ClaimsChangeOpDeserializationTest.php new file mode 100644 index 0000000..2530aa4 --- /dev/null +++ b/repo/tests/phpunit/includes/ChangeOp/Deserialization/ClaimsChangeOpDeserializationTest.php @@ -0,0 +1,121 @@ +<?php + +namespace Wikibase\Repo\Tests\ChangeOp\Deserialization; + +use DataValues\StringValue; +use Wikibase\DataModel\Entity\Item; +use Wikibase\DataModel\Entity\ItemId; +use Wikibase\DataModel\Entity\PropertyId; +use Wikibase\DataModel\Snak\PropertyNoValueSnak; +use Wikibase\DataModel\Snak\PropertyValueSnak; +use Wikibase\DataModel\Statement\Statement; +use Wikibase\DataModel\Statement\StatementList; +use Wikibase\Repo\WikibaseRepo; +use Wikibase\Summary; + +/** + * Set of test methods that can be reused in ClaimsChangeOpDeserializerTest and tests for + * ChangeOpDeserializers of entities that have claims + */ +trait ClaimsChangeOpDeserializationTest { + /** + * @dataProvider setStatementProvider + */ + public function testGivenNewStatementChangeRequest_setsStatement( $changeRequest, Item $item, $property ) { + $changeOp = $this->getChangeOpDeserializer()->createEntityChangeOp( $changeRequest ); + $changeOp->apply( $item, new Summary() ); + + $this->assertFalse( + $item->getStatements()->getByPropertyId( $property )->isEmpty() + ); + } + + public function setStatementProvider() { + $property = new PropertyId( 'P7' ); + $statement = new Statement( new PropertyNoValueSnak( $property ) ); + $statementSerialization = $this->getStatementSerializer()->serialize( $statement ); + $item = new Item( new ItemId( 'Q23' ) ); + + return [ + 'numeric index format' => [ [ 'claims' => [ $statementSerialization ] ], $item, $property ], + 'associative format' => [ [ 'claims' => [ 'P7' => [ $statementSerialization ] ] ], $item, $property ], + ]; + } + + /** + * @dataProvider deleteStatementProvider + */ + public function testGivenRemoveChangeRequest_removesStatement( $changeRequest, Item $item, $property ) { + $changeOp = $this->getChangeOpDeserializer()->createEntityChangeOp( $changeRequest ); + $changeOp->apply( $item, new Summary() ); + + $this->assertTrue( $item->getStatements()->getByPropertyId( $property )->isEmpty() ); + } + + public function deleteStatementProvider() { + $property = new PropertyId( 'P7' ); + $statement = new Statement( new PropertyNoValueSnak( $property ) ); + $statement->setGuid( 'test-guid' ); + $item = new Item( new ItemId( 'Q23' ) ); + $item->setStatements( new StatementList( [ $statement ] ) ); + + return [ + 'numeric index format' => [ + [ 'claims' => [ + [ 'remove' => '', 'id' => $statement->getGuid() ] + ] ], + $item, + $property + ], + 'associative format' => [ + [ 'claims' => [ + 'P7' => [ [ 'remove' => '', 'id' => $statement->getGuid() ] ] + ] ], + $item->copy(), + $property + ], + ]; + } + + /** + * @dataProvider editStatementProvider + */ + public function testGivenEditChangeRequest_statementGetsChanged( $changeRequest, Item $item ) { + $changeOp = $this->getChangeOpDeserializer()->createEntityChangeOp( $changeRequest ); + $changeOp->apply( $item, new Summary() ); + + $this->assertCount( 1, $item->getStatements()->toArray() ); + $this->assertSame( + 'bar', + $item->getStatements()->toArray()[0] + ->getMainSnak() + ->getDataValue() + ->getValue() + ); + } + + public function editStatementProvider() { + $property = new PropertyId( 'P7' ); + $statement = new Statement( new PropertyValueSnak( $property, new StringValue( 'foo' ) ) ); + $statement->setGuid( 'Q23$D8404CDA-25E4-4334-AF13-A3290BC66666' ); + $item = new Item( new ItemId( 'Q23' ) ); + $item->setStatements( new StatementList( [ $statement ] ) ); + $statementSerialization = $this->getStatementSerializer()->serialize( $statement ); + $statementSerialization['mainsnak']['datavalue']['value'] = 'bar'; + + return [ + 'numeric index format' => [ + [ 'claims' => [ $statementSerialization ] ], + $item + ], + 'associative format' => [ + [ 'claims' => [ 'P7' => [ $statementSerialization ] ] ], + $item + ], + ]; + } + + private function getStatementSerializer() { + return WikibaseRepo::getDefaultInstance()->getStatementSerializer(); + } +} diff --git a/repo/tests/phpunit/includes/ChangeOp/Deserialization/ClaimsChangeOpDeserializerTest.php b/repo/tests/phpunit/includes/ChangeOp/Deserialization/ClaimsChangeOpDeserializerTest.php index f32bb10..05efc34 100644 --- a/repo/tests/phpunit/includes/ChangeOp/Deserialization/ClaimsChangeOpDeserializerTest.php +++ b/repo/tests/phpunit/includes/ChangeOp/Deserialization/ClaimsChangeOpDeserializerTest.php @@ -2,17 +2,8 @@ namespace Wikibase\Repo\Tests\ChangeOp\Deserialization; -use DataValues\StringValue; -use Wikibase\DataModel\Entity\Item; -use Wikibase\DataModel\Entity\ItemId; -use Wikibase\DataModel\Entity\PropertyId; -use Wikibase\DataModel\Snak\PropertyNoValueSnak; -use Wikibase\DataModel\Snak\PropertyValueSnak; -use Wikibase\DataModel\Statement\Statement; -use Wikibase\DataModel\Statement\StatementList; use Wikibase\Repo\ChangeOp\Deserialization\ClaimsChangeOpDeserializer; use Wikibase\Repo\WikibaseRepo; -use Wikibase\Summary; /** * @covers Wikibase\Repo\ChangeOp\Deserialization\ClaimsChangeOpDeserializer @@ -21,7 +12,9 @@ * * @license GPL-2.0+ */ -class ClaimsChangeOpDeserializerTest extends \PHPUnit_Framework_TestCase { +class ClaimsChangeOpDeserializerTest extends \PHPUnit_Framework_TestCase implements ChangeOpDeserializerTest { + + use ClaimsChangeOpDeserializationTest; public function testGivenClaimsFieldNotAnArray_createEntityChangeOpThrowsError() { ChangeOpDeserializationAssert::assertThrowsChangeOpDeserializationException( @@ -54,110 +47,6 @@ ); } - /** - * @dataProvider setStatementProvider - */ - public function testGivenNewStatementChangeRequest_setsStatement( $changeRequest, Item $item, $property ) { - $changeOp = $this->newClaimsChangeOpDeserializer() - ->createEntityChangeOp( $changeRequest ); - $changeOp->apply( $item, new Summary() ); - - $this->assertFalse( - $item->getStatements()->getByPropertyId( $property )->isEmpty() - ); - } - - public function setStatementProvider() { - $property = new PropertyId( 'P7' ); - $statement = new Statement( new PropertyNoValueSnak( $property ) ); - $statementSerialization = $this->getStatementSerializer()->serialize( $statement ); - $item = new Item( new ItemId( 'Q23' ) ); - - return [ - 'numeric index format' => [ [ 'claims' => [ $statementSerialization ] ], $item, $property ], - 'associative format' => [ [ 'claims' => [ 'P7' => [ $statementSerialization ] ] ], $item, $property ], - ]; - } - - /** - * @dataProvider deleteStatementProvider - */ - public function testGivenRemoveChangeRequest_removesStatement( $changeRequest, Item $item, $property ) { - $changeOp = $this->newClaimsChangeOpDeserializer() - ->createEntityChangeOp( $changeRequest ); - $changeOp->apply( $item, new Summary() ); - - $this->assertTrue( $item->getStatements()->getByPropertyId( $property )->isEmpty() ); - } - - public function deleteStatementProvider() { - $property = new PropertyId( 'P7' ); - $statement = new Statement( new PropertyNoValueSnak( $property ) ); - $statement->setGuid( 'test-guid' ); - $item = new Item( new ItemId( 'Q23' ) ); - $item->setStatements( new StatementList( [ $statement ] ) ); - - return [ - 'numeric index format' => [ - [ 'claims' => [ - [ 'remove' => '', 'id' => $statement->getGuid() ] - ] ], - $item, - $property - ], - 'associative format' => [ - [ 'claims' => [ - 'P7' => [ [ 'remove' => '', 'id' => $statement->getGuid() ] ] - ] ], - $item->copy(), - $property - ], - ]; - } - - /** - * @dataProvider editStatementProvider - */ - public function testGivenEditChangeRequest_statementGetsChanged( $changeRequest, Item $item ) { - $changeOp = $this->newClaimsChangeOpDeserializer() - ->createEntityChangeOp( $changeRequest ); - $changeOp->apply( $item, new Summary() ); - - $this->assertCount( 1, $item->getStatements()->toArray() ); - $this->assertSame( - 'bar', - $item->getStatements()->toArray()[0] - ->getMainSnak() - ->getDataValue() - ->getValue() - ); - } - - public function editStatementProvider() { - $property = new PropertyId( 'P7' ); - $statement = new Statement( new PropertyValueSnak( $property, new StringValue( 'foo' ) ) ); - $statement->setGuid( 'Q23$D8404CDA-25E4-4334-AF13-A3290BC66666' ); - $item = new Item( new ItemId( 'Q23' ) ); - $item->setStatements( new StatementList( [ $statement ] ) ); - $statementSerialization = $this->getStatementSerializer()->serialize( $statement ); - $statementSerialization['mainsnak']['datavalue']['value'] = 'bar'; - - return [ - 'numeric index format' => [ - [ 'claims' => [ $statementSerialization ] ], - $item - ], - 'associative format' => [ - [ 'claims' => [ 'P7' => [ $statementSerialization ] ] ], - $item - ], - ]; - } - - private function getStatementSerializer() { - return WikibaseRepo::getDefaultInstance()->getStatementSerializer(); - } - private function newClaimsChangeOpDeserializer() { $wikibaseRepo = WikibaseRepo::getDefaultInstance(); @@ -167,4 +56,7 @@ ); } + public function getChangeOpDeserializer() { + return $this->newClaimsChangeOpDeserializer(); + } } diff --git a/repo/tests/phpunit/includes/ChangeOp/Deserialization/DescriptionsChangeOpDeserializationTest.php b/repo/tests/phpunit/includes/ChangeOp/Deserialization/DescriptionsChangeOpDeserializationTest.php new file mode 100644 index 0000000..3f88b37 --- /dev/null +++ b/repo/tests/phpunit/includes/ChangeOp/Deserialization/DescriptionsChangeOpDeserializationTest.php @@ -0,0 +1,68 @@ +<?php + +namespace Wikibase\Repo\Tests\ChangeOp\Deserialization; + +use Wikibase\DataModel\Entity\Item; +use Wikibase\Summary; + +/** + * Set of test methods that can be reused in DescriptionsChangeOpDeserializerTest and tests for + * ChangeOpDeserializers of entities that have descriptions + */ +trait DescriptionsChangeOpDeserializationTest { + + public function testGivenChangeRequestWithDescription_addsDescription() { + $item = $this->getItemWithoutEnDescription(); + $description = 'foo'; + $changeOp = $this->getChangeOpDeserializer()->createEntityChangeOp( + [ + 'descriptions' => [ 'en' => [ 'language' => 'en', 'value' => $description ] ] + ] ); + + $changeOp->apply( $item, new Summary() ); + $this->assertSame( $description, $item->getDescriptions()->getByLanguage( 'en' )->getText() ); + } + + public function testGivenChangeRequestWithNewDescription_overridesExistingDescription() { + $item = $this->getItemWithEnDescription(); + $newDescription = 'foo'; + $changeOp = $this->getChangeOpDeserializer()->createEntityChangeOp( + [ 'descriptions' => [ 'en' => [ 'language' => 'en', 'value' => $newDescription ] ] ] + ); + + $changeOp->apply( $item, new Summary() ); + $this->assertSame( $newDescription, $item->getDescriptions()->getByLanguage( 'en' )->getText() ); + } + + public function testGivenChangeRequestWithRemove_removesDescription() { + $item = $this->getItemWithEnDescription(); + $changeOp = $this->getChangeOpDeserializer()->createEntityChangeOp( + [ 'descriptions' => [ 'en' => [ 'language' => 'en', 'remove' => '' ] ] ] + ); + + $changeOp->apply( $item, new Summary() ); + $this->assertFalse( $item->getDescriptions()->hasTermForLanguage( 'en' ) ); + } + + public function testGivenChangeRequestWithEmptyValue_removesDescription() { + $item = $this->getItemWithEnDescription(); + $changeOp = $this->getChangeOpDeserializer()->createEntityChangeOp( + [ 'descriptions' => [ 'en' => [ 'language' => 'en', 'value' => '' ] ] ] + ); + + $changeOp->apply( $item, new Summary() ); + $this->assertFalse( $item->getDescriptions()->hasTermForLanguage( 'en' ) ); + } + + private function getItemWithoutEnDescription() { + return new Item(); + } + + private function getItemWithEnDescription() { + $item = new Item(); + $item->setDescription( 'en', 'en-description' ); + + return $item; + } + +} diff --git a/repo/tests/phpunit/includes/ChangeOp/Deserialization/DescriptionsChangeOpDeserializerTest.php b/repo/tests/phpunit/includes/ChangeOp/Deserialization/DescriptionsChangeOpDeserializerTest.php index 069876e..f89d2c1 100644 --- a/repo/tests/phpunit/includes/ChangeOp/Deserialization/DescriptionsChangeOpDeserializerTest.php +++ b/repo/tests/phpunit/includes/ChangeOp/Deserialization/DescriptionsChangeOpDeserializerTest.php @@ -3,14 +3,12 @@ namespace Wikibase\Repo\Tests\ChangeOp\Deserialization; use Wikibase\ChangeOp\FingerprintChangeOpFactory; -use Wikibase\DataModel\Entity\Item; use Wikibase\Lib\StaticContentLanguages; use Wikibase\Repo\ChangeOp\Deserialization\ChangeOpDeserializationException; use Wikibase\Repo\ChangeOp\Deserialization\DescriptionsChangeOpDeserializer; use Wikibase\Repo\ChangeOp\Deserialization\TermChangeOpSerializationValidator; use Wikibase\Repo\Tests\ChangeOp\ChangeOpTestMockProvider; use Wikibase\StringNormalizer; -use Wikibase\Summary; /** * @covers Wikibase\Repo\ChangeOp\Deserialization\DescriptionsChangeOpDeserializer @@ -19,7 +17,9 @@ * * @license GPL-2.0+ */ -class DescriptionsChangeOpDeserializerTest extends \PHPUnit_Framework_TestCase { +class DescriptionsChangeOpDeserializerTest extends \PHPUnit_Framework_TestCase implements ChangeOpDeserializerTest { + + use DescriptionsChangeOpDeserializationTest; public function testGivenDescriptionsFieldNotAnArray_createEntityChangeOpThrowsError() { ChangeOpDeserializationAssert::assertThrowsChangeOpDeserializationException( @@ -54,65 +54,12 @@ ); } - public function testGivenChangeRequestWithDescription_addsDescription() { - $item = $this->getItemWithoutEnDescription(); - $description = 'foo'; - $changeOp = $this->newDescriptionsChangeOpDeserializer( $this->getTermChangeOpValidator() ) - ->createEntityChangeOp( [ - 'descriptions' => [ 'en' => [ 'language' => 'en', 'value' => $description ] ] - ] ); - - $changeOp->apply( $item, new Summary() ); - $this->assertSame( $description, $item->getDescriptions()->getByLanguage( 'en' )->getText() ); - } - - public function testGivenChangeRequestWithNewDescription_overridesExistingDescription() { - $item = $this->getItemWithEnDescription(); - $newDescription = 'foo'; - $changeOp = $this->newDescriptionsChangeOpDeserializer( $this->getTermChangeOpValidator() ) - ->createEntityChangeOp( [ - 'descriptions' => [ 'en' => [ 'language' => 'en', 'value' => $newDescription ] ] - ] ); - - $changeOp->apply( $item, new Summary() ); - $this->assertSame( $newDescription, $item->getDescriptions()->getByLanguage( 'en' )->getText() ); - } - - public function testGivenChangeRequestWithRemove_removesDescription() { - $item = $this->getItemWithEnDescription(); - $changeOp = $this->newDescriptionsChangeOpDeserializer( $this->getTermChangeOpValidator() ) - ->createEntityChangeOp( [ 'descriptions' => [ 'en' => [ 'language' => 'en', 'remove' => '' ] ] ] ); - - $changeOp->apply( $item, new Summary() ); - $this->assertFalse( $item->getDescriptions()->hasTermForLanguage( 'en' ) ); - } - - public function testGivenChangeRequestWithEmptyValue_removesDescription() { - $item = $this->getItemWithEnDescription(); - $changeOp = $this->newDescriptionsChangeOpDeserializer( $this->getTermChangeOpValidator() ) - ->createEntityChangeOp( [ 'descriptions' => [ 'en' => [ 'language' => 'en', 'value' => '' ] ] ] ); - - $changeOp->apply( $item, new Summary() ); - $this->assertFalse( $item->getDescriptions()->hasTermForLanguage( 'en' ) ); - } - private function newDescriptionsChangeOpDeserializer( TermChangeOpSerializationValidator $validator ) { return new DescriptionsChangeOpDeserializer( $this->getFingerPrintChangeOpFactory(), $this->getStringNormalizer(), $validator ); - } - - private function getItemWithoutEnDescription() { - return new Item(); - } - - private function getItemWithEnDescription() { - $item = new Item(); - $item->setDescription( 'en', 'en-description' ); - - return $item; } private function getStringNormalizer() { @@ -128,4 +75,7 @@ return new TermChangeOpSerializationValidator( new StaticContentLanguages( [ 'en' ] ) ); } + public function getChangeOpDeserializer() { + return $this->newDescriptionsChangeOpDeserializer( $this->getTermChangeOpValidator() ); + } } diff --git a/repo/tests/phpunit/includes/ChangeOp/Deserialization/ItemChangeOpDeserializerTest.php b/repo/tests/phpunit/includes/ChangeOp/Deserialization/ItemChangeOpDeserializerTest.php index 3c7efb8..3893b5b 100644 --- a/repo/tests/phpunit/includes/ChangeOp/Deserialization/ItemChangeOpDeserializerTest.php +++ b/repo/tests/phpunit/includes/ChangeOp/Deserialization/ItemChangeOpDeserializerTest.php @@ -26,7 +26,15 @@ * * @license GPL-2.0+ */ -class ItemChangeOpDeserializerTest extends \PHPUnit_Framework_TestCase { +class ItemChangeOpDeserializerTest extends \PHPUnit_Framework_TestCase implements ChangeOpDeserializerTest { + + use LabelsChangeOpDeserialization; + + use DescriptionsChangeOpDeserializationTest; + + use AliasChangeOpDeserializationTest; + + use ClaimsChangeOpDeserializationTest; const SITE_ID = 'some-wiki'; @@ -61,7 +69,7 @@ 'sitelinks' => [ self::SITE_ID => [ 'site' => self::SITE_ID, 'title' => $pageTitle ] ] ]; - $changeOp = $this->newChangeOpDeserializer()->createEntityChangeOp( $changeRequest ); + $changeOp = $this->newItemChangeOpDeserializer()->createEntityChangeOp( $changeRequest ); $changeOp->apply( $item, new Summary() ); @@ -77,7 +85,7 @@ ); } - private function newChangeOpDeserializer() { + private function newItemChangeOpDeserializer() { $wikibaseRepo = WikibaseRepo::getDefaultInstance(); $changeOpFactoryProvider = $wikibaseRepo->getChangeOpFactoryProvider(); @@ -114,4 +122,7 @@ return new SiteLinkTargetProvider( new HashSiteStore( [ $wiki ] ) ); } + public function getChangeOpDeserializer() { + return $this->newItemChangeOpDeserializer(); + } } diff --git a/repo/tests/phpunit/includes/ChangeOp/Deserialization/LabelsChangeOpDeserialization.php b/repo/tests/phpunit/includes/ChangeOp/Deserialization/LabelsChangeOpDeserialization.php new file mode 100644 index 0000000..832029c --- /dev/null +++ b/repo/tests/phpunit/includes/ChangeOp/Deserialization/LabelsChangeOpDeserialization.php @@ -0,0 +1,67 @@ +<?php + +namespace Wikibase\Repo\Tests\ChangeOp\Deserialization; + +use Wikibase\DataModel\Entity\Item; +use Wikibase\Summary; + +/** + * Set of test methods that can be reused in LabelsChangeOpDeserializerTest and tests for + * ChangeOpDeserializers of entities that have labels + */ +trait LabelsChangeOpDeserialization { + + public function testGivenChangeRequestWithLabel_addsLabel() { + $item = $this->getItemWithoutEnLabel(); + $label = 'foo'; + $changeOp = $this->getChangeOpDeserializer()->createEntityChangeOp( + [ 'labels' => [ 'en' => [ 'language' => 'en', 'value' => $label ] ] ] + ); + + $changeOp->apply( $item, new Summary() ); + $this->assertSame( $label, $item->getLabels()->getByLanguage( 'en' )->getText() ); + } + + public function testGivenChangeRequestWithNewLabel_overridesExistingLabel() { + $item = $this->getItemWithEnLabel(); + $newLabel = 'foo'; + $changeOp = $this->getChangeOpDeserializer()->createEntityChangeOp( + [ 'labels' => [ 'en' => [ 'language' => 'en', 'value' => $newLabel ] ] ] + ); + + $changeOp->apply( $item, new Summary() ); + $this->assertSame( $newLabel, $item->getLabels()->getByLanguage( 'en' )->getText() ); + } + + public function testGivenChangeRequestWithRemove_removesLabel() { + $item = $this->getItemWithEnLabel(); + $changeOp = $this->getChangeOpDeserializer()->createEntityChangeOp( + [ 'labels' => [ 'en' => [ 'language' => 'en', 'remove' => '' ] ] ] + ); + + $changeOp->apply( $item, new Summary() ); + $this->assertFalse( $item->getLabels()->hasTermForLanguage( 'en' ) ); + } + + public function testGivenChangeRequestWithEmptyLabel_removesLabel() { + $item = $this->getItemWithEnLabel(); + $changeOp = $this->getChangeOpDeserializer()->createEntityChangeOp( + [ 'labels' => [ 'en' => [ 'language' => 'en', 'value' => '' ] ] ] + ); + + $changeOp->apply( $item, new Summary() ); + $this->assertFalse( $item->getLabels()->hasTermForLanguage( 'en' ) ); + } + + private function getItemWithoutEnLabel() { + return new Item(); + } + + private function getItemWithEnLabel() { + $item = new Item(); + $item->setLabel( 'en', 'en-label' ); + + return $item; + } + +} diff --git a/repo/tests/phpunit/includes/ChangeOp/Deserialization/LabelsChangeOpDeserializerTest.php b/repo/tests/phpunit/includes/ChangeOp/Deserialization/LabelsChangeOpDeserializerTest.php index f2f34c5..9b163c8 100644 --- a/repo/tests/phpunit/includes/ChangeOp/Deserialization/LabelsChangeOpDeserializerTest.php +++ b/repo/tests/phpunit/includes/ChangeOp/Deserialization/LabelsChangeOpDeserializerTest.php @@ -3,14 +3,12 @@ namespace Wikibase\Repo\Tests\ChangeOp\Deserialization; use Wikibase\ChangeOp\FingerprintChangeOpFactory; -use Wikibase\DataModel\Entity\Item; use Wikibase\Lib\StaticContentLanguages; use Wikibase\Repo\ChangeOp\Deserialization\ChangeOpDeserializationException; use Wikibase\Repo\ChangeOp\Deserialization\LabelsChangeOpDeserializer; use Wikibase\Repo\ChangeOp\Deserialization\TermChangeOpSerializationValidator; use Wikibase\Repo\Tests\ChangeOp\ChangeOpTestMockProvider; use Wikibase\StringNormalizer; -use Wikibase\Summary; /** * @covers Wikibase\Repo\ChangeOp\Deserialization\LabelsChangeOpDeserializer @@ -19,7 +17,9 @@ * * @license GPL-2.0+ */ -class LabelsChangeOpDeserializerTest extends \PHPUnit_Framework_TestCase { +class LabelsChangeOpDeserializerTest extends \PHPUnit_Framework_TestCase implements ChangeOpDeserializerTest { + + use LabelsChangeOpDeserialization; public function testGivenLabelsFieldNotAnArray_createEntityChangeOpThrowsError() { ChangeOpDeserializationAssert::assertThrowsChangeOpDeserializationException( @@ -54,61 +54,12 @@ ); } - public function testGivenChangeRequestWithLabel_addsLabel() { - $item = $this->getItemWithoutEnLabel(); - $label = 'foo'; - $changeOp = $this->newLabelsChangeOpDeserializer( $this->getTermChangeOpValidator() ) - ->createEntityChangeOp( [ 'labels' => [ 'en' => [ 'language' => 'en', 'value' => $label ] ] ] ); - - $changeOp->apply( $item, new Summary() ); - $this->assertSame( $label, $item->getLabels()->getByLanguage( 'en' )->getText() ); - } - - public function testGivenChangeRequestWithNewLabel_overridesExistingLabel() { - $item = $this->getItemWithEnLabel(); - $newLabel = 'foo'; - $changeOp = $this->newLabelsChangeOpDeserializer( $this->getTermChangeOpValidator() ) - ->createEntityChangeOp( [ 'labels' => [ 'en' => [ 'language' => 'en', 'value' => $newLabel ] ] ] ); - - $changeOp->apply( $item, new Summary() ); - $this->assertSame( $newLabel, $item->getLabels()->getByLanguage( 'en' )->getText() ); - } - - public function testGivenChangeRequestWithRemove_removesLabel() { - $item = $this->getItemWithEnLabel(); - $changeOp = $this->newLabelsChangeOpDeserializer( $this->getTermChangeOpValidator() ) - ->createEntityChangeOp( [ 'labels' => [ 'en' => [ 'language' => 'en', 'remove' => '' ] ] ] ); - - $changeOp->apply( $item, new Summary() ); - $this->assertFalse( $item->getLabels()->hasTermForLanguage( 'en' ) ); - } - - public function testGivenChangeRequestWithEmptyLabel_removesLabel() { - $item = $this->getItemWithEnLabel(); - $changeOp = $this->newLabelsChangeOpDeserializer( $this->getTermChangeOpValidator() ) - ->createEntityChangeOp( [ 'labels' => [ 'en' => [ 'language' => 'en', 'value' => '' ] ] ] ); - - $changeOp->apply( $item, new Summary() ); - $this->assertFalse( $item->getLabels()->hasTermForLanguage( 'en' ) ); - } - private function newLabelsChangeOpDeserializer( TermChangeOpSerializationValidator $validator ) { return new LabelsChangeOpDeserializer( $this->getFingerPrintChangeOpFactory(), $this->getStringNormalizer(), $validator ); - } - - private function getItemWithoutEnLabel() { - return new Item(); - } - - private function getItemWithEnLabel() { - $item = new Item(); - $item->setLabel( 'en', 'en-label' ); - - return $item; } private function getStringNormalizer() { @@ -124,4 +75,8 @@ return new TermChangeOpSerializationValidator( new StaticContentLanguages( [ 'en' ] ) ); } + public function getChangeOpDeserializer() { + return $this->newLabelsChangeOpDeserializer( $this->getTermChangeOpValidator() ); + } + } -- To view, visit https://gerrit.wikimedia.org/r/340114 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I5b3dc0eddea5a6e236cb14885d65e63d5866a310 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Jakob <jakob.warkot...@wikimedia.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits