Lucas Werkmeister (WMDE) has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/358973 )

Change subject: Import Qualifiers constraints from statements
......................................................................

Import Qualifiers constraints from statements

Analogous to I2571db085f et al.

Also updates the existing tests to use the CheckResult assertion, which
for some reason was not done in 7372f424e1 (Ia1b1e15154).

Bug: T167415
Change-Id: I1d4ed2e0ae4365b6fb7e0d82bbb043a6557370bc
---
M extension.json
M includes/ConstraintCheck/Checker/QualifiersChecker.php
M includes/ConstraintReportFactory.php
M tests/phpunit/Checker/QualifierChecker/QualifiersCheckerTest.php
4 files changed, 57 insertions(+), 41 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseQualityConstraints
 refs/changes/73/358973/1

diff --git a/extension.json b/extension.json
index c67acb3..f4d92e0 100644
--- a/extension.json
+++ b/extension.json
@@ -156,6 +156,11 @@
                        "description": "The item ID of the 'mandatory 
qualifier' item, which, when used in a 'property constraint' statement on a 
property, indicates a given statement should have the given qualifier.",
                        "public": true
                },
+               "WBQualityConstraintsAllowedQualifiersConstraintId": {
+                       "value": "Q21510851",
+                       "description": "The item ID of the 'allowed qualifiers 
constraint' item, which, when used in a 'property constraint' statement on a 
property, indicates a given statement should only have the given qualifiers.",
+                       "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/QualifiersChecker.php 
b/includes/ConstraintCheck/Checker/QualifiersChecker.php
index 16bff87..391e980 100644
--- a/includes/ConstraintCheck/Checker/QualifiersChecker.php
+++ b/includes/ConstraintCheck/Checker/QualifiersChecker.php
@@ -7,7 +7,7 @@
 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\ConstraintParameterRenderer;
 use Wikibase\DataModel\Statement\Statement;
@@ -20,11 +20,9 @@
 class QualifiersChecker implements ConstraintChecker {
 
        /**
-        * Class for helper functions for constraint checkers.
-        *
-        * @var ConstraintParameterParser
+        * @var ConstraintStatementParameterParser
         */
-       private $helper;
+       private $constraintParameterParser;
 
        /**
         * @var ConstraintParameterRenderer
@@ -32,14 +30,14 @@
        private $constraintParameterRenderer;
 
        /**
-        * @param ConstraintParameterParser $helper
+        * @param ConstraintStatementParameterParser $constraintParameterParser
         * @param ConstraintParameterRenderer $constraintParameterRenderer 
should return HTML
         */
        public function __construct(
-               ConstraintParameterParser $helper,
+               ConstraintStatementParameterParser $constraintParameterParser,
                ConstraintParameterRenderer $constraintParameterRenderer
        ) {
-               $this->helper = $helper;
+               $this->constraintParameterParser = $constraintParameterParser;
                $this->constraintParameterRenderer = 
$constraintParameterRenderer;
        }
 
@@ -56,26 +54,22 @@
                $parameters = [];
                $constraintParameters = $constraint->getConstraintParameters();
 
-               $parameters['property'] = $this->helper->parseParameterArray( 
explode( ',', $constraintParameters['property'] ) );
-
-               /*
-                * error handling:
-                *  $constraintParameters['property'] can be array( '' ), 
meaning that there are explicitly no qualifiers allowed
-                */
-
-               $properties = [];
-               if ( array_key_exists( 'property', $constraintParameters ) ) {
-                       $properties = explode( ',', 
$constraintParameters['property'] );
-                       $properties = array_map( 'strtoupper', $properties ); 
// FIXME strtoupper should not be necessary, remove once constraints are 
imported from statements
-               }
+               $properties = 
$this->constraintParameterParser->parsePropertiesParameter( 
$constraintParameters, $constraint->getConstraintTypeName() );
+               $parameters['property'] = $properties;
 
                $message = '';
                $status = CheckResult::STATUS_COMPLIANCE;
 
                /** @var Snak $qualifier */
                foreach ( $statement->getQualifiers() as $qualifier ) {
-                       $pid = $qualifier->getPropertyId()->getSerialization();
-                       if ( !in_array( $pid, $properties ) ) {
+                       $allowedQualifier = false;
+                       foreach ( $properties as $property ) {
+                               if ( $qualifier->getPropertyId()->equals( 
$property ) ) {
+                                       $allowedQualifier = true;
+                                       break;
+                               }
+                       }
+                       if ( !$allowedQualifier ) {
                                if ( empty( $properties ) || $properties === [ 
'' ] ) {
                                        $message = wfMessage( 
'wbqc-violation-message-no-qualifiers' );
                                        $message->rawParams(
@@ -85,7 +79,7 @@
                                        $message = wfMessage( 
"wbqc-violation-message-qualifiers" );
                                        $message->rawParams(
                                                
$this->constraintParameterRenderer->formatEntityId( $statement->getPropertyId() 
),
-                                               
$this->constraintParameterRenderer->formatPropertyId( $pid )
+                                               
$this->constraintParameterRenderer->formatEntityId( $qualifier->getPropertyId() 
)
                                        );
                                        $message->numParams( count( $properties 
) );
                                        $message->rawParams( 
$this->constraintParameterRenderer->formatPropertyIdList( $properties ) );
diff --git a/includes/ConstraintReportFactory.php 
b/includes/ConstraintReportFactory.php
index 16e9e8b..e6dfec8 100644
--- a/includes/ConstraintReportFactory.php
+++ b/includes/ConstraintReportFactory.php
@@ -205,7 +205,7 @@
                                'Symmetric' => new SymmetricChecker( 
$this->lookup, $constraintParameterParser, $connectionCheckerHelper, 
$this->constraintParameterRenderer ),
                                'Inverse' => new InverseChecker( $this->lookup, 
$this->constraintStatementParameterParser, $connectionCheckerHelper, 
$this->constraintParameterRenderer ),
                                'Qualifier' => new QualifierChecker(),
-                               'Qualifiers' => new QualifiersChecker( 
$constraintParameterParser, $this->constraintParameterRenderer ),
+                               'Qualifiers' => new QualifiersChecker( 
$this->constraintStatementParameterParser, $this->constraintParameterRenderer ),
                                'Mandatory qualifiers' => new 
MandatoryQualifiersChecker( $this->constraintStatementParameterParser, 
$this->constraintParameterRenderer ),
                                'Range' => new RangeChecker( 
$constraintParameterParser, $rangeCheckerHelper, 
$this->constraintParameterRenderer ),
                                'Diff within range' => new 
DiffWithinRangeChecker( $constraintParameterParser, $rangeCheckerHelper, 
$this->constraintParameterRenderer ),
@@ -234,6 +234,7 @@
                                $this->config->get( 
'WBQualityConstraintsConflictsWithConstraintId' ) => 
$this->constraintCheckerMap['Conflicts with'],
                                $this->config->get( 
'WBQualityConstraintsOneOfConstraintId' ) => $this->constraintCheckerMap['One 
of'],
                                $this->config->get( 
'WBQualityConstraintsMandatoryQualifierConstraintId' ) => 
$this->constraintCheckerMap['Mandatory qualifiers'],
+                               $this->config->get( 
'WBQualityConstraintsAllowedQualifiersConstraintId' ) => 
$this->constraintCheckerMap['Qualifiers'],
                        ];
                }
 
diff --git a/tests/phpunit/Checker/QualifierChecker/QualifiersCheckerTest.php 
b/tests/phpunit/Checker/QualifierChecker/QualifiersCheckerTest.php
index cf7820a..a9f0855 100644
--- a/tests/phpunit/Checker/QualifierChecker/QualifiersCheckerTest.php
+++ b/tests/phpunit/Checker/QualifierChecker/QualifiersCheckerTest.php
@@ -2,14 +2,18 @@
 
 namespace WikibaseQuality\ConstraintReport\Test\QualifierChecker;
 
+use Wikibase\DataModel\Entity\EntityIdValue;
 use Wikibase\DataModel\Entity\Item;
 use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Entity\PropertyId;
+use Wikibase\DataModel\Snak\PropertyValueSnak;
 use Wikibase\DataModel\Statement\Statement;
 use Wikibase\DataModel\Statement\StatementListProvider;
+use Wikibase\Repo\WikibaseRepo;
 use WikibaseQuality\ConstraintReport\Constraint;
 use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\QualifiersChecker;
-use 
WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterParser;
 use WikibaseQuality\ConstraintReport\Tests\ConstraintParameters;
+use WikibaseQuality\ConstraintReport\Tests\ResultAssertions;
 use WikibaseQuality\Tests\Helper\JsonFileEntityLookup;
 
 /**
@@ -18,19 +22,14 @@
  * @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+
  */
 class QualifiersCheckerTest extends \MediaWikiTestCase {
 
-       use ConstraintParameters;
-
-       /**
-        * @var ConstraintParameterParser
-        */
-       private $helper;
+       use ConstraintParameters, ResultAssertions;
 
        /**
         * @var string
@@ -44,13 +43,11 @@
 
        protected function setUp() {
                parent::setUp();
-               $this->helper = new ConstraintParameterParser();
                $this->qualifiersList = 'P580,P582,P1365,P1366,P642,P805';
                $this->lookup = new JsonFileEntityLookup( __DIR__ );
        }
 
        protected function tearDown() {
-               unset( $this->helper );
                unset( $this->qualifiersList );
                unset( $this->lookup );
                parent::tearDown();
@@ -69,25 +66,44 @@
        public function testQualifiersConstraint() {
                /** @var Item $entity */
                $entity = $this->lookup->getEntity( new ItemId( 'Q2' ) );
-               $qualifiersChecker = new QualifiersChecker( $this->helper, 
$this->getConstraintParameterRenderer() );
+               $qualifiersChecker = new QualifiersChecker( 
$this->getConstraintParameterParser(), $this->getConstraintParameterRenderer() 
);
                $checkResult = $qualifiersChecker->checkConstraint( 
$this->getFirstStatement( $entity ), $this->getConstraintMock( [ 'property' => 
$this->qualifiersList ] ), $entity );
-               $this->assertEquals( 'compliance', $checkResult->getStatus(), 
'check should comply' );
+               $this->assertCompliance( $checkResult );
        }
 
-       public function testQualifiersConstraintToManyQualifiers() {
+       public function testQualifiersConstraintTooManyQualifiers() {
                /** @var Item $entity */
                $entity = $this->lookup->getEntity( new ItemId( 'Q3' ) );
-               $qualifiersChecker = new QualifiersChecker( $this->helper, 
$this->getConstraintParameterRenderer() );
+               $qualifiersChecker = new QualifiersChecker( 
$this->getConstraintParameterParser(), $this->getConstraintParameterRenderer() 
);
                $checkResult = $qualifiersChecker->checkConstraint( 
$this->getFirstStatement( $entity ), $this->getConstraintMock( [ 'property' => 
$this->qualifiersList ] ), $entity );
-               $this->assertEquals( 'violation', $checkResult->getStatus(), 
'check should not comply' );
+               $this->assertViolation( $checkResult, 
'wbqc-violation-message-qualifiers' );
        }
 
        public function testQualifiersConstraintNoQualifiers() {
                /** @var Item $entity */
                $entity = $this->lookup->getEntity( new ItemId( 'Q4' ) );
-               $qualifiersChecker = new QualifiersChecker( $this->helper, 
$this->getConstraintParameterRenderer() );
+               $qualifiersChecker = new QualifiersChecker( 
$this->getConstraintParameterParser(), $this->getConstraintParameterRenderer() 
);
                $checkResult = $qualifiersChecker->checkConstraint( 
$this->getFirstStatement( $entity ), $this->getConstraintMock( [ 'property' => 
$this->qualifiersList ] ), $entity );
-               $this->assertEquals( 'compliance', $checkResult->getStatus(), 
'check should comply' );
+               $this->assertCompliance( $checkResult );
+       }
+
+       public function testQualifiersConstraintWithStatement() {
+               /** @var Item $entity */
+               $entity = $this->lookup->getEntity( new ItemId( 'Q2' ) );
+               $qualifiersChecker = new QualifiersChecker( 
$this->getConstraintParameterParser(), $this->getConstraintParameterRenderer() 
);
+
+               $snakSerializer = 
WikibaseRepo::getDefaultInstance()->getSerializerFactory()->newSnakSerializer();
+               $config = $this->getDefaultConfig();
+               $propertyId = $config->get( 'WBQualityConstraintsPropertyId' );
+               $parameters = [ $propertyId => array_map(
+                       function( $id ) use ( $snakSerializer, $propertyId ) {
+                               return $snakSerializer->serialize( new 
PropertyValueSnak( new PropertyId( $propertyId ), new EntityIdValue( new 
PropertyId( $id ) ) ) );
+                       },
+                       explode( ',', $this->qualifiersList )
+               ) ];
+
+               $checkResult = $qualifiersChecker->checkConstraint( 
$this->getFirstStatement( $entity ), $this->getConstraintMock( $parameters ), 
$entity );
+               $this->assertCompliance( $checkResult );
        }
 
        /**

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1d4ed2e0ae4365b6fb7e0d82bbb043a6557370bc
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseQualityConstraints
Gerrit-Branch: master
Gerrit-Owner: Lucas Werkmeister (WMDE) <[email protected]>

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

Reply via email to