jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/382715 )

Change subject: Add “used as reference” constraint type
......................................................................


Add “used as reference” constraint type

Bug: T166892
Change-Id: Ica05406e144aef7e8a7c37e7f5b97e4f27f79092
---
M extension.json
M i18n/en.json
M i18n/qqq.json
A includes/ConstraintCheck/Checker/ReferenceChecker.php
M includes/ConstraintReportFactory.php
A tests/phpunit/Checker/ReferenceCheckerTest.php
M tests/phpunit/DelegatingConstraintCheckerTest.php
7 files changed, 144 insertions(+), 0 deletions(-)

Approvals:
  Jonas Kress (WMDE): Looks good to me, approved
  jenkins-bot: Verified



diff --git a/extension.json b/extension.json
index 17b6f0b..32ce089 100644
--- a/extension.json
+++ b/extension.json
@@ -283,6 +283,11 @@
                        "description": "The item ID of the 'used for values 
only constraint' item, which, when used in a 'property constraint' statement on 
a property, indicates that the property should only be used for the main value 
of a statement, not for qualifiers or references.",
                        "public": true
                },
+               "WBQualityConstraintsUsedAsReferenceConstraintId": {
+                       "value": "Q21528959",
+                       "description": "The item ID of the 'used as reference 
constraint' item, which, when used in a 'property constraint' statement on a 
property, indicates that the property should only be used for references, not 
for the main value of a statement or for qualifiers.",
+                       "public": true
+               },
                "WBQualityConstraintsClassId": {
                        "value": "P2308",
                        "description": "The property ID of the 'relation' 
property (data type: item), which specifies the class/type of a 'type' or 
'value type' constraint.",
diff --git a/i18n/en.json b/i18n/en.json
index 265731a..da9c9f8 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -125,6 +125,7 @@
        "wbqc-violation-message-target-required-claim": "$1 should have 
{{PLURAL:$3|0=a statement $2.|1=a statement $2 $5.|a statement for $2 with one 
of the following values:$4}}",
        "wbqc-violation-message-unique-value": "This property's value must not 
be present on any other item, but is also present on {{PLURAL:$1|1=$3.|2=$3 and 
$4.|the following items: $2}}",
        "wbqc-violation-message-valueOnly": "This property should only be used 
for the main value of a statement, not for qualifiers or references.",
+       "wbqc-violation-message-reference": "The property should only be used 
in references, not for the main value of a statement or for a qualifier.",
 
        "wbqc-exception-message": "This entity is a known exception for this 
constraint and has been marked as such."
 }
diff --git a/i18n/qqq.json b/i18n/qqq.json
index 3d169ce..37ec567 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -121,5 +121,6 @@
        "wbqc-violation-message-target-required-claim": "Message for a 
violation of the “Target required claim” constraint, when the target entity of 
a statement is missing an expected statement. Parameters:\n* $1 is the subject 
entity of the missing statement, i. e. the target entity of the statement that 
has the constraint.\n* $2 is the property of the missing statement.\n* $3 is 
the number of values permitted for the missing statement (or 0, in which case 
the constraint only specifies that there should be a statement but not the 
values it should have).\n* $4 is an HTML list of all values permitted for the 
missing statement.\n* $5, $6 etc. are the individual values permitted for the 
missing statement.\n{{Related|wbqc-violation-message-item}}",
        "wbqc-violation-message-unique-value": "Message for violation of the 
Unique Value constraint, when other items are found. Parameters:\n* $1 is the 
number of other items with the same value.\n* $2 is an HTML list of all other 
items found with the same value.\n* $3, $4 etc. are the individual other items 
with the same value.",
        "wbqc-violation-message-valueOnly": "Message for a violation of the 
“used for values only” constraint, when a property intended for the main value 
only was used in a qualifier or reference.",
+       "wbqc-violation-message-reference": "Message for a violation of the 
“used as reference” constraint, when a property intended for references only 
was used for the main value or in a qualifier.",
        "wbqc-exception-message": "Message for a constraint check result on an 
