Jeroen De Dauw has submitted this change and it was merged. Change subject: Create ChangeOpClaim ......................................................................
Create ChangeOpClaim Change-Id: I7d8a56c447031bf1c3debb8c4db4769d59d0e00f --- M repo/Wikibase.classes.php A repo/includes/changeop/ChangeOpClaim.php A repo/tests/phpunit/includes/changeop/ChangeOpClaimTest.php 3 files changed, 218 insertions(+), 0 deletions(-) Approvals: Jeroen De Dauw: Looks good to me, approved jenkins-bot: Verified diff --git a/repo/Wikibase.classes.php b/repo/Wikibase.classes.php index cb8a26e..fc7b3c9 100644 --- a/repo/Wikibase.classes.php +++ b/repo/Wikibase.classes.php @@ -57,6 +57,7 @@ 'Wikibase\ChangeOpAliases' => 'includes/changeop/ChangeOpAliases.php', 'Wikibase\ChangeOpSiteLink' => 'includes/changeop/ChangeOpSiteLink.php', 'Wikibase\ChangeOpMainSnak' => 'includes/changeop/ChangeOpMainSnak.php', + 'Wikibase\ChangeOpClaim' => 'includes/changeop/ChangeOpClaim.php', 'Wikibase\ChangeOpQualifier' => 'includes/changeop/ChangeOpQualifier.php', 'Wikibase\ChangeOpReference' => 'includes/changeop/ChangeOpReference.php', 'Wikibase\ChangeOpStatementRank' => 'includes/changeop/ChangeOpStatementRank.php', diff --git a/repo/includes/changeop/ChangeOpClaim.php b/repo/includes/changeop/ChangeOpClaim.php new file mode 100644 index 0000000..af11807 --- /dev/null +++ b/repo/includes/changeop/ChangeOpClaim.php @@ -0,0 +1,98 @@ +<?php + +namespace Wikibase; + +use InvalidArgumentException; +use Wikibase\Lib\ClaimGuidGenerator; + +/** + * Class for claim change operation + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @since 0.4 + * + * @ingroup WikibaseRepo + * + * @licence GNU GPL v2+ + * @author Adam Shorland + */ +class ChangeOpClaim extends ChangeOp { + + /** + * @since 0.4 + * + * @var Claim + */ + protected $claim; + + /** + * @since 0.4 + * + * @var array + */ + protected $action; + + /** + * @since 0.4 + * + * @param Claim $claim + * @param string $action should be add|remove + * + * @throws InvalidArgumentException + */ + public function __construct( $claim, $action ) { + if ( !$claim instanceof Claim ) { + throw new InvalidArgumentException( '$claim needs to be an instance of Claim' ); + } + + if ( !is_string( $action ) ) { + throw new InvalidArgumentException( '$action needs to be a string' ); + } + + $this->claim = $claim; + $this->action = $action; + } + + /** + * Applies the change to the given entity + * + * @since 0.4 + * + * @param Entity $entity + * @param Summary|null $summary + * + * @return bool + * + * @throws ChangeOpException + */ + public function apply( Entity $entity, Summary $summary = null ) { + if ( $this->action === "add" ) { + $guidGenerator = new ClaimGuidGenerator( $entity->getId() ); + $this->claim->setGuid( $guidGenerator->newGuid() ); + $entity->addClaim( $this->claim ); + $this->updateSummary( $summary, 'add' ); + } elseif ( $this->action === "remove" ) { + $claims = new Claims ( $entity->getClaims() ); + $claims->removeClaim( $this->claim ); + $entity->setClaims( $claims ); + $this->updateSummary( $summary, 'remove' ); + } else { + throw new ChangeOpException( "Unknown action for change op: $this->action" ); + } + return true; + } +} diff --git a/repo/tests/phpunit/includes/changeop/ChangeOpClaimTest.php b/repo/tests/phpunit/includes/changeop/ChangeOpClaimTest.php new file mode 100644 index 0000000..00726fa --- /dev/null +++ b/repo/tests/phpunit/includes/changeop/ChangeOpClaimTest.php @@ -0,0 +1,119 @@ +<?php + +namespace Wikibase\Test; + +use Wikibase\ChangeOpClaim; +use Wikibase\Claim; +use Wikibase\Claims; +use Wikibase\Entity; +use Wikibase\EntityId; +use Wikibase\ItemContent; +use InvalidArgumentException; +use Wikibase\PropertyNoValueSnak; +use Wikibase\PropertySomeValueSnak; +use Wikibase\SnakObject; + +/** + * @covers Wikibase\ChangeOpClaim + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + * + * @file + * @since 0.4 + * + * @ingroup Wikibase + * @ingroup Test + * + * @group Wikibase + * @group WikibaseRepo + * @group ChangeOp + * + * @licence GNU GPL v2+ + * @author Adam Shorland + */ +class ChangeOpClaimTest extends \PHPUnit_Framework_TestCase { + + public function invalidConstructorProvider() { + $args = array(); + $args[] = array( 42, 'add' ); + $args[] = array( 'en', 'remove' ); + $args[] = array( array(), 'remove' ); + return $args; + } + + /** + * @dataProvider invalidConstructorProvider + * @expectedException InvalidArgumentException + * + * @param Claim $claim + * @param string $action + */ + public function testInvalidConstruct( $claim, $action ) { + $changeOp = new ChangeOpClaim( $claim, $action ); + } + + public function changeOpClaimProvider() { + $noValueClaim = new Claim( new PropertyNoValueSnak( 43 ) ); + + $differentEntity = ItemContent::newEmpty()->getEntity(); + $differentEntity->setId( new EntityId( 'item', 777 ) ); + $oldNoValueClaim = $differentEntity->newClaim( new PropertyNoValueSnak( 43 ) ); + + $entity = ItemContent::newEmpty()->getEntity(); + $entity->setId( new EntityId( 'item', 555 ) ); + $someValueClaim = new Claim( new PropertySomeValueSnak( 44 ) ); + $newNoValueClaim = $entity->newClaim( new PropertyNoValueSnak( 43 ) ); + $args = array(); + + $args[] = array ( $entity, clone $noValueClaim , 'add' , array( clone $noValueClaim ) ); + $args[] = array ( $entity, clone $someValueClaim , 'add' , array( clone $noValueClaim, clone $someValueClaim ) ); + $args[] = array ( $entity, clone $noValueClaim , 'remove' , array( clone $someValueClaim ) ); + $args[] = array ( $entity, clone $someValueClaim , 'remove' , array( ) ); + $args[] = array ( $entity, clone $oldNoValueClaim , 'add' , array( clone $newNoValueClaim ) ); + $args[] = array ( $entity, clone $newNoValueClaim , 'remove' , array( ) ); + + return $args; + } + + /** + * @dataProvider changeOpClaimProvider + * + * @param Entity $entity + * @param $claim + * @param $action + * @param Claim[] $expectedClaims + * @internal param \Wikibase\ChangeOpClaim $changeOpClaim + */ + public function testApply( $entity, $claim, $action, $expectedClaims ) { + $changeOpClaim = new ChangeOpClaim( $claim, $action ); + $changeOpClaim->apply( $entity ); + $entityClaims = new Claims( $entity->getClaims() ); + foreach( $expectedClaims as $expectedClaim ){ + $this->assertTrue( $entityClaims->hasClaim( $expectedClaim ) ); + } + $this->assertEquals( count( $expectedClaims ), $entityClaims->count() ); + } + + /** + * @expectedException \Wikibase\ChangeOpException + */ + public function testApplyWithInvalidAction() { + $item = ItemContent::newEmpty(); + $entity = $item->getEntity(); + $changeOpClaim = new ChangeOpClaim( new Claim( new PropertyNoValueSnak( 43 ) ) , 'invalidAction' ); + $changeOpClaim->apply( $entity ); + } + +} -- To view, visit https://gerrit.wikimedia.org/r/81479 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I7d8a56c447031bf1c3debb8c4db4769d59d0e00f Gerrit-PatchSet: 4 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Addshore <addshorew...@gmail.com> Gerrit-Reviewer: Addshore <addshorew...@gmail.com> Gerrit-Reviewer: Aude <aude.w...@gmail.com> Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de> Gerrit-Reviewer: Denny Vrandecic <denny.vrande...@wikimedia.de> Gerrit-Reviewer: Jeroen De Dauw <jeroended...@gmail.com> Gerrit-Reviewer: Tobias Gritschacher <tobias.gritschac...@wikimedia.de> Gerrit-Reviewer: jenkins-bot _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits