Tamslo has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/203807

Change subject: Implemented violations table
......................................................................

Implemented violations table

Added sql script for creating the violations table. Added
wqdq_violations_table constant and registered in update method in hooks.
Implemented and tested Violation, a structure that holds all information of a
database entry, ViolationStore to insert, update and delete and
ViolationLookup to find violations in the database.

Change-Id: Iaa0ab312a9c4118d28fdab4577c1d431dee90069
---
M WikidataQuality.php
M WikidataQualityHooks.php
A includes/Violations/Violation.php
A includes/Violations/ViolationLookup.php
A includes/Violations/ViolationStore.php
A sql/create_wdqa_violations.sql
A tests/phpunit/Violations/ViolationLookupTest.php
A tests/phpunit/Violations/ViolationStoreTest.php
A tests/phpunit/Violations/ViolationTest.php
9 files changed, 792 insertions(+), 0 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikidataQuality 
refs/changes/07/203807/1

diff --git a/WikidataQuality.php b/WikidataQuality.php
index f7cb453..dd9d589 100644
--- a/WikidataQuality.php
+++ b/WikidataQuality.php
@@ -54,3 +54,4 @@
 define( "CONSTRAINT_TABLE", "wdqa_constraints" );
 define( "DUMP_DATA_TABLE", "wdq_external_data" );
 define( "DUMP_META_TABLE", "wdq_dump_information" );
+define( "VIOLATION_TABLE", "wdqa_violations" );
diff --git a/WikidataQualityHooks.php b/WikidataQualityHooks.php
index 7c00d55..3189921 100644
--- a/WikidataQualityHooks.php
+++ b/WikidataQualityHooks.php
@@ -11,6 +11,7 @@
                $updater->addExtensionTable( 'wdqa_constraints', __DIR__ . 
'/constraint-report/sql/create_wdqa_constraints.sql' );
                $updater->addExtensionTable( 'wdq_dump_information', __DIR__ . 
'/external-validation/sql/create_wdq_dump_information.sql' );
                $updater->addExtensionTable( 'wdq_external_data', __DIR__ . 
'/external-validation/sql/create_wdq_external_data.sql' );
+               $updater->addExtensionTable( 'wdqa_violations', __DIR__ . 
'/sql/create_wdqa_violations.sql' );
 
                return true;
        }