entity that has been marked as an exception to the constraint. This message 
only appears on [[Special:ConstraintReport]]; the gadget does not show 
exception reports."
 }
diff --git a/includes/ConstraintCheck/Checker/ReferenceChecker.php 
b/includes/ConstraintCheck/Checker/ReferenceChecker.php
new file mode 100644
index 0000000..3977c52
--- /dev/null
+++ b/includes/ConstraintCheck/Checker/ReferenceChecker.php
@@ -0,0 +1,31 @@
+<?php
+
+namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Checker;
+
+use WikibaseQuality\ConstraintReport\Constraint;
+use WikibaseQuality\ConstraintReport\ConstraintCheck\ConstraintChecker;
+use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\Context;
+use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\CheckResult;
+
+/**
+ * @package WikibaseQuality\ConstraintReport\ConstraintCheck\Checker
+ * @author Lucas Werkmeister
+ * @license GNU GPL v2+
+ */
+class ReferenceChecker implements ConstraintChecker {
+
+       public function checkConstraint( Context $context, Constraint 
$constraint ) {
+               if ( $context->getType() === Context::TYPE_REFERENCE ) {
+                       return new CheckResult( $context, $constraint, [], 
CheckResult::STATUS_COMPLIANCE, '' );
+               } else {
+                       $message = wfMessage( 
'wbqc-violation-message-reference' )->escaped();
+                       return new CheckResult( $context, $constraint, [], 
CheckResult::STATUS_VIOLATION, $message );
+               }
+       }
+
+       public function checkConstraintParameters( Constraint $constraint ) {
+               // no parameters
+               return [];
+       }
+
+}
diff --git a/includes/ConstraintReportFactory.php 
b/includes/ConstraintReportFactory.php
index 7750506..98eb7d2 100644
--- a/includes/ConstraintReportFactory.php
+++ b/includes/ConstraintReportFactory.php
@@ -13,6 +13,7 @@
 use Wikibase\Lib\SnakFormatter;
 use Wikibase\Rdf\RdfVocabulary;
 use Wikibase\Repo\WikibaseRepo;
+use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\ReferenceChecker;
 use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\MainSnakContext;
 use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\StatementContext;
 use 
WikibaseQuality\ConstraintReport\ConstraintCheck\DelegatingConstraintChecker;
@@ -331,6 +332,8 @@
                                        ),
                                $this->config->get( 
'WBQualityConstraintsUsedForValuesOnlyConstraintId' )
                                        => new ValueOnlyChecker(),
+                               $this->config->get( 
'WBQualityConstraintsUsedAsReferenceConstraintId' )
+                                       => new ReferenceChecker(),
                        ];
                }
 
