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

Change subject: Add Context::getSnakGroup method
......................................................................


Add Context::getSnakGroup method

The “snak group” concept could be used by several checkers, but for now,
we will start with the “single value” and “multiple values” checkers.
This is also why we copy the ValueCountCheckerHelper semantics of
ignoring deprecated statements in MainSnakContext (though we might make
this configurable with a method parameter later).

Bug: T185209
Change-Id: I6b926bfe4791b2351c16143b478e06980f03b8f7
---
M src/ConstraintCheck/Context/Context.php
M src/ConstraintCheck/Context/MainSnakContext.php
M src/ConstraintCheck/Context/QualifierContext.php
M src/ConstraintCheck/Context/ReferenceContext.php
M tests/phpunit/Context/MainSnakContextTest.php
M tests/phpunit/Context/QualifierContextTest.php
M tests/phpunit/Context/ReferenceContextTest.php
M tests/phpunit/Fake/FakeSnakContext.php
8 files changed, 103 insertions(+), 0 deletions(-)

Approvals:
  Lucas Werkmeister (WMDE): Looks good to me, approved
  jenkins-bot: Verified



diff --git a/src/ConstraintCheck/Context/Context.php 
b/src/ConstraintCheck/Context/Context.php
index 07d590b..b161b73 100644
--- a/src/ConstraintCheck/Context/Context.php
+++ b/src/ConstraintCheck/Context/Context.php
@@ -75,6 +75,21 @@
        public function getSnakStatement();
 
        /**
+        * The group of snaks that the snak being checked resides in.
+        * For a statement context, this is the main snaks of non-deprecated 
statements;
+        * for a qualifier context, the qualifiers of the same statement;
+        * and for a reference context, the snaks of the same reference.
+        *
+        * The snak being checked ({@link getSnak}) is always included,
+        * possibly more than once in the case of a statement context,
+        * since an entity can have several statements with the same main snak.
+        *
+        * @return Snak[] not a SnakList because for a statement context,
+        * the returned value might contain the same snak several times.
+        */
+       public function getSnakGroup();
+
+       /**
         * Store the check result serialization $result
         * at the appropriate location for this context in $container.
         *
diff --git a/src/ConstraintCheck/Context/MainSnakContext.php 
b/src/ConstraintCheck/Context/MainSnakContext.php
index ad5b9aa..dbb2f70 100644
--- a/src/ConstraintCheck/Context/MainSnakContext.php
+++ b/src/ConstraintCheck/Context/MainSnakContext.php
@@ -4,6 +4,9 @@
 
 use Wikibase\DataModel\Entity\EntityDocument;
 use Wikibase\DataModel\Statement\Statement;
+use Wikibase\DataModel\Statement\StatementList;
+use Wikibase\DataModel\Statement\StatementListProvider;
+use Wikimedia\Assert\Assert;
 
 /**
  * A constraint check context for the main snak of a statement.
@@ -18,6 +21,7 @@
        private $statement;
 
        public function __construct( EntityDocument $entity, Statement 
$statement ) {
+               Assert::parameterType( StatementListProvider::class, $entity, 
'$entity' );
                parent::__construct( $entity, $statement->getMainSnak() );
 
                $this->statement = $statement;
@@ -35,6 +39,14 @@
                return $this->statement;
        }
 
+       public function getSnakGroup() {
+               /** @var StatementList $statements */
+               $statements = $this->entity->getStatements();
+               return $statements
+                       ->getByRank( [ Statement::RANK_NORMAL, 
Statement::RANK_PREFERRED ] )
+                       ->getMainSnaks();
+       }
+
        protected function &getMainArray( array &$container ) {
                $statementArray = &$this->getStatementArray(
                        $container,
diff --git a/src/ConstraintCheck/Context/QualifierContext.php 
b/src/ConstraintCheck/Context/QualifierContext.php
index 3f8fc0b..b7c8c00 100644
--- a/src/ConstraintCheck/Context/QualifierContext.php
+++ b/src/ConstraintCheck/Context/QualifierContext.php
@@ -31,6 +31,11 @@
                return self::TYPE_QUALIFIER;
        }
 
+       public function getSnakGroup() {
+               $snaks = $this->statement->getQualifiers();
+               return array_values( $snaks->getArrayCopy() );
+       }
+
        protected function &getMainArray( array &$container ) {
                $statementArray = &$this->getStatementArray(
                        $container,
diff --git a/src/ConstraintCheck/Context/ReferenceContext.php 
b/src/ConstraintCheck/Context/ReferenceContext.php
index d0ba238..5854034 100644
--- a/src/ConstraintCheck/Context/ReferenceContext.php
+++ b/src/ConstraintCheck/Context/ReferenceContext.php
@@ -39,6 +39,11 @@
                return self::TYPE_REFERENCE;
        }
 
+       public function getSnakGroup() {
+               $snaks = $this->reference->getSnaks();
+               return array_values( $snaks->getArrayCopy() );
+       }
+
        protected function &getMainArray( array &$container ) {
                $statementArray = &$this->getStatementArray(
                        $container,
diff --git a/tests/phpunit/Context/MainSnakContextTest.php 
b/tests/phpunit/Context/MainSnakContextTest.php
index dc77a12..09ecc43 100644
--- a/tests/phpunit/Context/MainSnakContextTest.php
+++ b/tests/phpunit/Context/MainSnakContextTest.php
@@ -63,6 +63,24 @@
                $this->assertSame( $statement, $context->getSnakStatement() );
        }
 
+       public function testGetSnakGroup() {
+               $statement1 = NewStatement::noValueFor( 'P1' )->build();
+               $statement2 = NewStatement::noValueFor( 'P1' )->build();
+               $statement3 = NewStatement::noValueFor( 'P2' )
+                       ->withDeprecatedRank()
+                       ->build();
+               $entity = NewItem::withId( 'Q1' )
+                       ->andStatement( $statement1 )
+                       ->andStatement( $statement2 )
+                       ->andStatement( $statement3 )
+                       ->build();
+               $context = new MainSnakContext( $entity, $statement1 );
+
+               $snakGroup = $context->getSnakGroup();
+
+               $this->assertSame( [ $statement1->getMainSnak(), 
$statement2->getMainSnak() ], $snakGroup );
+       }
+
        public function testStoreCheckResultInArray() {
                $entity = NewItem::withId( 'Q1' )->build();
                $statement1 = NewStatement::noValueFor( 'P1' )
diff --git a/tests/phpunit/Context/QualifierContextTest.php 
b/tests/phpunit/Context/QualifierContextTest.php
index 45a321a..3affb26 100644
--- a/tests/phpunit/Context/QualifierContextTest.php
+++ b/tests/phpunit/Context/QualifierContextTest.php
@@ -2,6 +2,9 @@
 
 namespace WikibaseQuality\ConstraintReport\Test\Context;
 
+use Wikibase\DataModel\Entity\PropertyId;
+use Wikibase\DataModel\Snak\PropertyNoValueSnak;
+use Wikibase\DataModel\Snak\SnakList;
 use Wikibase\DataModel\Statement\Statement;
 use Wikibase\Repo\Tests\NewItem;
 use Wikibase\Repo\Tests\NewStatement;
@@ -68,6 +71,24 @@
                $this->assertSame( null, $context->getSnakStatement() );
        }
 
+       public function testGetSnakGroup() {
+               $qualifier1 = NewStatement::noValueFor( 'P10' 
)->build()->getMainSnak();
+               $qualifier2 = NewStatement::someValueFor( 'P20' 
)->build()->getMainSnak();
+               $statement = new Statement(
+                       new PropertyNoValueSnak( new PropertyId( 'P1' ) ),
+                       new SnakList( [ $qualifier1, $qualifier2 ] )
+               );
+               $entity = NewItem::withId( 'Q1' )
+                       ->andStatement( $statement )
+                       ->andStatement( NewStatement::someValueFor( 'P2' ) )
+                       ->build();
+               $context = new QualifierContext( $entity, $statement, 
$qualifier1 );
+
+               $snakGroup = $context->getSnakGroup();
+
+               $this->assertSame( [ $qualifier1, $qualifier2 ], $snakGroup );
+       }
+
        public function testStoreCheckResultInArray() {
                $entity = NewItem::withId( 'Q1' )->build();
                $statement1 = NewStatement::noValueFor( 'P1' )
diff --git a/tests/phpunit/Context/ReferenceContextTest.php 
b/tests/phpunit/Context/ReferenceContextTest.php
index 7cb5c63..5d9ec29 100644
--- a/tests/phpunit/Context/ReferenceContextTest.php
+++ b/tests/phpunit/Context/ReferenceContextTest.php
@@ -2,7 +2,10 @@
 
 namespace WikibaseQuality\ConstraintReport\Test\Context;
 
+use Wikibase\DataModel\Entity\PropertyId;
 use Wikibase\DataModel\Reference;
+use Wikibase\DataModel\ReferenceList;
+use Wikibase\DataModel\Snak\PropertyNoValueSnak;
 use Wikibase\DataModel\Statement\Statement;
 use Wikibase\Repo\Tests\NewItem;
 use Wikibase\Repo\Tests\NewStatement;
@@ -79,6 +82,26 @@
                $this->assertSame( null, $context->getSnakStatement() );
        }
 
+       public function testGetSnakGroup() {
+               $referenceSnak1 = NewStatement::noValueFor( 'P100' 
)->build()->getMainSnak();
+               $referenceSnak2 = NewStatement::someValueFor( 'P200' 
)->build()->getMainSnak();
+               $reference1 = new Reference( [ $referenceSnak1, $referenceSnak2 
] );
+               $reference2 = new Reference( [ new PropertyNoValueSnak( new 
PropertyId( 'P300' ) ) ] );
+               $statement = new Statement(
+                       new PropertyNoValueSnak( new PropertyId( 'P1' ) ),
+                       null,
+                       new ReferenceList( [ $reference1, $reference2 ] )
+               );
+               $entity = NewItem::withId( 'Q1' )
+                       ->andStatement( $statement )
+                       ->build();
+               $context = new ReferenceContext( $entity, $statement, 
$reference1, $referenceSnak1 );
+
+               $snakGroup = $context->getSnakGroup();
+
+               $this->assertSame( [ $referenceSnak1, $referenceSnak2 ], 
$snakGroup );
+       }
+
        public function testStoreCheckResultInArray() {
                $entity = NewItem::withId( 'Q1' )->build();
                $statement1 = NewStatement::noValueFor( 'P1' )
diff --git a/tests/phpunit/Fake/FakeSnakContext.php 
b/tests/phpunit/Fake/FakeSnakContext.php
index 175c612..29febce 100644
--- a/tests/phpunit/Fake/FakeSnakContext.php
+++ b/tests/phpunit/Fake/FakeSnakContext.php
@@ -35,6 +35,10 @@
                return 'statement';
        }
 
+       public function getSnakGroup() {
+               return [ $this->snak ];
+       }
+
        /**
         * @param array|null $result
         * @param array[] &$container

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I6b926bfe4791b2351c16143b478e06980f03b8f7
Gerrit-PatchSet: 8
Gerrit-Project: mediawiki/extensions/WikibaseQualityConstraints
Gerrit-Branch: master
Gerrit-Owner: Lucas Werkmeister (WMDE) <[email protected]>
Gerrit-Reviewer: Lucas Werkmeister (WMDE) <[email protected]>
Gerrit-Reviewer: Thiemo Kreuz (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