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

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


Import Format constraints from statements

FormatChecker gets a ConstraintStatementParameterParser injected, which
it uses to parse the format/pattern parameter. A config variable for the
constraint type item is added, and ConstraintReportFactory aliases it to
the “Format” constraint type.

The ConstraintParameters test trait gains a method to create a format
parameter, and all tests for FormatChecker are updated to use that
method. This way the tests will continue to work as we remove support
for template parameters.

A test for a missing format parameter is removed, since that is now
covered by the ConstraintStatementParameterParser tests and throws an
exception instead of returning a check result.

Bug: T167418
Change-Id: I1a69a7485cbb8878c633515f693a7248ba9603e0
---
M extension.json
M includes/ConstraintCheck/Checker/FormatChecker.php
M includes/ConstraintReportFactory.php
M tests/phpunit/Checker/FormatChecker/FormatCheckerTest.php
M tests/phpunit/ConstraintParameters.php
5 files changed, 53 insertions(+), 60 deletions(-)

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



diff --git a/extension.json b/extension.json
index 9e61b60..6053844 100644
--- a/extension.json
+++ b/extension.json
@@ -189,6 +189,11 @@
                        "description": "The item ID of the 'commons link 
constraint' item, which, when used in a 'property constraint' statement on a 
property, indicates that the value of a given statement should be a valid link 
to Wikimedia Commons.",
                        "public": true
                },