diff --git a/tests/phpunit/Checker/ReferenceCheckerTest.php 
b/tests/phpunit/Checker/ReferenceCheckerTest.php
new file mode 100644
index 0000000..e4420a6
--- /dev/null
+++ b/tests/phpunit/Checker/ReferenceCheckerTest.php
@@ -0,0 +1,97 @@
+<?php
+
+namespace WikibaseQuality\ConstraintReport\Test;
+
+use Wikibase\Repo\Tests\NewItem;
+use Wikibase\Repo\Tests\NewStatement;
+use WikibaseQuality\ConstraintReport\Constraint;
+use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\ReferenceChecker;
+use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\Context;
+use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\StatementContext;
+use WikibaseQuality\ConstraintReport\Tests\ResultAssertions;
+
+/**
+ * @covers 
\WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\ReferenceChecker
+ *
+ * @group WikibaseQualityConstraints
+ *
+ * @uses \WikibaseQuality\ConstraintReport\ConstraintCheck\Result\CheckResult
+ *
+ * @author Lucas Werkmeister
+ * @license GNU GPL v2+
+ */
+class ReferenceCheckerTest extends \PHPUnit_Framework_TestCase {
+
+       use ResultAssertions;
+
+       /**
+        * @param string $type context type
+        * @param string|null $messageKey key of violation message, or null if 
compliance is expected
+        * @dataProvider contextTypes
+        */
+       public function testReferenceConstraint( $type, $messageKey ) {
+               $context = $this->getMock( Context::class );
+               $context->method( 'getType' )->willReturn( $type );
+               $checker = new ReferenceChecker();
+               $constraint = $this->getConstraintMock( [] );
+
+               $checkResult = $checker->checkConstraint( $context, $constraint 
);
+
+               if ( $messageKey === null ) {
+                       $this->assertCompliance( $checkResult );
+               } else {
+                       $this->assertViolation( $checkResult, $messageKey );
+               }
+       }
+
+       public function contextTypes() {
+               return [
+                       [ Context::TYPE_STATEMENT, 
'wbqc-violation-message-reference' ],
+                       [ Context::TYPE_QUALIFIER, 
'wbqc-violation-message-reference' ],
+                       [ Context::TYPE_REFERENCE, null ],
+               ];
+       }
+
+       public function testReferenceConstraintDeprecatedStatement() {
+               $checker = new ReferenceChecker();
+               $statement = NewStatement::noValueFor( 'P1' )
+                       ->withDeprecatedRank()
+                       ->build();
+               $constraint = $this->getConstraintMock( [] );
+               $entity = NewItem::withId( 'Q1' )
+                       ->build();
+
+               $checkResult = $checker->checkConstraint( new StatementContext( 
$entity, $statement ), $constraint );
+
+               // this constraint is still checked on deprecated statements
+               $this->assertViolation( $checkResult, 
'wbqc-violation-message-reference' );
+       }
+
+       public function testCheckConstraintParameters() {
+               $checker = new ReferenceChecker();
+               $constraint = $this->getConstraintMock( [] );
+
+               $result = $checker->checkConstraintParameters( $constraint );
+
+               $this->assertCount( 0, $result );
+       }
+
+       /**
+        * @return Constraint
+        */
+       private function getConstraintMock() {
+               $mock = $this
+                       ->getMockBuilder( Constraint::class )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+               $mock->expects( $this->any() )
+                       ->method( 'getConstraintParameters' )
+                       ->will( $this->returnValue( [] ) );
+               $mock->expects( $this->any() )
+                       ->method( 'getConstraintTypeItemId' )
+                       ->will( $this->returnValue( 'Reference' ) );
+
+               return $mock;
+       }
+
+}
diff --git a/tests/phpunit/DelegatingConstraintCheckerTest.php 
b/tests/phpunit/DelegatingConstraintCheckerTest.php
index b518e5c..6cb6b7b 100644
--- a/tests/phpunit/DelegatingConstraintCheckerTest.php
+++ b/tests/phpunit/DelegatingConstraintCheckerTest.php
@@ -315,6 +315,12 @@
                                'constraint_type_qid' => 
$this->getConstraintTypeItemId( 'UsedForValuesOnly' ),
                                'constraint_parameters' => '{}'
                        ],
+                       [
+                               'constraint_guid' => 
'P1$d7398ac7-aee4-4a29-9e8c-79944e664b67',
+                               'pid' => 1,
+                               'constraint_type_qid' => 
$this->getConstraintTypeItemId( 'UsedAsReference' ),
+                               'constraint_parameters' => '{}'
+                       ],
                ];
                $this->constraintCount = count( array_filter(
                        $constraints,

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ica05406e144aef7e8a7c37e7f5b97e4f27f79092
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/WikibaseQualityConstraints
Gerrit-Branch: master
Gerrit-Owner: Lucas Werkmeister (WMDE) <[email protected]>
Gerrit-Reviewer: Jonas Kress (WMDE) <[email protected]>
Gerrit-Reviewer: Siebrand <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to