diff --git a/includes/Violations/Violation.php 
b/includes/Violations/Violation.php
new file mode 100644
index 0000000..948d397
--- /dev/null
+++ b/includes/Violations/Violation.php
@@ -0,0 +1,206 @@
+<?php
+
+namespace WikidataQuality\Violations;
+
+use Doctrine\Instantiator\Exception\InvalidArgumentException;
+use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Statement\Statement;
+
+
+/**
+ * Class Violation
+ *
+ * holds data that will be inserted into the violation table
+ *
+ * @package WikidataQuality\Violation
+ * @author BP2014N1
+ * @license GNU GPL v2+
+ */
+class Violation {
+
+       /**
+        * entity that contains the violation
+        *
+        * @var string $entityId
+        */
+       private $entityId;
+
+       /**
+        * property of the claim that contains the violation
+        *
+        * @var string $pid
+        */
+       private $pid;
+
+       /**
+        * claim that contains the violation
+        *
+        * @var string $claimGuid
+        */
+       private $claimGuid;
+
+       /**
+        * constraint that is violated
+        *
+        * @var string $constraintClaimGuid
+        */
+       private $constraintClaimGuid;
+
+       /**
+        * type of the constraint that is violated
+        *
+        * @var $constraintTypeEntityId
+        */
+       private $constraintTypeEntityId;
+
+       /**
+        * additional information that is used when the violation is displayed 
in the UI
+        *
+        * @var string $additionalInfo
+        */
+       private $additionalInfo;
+
+       /**
+        * wfTimestamp of last update in table
+        *
+        * @var string $updatedAt
+        */
+       private $updatedAt;
+
+       /**
+        * @var int $revisionId
+        */
+       private $revisionId;
+
+       /**
+        * status of the violation
+        *
+        * @var string $status
+        */
+       private $status;
+
+       /**
+        * @param EntityId $entityId
+        * @param Statement|array $statement
+        * @param string $constraintClaimGuid
+        * @param EntityId $constraintTypeEntityId
+        * @param $revisionId
+        * @param string $status
+        * @param string $additionalInfo
+        */
+       public function __construct( EntityId $entityId, $statement, 
$constraintClaimGuid, EntityId $constraintTypeEntityId, $revisionId, $status, 
$additionalInfo = null, $updatedAt = null ) {
+               $this->entityId = $entityId->getSerialization();
+               if ( $statement instanceof Statement ) {
+                       $this->pid = 
$statement->getPropertyId()->getSerialization();
+                       $this->claimGuid = $statement->getGuid();
+               } else if ( is_array( $statement ) ) {
+                       $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->constraintTypeEntityId = 
$constraintTypeEntityId->getSerialization();
+               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' );
+               }
+               if ( $updatedAt ) {
+                       $this->setUpdatedAt( $updatedAt );
+               }
+       }
+
+       /**
+        * @param string $updatedAt
+        */
+       public function setUpdatedAt( $updatedAt ) {
+               $timestamp = wfTimestamp( TS_MW, $updatedAt );
+               if ( $timestamp ) {
+                       $this->updatedAt = $timestamp;
+               } else {
+                       throw new InvalidArgumentException( 'Please provide a 
proper timestamp format for wfTimestamp()' );
+               }
+
+       }
+
+       /**
+        * @return string
+        */
+       public function getEntityId() {
+               return $this->entityId;
+       }
+
+       /**
+        * @return string
+        */
+       public function getPropertyId() {
+               return $this->pid;
+       }
+
+       /**
+        * @return string
+        */
+       public function getClaimGuid() {
+               return $this->claimGuid;
+       }
+
+       /**
+        * @return string
+        */
+       public function getConstraintClaimGuid() {
+               return $this->constraintClaimGuid;
+       }
+
+       /**
+        * @return string
+        */
+       public function getConstraintTypeEntityId() {
+               return $this->constraintTypeEntityId;
+       }
+
+       /**
+        * @return string
+        */
+       public function getAdditionalInfo() {
+               return $this->additionalInfo;
+       }
+
+       /**
+        * @return string
+        */
+       public function getUpdatedAt() {
+               return $this->updatedAt;
+       }
+
+       /**
+        * @return int
+        */
+       public function getRevisionId() {
+               return $this->revisionId;
+       }
+
+       /**
+        * @return string
+        */
+       public function getStatus() {
+               return $this->status;
+       }
+}
\ No newline at end of file
diff --git a/includes/Violations/ViolationLookup.php 
b/includes/Violations/ViolationLookup.php
new file mode 100644
index 0000000..c1d801d
--- /dev/null
+++ b/includes/Violations/ViolationLookup.php
@@ -0,0 +1,73 @@
+<?php
+
+namespace WikidataQuality\Violations;
+
+use Doctrine\Instantiator\Exception\InvalidArgumentException;
+use Wikibase\DataModel\Entity\ItemId;
+
+
+/**
+ * Class ViolationLookup
+ *
+ *
+ * @package WikidataQuality\ViolationLookup
+ * @author BP2014N1
+ * @license GNU GPL v2+
+ */
+class ViolationLookup {
+
+       private $db;
+
+       /**
+        * @param array $conditions
+        *
+        * @return array of Violations|null
+        */
+       public function getWhere( array $conditions ) {
+               $queryConditions = $this->convertConditions( $conditions );
+               $this->getDBConnection();
+               $queryResult = $this->db->select( VIOLATION_TABLE, '*', 
$queryConditions );
+               return $this->queryResultToViolationObjects( $queryResult );
+       }
+
+       private function convertConditions( $conditions ) {
+               $queryConditions = array ();
+               foreach ( $conditions as $column => $value ) {
+                       if ( !is_string( $column ) || !is_string( $value ) ) {
+                               throw new InvalidArgumentException( 'The values 
provided in $opts have to be of type string' );
+                       }
+                       $queryConditions[ ] = $column . '="' . $value . '"';
+               }
+               return $queryConditions;
+       }
+
+       private function queryResultToViolationObjects( $queryResult ) {
+
+               if ( $queryResult->numRows() == 0 ) {
+                       return null;
+               }
+
+               $violations = array ();
+               foreach ( $queryResult as $result ) {
+                       $violation = new Violation(
+                               new ItemId( $result->entity_id ),
+                               array ( 'pid' => $result->pid, 'claimGuid' => 
$result->claim_guid ),
+                               $result->constraint_claim_guid,
+                               new ItemId( $result->constraint_type_entity_id 
),
+                               (int) $result->revision_id,
+                               $result->status,
+                               $result->additional_info,
+                               $result->updated_at
+                       );
+                       $violations[ ] = $violation;
+               }
+
+               return $violations;
+       }
+
+       private function getDBConnection() {
+               wfWaitForSlaves();
+               $loadBalancer = wfGetLB();
+               $this->db = $loadBalancer->getConnection( DB_MASTER );
+       }
+}
\ No newline at end of file
diff --git a/includes/Violations/ViolationStore.php 
b/includes/Violations/ViolationStore.php
new file mode 100644
index 0000000..f750d60
--- /dev/null
+++ b/includes/Violations/ViolationStore.php
@@ -0,0 +1,132 @@
+<?php
+
+namespace WikidataQuality\Violations;
+
+use Doctrine\Instantiator\Exception\InvalidArgumentException;
+use WikidataQuality\Violations\Violation;
+
+
+/**
+ * Class ViolationStore
+ *
+ * Inserts, updates and deletes entries in the violation table
+ *
+ * @package WikidataQuality\ViolationStore
+ * @author BP2014N1
+ * @license GNU GPL v2+
+ */
+class ViolationStore {
+
+       private $db;
+
+       /**
+        * @param array $violations
+        *
+        * @throws \DBError
+        */
+       public function insertViolation( array $violations ) {
+               foreach ( $violations as $violation ) {
+                       if ( !( $violation instanceof Violation ) ) {
+                               throw new InvalidArgumentException( 'Input of 
ValueStore has to be of type WikidataQuality\Violations\Violation' );
+                       }
+               }
+
+               $this->getDBConnection();
+
+               foreach ( $violations as $violation ) {
+                       $updatedAt = wfTimestamp( TS_MW );
+                       $accumulator = array (
+                               'entity_id' => $violation->getEntityId(),
+                               'pid' => $violation->getPropertyId(),
+                               'claim_guid' => $violation->getClaimGuid(),
+                               'constraint_claim_guid' => 
$violation->getConstraintClaimGuid(),
+                               'constraint_type_entity_id' => 
$violation->getConstraintTypeEntityId(),
+                               '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 \WikidataQuality\Violations\Violation $violation
+        *
+        * @return mixed
+        * @throws \DBError
+        */
+       public function removeViolationWith( $claimGuid, $constraintClaimGuid ) 
{
+
+               if ( !is_string( $claimGuid ) || !is_string( 
$constraintClaimGuid ) ) {
+                       throw new InvalidArgumentException( 'Input of 
ViolationStore::removeViolationWith() has to be string' );
+               }
+
+               $this->getDBConnection();
+
+               $success = $this->db->delete( VIOLATION_TABLE, array (
+                       "claim_guid=\"$claimGuid\"",
+                       "constraint_claim_guid=\"$constraintClaimGuid\""
+               ) );
+
+               return $success;
+       }
+
+       /**
+        * @throws \DBError
+        * @throws \MWException
+        */
+       private function getDBConnection() {
+               wfWaitForSlaves();
+               $loadBalancer = wfGetLB();
+               $this->db = $loadBalancer->getConnection( DB_MASTER );
+       }
+
+       /**
+        * @param \WikidataQuality\Violations\Violation $violation
+        *
+        * @return bool
+        */
+       private function violationExists( Violation $violation ) {
+               $claimGuid = $violation->getClaimGuid();
+               $constraintClaimGuid = $violation->getConstraintClaimGuid();
+               $existing = $this->db->selectRow( VIOLATION_TABLE,
+                                                                               
  array ( 'claim_guid', 'constraint_claim_guid' ),
+                                                                               
  array (
+                                                                               
          "claim_guid=\"$claimGuid\"",
+                                                                               
          "constraint_claim_guid=\"$constraintClaimGuid\""
+                                                                               
  ) );
+               return $existing ? true : false;
+       }
+
+       /**
+        * @param array $accumulator
+        *
+        * @return mixed
+        */
+       private function insertNewViolation( array $accumulator ) {
+               $success = $this->db->insert( VIOLATION_TABLE, $accumulator );
+               return $success;
+       }
+
+       /**
+        * @param array $accumulator
+        *
+        * @return mixed
+        */
+       private function updateViolation( array $accumulator ) {
+               $claimGuid = $accumulator[ 'claim_guid' ];
+               $constraintClaimGuid = $accumulator[ 'constraint_claim_guid' ];
+               $success = $this->db->update(
+                       VIOLATION_TABLE,
+                       $accumulator,
+                       array ( "claim_guid=\"$claimGuid\"", 
"constraint_claim_guid=\"$constraintClaimGuid\"" )
+               );
+               return $success;
+       }
+}
\ No newline at end of file
diff --git a/sql/create_wdqa_violations.sql b/sql/create_wdqa_violations.sql
new file mode 100644
index 0000000..371fa14
--- /dev/null
+++ b/sql/create_wdqa_violations.sql
@@ -0,0 +1,15 @@
+CREATE TABLE IF NOT EXISTS wdqa_violations (
+  entity_id                     VARBINARY(15)     NOT NULL,
+  pid                           VARBINARY(15)     NOT NULL,
+  claim_guid                    VARBINARY(63)     NOT NULL,
+  constraint_claim_guid         VARBINARY(63)     NOT NULL,
+  constraint_type_entity_id     VARBINARY(15)     NOT NULL,
+  additional_info               TEXT              DEFAULT NULL,
+  updated_at                    VARBINARY(31)     NOT NULL,
+  revision_id                   INT(10) UNSIGNED  NOT NULL,
+  status                        VARBINARY(31)     NOT NULL,
+  PRIMARY KEY (claim_guid, constraint_claim_guid)
+);
+
+CREATE INDEX claim_guid ON wdqa_violations (claim_guid);
+CREATE INDEX constraint_claim_guid ON wdqa_violations (constraint_claim_guid);
\ No newline at end of file
diff --git a/tests/phpunit/Violations/ViolationLookupTest.php 
b/tests/phpunit/Violations/ViolationLookupTest.php
new file mode 100644
index 0000000..d2eac96
--- /dev/null
+++ b/tests/phpunit/Violations/ViolationLookupTest.php
@@ -0,0 +1,79 @@
+<?php
+
+namespace WikidataQuality\Tests\Violation;
+
+use WikidataQuality\Violations\ViolationLookup;
+
+
+/**
+ * @covers WikidataQuality\Violations\ViolationLookup
+ *
+ * @uses   WikidataQuality\Violations\Violation
+ *
+ * @group database
+ * @group medium
+ *
+ * @author BP2014N1
+ * @license GNU GPL v2+
+ */
+class ViolationLookupTest extends \MediaWikiTestCase {
+
+       public function testGet( ) {
+               $violationLookup = new ViolationLookup();
+
+               $queryResult = $violationLookup->getWhere( array( 'entity_id' 
=> 'Q42') );
+               $this->assertEquals( 3, count( $queryResult ) );
+
+               $queryResult = $violationLookup->getWhere( array( 'claim_guid' 
=> 'P13$1-2-3-4', 'constraint_claim_guid' => 'P667$1-2-3-4' ) );
+               $this->assertEquals( 1, count( $queryResult ) );
+               $this->assertEquals( 'second', $queryResult[ 0 
]->getAdditionalInfo() );
+
+               $queryResult = $violationLookup->getWhere( array( 'status' => 
'exception' ) );
+               $this->assertNull( $queryResult );
+       }
+
+       public function testGetWithInvalidArguments() {
+               $violationLookup = new ViolationLookup();
+               $this->setExpectedException( 'InvalidArgumentException' );
+               $violationLookup->getWhere( array( 'pid' => 'P42', 
'constraint_type_entity_id' => 1234 ) );
+       }
+
+       public function addDBData() {
+               $this->db->delete( VIOLATION_TABLE, '*' );
+               $this->db->insert( VIOLATION_TABLE, array (
+                       array (
+                               'entity_id' => 'Q42',
+                               'pid' => 'P13',
+                               'claim_guid' => 'P13$1-2-3-4',
+                               'constraint_claim_guid' => 'P666$1-2-3-4',
+                               'constraint_type_entity_id' => 'Q666',
+                               'additional_info' => 'first',
+                               'updated_at' => '20141015150000',
+                               'revision_id' => 1234,
+                               'status' => 'verified'
+                       ),
+                       array (
+                               'entity_id' => 'Q42',
+                               'pid' => 'P13',
+                               'claim_guid' => 'P13$1-2-3-4',
+                               'constraint_claim_guid' => 'P667$1-2-3-4',
+                               'constraint_type_entity_id' => 'Q666',
+                               'additional_info' => 'second',
+                               'updated_at' => '20141015150000',
+                               'revision_id' => 1234,
+                               'status' => 'verified'
+                       ),
+                       array (
+                               'entity_id' => 'Q42',
+                               'pid' => 'P13',
+                               'claim_guid' => 'P13$1-2-3-4',
+                               'constraint_claim_guid' => 'P668$1-2-3-4',
+                               'constraint_type_entity_id' => 'Q666',
+                               'additional_info' => 'third',
+                               'updated_at' => '20141015150000',
+                               'revision_id' => 1234,
+                               'status' => 'verified'
+                       )
+               ) );
+       }
+}
diff --git a/tests/phpunit/Violations/ViolationStoreTest.php 
b/tests/phpunit/Violations/ViolationStoreTest.php
new file mode 100644
index 0000000..10eca25
--- /dev/null
+++ b/tests/phpunit/Violations/ViolationStoreTest.php
@@ -0,0 +1,105 @@
+<?php
+
+namespace WikidataQuality\Tests\Violation;
+
+use DataValues\StringValue;
+use Wikibase\DataModel\Claim\Claim;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Entity\PropertyId;
+use Wikibase\DataModel\Snak\PropertyValueSnak;
+use Wikibase\DataModel\Statement\Statement;
+use WikidataQuality\Violations\Violation;
+use WikidataQuality\Violations\ViolationStore;
+
+
+/**
+ * @covers WikidataQuality\Violations\ViolationStore
+ *
+ * @uses WikidataQuality\Violations\Violation
+ *
+ * @group database
+ * @group medium
+ *
+ * @author BP2014N1
+ * @license GNU GPL v2+
+ */
+class ViolationStoreTest extends \MediaWikiTestCase {
+
+       public function testRemoveViolation() {
+               $violationStore = new ViolationStore();
+
+               $actual = $this->db->selectRow( VIOLATION_TABLE, array (
+                       'claim_guid',
+                       'constraint_claim_guid'
+               ), array ( 'claim_guid="P13$1-2-3-4"', 
'constraint_claim_guid="P666$1-2-3-4"' ) );
+               $this->assertNotNull( $actual );
+
+               $violationStore->removeViolationWith( 'P13$1-2-3-4', 
'P666$1-2-3-4' );
+
+               // Doc: dbSelectRow return
+               $actual = $this->db->selectRow( VIOLATION_TABLE, array (
+                       'claim_guid',
+                       'constraint_claim_guid'
+               ), array ( 'claim_guid="P13$1-2-3-4"', 
'constraint_claim_guid="P666$1-2-3-4"' ) );
+               $this->assertFalse( $actual );
+
+               $this->setExpectedException( 'InvalidArgumentException' );
+               $violationStore->removeViolationWith( '1234', 1234 );
+               $violationStore->removeViolationWith( 1234, '1234' );
+       }
+
+       public function testInsertViolation() {
+               $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' );
+
+               $anotherViolation = new Violation( new ItemId( 'Q42' ), 
$statement, 'P668$1-2-3-4', new ItemId( 'Q666' ), 1234, 'verified', 
'{additional:information}' );
+
+               $count = $this->db->select( VIOLATION_TABLE, array (
+                       'claim_guid',
+                       'constraint_claim_guid'
+               ), array ( 'claim_guid="P13$1-2-3-4"' ) )->numRows();
+               $violationStore->insertViolation( array ( $violation, 
$anotherViolation ) );
+               $newCount = $this->db->select( VIOLATION_TABLE, array (
+                       'claim_guid',
+                       'constraint_claim_guid'
+               ), array ( 'claim_guid="P13$1-2-3-4"' ) )->numRows();
+               $this->assertEquals( $count + 2, $newCount );
+
+               $violation = new Violation( new ItemId( 'Q42' ), $statement, 
'P666$1-2-3-4', new ItemId( 'Q666' ), 1234, 'unverified' );
+               $violationStore->insertViolation( array ( $violation ) );
+
+               $updated = $this->db->selectRow( VIOLATION_TABLE, array( 
'status', 'updated_at' ), array (
+                       'claim_guid="P13$1-2-3-4"',
+                       'constraint_claim_guid="P666$1-2-3-4"'
+               ) );
+               $this->assertEquals( 'unverified', $updated->status );
+               $this->assertNotEquals( '20141015150000', $updated->updated_at 
);
+
+               $this->setExpectedException( 'InvalidArgumentException' );
+               $violationStore->insertViolation( array ( $violation, 'abcd' ) 
);
+       }
+
+       public function addDBData() {
+               $this->db->delete( VIOLATION_TABLE, '*' );
+               $this->db->insert( VIOLATION_TABLE,
+                                                  array (
+                                                          'entity_id' => 'Q42',
+                                                          'pid' => 'P13',
+                                                          'claim_guid' => 
'P13$1-2-3-4',
+                                                          
'constraint_claim_guid' => 'P666$1-2-3-4',
+                                                          
'constraint_type_entity_id' => 'Q666',
+                                                          'additional_info' => 
null,
+                                                          'updated_at' => 
wfTimestamp( TS_MW, '2014-10-15T15:00:00Z' ),
+                                                          'revision_id' => 
1234,
+                                                          'status' => 'valid'
+                                                  )
+               );
+       }
+}
diff --git a/tests/phpunit/Violations/ViolationTest.php 
b/tests/phpunit/Violations/ViolationTest.php
new file mode 100644
index 0000000..60b9f4b
--- /dev/null
+++ b/tests/phpunit/Violations/ViolationTest.php
@@ -0,0 +1,180 @@
+<?php
+
+namespace WikidataQuality\Tests\Violation;
+
+use DataValues\StringValue;
+use Wikibase\DataModel\Claim\Claim;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Entity\PropertyId;
+use Wikibase\DataModel\Snak\PropertyValueSnak;
+use Wikibase\DataModel\Statement\Statement;
+use WikidataQuality\Violations\Violation;
+
+
+/**
+ * @covers WikidataQuality\Violations\Violation
+ *
+ * @author BP2014N1
+ * @license GNU GPL v2+
+ */
+class ViolationTest extends \MediaWikiTestCase {
+
+       /**
+        * @dataProvider validArgumentsProvider
+        */
+       public function testConstructWithValidArguments( $entityId, $statement, 
$constraintClaimGuid, $constraintTypeEntityId, $revisionId, $status, 
$additionalInfo, $updatedAt ) {
+               $violation = new Violation( $entityId, $statement, 
$constraintClaimGuid, $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( $constraintTypeEntityId, 
$violation->getConstraintTypeEntityId() );
+               $this->assertEquals( $revisionId, $violation->getRevisionId() );
+               $this->assertEquals( $status, $violation->getStatus() );
+               $this->assertEquals( $additionalInfo, 
$violation->getAdditionalInfo() );
+               if ($updatedAt) {
+                       $this->assertEquals( wfTimestamp( TS_MW, $updatedAt ), 
$violation->getUpdatedAt() );
+               } else {
+                       $this->assertEquals( null, $violation->getUpdatedAt() );
+               }
+       }
+
+       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';
+               $constraintTypeEntityId = new ItemId( 'Q666' );
+               $revisionId = 1234;
+               $status = 'verified';
+
+               return array(
+                       array(
+                               $entityId,
+                               $statement,
+                               $constraintClaimGuid,
+                               $constraintTypeEntityId,
+                               $revisionId,
+                               $status,
+                               '{"type":"JSON", "mandatory":false}',
+                               '2014-10-15T15:00:00Z'
+                       ),
+                       array(
+                               $entityId,
+                               $statement,
+                               $constraintClaimGuid,
+                               $constraintTypeEntityId,
+                               $revisionId,
+                               $status,
+                               null,
+                               null
+                       ),
+                       array(
+                               $entityId,
+                               array( 'pid' => 'P1', 'claimGuid' => $claimGuid 
),
+                               $constraintClaimGuid,
+                               $constraintTypeEntityId,
+                               $revisionId,
+                               $status,
+                               null,
+                               null
+                       )
+               );
+       }
+
+       /**
+        * @dataProvider invalidArgumentsProvider
+        */
+       public function testConstructWithInvalidArguments( $entityId, 
$statement, $constraintClaimGuid, $constraintTypeEntityId, $revisionId, 
$status, $additionalInfo, $updatedAt ) {
+               $this->setExpectedException( 'InvalidArgumentException' );
+
+               $violation = new Violation( $entityId, $statement, 
$constraintClaimGuid, $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';
+               $constraintTypeEntityId = new ItemId( 'Q666' );
+               $revisionId = 1234;
+               $status = 'verified';
+
+               return array(
+                       array(
+                               $entityId,
+                               1234,
+                               $constraintClaimGuid,
+                               $constraintTypeEntityId,
+                               $revisionId,
+                               $status,
+                               null,
+                               null
+                       ),
+                       array(
+                               $entityId,
+                               $statement,
+                               1234,
+                               $constraintTypeEntityId,
+                               $revisionId,
+                               $status,
+                               null,
+                               null
+                       ),
+                       array(
+                               $entityId,
+                               $statement,
+                               $constraintClaimGuid,
+                               $constraintTypeEntityId,
+                               '1234',
+                               $status,
+                               null,
+                               null
+                       ),
+                       array(
+                               $entityId,
+                               $statement,
+                               $constraintClaimGuid,
+                               $constraintTypeEntityId,
+                               $revisionId,
+                               1234,
+                               null,
+                               null
+                       ),
+                       array(
+                               $entityId,
+                               $statement,
+                               $constraintClaimGuid,
+                               $constraintTypeEntityId,
+                               $revisionId,
+                               $status,
+                               1234,
+                               null
+                       ),
+                       array(
+                               $entityId,
+                               $statement,
+                               $constraintClaimGuid,
+                               $constraintTypeEntityId,
+                               $revisionId,
+                               $status,
+                               null,
+                               '2014-10-15'
+                       )
+               );
+       }
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iaa0ab312a9c4118d28fdab4577c1d431dee90069
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikidataQuality
Gerrit-Branch: master
Gerrit-Owner: Tamslo <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to