+               "WBQualityConstraintsFormatConstraintId": {
+                       "value": "Q21502404",
+                       "description": "The item ID of the 'format constraint' 
item, which, when used in a 'property constraint' statement on a property, 
indicates that the value of a given statement should conform to a given 
pattern.",
+                       "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/includes/ConstraintCheck/Checker/FormatChecker.php 
b/includes/ConstraintCheck/Checker/FormatChecker.php
index 7f9a83a..01eac3f 100644
--- a/includes/ConstraintCheck/Checker/FormatChecker.php
+++ b/includes/ConstraintCheck/Checker/FormatChecker.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 Wikibase\DataModel\Statement\Statement;
 
@@ -19,15 +19,15 @@
 class FormatChecker implements ConstraintChecker {
 
        /**
-        * @var ConstraintParameterParser
+        * @var ConstraintStatementParameterParser
         */
-       private $helper;
+       private $constraintParameterParser;
 
        /**
-        * @param ConstraintParameterParser $helper
+        * @param ConstraintStatementParameterParser $constraintParameterParser
         */
-       public function __construct( $helper ) {
-               $this->helper = $helper;
+       public function __construct( $constraintParameterParser ) {
+               $this->constraintParameterParser = $constraintParameterParser;
        }
 
        /**
@@ -43,13 +43,8 @@
                $parameters = [];
                $constraintParameters = $constraint->getConstraintParameters();
 
-               if ( array_key_exists( 'pattern', $constraintParameters ) ) {
-                       $pattern = $constraintParameters['pattern'];
-                       $parameters['pattern'] = 
$this->helper->parseSingleParameter( $pattern, true );
-               } else {
-                       $message = wfMessage( 
"wbqc-violation-message-parameter-needed" )->params( 
$constraint->getConstraintTypeName(), 'pattern' )->escaped();
-                       return new CheckResult( $entity->getId(), $statement, 
$constraint->getConstraintTypeQid(), $constraint->getConstraintId(),  
$parameters, CheckResult::STATUS_VIOLATION, $message );
-               }
+               $format = 
$this->constraintParameterParser->parseFormatParameter( $constraintParameters, 
$constraint->getConstraintTypeName() );
+               $parameters['pattern'] = [ $format ];
 
                $mainSnak = $statement->getMainSnak();
 
@@ -67,7 +62,6 @@
                /*
                 * error handling:
                 *   type of $dataValue for properties with 'Format' constraint 
has to be 'string' or 'monolingualtext'
-                *   parameter $pattern must not be null
                 */
                $type = $dataValue->getType();
                if ( $type !== 'string' && $type !== 'monolingualtext' ) {
diff --git a/includes/ConstraintReportFactory.php 
b/includes/ConstraintReportFactory.php
index c8a362a..c828e87 100644
--- a/includes/ConstraintReportFactory.php
+++ b/includes/ConstraintReportFactory.php
@@ -212,7 +212,7 @@
                                'Single value' => new SingleValueChecker(),
                                'Multi value' => new MultiValueChecker(),
                                'Unique value' => new UniqueValueChecker( 
$sparqlHelper ),
-                               'Format' => new FormatChecker( 
$constraintParameterParser ),
+                               'Format' => new FormatChecker( 
$this->constraintStatementParameterParser ),
                                'Commons link' => new CommonsLinkChecker( 
$this->constraintStatementParameterParser ),
                                'One of' => new OneOfChecker( 
$this->constraintStatementParameterParser, $this->constraintParameterRenderer ),
                        ];
@@ -234,6 +234,7 @@
                                $this->config->get( 
'WBQualityConstraintsRangeConstraintId' ) => 
$this->constraintCheckerMap['Range'],
                                $this->config->get( 
'WBQualityConstraintsDifferenceWithinRangeConstraintId' ) => 
$this->constraintCheckerMap['Diff within range'],
                                $this->config->get( 
'WBQualityConstraintsCommonsLinkConstraintId' ) => 
$this->constraintCheckerMap['Commons link'],
+                               $this->config->get( 
'WBQualityConstraintsFormatConstraintId' ) => 
$this->constraintCheckerMap['Format'],
                        ];
                }
 
diff --git a/tests/phpunit/Checker/FormatChecker/FormatCheckerTest.php 
b/tests/phpunit/Checker/FormatChecker/FormatCheckerTest.php
index 5a58a47..a99615b 100644
--- a/tests/phpunit/Checker/FormatChecker/FormatCheckerTest.php
+++ b/tests/phpunit/Checker/FormatChecker/FormatCheckerTest.php
@@ -13,7 +13,8 @@
 use Wikibase\DataModel\Entity\PropertyId;
 use WikibaseQuality\ConstraintReport\Constraint;
 use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\FormatChecker;
-use 
WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterParser;
+use 
WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintStatementParameterParser;
+use WikibaseQuality\ConstraintReport\Tests\ConstraintParameters;
 use WikibaseQuality\ConstraintReport\Tests\ResultAssertions;
 
 /**
@@ -22,19 +23,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 FormatCheckerTest extends \MediaWikiTestCase {
 
-       use ResultAssertions;
-
-       /**
-        * @var ConstraintParameterParser
-        */
-       private $helper;
+       use ConstraintParameters, ResultAssertions;
 
        /**
         * @var FormatChecker
@@ -43,8 +39,7 @@
 
        protected function setUp() {
                parent::setUp();
-               $this->helper = new ConstraintParameterParser();
-               $this->formatChecker = new FormatChecker( $this->helper );
+               $this->formatChecker = new FormatChecker( 
$this->getConstraintParameterParser() );
        }
 
        public function testFormatConstraintImdb() {
@@ -74,70 +69,70 @@
 
                $result = $this->formatChecker->checkConstraint(
                        $statement1,
-                       $this->getConstraintMock( [ 'pattern' => $pattern ] ),
+                       $this->getConstraintMock( $this->formatParameter( 
$pattern ) ),
                        $this->getEntity()
                );
                $this->assertTodoCompliance( $result );
 
                $result = $this->formatChecker->checkConstraint(
                        $statement2,
-                       $this->getConstraintMock( [ 'pattern' => $pattern ] ),
+                       $this->getConstraintMock( $this->formatParameter( 
$pattern ) ),
                        $this->getEntity()
                );
                $this->assertTodoCompliance( $result );
 
                $result = $this->formatChecker->checkConstraint(
                        $statement3,
-                       $this->getConstraintMock( [ 'pattern' => $pattern ] ),
+                       $this->getConstraintMock( $this->formatParameter( 
$pattern ) ),
                        $this->getEntity()
                );
                $this->assertTodoCompliance( $result );
 
                $result = $this->formatChecker->checkConstraint(
                        $statement4,
-                       $this->getConstraintMock( [ 'pattern' => $pattern ] ),
+                       $this->getConstraintMock( $this->formatParameter( 
$pattern ) ),
                        $this->getEntity()
                );
                $this->assertTodoCompliance( $result );
 
                $result = $this->formatChecker->checkConstraint(
                        $statement5,
-                       $this->getConstraintMock( [ 'pattern' => $pattern ] ),
+                       $this->getConstraintMock( $this->formatParameter( 
$pattern ) ),
                        $this->getEntity()
                );
                $this->assertTodoViolation( $result );
 
                $result = $this->formatChecker->checkConstraint(
                        $statement6,
-                       $this->getConstraintMock( [ 'pattern' => $pattern ] ),
+                       $this->getConstraintMock( $this->formatParameter( 
$pattern ) ),
                        $this->getEntity()
                );
                $this->assertTodoViolation( $result );
 
                $result = $this->formatChecker->checkConstraint(
                        $statement7,
-                       $this->getConstraintMock( [ 'pattern' => $pattern ] ),
+                       $this->getConstraintMock( $this->formatParameter( 
$pattern ) ),
                        $this->getEntity()
                );
                $this->assertTodoViolation( $result );
 
                $result = $this->formatChecker->checkConstraint(
                        $statement8,
-                       $this->getConstraintMock( [ 'pattern' => $pattern ] ),
+                       $this->getConstraintMock( $this->formatParameter( 
$pattern ) ),
                        $this->getEntity()
                );
                $this->assertTodoViolation( $result );
 
                $result = $this->formatChecker->checkConstraint(
                        $statement9,
-                       $this->getConstraintMock( [ 'pattern' => $pattern ] ),
+                       $this->getConstraintMock( $this->formatParameter( 
$pattern ) ),
                        $this->getEntity()
                );
                $this->assertTodoViolation( $result );
 
                $result = $this->formatChecker->checkConstraint(
                        $statement10,
-                       $this->getConstraintMock( [ 'pattern' => $pattern ] ),
+                       $this->getConstraintMock( $this->formatParameter( 
$pattern ) ),
                        $this->getEntity()
                );
                $this->assertTodoViolation( $result );
@@ -177,83 +172,70 @@
 
                $result = $this->formatChecker->checkConstraint(
                        $statement1,
-                       $this->getConstraintMock( [ 'pattern' => $pattern ] ),
+                       $this->getConstraintMock( $this->formatParameter( 
$pattern ) ),
                        $this->getEntity()
                );
                $this->assertTodoCompliance( $result );
 
                $result = $this->formatChecker->checkConstraint(
                        $statement2,
-                       $this->getConstraintMock( [ 'pattern' => $pattern ] ),
+                       $this->getConstraintMock( $this->formatParameter( 
$pattern ) ),
                        $this->getEntity()
                );
                $this->assertTodoCompliance( $result );
 
                $result = $this->formatChecker->checkConstraint(
                        $statement3,
-                       $this->getConstraintMock( [ 'pattern' => $pattern ] ),
+                       $this->getConstraintMock( $this->formatParameter( 
$pattern ) ),
                        $this->getEntity()
                );
                $this->assertTodoCompliance( $result );
 
                $result = $this->formatChecker->checkConstraint(
                        $statement4,
-                       $this->getConstraintMock( [ 'pattern' => $pattern ] ),
+                       $this->getConstraintMock( $this->formatParameter( 
$pattern ) ),
                        $this->getEntity()
                );
                $this->assertTodoCompliance( $result );
 
                $result = $this->formatChecker->checkConstraint(
                        $statement5,
-                       $this->getConstraintMock( [ 'pattern' => $pattern ] ),
+                       $this->getConstraintMock( $this->formatParameter( 
$pattern ) ),
                        $this->getEntity()
                );
                $this->assertTodoViolation( $result );
 
                $result = $this->formatChecker->checkConstraint(
                        $statement6,
-                       $this->getConstraintMock( [ 'pattern' => $pattern ] ),
+                       $this->getConstraintMock( $this->formatParameter( 
$pattern ) ),
                        $this->getEntity()
                );
                $this->assertTodoViolation( $result );
 
                $result = $this->formatChecker->checkConstraint(
                        $statement7,
-                       $this->getConstraintMock( [ 'pattern' => $pattern ] ),
+                       $this->getConstraintMock( $this->formatParameter( 
$pattern ) ),
                        $this->getEntity()
                );
                $this->assertTodoViolation( $result );
 
                $result = $this->formatChecker->checkConstraint(
                        $statement8,
-                       $this->getConstraintMock( [ 'pattern' => $pattern ] ),
+                       $this->getConstraintMock( $this->formatParameter( 
$pattern ) ),
                        $this->getEntity()
                );
                $this->assertTodoViolation( $result );
 
                $result = $this->formatChecker->checkConstraint(
                        $statement9,
-                       $this->getConstraintMock( [ 'pattern' => $pattern ] ),
+                       $this->getConstraintMock( $this->formatParameter( 
$pattern ) ),
                        $this->getEntity()
                );
                $this->assertTodoViolation( $result );
 
                $result = $this->formatChecker->checkConstraint(
                        $statement10,
-                       $this->getConstraintMock( [ 'pattern' => $pattern ] ),
-                       $this->getEntity()
-               );
-               $this->assertTodoViolation( $result );
-       }
-
-       public function testFormatConstraintEmptyPattern() {
-               $pattern = null;
-               $value = new StringValue( 'Populus × canescens' );
-               $statement = new Statement( new PropertyValueSnak( new 
PropertyId( 'P345' ), $value ) );
-
-               $result = $this->formatChecker->checkConstraint(
-                       $statement,
-                       $this->getConstraintMock( [ 'pattern' => $pattern ] ),
+                       $this->getConstraintMock( $this->formatParameter( 
$pattern ) ),
                        $this->getEntity()
                );
                $this->assertTodoViolation( $result );
@@ -274,7 +256,7 @@
 
                $result = $this->formatChecker->checkConstraint(
                        $statement,
-                       $this->getConstraintMock( [ 'pattern' => $pattern ] ),
+                       $this->getConstraintMock( $this->formatParameter( 
$pattern ) ),
                        $this->getEntity()
                );
                $this->assertEquals( 'violation', $result->getStatus(), 'check 
should not comply' );
@@ -286,7 +268,7 @@
 
                $result = $this->formatChecker->checkConstraint(
                        $statement,
-                       $this->getConstraintMock( [ 'pattern' => $pattern ] ),
+                       $this->getConstraintMock( $this->formatParameter( 
$pattern ) ),
                        $this->getEntity()
                );
                $this->assertEquals( 'violation', $result->getStatus(), 'check 
should not comply' );
diff --git a/tests/phpunit/ConstraintParameters.php 
b/tests/phpunit/ConstraintParameters.php
index c616939..cd8e861 100644
--- a/tests/phpunit/ConstraintParameters.php
+++ b/tests/phpunit/ConstraintParameters.php
@@ -151,4 +151,15 @@
                return [ $namespaceId => [ 
$this->getSnakSerializer()->serialize( $snak ) ] ];
        }
 
+       /**
+        * @param string $format
+        * @return array
+        */
+       public function formatParameter( $format ) {
+               $formatId = $this->getDefaultConfig()->get( 
'WBQualityConstraintsFormatAsARegularExpressionId' );
+               $value = new StringValue( $format );
+               $snak = new PropertyValueSnak( new PropertyId( $formatId ), 
$value );
+               return [ $formatId => [ $this->getSnakSerializer()->serialize( 
$snak ) ] ];
+       }
+
 }

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

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

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

Reply via email to