jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/352865 )
Change subject: Import Type and Value type constraints from statements
......................................................................
Import Type and Value type constraints from statements
ConstraintReportFactory, TypeChecker and ValueTypeChecker all get a
ConstraintStatementParameterParser injected. ConstraintReportFactory
passes the helper to the checkers, which delegate all parameter parsing
to the helper.
Configuration variables for the two constraint type item IDs are added,
adn ConstraintReportFactory aliases them to the “Type” and “Value type”
constraint types.
Tests for the checkers’ behavior with missing parameters are removed,
since those tests now throw an exception (which is only turned into a
CheckResult in DelegatingConstraintChecker, not in the individual
checkers) and are covered by the dedicated tests for
ConstraintStatementParameterParser.
A few new tests for the checkers are added, which use statement-type
parameters instead of template-style ones.
Bug: T167106
Change-Id: Id47731c99b6e8ae10d540315fc68994cf2f0f24e
---
M api/CheckConstraints.php
M extension.json
M includes/ConstraintCheck/Checker/TypeChecker.php
M includes/ConstraintCheck/Checker/ValueTypeChecker.php
M includes/ConstraintReportFactory.php
M tests/phpunit/Checker/TypeChecker/TypeCheckerTest.php
M tests/phpunit/Checker/TypeChecker/ValueTypeCheckerTest.php
M tests/phpunit/DelegatingConstraintCheckerTest.php
8 files changed, 182 insertions(+), 120 deletions(-)
Approvals:
WMDE-leszek: Looks good to me, but someone else must approve
Jonas Kress (WMDE): Looks good to me, approved
jenkins-bot: Verified
diff --git a/api/CheckConstraints.php b/api/CheckConstraints.php
index c5c9d8e..37e84e8 100644
--- a/api/CheckConstraints.php
+++ b/api/CheckConstraints.php
@@ -24,6 +24,7 @@
use Wikibase\Repo\EntityIdLabelFormatterFactory;
use Wikibase\Repo\WikibaseRepo;
use
WikibaseQuality\ConstraintReport\ConstraintCheck\DelegatingConstraintChecker;
+use
WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintStatementParameterParser;
use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\CheckResult;
use WikibaseQuality\ConstraintReport\ConstraintParameterRenderer;
use WikibaseQuality\ConstraintReport\ConstraintReportFactory;
@@ -112,11 +113,17 @@
$entityIdFormatter =
$entityIdHtmlLinkFormatterFactory->getEntityIdFormatter(
$labelDescriptionLookup );
$statementGuidParser = $repo->getStatementGuidParser();
$constraintParameterRenderer = new ConstraintParameterRenderer(
$entityIdFormatter, $valueFormatter );
+ $config = MediaWikiServices::getInstance()->getMainConfig();
$constraintReportFactory = new ConstraintReportFactory(
$repo->getEntityLookup(),
$statementGuidParser,
- MediaWikiServices::getInstance()->getMainConfig(),
+ $config,
$constraintParameterRenderer,
+ new ConstraintStatementParameterParser(
+ $config,
+ $repo->getBaseDataModelDeserializerFactory(),
+ $constraintParameterRenderer
+ ),
$repo->getRdfVocabulary(),
$repo->getEntityIdParser()
);
diff --git a/extension.json b/extension.json
index ce0160a..d3cc147 100644
--- a/extension.json
+++ b/extension.json
@@ -111,6 +111,16 @@
"description": "The item ID of the 'symmetric
constraint' item, which, when used in a 'property constraint' statement on a
property, indicates that a referenced entity should refer back to the original
entity.",
"public": true
},
+ "WBQualityConstraintsTypeConstraintId": {
+ "value": "Q21503250",
+ "description": "The item ID of the 'type constraint'
item, which, when used in a 'property constraint' statement on a property,
indicates that the subject entity should have a certain type, with the relation
and type given in the parameters.",
+ "public": true
+ },
+ "WBQualityConstraintsValueTypeConstraintId": {
+ "value": "Q21510865",
+ "description": "The item ID of the 'value type
constraint' item, which, when used in a 'property constraint' statement on a
property, indicates that the referenced entity should have a certain type, with
the class and relation given in the parameters.",
+ "public": true
+ },
"WBQualityConstraintsClassId": {
"value": "P2308",
"description": "The property ID of the 'relation'
property, which specifies the class/type of a 'type' or 'value type'
constraint.",
diff --git a/includes/ConstraintCheck/Checker/TypeChecker.php
b/includes/ConstraintCheck/Checker/TypeChecker.php
index 87e28db..494db1e 100644
--- a/includes/ConstraintCheck/Checker/TypeChecker.php
+++ b/includes/ConstraintCheck/Checker/TypeChecker.php
@@ -4,11 +4,12 @@
use Config;
use Wikibase\DataModel\Entity\EntityDocument;
+use Wikibase\DataModel\Entity\ItemId;
use Wikibase\DataModel\Services\Lookup\EntityLookup;
use Wikibase\DataModel\Statement\StatementListProvider;
use WikibaseQuality\ConstraintReport\Constraint;
use WikibaseQuality\ConstraintReport\ConstraintCheck\ConstraintChecker;
-use
WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterParser;
+use
WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintStatementParameterParser;
use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\CheckResult;
use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\TypeCheckerHelper;
use Wikibase\DataModel\Statement\Statement;
@@ -21,9 +22,9 @@
class TypeChecker implements ConstraintChecker {
/**
- * @var ConstraintParameterParser
+ * @var ConstraintStatementParameterParser
*/
- private $helper;
+ private $constraintParameterParser;
/**
* @var EntityLookup
@@ -42,13 +43,18 @@
/**
* @param EntityLookup $lookup
- * @param ConstraintParameterParser $helper
+ * @param ConstraintStatementParameterParser $constraintParameterParser
* @param TypeCheckerHelper $typeCheckerHelper
* @param Config $config
*/
- public function __construct( EntityLookup $lookup,
ConstraintParameterParser $helper, TypeCheckerHelper $typeCheckerHelper, Config
$config ) {
+ public function __construct(
+ EntityLookup $lookup,
+ ConstraintStatementParameterParser $constraintParameterParser,
+ TypeCheckerHelper $typeCheckerHelper,
+ Config $config
+ ) {
$this->entityLookup = $lookup;
- $this->helper = $helper;
+ $this->constraintParameterParser = $constraintParameterParser;
$this->typeCheckerHelper = $typeCheckerHelper;
$this->config = $config;
}
@@ -66,39 +72,21 @@
$parameters = [];
$constraintParameters = $constraint->getConstraintParameters();
- $classes = false;
- if ( array_key_exists( 'class', $constraintParameters ) ) {
- $classes = explode( ',', $constraintParameters['class']
);
- $parameters['class'] =
$this->helper->parseParameterArray( $classes );
- }
+ $classes =
$this->constraintParameterParser->parseClassParameter( $constraintParameters,
$constraint->getConstraintTypeName() );
+ $parameters['class'] = array_map(
+ function( $id ) {
+ return new ItemId( $id );
+ },
+ $classes
+ );
- $relation = false;
- if ( array_key_exists( 'relation', $constraintParameters ) ) {
- $relation = $constraintParameters['relation'];
- $parameters['relation'] =
$this->helper->parseSingleParameter( $relation, true );
- }
-
- /*
- * error handling:
- * parameter $constraintParameters['class'] must not be null
- */
- if ( !$classes ) {
- $message = wfMessage(
"wbqc-violation-message-parameter-needed" )->params(
$constraint->getConstraintTypeName(), 'class' )->escaped();
- return new CheckResult( $entity->getId(), $statement,
$constraint->getConstraintTypeQid(), $constraint->getConstraintId(),
$parameters, CheckResult::STATUS_VIOLATION, $message );
- }
-
- /*
- * error handling:
- * parameter $constraintParameters['relation'] must be either
'instance' or 'subclass'
- */
+ $relation =
$this->constraintParameterParser->parseRelationParameter(
$constraintParameters, $constraint->getConstraintTypeName() );
if ( $relation === 'instance' ) {
$relationId = $this->config->get(
'WBQualityConstraintsInstanceOfId' );
} elseif ( $relation === 'subclass' ) {
$relationId = $this->config->get(
'WBQualityConstraintsSubclassOfId' );
- } else {
- $message = wfMessage(
"wbqc-violation-message-type-relation-instance-or-subclass" )->escaped();
- return new CheckResult( $entity->getId(), $statement,
$constraint->getConstraintTypeQid(), $constraint->getConstraintId(),
$parameters, CheckResult::STATUS_VIOLATION, $message );
}
+ $parameters['relation'] = [ $relation ];
if ( $this->typeCheckerHelper->hasClassInRelation(
$entity->getStatements(), $relationId, $classes ) ) {
$message = '';
diff --git a/includes/ConstraintCheck/Checker/ValueTypeChecker.php
b/includes/ConstraintCheck/Checker/ValueTypeChecker.php
index b4a15ed..218a182 100644
--- a/includes/ConstraintCheck/Checker/ValueTypeChecker.php
+++ b/includes/ConstraintCheck/Checker/ValueTypeChecker.php
@@ -5,12 +5,13 @@
use Config;
use Wikibase\DataModel\Entity\EntityDocument;
use Wikibase\DataModel\Entity\EntityIdValue;
+use Wikibase\DataModel\Entity\ItemId;
use Wikibase\DataModel\Services\Lookup\EntityLookup;
use Wikibase\DataModel\Snak\PropertyValueSnak;
use Wikibase\DataModel\Statement\StatementListProvider;
use WikibaseQuality\ConstraintReport\Constraint;
use WikibaseQuality\ConstraintReport\ConstraintCheck\ConstraintChecker;
-use
WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterParser;
+use
WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintStatementParameterParser;
use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\CheckResult;
use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\TypeCheckerHelper;
use Wikibase\DataModel\Statement\Statement;
@@ -23,9 +24,9 @@
class ValueTypeChecker implements ConstraintChecker {
/**
- * @var ConstraintParameterParser
+ * @var ConstraintStatementParameterParser
*/
- private $helper;
+ private $constraintParameterParser;
/**
* @var EntityLookup
@@ -44,13 +45,18 @@
/**
* @param EntityLookup $lookup
- * @param ConstraintParameterParser $helper
+ * @param ConstraintStatementParameterParser $constraintParameterParser
* @param TypeCheckerHelper $typeCheckerHelper
* @param Config $config
*/
- public function __construct( EntityLookup $lookup,
ConstraintParameterParser $helper, TypeCheckerHelper $typeCheckerHelper, Config
$config ) {
+ public function __construct(
+ EntityLookup $lookup,
+ ConstraintStatementParameterParser $constraintParameterParser,
+ TypeCheckerHelper $typeCheckerHelper,
+ Config $config
+ ) {
$this->entityLookup = $lookup;
- $this->helper = $helper;
+ $this->constraintParameterParser = $constraintParameterParser;
$this->typeCheckerHelper = $typeCheckerHelper;
$this->config = $config;
}
@@ -68,20 +74,24 @@
$parameters = [];
$constraintParameters = $constraint->getConstraintParameters();
- $classes = false;
- if ( array_key_exists( 'class', $constraintParameters ) ) {
- $classes = explode( ',', $constraintParameters['class']
);
- $parameters['class'] =
$this->helper->parseParameterArray( $classes );
- }
+ $classes =
$this->constraintParameterParser->parseClassParameter( $constraintParameters,
$constraint->getConstraintTypeName() );
+ $parameters['class'] = array_map(
+ function( $id ) {
+ return new ItemId( $id );
+ },
+ $classes
+ );
- $relation = false;
- if ( array_key_exists( 'relation', $constraintParameters ) ) {
- $relation = $constraintParameters['relation'];
- $parameters['relation'] =
$this->helper->parseSingleParameter( $relation, true );
+ $relation =
$this->constraintParameterParser->parseRelationParameter(
$constraintParameters, $constraint->getConstraintTypeName() );
+ if ( $relation === 'instance' ) {
+ $relationId = $this->config->get(
'WBQualityConstraintsInstanceOfId' );
+ } elseif ( $relation === 'subclass' ) {
+ $relationId = $this->config->get(
'WBQualityConstraintsSubclassOfId' );
}
+ $parameters['relation'] = [ $relation ];
if ( array_key_exists( 'constraint_status',
$constraintParameters ) ) {
- $parameters['constraint_status'] =
$this->helper->parseSingleParameter(
$constraintParameters['constraint_status'], true );
+ $parameters['constraint_status'] = strval(
$constraintParameters['constraint_status'] );
}
$mainSnak = $statement->getMainSnak();
@@ -100,31 +110,12 @@
/*
* error handling:
* type of $dataValue for properties with 'Value type'
constraint has to be 'wikibase-entityid'
- * parameter $constraintParameters['class'] must not be null
*/
if ( $dataValue->getType() !== 'wikibase-entityid' ) {
$message = wfMessage(
"wbqc-violation-message-value-needed-of-type" )->params(
$constraint->getConstraintTypeQid(), 'wikibase-entityid' )->escaped();
return new CheckResult( $entity->getId(), $statement,
$constraint->getConstraintTypeQid(), $constraint->getConstraintId(),
$parameters, CheckResult::STATUS_VIOLATION, $message );
}
/** @var EntityIdValue $dataValue */
-
- if ( !$classes ) {
- $message = wfMessage(
"wbqc-violation-message-parameter-needed" )->params(
$constraint->getConstraintTypeQid(), 'class' )->escaped();
- return new CheckResult( $entity->getId(), $statement,
$constraint->getConstraintTypeQid(), $constraint->getConstraintId(),
$parameters, CheckResult::STATUS_VIOLATION, $message );
- }
-
- /*
- * error handling:
- * parameter $constraintParameters['relation'] must be either
'instance' or 'subclass'
- */
- if ( $relation === 'instance' ) {
- $relationId = $this->config->get(
'WBQualityConstraintsInstanceOfId' );
- } elseif ( $relation === 'subclass' ) {
- $relationId = $this->config->get(
'WBQualityConstraintsSubclassOfId' );
- } else {
- $message = wfMessage(
"wbqc-violation-message-type-relation-instance-or-subclass" )->escaped();
- return new CheckResult( $entity->getId(), $statement,
$constraint->getConstraintTypeQid(), $constraint->getConstraintId(),
$parameters, CheckResult::STATUS_VIOLATION, $message );
- }
$item = $this->entityLookup->getEntity(
$dataValue->getEntityId() );
diff --git a/includes/ConstraintReportFactory.php
b/includes/ConstraintReportFactory.php
index a1823c0..5cc08be 100644
--- a/includes/ConstraintReportFactory.php
+++ b/includes/ConstraintReportFactory.php
@@ -12,6 +12,7 @@
use Wikibase\Repo\WikibaseRepo;
use
WikibaseQuality\ConstraintReport\ConstraintCheck\DelegatingConstraintChecker;
use
WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterParser;
+use
WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintStatementParameterParser;
use
WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\CommonsLinkChecker;
use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\FormatChecker;
use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\OneOfChecker;
@@ -83,6 +84,11 @@
private $constraintParameterRenderer;
/**
+ * @var ConstraintStatementParameterParser
+ */
+ private $constraintStatementParameterParser;
+
+ /**
* @var RdfVocabulary
*/
private $rdfVocabulary;
@@ -108,16 +114,23 @@
$wikibaseRepo->getUserLanguage()
)
);
+ $config =
MediaWikiServices::getInstance()->getMainConfig();
+ $constraintParameterRenderer = new
ConstraintParameterRenderer(
+ $entityIdFormatter,
+
$wikibaseRepo->getValueFormatterFactory()->getValueFormatter(
+ SnakFormatter::FORMAT_HTML,
+ new FormatterOptions()
+ )
+ );
$instance = new self(
$wikibaseRepo->getEntityLookup(),
$wikibaseRepo->getStatementGuidParser(),
-
MediaWikiServices::getInstance()->getMainConfig(),
- new ConstraintParameterRenderer(
- $entityIdFormatter,
-
$wikibaseRepo->getValueFormatterFactory()->getValueFormatter(
- SnakFormatter::FORMAT_HTML,
- new FormatterOptions()
- )
+ $config,
+ $constraintParameterRenderer,
+ new ConstraintStatementParameterParser(
+ $config,
+
$wikibaseRepo->getBaseDataModelDeserializerFactory(),
+ $constraintParameterRenderer
),
$wikibaseRepo->getRdfVocabulary(),
$wikibaseRepo->getEntityIdParser()
@@ -132,6 +145,7 @@
StatementGuidParser $statementGuidParser,
Config $config,
ConstraintParameterRenderer $constraintParameterRenderer,
+ ConstraintStatementParameterParser
$constraintStatementParameterParser,
RdfVocabulary $rdfVocabulary,
EntityIdParser $entityIdParser
) {
@@ -139,6 +153,7 @@
$this->statementGuidParser = $statementGuidParser;
$this->config = $config;
$this->constraintParameterRenderer =
$constraintParameterRenderer;
+ $this->constraintStatementParameterParser =
$constraintStatementParameterParser;
$this->rdfVocabulary = $rdfVocabulary;
$this->entityIdParser = $entityIdParser;
}
@@ -184,8 +199,8 @@
'Mandatory qualifiers' => new
MandatoryQualifiersChecker( $constraintParameterParser,
$this->constraintParameterRenderer ),
'Range' => new RangeChecker(
$constraintParameterParser, $rangeCheckerHelper,
$this->constraintParameterRenderer ),
'Diff within range' => new
DiffWithinRangeChecker( $constraintParameterParser, $rangeCheckerHelper,
$this->constraintParameterRenderer ),
- 'Type' => new TypeChecker( $this->lookup,
$constraintParameterParser, $typeCheckerHelper, $this->config ),
- 'Value type' => new ValueTypeChecker(
$this->lookup, $constraintParameterParser, $typeCheckerHelper, $this->config ),
+ 'Type' => new TypeChecker( $this->lookup,
$this->constraintStatementParameterParser, $typeCheckerHelper, $this->config ),
+ 'Value type' => new ValueTypeChecker(
$this->lookup, $this->constraintStatementParameterParser, $typeCheckerHelper,
$this->config ),
'Single value' => new SingleValueChecker(),
'Multi value' => new MultiValueChecker(),
'Unique value' => new UniqueValueChecker(
$sparqlHelper ),
@@ -201,6 +216,8 @@
$this->config->get(
'WBQualityConstraintsUsedAsQualifierConstraintId' ) =>
$this->constraintCheckerMap['Qualifier'],
$this->config->get(
'WBQualityConstraintsSingleValueConstraintId' ) =>
$this->constraintCheckerMap['Single value'],
$this->config->get(
'WBQualityConstraintsSymmetricConstraintId' ) =>
$this->constraintCheckerMap['Symmetric'],
+ $this->config->get(
'WBQualityConstraintsTypeConstraintId' ) => $this->constraintCheckerMap['Type'],
+ $this->config->get(
'WBQualityConstraintsValueTypeConstraintId' ) =>
$this->constraintCheckerMap['Value type'],
];
}
diff --git a/tests/phpunit/Checker/TypeChecker/TypeCheckerTest.php
b/tests/phpunit/Checker/TypeChecker/TypeCheckerTest.php
index b36d123..9c68443 100644
--- a/tests/phpunit/Checker/TypeChecker/TypeCheckerTest.php
+++ b/tests/phpunit/Checker/TypeChecker/TypeCheckerTest.php
@@ -9,12 +9,12 @@
use Wikibase\DataModel\Entity\PropertyId;
use WikibaseQuality\ConstraintReport\Constraint;
use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\TypeChecker;
-use
WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterParser;
use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\TypeCheckerHelper;
use WikibaseQuality\ConstraintReport\ConstraintParameterRenderer;
use WikibaseQuality\ConstraintReport\Tests\ConstraintParameters;
use WikibaseQuality\ConstraintReport\Tests\ResultAssertions;
use WikibaseQuality\Tests\Helper\JsonFileEntityLookup;
+use Wikibase\Repo\WikibaseRepo;
/**
* @covers
\WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\TypeChecker
@@ -22,7 +22,7 @@
* @group WikibaseQualityConstraints
*
* @uses \WikibaseQuality\ConstraintReport\ConstraintCheck\Result\CheckResult
- * @uses
\WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterParser
+ * @uses
\WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintStatementParameterParser
*
* @author BP2014N1
* @license GNU GPL v2+
@@ -51,7 +51,7 @@
$this->lookup = new JsonFileEntityLookup( __DIR__ );
$this->checker = new TypeChecker(
$this->lookup,
- new ConstraintParameterParser(),
+ $this->getConstraintParameterParser(),
new TypeCheckerHelper(
$this->lookup,
$this->getDefaultConfig(),
@@ -98,6 +98,27 @@
$this->assertCompliance( $checkResult );
}
+ public function testTypeConstraintInstanceValidWithStatement() {
+ $entity = $this->lookup->getEntity( new ItemId( 'Q1' ) );
+ $snakSerializer =
WikibaseRepo::getDefaultInstance()->getSerializerFactory()->newSnakSerializer();
+ $classId = $this->getDefaultConfig()->get(
'WBQualityConstraintsClassId' );
+ $relationId = $this->getDefaultConfig()->get(
'WBQualityConstraintsRelationId' );
+ $constraintParameters = [
+ $classId => [
+ $snakSerializer->serialize( new
PropertyValueSnak( new PropertyId( $classId ), new EntityIdValue( new ItemId(
'Q100' ) ) ) ),
+ $snakSerializer->serialize( new
PropertyValueSnak( new PropertyId( $classId ), new EntityIdValue( new ItemId(
'Q101' ) ) ) )
+ ],
+ $relationId => [
+ $snakSerializer->serialize( new
PropertyValueSnak(
+ new PropertyId( $relationId ),
+ new EntityIdValue( new ItemId(
$this->getDefaultConfig()->get( 'WBQualityConstraintsInstanceOfRelationId' ) ) )
+ ) )
+ ]
+ ];
+ $checkResult = $this->checker->checkConstraint(
$this->typeStatement, $this->getConstraintMock( $constraintParameters ),
$entity );
+ $this->assertCompliance( $checkResult );
+ }
+
public function testTypeConstraintSubclassValid() {
$entity = $this->lookup->getEntity( new ItemId( 'Q4' ) );
$constraintParameters = [
@@ -123,6 +144,27 @@
$constraintParameters = [
'class' => 'Q100,Q101',
'relation' => 'subclass'
+ ];
+ $checkResult = $this->checker->checkConstraint(
$this->typeStatement, $this->getConstraintMock( $constraintParameters ),
$entity );
+ $this->assertCompliance( $checkResult );
+ }
+
+ public function testTypeConstraintSubclassValidWithStatement() {
+ $entity = $this->lookup->getEntity( new ItemId( 'Q4' ) );
+ $snakSerializer =
WikibaseRepo::getDefaultInstance()->getSerializerFactory()->newSnakSerializer();
+ $classId = $this->getDefaultConfig()->get(
'WBQualityConstraintsClassId' );
+ $relationId = $this->getDefaultConfig()->get(
'WBQualityConstraintsRelationId' );
+ $constraintParameters = [
+ $classId => [
+ $snakSerializer->serialize( new
PropertyValueSnak( new PropertyId( $classId ), new EntityIdValue( new ItemId(
'Q100' ) ) ) ),
+ $snakSerializer->serialize( new
PropertyValueSnak( new PropertyId( $classId ), new EntityIdValue( new ItemId(
'Q101' ) ) ) )
+ ],
+ $relationId => [
+ $snakSerializer->serialize( new
PropertyValueSnak(
+ new PropertyId( $relationId ),
+ new EntityIdValue( new ItemId(
$this->getDefaultConfig()->get( 'WBQualityConstraintsSubclassOfRelationId' ) ) )
+ ) )
+ ]
];
$checkResult = $this->checker->checkConstraint(
$this->typeStatement, $this->getConstraintMock( $constraintParameters ),
$entity );
$this->assertCompliance( $checkResult );
@@ -186,24 +228,6 @@
];
$checkResult = $this->checker->checkConstraint(
$this->typeStatement, $this->getConstraintMock( $constraintParameters ),
$entity );
$this->assertViolation( $checkResult,
'wbqc-violation-message-type-subclass' );
- }
-
- public function testTypeConstraintMissingRelation() {
- $entity = $this->lookup->getEntity( new ItemId( 'Q1' ) );
- $constraintParameters = [
- 'class' => 'Q100,Q101'
- ];
- $checkResult = $this->checker->checkConstraint(
$this->typeStatement, $this->getConstraintMock( $constraintParameters ),
$entity );
- $this->assertViolation( $checkResult,
'wbqc-violation-message-type-relation-instance-or-subclass' );
- }
-
- public function testTypeConstraintMissingClass() {
- $entity = $this->lookup->getEntity( new ItemId( 'Q1' ) );
- $constraintParameters = [
- 'relation' => 'subclass'
- ];
- $checkResult = $this->checker->checkConstraint(
$this->typeStatement, $this->getConstraintMock( $constraintParameters ),
$entity );
- $this->assertViolation( $checkResult,
'wbqc-violation-message-parameter-needed' );
}
public function testTypeConstraintSubclassCycle() {
diff --git a/tests/phpunit/Checker/TypeChecker/ValueTypeCheckerTest.php
b/tests/phpunit/Checker/TypeChecker/ValueTypeCheckerTest.php
index 5abf14f..b5b5019 100644
--- a/tests/phpunit/Checker/TypeChecker/ValueTypeCheckerTest.php
+++ b/tests/phpunit/Checker/TypeChecker/ValueTypeCheckerTest.php
@@ -13,12 +13,12 @@
use DataValues\StringValue;
use WikibaseQuality\ConstraintReport\Constraint;
use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\ValueTypeChecker;
-use
WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterParser;
use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\TypeCheckerHelper;
use WikibaseQuality\ConstraintReport\ConstraintParameterRenderer;
use WikibaseQuality\ConstraintReport\Tests\ConstraintParameters;
use WikibaseQuality\ConstraintReport\Tests\ResultAssertions;
use WikibaseQuality\Tests\Helper\JsonFileEntityLookup;
+use Wikibase\Repo\WikibaseRepo;
/**
* @covers
\WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\ValueTypeChecker
@@ -26,7 +26,7 @@
* @group WikibaseQualityConstraints
*
* @uses \WikibaseQuality\ConstraintReport\ConstraintCheck\Result\CheckResult
- * @uses
\WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterParser
+ * @uses
\WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintStatementParameterParser
*
* @author BP2014N1
* @license GNU GPL v2+
@@ -56,7 +56,7 @@
$this->lookup = new JsonFileEntityLookup( __DIR__ );
$this->checker = new ValueTypeChecker(
$this->lookup,
- new ConstraintParameterParser(),
+ $this->getConstraintParameterParser(),
new TypeCheckerHelper(
$this->lookup,
$this->getDefaultConfig(),
@@ -104,6 +104,27 @@
$this->assertCompliance( $checkResult );
}
+ public function testTypeConstraintInstanceValidWithStatement() {
+ $statement = new Statement( new PropertyValueSnak(
$this->valueTypePropertyId, new EntityIdValue( new ItemId( 'Q3' ) ) ) );
+ $snakSerializer =
WikibaseRepo::getDefaultInstance()->getSerializerFactory()->newSnakSerializer();
+ $classId = $this->getDefaultConfig()->get(
'WBQualityConstraintsClassId' );
+ $relationId = $this->getDefaultConfig()->get(
'WBQualityConstraintsRelationId' );
+ $constraintParameters = [
+ $classId => [
+ $snakSerializer->serialize( new
PropertyValueSnak( new PropertyId( $classId ), new EntityIdValue( new ItemId(
'Q100' ) ) ) ),
+ $snakSerializer->serialize( new
PropertyValueSnak( new PropertyId( $classId ), new EntityIdValue( new ItemId(
'Q101' ) ) ) )
+ ],
+ $relationId => [
+ $snakSerializer->serialize( new
PropertyValueSnak(
+ new PropertyId( $relationId ),
+ new EntityIdValue( new ItemId(
$this->getDefaultConfig()->get( 'WBQualityConstraintsInstanceOfRelationId' ) ) )
+ ) )
+ ]
+ ];
+ $checkResult = $this->checker->checkConstraint( $statement,
$this->getConstraintMock( $constraintParameters ), $this->getEntity() );
+ $this->assertCompliance( $checkResult );
+ }
+
public function testValueTypeConstraintSubclassValid() {
$statement = new Statement( new PropertyValueSnak(
$this->valueTypePropertyId, new EntityIdValue( new ItemId( 'Q4' ) ) ) );
$constraintParameters = [
@@ -129,6 +150,27 @@
$constraintParameters = [
'relation' => 'subclass',
'class' => 'Q100,Q101'
+ ];
+ $checkResult = $this->checker->checkConstraint( $statement,
$this->getConstraintMock( $constraintParameters ), $this->getEntity() );
+ $this->assertCompliance( $checkResult );
+ }
+
+ public function testTypeConstraintSubclassValidWithStatement() {
+ $statement = new Statement( new PropertyValueSnak(
$this->valueTypePropertyId, new EntityIdValue( new ItemId( 'Q4' ) ) ) );
+ $snakSerializer =
WikibaseRepo::getDefaultInstance()->getSerializerFactory()->newSnakSerializer();
+ $classId = $this->getDefaultConfig()->get(
'WBQualityConstraintsClassId' );
+ $relationId = $this->getDefaultConfig()->get(
'WBQualityConstraintsRelationId' );
+ $constraintParameters = [
+ $classId => [
+ $snakSerializer->serialize( new
PropertyValueSnak( new PropertyId( $classId ), new EntityIdValue( new ItemId(
'Q100' ) ) ) ),
+ $snakSerializer->serialize( new
PropertyValueSnak( new PropertyId( $classId ), new EntityIdValue( new ItemId(
'Q101' ) ) ) )
+ ],
+ $relationId => [
+ $snakSerializer->serialize( new
PropertyValueSnak(
+ new PropertyId( $relationId ),
+ new EntityIdValue( new ItemId(
$this->getDefaultConfig()->get( 'WBQualityConstraintsSubclassOfRelationId' ) ) )
+ ) )
+ ]
];
$checkResult = $this->checker->checkConstraint( $statement,
$this->getConstraintMock( $constraintParameters ), $this->getEntity() );
$this->assertCompliance( $checkResult );
@@ -192,24 +234,6 @@
];
$checkResult = $this->checker->checkConstraint( $statement,
$this->getConstraintMock( $constraintParameters ), $this->getEntity() );
$this->assertViolation( $checkResult,
'wbqc-violation-message-valueType-subclass' );
- }
-
- public function testValueTypeConstraintMissingRelation() {
- $statement = new Statement( new PropertyValueSnak(
$this->valueTypePropertyId, new EntityIdValue( new ItemId( 'Q1' ) ) ) );
- $constraintParameters = [
- 'class' => 'Q100,Q101'
- ];
- $checkResult = $this->checker->checkConstraint( $statement,
$this->getConstraintMock( $constraintParameters ), $this->getEntity() );
- $this->assertViolation( $checkResult,
'wbqc-violation-message-type-relation-instance-or-subclass' );
- }
-
- public function testValueTypeConstraintMissingClass() {
- $statement = new Statement( new PropertyValueSnak(
$this->valueTypePropertyId, new EntityIdValue( new ItemId( 'Q1' ) ) ) );
- $constraintParameters = [
- 'relation' => 'instance'
- ];
- $checkResult = $this->checker->checkConstraint( $statement,
$this->getConstraintMock( $constraintParameters ), $this->getEntity() );
- $this->assertViolation( $checkResult,
'wbqc-violation-message-parameter-needed' );
}
public function testValueTypeConstraintWrongType() {
diff --git a/tests/phpunit/DelegatingConstraintCheckerTest.php
b/tests/phpunit/DelegatingConstraintCheckerTest.php
index dc373de..7a758af 100644
--- a/tests/phpunit/DelegatingConstraintCheckerTest.php
+++ b/tests/phpunit/DelegatingConstraintCheckerTest.php
@@ -76,6 +76,7 @@
$this->statementGuidParser,
$config,
$this->getConstraintParameterRenderer(),
+ $this->getConstraintParameterParser(),
$rdfVocabulary,
$itemIdParser
);
--
To view, visit https://gerrit.wikimedia.org/r/352865
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Id47731c99b6e8ae10d540315fc68994cf2f0f24e
Gerrit-PatchSet: 13
Gerrit-Project: mediawiki/extensions/WikibaseQualityConstraints
Gerrit-Branch: master
Gerrit-Owner: Lucas Werkmeister (WMDE) <[email protected]>
Gerrit-Reviewer: Jonas Kress (WMDE) <[email protected]>
Gerrit-Reviewer: Lucas Werkmeister (WMDE) <[email protected]>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: WMDE-leszek <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits