Tamslo has uploaded a new change for review. https://gerrit.wikimedia.org/r/206791
Change subject: Refactoring of Violations ...................................................................... Refactoring of Violations * Violation::__construct() - Type checks in setters * Violation::constraintClaimGuid - Renamed to constraintId * ViolationStore::insertViolations - Added method insertViolation that is called in the loop * ViolationStore & ViolationLookup - db is no longer global * Tests - Added @group WikidataQuality Change-Id: I6dd79a0441156ee8eb72eaa782243d8e0fc48cfb --- M includes/Violations/Violation.php M includes/Violations/ViolationLookup.php M includes/Violations/ViolationStore.php M tests/phpunit/Html/HtmlTableCellTest.php M tests/phpunit/Html/HtmlTableHeaderTest.php M tests/phpunit/Html/HtmlTableTest.php M tests/phpunit/Violations/ViolationLookupTest.php M tests/phpunit/Violations/ViolationQueryTest.php M tests/phpunit/Violations/ViolationStoreTest.php M tests/phpunit/Violations/ViolationTest.php 10 files changed, 230 insertions(+), 210 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikidataQuality refs/changes/91/206791/1 diff --git a/includes/Violations/Violation.php b/includes/Violations/Violation.php index 871f37a..cbe249e 100644 --- a/includes/Violations/Violation.php +++ b/includes/Violations/Violation.php @@ -6,6 +6,7 @@ use Wikibase\DataModel\Entity\PropertyId; use Wikibase\DataModel\Statement\Statement; use InvalidArgumentException; +use Wikibase\Repo\WikibaseRepo; /** @@ -35,7 +36,7 @@ * * @var PropertyId $pid */ - private $pid; + private $propertyId; /** * claim that contains the violation @@ -47,9 +48,9 @@ /** * constraint that is violated * - * @var string $constraintClaimGuid + * @var string $constraintId */ - private $constraintClaimGuid; + private $constraintId; /** * type of the constraint that is violated @@ -86,58 +87,79 @@ /** * @param EntityId $entityId - * @param Statement|array $statement - * @param string $constraintClaimGuid + * @param PropertyId $propertyId + * @param string $claimGuid + * @param string $constraintId * @param EntityId $constraintTypeEntityId * @param $revisionId * @param string $status * @param string $additionalInfo */ // TODO: Argument 4 --> EntityId as TypeHint - public function __construct( EntityId $entityId, $statement, $constraintClaimGuid, $constraintTypeEntityId, $revisionId, $status, $additionalInfo = null, $updatedAt = null ) { + public function __construct( EntityId $entityId, PropertyId $propertyId, $claimGuid, $constraintId, $constraintTypeEntityId, $revisionId, $status, $additionalInfo = null, $updatedAt = null ) { $this->entityId = $entityId; - if ( $statement instanceof Statement ) { - $this->pid = $statement->getPropertyId(); - $this->claimGuid = $statement->getGuid(); - } else if ( is_array( $statement ) ) { - if ( !( $statement[ 'pid' ] instanceof PropertyId ) ) { - throw new InvalidArgumentException( 'pid must be of type PropertyId' ); - } - if ( !is_string( $statement[ 'claimGuid' ] ) ) { - throw new InvalidArgumentException( 'claimGuid must be of type string' ); - } - $this->pid = $statement[ 'pid' ]; - $this->claimGuid = $statement[ 'claimGuid' ]; - } else { - throw new InvalidArgumentException( 'Provide either a statement or an array with keys "pid" and "claimGuid" as parameter for $statement' ); - } - if ( is_string( $constraintClaimGuid ) ) { - $this->constraintClaimGuid = $constraintClaimGuid; - } else { - throw new InvalidArgumentException( '$constraintClaimGuid must be of type string' ); - } + $this->propertyId = $propertyId; + $this->setClaimGuid( $claimGuid ); + $this->setConstraintId( $constraintId ); $this->constraintTypeEntityId = $constraintTypeEntityId; - if ( is_int( $revisionId ) ) { - $this->revisionId = $revisionId; - } else { - throw new InvalidArgumentException( '$revisionId must be of type int' ); - } - - if ( is_string( $status ) ) { - $this->status = $status; - } else { - throw new InvalidArgumentException( '$status must be of type string' ); - } - if ( is_string( $additionalInfo ) ) { - $this->additionalInfo = $additionalInfo; - } else if ( is_null( $additionalInfo ) ) { - $this->additionalInfo = $additionalInfo; - } else { - throw new InvalidArgumentException( '$additionalInfo must be of type string' ); + $this->setRevisionId( $revisionId ); + $this->setStatus( $status ); + if ( $additionalInfo ) { + $this->setAdditionalInfo( $additionalInfo ); } if ( $updatedAt ) { $this->setUpdatedAt( $updatedAt ); } + } + + private function setClaimGuid( $claimGuid ) { + $repo = WikibaseRepo::getDefaultInstance(); + $validator = $repo->getClaimGuidValidator(); + $validated = $validator->validate( $claimGuid ); + if ( !$validated ) { + throw new InvalidArgumentException( '$claimGuid is not valid.' ); + } + $this->claimGuid = $claimGuid; + } + + /** + * @param string $constraintId + */ + private function setConstraintId( $constraintId ) { + if ( !is_string( $constraintId ) ) { + throw new InvalidArgumentException( '$constraintId must be string.' ); + } + $this->constraintId = $constraintId; + } + + /** + * @param int $revisionId + */ + private function setRevisionId( $revisionId ) { + if ( !is_int( $revisionId ) ) { + throw new InvalidArgumentException( '$revisionId must be of type int' ); + } + $this->revisionId = $revisionId; + } + + /** + * @param string $status + */ + private function setStatus( $status ) { + if ( !is_string( $status ) ) { + throw new InvalidArgumentException( '$status must be string.' ); + } + $this->status = $status; + } + + /** + * @param string $additionalInfo + */ + private function setAdditionalInfo( $additionalInfo ) { + if ( !is_string( $additionalInfo ) ) { + throw new InvalidArgumentException( '$additionalInfo must be string.' ); + } + $this->additionalInfo = $additionalInfo; } /** @@ -148,7 +170,7 @@ if ( $timestamp ) { $this->updatedAt = $timestamp; } else { - throw new InvalidArgumentException( 'Please provide a proper timestamp format for wfTimestamp()' ); + throw new InvalidArgumentException( 'Please provide a proper timestamp format for wfTimestamp().' ); } } @@ -164,7 +186,7 @@ * @return PropertyId */ public function getPropertyId() { - return $this->pid; + return $this->propertyId; } /** @@ -177,8 +199,8 @@ /** * @return string */ - public function getConstraintClaimGuid() { - return $this->constraintClaimGuid; + public function getConstraintId() { + return $this->constraintId; } /** diff --git a/includes/Violations/ViolationLookup.php b/includes/Violations/ViolationLookup.php index 4aa8700..283ce28 100644 --- a/includes/Violations/ViolationLookup.php +++ b/includes/Violations/ViolationLookup.php @@ -37,19 +37,20 @@ return null; } - $violations = array (); + $violations = array(); foreach ( $queryResult as $result ) { $violation = new Violation( new ItemId( $result->entity_id ), - array ( 'pid' => new PropertyId( $result->pid ), 'claimGuid' => $result->claim_guid ), + new PropertyId( $result->pid ), + $result->claim_guid, $result->constraint_id, new ItemId( $result->constraint_type_entity_id ), - (int) $result->revision_id, + (int)$result->revision_id, $result->status, $result->additional_info, $result->updated_at ); - $violations[ ] = $violation; + $violations[] = $violation; } return $violations; diff --git a/includes/Violations/ViolationStore.php b/includes/Violations/ViolationStore.php index a84d6c9..c270a6f 100644 --- a/includes/Violations/ViolationStore.php +++ b/includes/Violations/ViolationStore.php @@ -3,6 +3,7 @@ namespace WikidataQuality\Violations; use Doctrine\Instantiator\Exception\InvalidArgumentException; +use DatabaseBase; use WikidataQuality\Violations\Violation; @@ -17,53 +18,54 @@ */ class ViolationStore { - private $db; + /** + * @param Violation $violation + */ + public function insertViolation( Violation $violation ) { + $this->getDBConnection(); + + $updatedAt = wfTimestamp( TS_MW ); + $accumulator = array( + 'entity_id' => $violation->getEntityId()->getSerialization(), + 'pid' => $violation->getPropertyId()->getSerialization(), + 'claim_guid' => $violation->getClaimGuid(), + 'constraint_id' => $violation->getConstraintId(), + 'constraint_type_entity_id' => $violation->getConstraintTypeEntityId(), + //TODO: use this line: ->getSerialization(), + 'additional_info' => $violation->getAdditionalInfo(), + 'updated_at' => $updatedAt, + 'revision_id' => $violation->getRevisionId(), + 'status' => $violation->getStatus() + ); + + if ( !$this->violationExists( $violation ) ) { + $this->insertNewViolation( $accumulator ); + } else { + $this->updateViolation( $accumulator ); + } + + } /** - * @param array|Violation $violations - * - * @throws \DBError + * @param array $violations */ public function insertViolations( $violations ) { - if ( $violations instanceof Violation ) { - $violations = array ( $violations ); - } if ( !is_array( $violations ) ) { - throw new InvalidArgumentException( '$violationa must be instance of WikidataQuality\Violations\Violation or an array of those.' ); + throw new InvalidArgumentException( '$violations must be array.' ); } foreach ( $violations as $violation ) { if ( !( $violation instanceof Violation ) ) { - throw new InvalidArgumentException( 'Objects in $violations must be instance of WikidataQuality\Violations\Violation' ); + throw new InvalidArgumentException( 'Objects in $violations must be instance of Violation.' ); } } - $this->getDBConnection(); - foreach ( $violations as $violation ) { - $updatedAt = wfTimestamp( TS_MW ); - $accumulator = array ( - 'entity_id' => $violation->getEntityId()->getSerialization(), - 'pid' => $violation->getPropertyId()->getSerialization(), - 'claim_guid' => $violation->getClaimGuid(), - 'constraint_id' => $violation->getConstraintClaimGuid(), - 'constraint_type_entity_id' => $violation->getConstraintTypeEntityId(), - //TODO: use this line: ->getSerialization(), - 'additional_info' => $violation->getAdditionalInfo(), - 'updated_at' => $updatedAt, - 'revision_id' => $violation->getRevisionId(), - 'status' => $violation->getStatus() - ); - - if ( !$this->violationExists( $violation ) ) { - $this->insertNewViolation( $accumulator ); - } else { - $this->updateViolation( $accumulator ); - } + $this->insertViolation( $violation ); } } /** - * @param \WikidataQuality\Violations\Violation $violation + * @param Violation $violation * * @return mixed * @throws \DBError @@ -74,9 +76,9 @@ throw new InvalidArgumentException( 'Input of ViolationStore::removeViolationWith() has to be string' ); } - $this->getDBConnection(); + $db = $this->getDBConnection(); - $success = $this->db->delete( VIOLATION_TABLE, array ( + $success = $db->delete( VIOLATION_TABLE, array( "claim_guid=\"$claimGuid\"", "constraint_id=\"$constraintClaimGuid\"" ) ); @@ -85,29 +87,21 @@ } /** - * @throws \DBError - * @throws \MWException - */ - private function getDBConnection() { - wfWaitForSlaves(); - $loadBalancer = wfGetLB(); - $this->db = $loadBalancer->getConnection( DB_MASTER ); - } - - /** - * @param \WikidataQuality\Violations\Violation $violation + * @param Violation $violation * * @return bool */ private function violationExists( Violation $violation ) { + $db = $this->getDBConnection(); $claimGuid = $violation->getClaimGuid(); - $constraintClaimGuid = $violation->getConstraintClaimGuid(); - $existing = $this->db->selectRow( VIOLATION_TABLE, - array ( 'claim_guid', 'constraint_id' ), - array ( + $constraintClaimGuid = $violation->getConstraintId(); + $existing = $db->selectRow( VIOLATION_TABLE, + array( 'claim_guid', 'constraint_id' ), + array( "claim_guid=\"$claimGuid\"", "constraint_id=\"$constraintClaimGuid\"" ) ); + $this->reuseDBConnection( $db ); return $existing ? true : false; } @@ -117,7 +111,9 @@ * @return mixed */ private function insertNewViolation( array $accumulator ) { - $success = $this->db->insert( VIOLATION_TABLE, $accumulator ); + $db = $this->getDBConnection(); + $success = $db->insert( VIOLATION_TABLE, $accumulator ); + $this->reuseDBConnection( $db ); return $success; } @@ -127,13 +123,38 @@ * @return mixed */ private function updateViolation( array $accumulator ) { - $claimGuid = $accumulator[ 'claim_guid' ]; - $constraintClaimGuid = $accumulator[ 'constraint_id' ]; - $success = $this->db->update( + $db = $this->getDBConnection(); + $claimGuid = $accumulator['claim_guid']; + $constraintClaimGuid = $accumulator['constraint_id']; + $success = $db->update( VIOLATION_TABLE, $accumulator, - array ( "claim_guid=\"$claimGuid\"", "constraint_id=\"$constraintClaimGuid\"" ) + array( "claim_guid=\"$claimGuid\"", "constraint_id=\"$constraintClaimGuid\"" ) ); + $this->reuseDBConnection( $db ); return $success; } + + /** + * @return DatabaseBase + * + * @throws \MWException + */ + private function getDBConnection() { + wfWaitForSlaves(); + $loadBalancer = wfGetLB(); + $db = $loadBalancer->getConnection( DB_MASTER ); + return $db; + } + + /** + * @param DatabaseBase $db + * + * @throws \MWException + */ + private function reuseDBConnection( DatabaseBase $db ) { + wfWaitForSlaves(); + $loadBalancer = wfGetLB(); + $loadBalancer->reuseConnection( $db ); + } } \ No newline at end of file diff --git a/tests/phpunit/Html/HtmlTableCellTest.php b/tests/phpunit/Html/HtmlTableCellTest.php index c34b08a..c4e40a0 100644 --- a/tests/phpunit/Html/HtmlTableCellTest.php +++ b/tests/phpunit/Html/HtmlTableCellTest.php @@ -9,6 +9,8 @@ /** * @covers WikidataQuality\Html\HtmlTableCell * + * @group WikidataQuality + * * @author BP2014N1 * @license GNU GPL v2+ */ diff --git a/tests/phpunit/Html/HtmlTableHeaderTest.php b/tests/phpunit/Html/HtmlTableHeaderTest.php index cd883ea..a05d457 100644 --- a/tests/phpunit/Html/HtmlTableHeaderTest.php +++ b/tests/phpunit/Html/HtmlTableHeaderTest.php @@ -9,6 +9,8 @@ /** * @covers WikidataQuality\Html\HtmlTableHeader * + * @group WikidataQuality + * * @author BP2014N1 * @license GNU GPL v2+ */ diff --git a/tests/phpunit/Html/HtmlTableTest.php b/tests/phpunit/Html/HtmlTableTest.php index 8da9910..f4f0558 100644 --- a/tests/phpunit/Html/HtmlTableTest.php +++ b/tests/phpunit/Html/HtmlTableTest.php @@ -13,6 +13,8 @@ * @uses WikidataQuality\Html\HtmlTableHeader * @uses WikidataQuality\Html\HtmlTableCell * + * @group WikidataQuality + * * @author BP2014N1 * @license GNU GPL v2+ */ diff --git a/tests/phpunit/Violations/ViolationLookupTest.php b/tests/phpunit/Violations/ViolationLookupTest.php index 8ac41da..0826a7d 100644 --- a/tests/phpunit/Violations/ViolationLookupTest.php +++ b/tests/phpunit/Violations/ViolationLookupTest.php @@ -12,6 +12,7 @@ * @uses WikidataQuality\Violations\Violation * @uses WikidataQuality\Violations\ViolationQuery * + * @group WikidataQuality * @group database * @group medium * @@ -24,8 +25,8 @@ $violationLookup = new ViolationLookup(); $violationQuery = new ViolationQuery(); - $violationQuery->setClaimGuid( 'P13$1-2-3-4' ); - $violationQuery->setConstraintClaimGuid( 'P667$1-2-3-4' ); + $violationQuery->setClaimGuid( 'P13$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee' ); + $violationQuery->setConstraintClaimGuid( 'P667$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee' ); $queryResult = $violationLookup->getWhere( $violationQuery ); $this->assertEquals( 1, count( $queryResult ) ); @@ -58,8 +59,8 @@ array ( 'entity_id' => 'Q42', 'pid' => 'P13', - 'claim_guid' => 'P13$1-2-3-4', - 'constraint_id' => 'P666$1-2-3-4', + 'claim_guid' => 'P13$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee', + 'constraint_id' => 'P666$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee', 'constraint_type_entity_id' => 'Q666', 'additional_info' => 'first', 'updated_at' => '20141015150000', @@ -69,8 +70,8 @@ array ( 'entity_id' => 'Q42', 'pid' => 'P13', - 'claim_guid' => 'P13$1-2-3-4', - 'constraint_id' => 'P667$1-2-3-4', + 'claim_guid' => 'P13$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee', + 'constraint_id' => 'P667$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee', 'constraint_type_entity_id' => 'Q666', 'additional_info' => 'second', 'updated_at' => '20141015150000', @@ -80,8 +81,8 @@ array ( 'entity_id' => 'Q42', 'pid' => 'P13', - 'claim_guid' => 'P13$1-2-3-4', - 'constraint_id' => 'P668$1-2-3-4', + 'claim_guid' => 'P13$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee', + 'constraint_id' => 'P668$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee', 'constraint_type_entity_id' => 'Q666', 'additional_info' => 'third', 'updated_at' => '20141015150000', diff --git a/tests/phpunit/Violations/ViolationQueryTest.php b/tests/phpunit/Violations/ViolationQueryTest.php index 5e067cc..26987e0 100644 --- a/tests/phpunit/Violations/ViolationQueryTest.php +++ b/tests/phpunit/Violations/ViolationQueryTest.php @@ -9,6 +9,8 @@ /** * @covers WikidataQuality\Violations\ViolationQuery * + * @group WikidataQuality + * * @author BP2014N1 * @license GNU GPL v2+ */ diff --git a/tests/phpunit/Violations/ViolationStoreTest.php b/tests/phpunit/Violations/ViolationStoreTest.php index 8a5185c..c5d20c9 100644 --- a/tests/phpunit/Violations/ViolationStoreTest.php +++ b/tests/phpunit/Violations/ViolationStoreTest.php @@ -17,6 +17,7 @@ * * @uses WikidataQuality\Violations\Violation * + * @group WikidataQuality * @group database * @group medium * @@ -31,15 +32,15 @@ $actual = $this->db->selectRow( VIOLATION_TABLE, array ( 'claim_guid', 'constraint_id' - ), array ( 'claim_guid="P13$1-2-3-4"', 'constraint_id="P666$1-2-3-4"' ) ); + ), array ( 'claim_guid="P13$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"', 'constraint_id="P666$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"' ) ); $this->assertNotNull( $actual ); - $violationStore->removeViolationWith( 'P13$1-2-3-4', 'P666$1-2-3-4' ); + $violationStore->removeViolationWith( 'P13$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee', 'P666$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee' ); $actual = $this->db->selectRow( VIOLATION_TABLE, array ( 'claim_guid', 'constraint_id' - ), array ( 'claim_guid="P13$1-2-3-4"', 'constraint_id="P666$1-2-3-4"' ) ); + ), array ( 'claim_guid="P13$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"', 'constraint_id="P666$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"' ) ); $this->assertFalse( $actual ); } @@ -55,24 +56,20 @@ $violationStore = new ViolationStore(); $pid = new PropertyId( 'P13' ); - $claimGuid = 'P13$1-2-3-4'; - $snak = new PropertyValueSnak( $pid, new StringValue( 'abcd' ) ); - $claim = new Claim( $snak ); - $statement = new Statement( $claim ); - $statement->setGuid( $claimGuid ); - $violation = new Violation( new ItemId( 'Q42' ), $statement, 'P667$1-2-3-4', new ItemId( 'Q666' ), 1234, 'verified' ); + $claimGuid = 'P13$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'; + $violation = new Violation( new ItemId( 'Q42' ), $pid, $claimGuid, 'P667$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee', new ItemId( 'Q666' ), 1234, 'verified' ); - $anotherViolation = new Violation( new ItemId( 'Q42' ), $statement, 'P668$1-2-3-4', new ItemId( 'Q666' ), 1234, 'verified', '{additional:information}' ); + $anotherViolation = new Violation( new ItemId( 'Q42' ), $pid, $claimGuid, 'P668$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee', new ItemId( 'Q666' ), 1234, 'verified', '{additional:information}' ); $count = $this->db->select( VIOLATION_TABLE, array ( 'claim_guid', 'constraint_id' - ), array ( 'claim_guid="P13$1-2-3-4"' ) )->numRows(); + ), array ( 'claim_guid="P13$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"' ) )->numRows(); $violationStore->insertViolations( array ( $violation, $anotherViolation ) ); $newCount = $this->db->select( VIOLATION_TABLE, array ( 'claim_guid', 'constraint_id' - ), array ( 'claim_guid="P13$1-2-3-4"' ) )->numRows(); + ), array ( 'claim_guid="P13$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"' ) )->numRows(); $this->assertEquals( $count + 2, $newCount ); } @@ -80,33 +77,36 @@ $violationStore = new ViolationStore(); $pid = new PropertyId( 'P13' ); - $claimGuid = 'P13$1-2-3-4'; - $snak = new PropertyValueSnak( $pid, new StringValue( 'abcd' ) ); - $claim = new Claim( $snak ); - $statement = new Statement( $claim ); - $statement->setGuid( $claimGuid ); - $violation = new Violation( new ItemId( 'Q42' ), $statement, 'P666$1-2-3-4', new ItemId( 'Q666' ), 1234, 'unverified' ); + $claimGuid = 'P13$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'; + $violation = new Violation( new ItemId( 'Q42' ), $pid, $claimGuid, 'P666$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee', new ItemId( 'Q666' ), 1234, 'unverified' ); - $violationStore->insertViolations( $violation ); + $violationStore->insertViolation( $violation ); $updated = $this->db->selectRow( VIOLATION_TABLE, array ( 'status', 'updated_at' ), array ( - 'claim_guid="P13$1-2-3-4"', - 'constraint_id="P666$1-2-3-4"' + 'claim_guid="P13$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"', + 'constraint_id="P666$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"' ) ); $this->assertEquals( 'unverified', $updated->status ); $this->assertNotEquals( '20141015150000', $updated->updated_at ); } - public function testInsertViolationWithInvalidArguments() { + public function testInsertViolationsWithNoArray() { $violationStore = new ViolationStore(); $pid = new PropertyId( 'P13' ); - $claimGuid = 'P13$1-2-3-4'; - $snak = new PropertyValueSnak( $pid, new StringValue( 'abcd' ) ); - $claim = new Claim( $snak ); - $statement = new Statement( $claim ); - $statement->setGuid( $claimGuid ); - $violation = new Violation( new ItemId( 'Q42' ), $statement, 'P666$1-2-3-4', new ItemId( 'Q666' ), 1234, 'unverified' ); + $claimGuid = 'P13$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'; + $violation = new Violation( new ItemId( 'Q42' ), $pid, $claimGuid, 'P666$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee', new ItemId( 'Q666' ), 1234, 'unverified' ); + + $this->setExpectedException( 'InvalidArgumentException' ); + $violationStore->insertViolations( $violation ); + } + + public function testInsertViolationsWithInvalidArray() { + $violationStore = new ViolationStore(); + + $pid = new PropertyId( 'P13' ); + $claimGuid = 'P13$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'; + $violation = new Violation( new ItemId( 'Q42' ), $pid, $claimGuid, 'P666$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee', new ItemId( 'Q666' ), 1234, 'unverified' ); $this->setExpectedException( 'InvalidArgumentException' ); $violationStore->insertViolations( array ( $violation, 'abcd' ) ); @@ -118,8 +118,8 @@ array ( 'entity_id' => 'Q42', 'pid' => 'P13', - 'claim_guid' => 'P13$1-2-3-4', - 'constraint_id' => 'P666$1-2-3-4', + 'claim_guid' => 'P13$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee', + 'constraint_id' => 'P666$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee', 'constraint_type_entity_id' => 'Q666', 'additional_info' => null, 'updated_at' => wfTimestamp( TS_MW, '2014-10-15T15:00:00Z' ), diff --git a/tests/phpunit/Violations/ViolationTest.php b/tests/phpunit/Violations/ViolationTest.php index 5660d9a..88b6d04 100644 --- a/tests/phpunit/Violations/ViolationTest.php +++ b/tests/phpunit/Violations/ViolationTest.php @@ -14,6 +14,8 @@ /** * @covers WikidataQuality\Violations\Violation * + * @group WikidataQuality + * * @author BP2014N1 * @license GNU GPL v2+ */ @@ -22,18 +24,13 @@ /** * @dataProvider validArgumentsProvider */ - public function testConstructWithValidArguments( $entityId, $statement, $constraintClaimGuid, $constraintTypeEntityId, $revisionId, $status, $additionalInfo, $updatedAt ) { - $violation = new Violation( $entityId, $statement, $constraintClaimGuid, $constraintTypeEntityId, $revisionId, $status, $additionalInfo, $updatedAt ); + public function testConstructWithValidArguments( $entityId, $pid, $claimGuid, $constraintId, $constraintTypeEntityId, $revisionId, $status, $additionalInfo, $updatedAt ) { + $violation = new Violation( $entityId, $pid, $claimGuid, $constraintId, $constraintTypeEntityId, $revisionId, $status, $additionalInfo, $updatedAt ); $this->assertEquals( $entityId, $violation->getEntityId() ); - if ( $statement instanceof Statement ) { - $this->assertEquals( $statement->getPropertyId()->getSerialization(), $violation->getPropertyId() ); - $this->assertEquals( $statement->getGuid(), $violation->getClaimGuid() ); - } else if ( is_array( $statement ) ) { - $this->assertEquals( $statement[ 'pid' ], $violation->getPropertyId() ); - $this->assertEquals( $statement[ 'claimGuid' ], $violation->getClaimGuid() ); - } - $this->assertEquals( $constraintClaimGuid, $violation->getConstraintClaimGuid() ); + $this->assertEquals( $pid, $violation->getPropertyId() ); + $this->assertEquals( $claimGuid, $violation->getClaimGuid() ); + $this->assertEquals( $constraintId, $violation->getConstraintId() ); $this->assertEquals( $constraintTypeEntityId, $violation->getConstraintTypeEntityId() ); $this->assertEquals( $revisionId, $violation->getRevisionId() ); $this->assertEquals( $status, $violation->getStatus() ); @@ -48,12 +45,8 @@ public function validArgumentsProvider() { $entityId = new ItemId( 'Q42' ); $pid = new PropertyId( 'P1' ); - $claimGuid = 'P1$1-2-3-4'; - $snak = new PropertyValueSnak( $pid, new StringValue( 'abcd' ) ); - $claim = new Claim( $snak ); - $statement = new Statement( $claim ); - $statement->setGuid( $claimGuid ); - $constraintClaimGuid = 'P666$1-2-3-4'; + $claimGuid = 'P1$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'; + $constraintId = 'P666$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'; $constraintTypeEntityId = new ItemId( 'Q666' ); $revisionId = 1234; $status = 'verified'; @@ -61,8 +54,9 @@ return array ( array ( $entityId, - $statement, - $constraintClaimGuid, + $pid, + $claimGuid, + $constraintId, $constraintTypeEntityId, $revisionId, $status, @@ -71,18 +65,9 @@ ), array ( $entityId, - $statement, - $constraintClaimGuid, - $constraintTypeEntityId, - $revisionId, - $status, - null, - null - ), - array ( - $entityId, - array ( 'pid' => new PropertyId( 'P1' ), 'claimGuid' => $claimGuid ), - $constraintClaimGuid, + $pid, + $claimGuid, + $constraintId, $constraintTypeEntityId, $revisionId, $status, @@ -95,21 +80,17 @@ /** * @dataProvider invalidArgumentsProvider */ - public function testConstructWithInvalidArguments( $entityId, $statement, $constraintClaimGuid, $constraintTypeEntityId, $revisionId, $status, $additionalInfo, $updatedAt ) { + public function testConstructWithInvalidArguments( $entityId, $pid, $claimGuid, $constraintId, $constraintTypeEntityId, $revisionId, $status, $additionalInfo, $updatedAt ) { $this->setExpectedException( 'InvalidArgumentException' ); - new Violation( $entityId, $statement, $constraintClaimGuid, $constraintTypeEntityId, $revisionId, $status, $additionalInfo, $updatedAt ); + new Violation( $entityId, $pid, $claimGuid, $constraintId, $constraintTypeEntityId, $revisionId, $status, $additionalInfo, $updatedAt ); } public function invalidArgumentsProvider() { $entityId = new ItemId( 'Q42' ); $pid = new PropertyId( 'P1' ); - $claimGuid = 'P1$1-2-3-4'; - $snak = new PropertyValueSnak( $pid, new StringValue( 'abcd' ) ); - $claim = new Claim( $snak ); - $statement = new Statement( $claim ); - $statement->setGuid( $claimGuid ); - $constraintClaimGuid = 'P666$1-2-3-4'; + $claimGuid = 'P1$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'; + $constraintId = 'P666$aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'; $constraintTypeEntityId = new ItemId( 'Q666' ); $revisionId = 1234; $status = 'verified'; @@ -117,8 +98,9 @@ return array ( array ( $entityId, + $pid, 1234, - $constraintClaimGuid, + $constraintId, $constraintTypeEntityId, $revisionId, $status, @@ -127,27 +109,8 @@ ), array ( $entityId, - array ( 'pid' => '1234', 'claimGuid' => '1234' ), - $constraintClaimGuid, - $constraintTypeEntityId, - $revisionId, - $status, - null, - null - ), - array ( - $entityId, - array ( 'pid' => new PropertyId( 'P1234' ), 'claimGuid' => 1234 ), - $constraintClaimGuid, - $constraintTypeEntityId, - $revisionId, - $status, - null, - null - ), - array ( - $entityId, - $statement, + $pid, + $claimGuid, 1234, $constraintTypeEntityId, $revisionId, @@ -157,8 +120,9 @@ ), array ( $entityId, - $statement, - $constraintClaimGuid, + $pid, + $claimGuid, + $constraintId, $constraintTypeEntityId, '1234', $status, @@ -167,8 +131,9 @@ ), array ( $entityId, - $statement, - $constraintClaimGuid, + $pid, + $claimGuid, + $constraintId, $constraintTypeEntityId, $revisionId, 1234, @@ -177,8 +142,9 @@ ), array ( $entityId, - $statement, - $constraintClaimGuid, + $pid, + $claimGuid, + $constraintId, $constraintTypeEntityId, $revisionId, $status, @@ -187,8 +153,9 @@ ), array ( $entityId, - $statement, - $constraintClaimGuid, + $pid, + $claimGuid, + $constraintId, $constraintTypeEntityId, $revisionId, $status, -- To view, visit https://gerrit.wikimedia.org/r/206791 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I6dd79a0441156ee8eb72eaa782243d8e0fc48cfb Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/WikidataQuality Gerrit-Branch: master Gerrit-Owner: Tamslo <tamaraslosa...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits