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

Change subject: Add QualifierContext implementation and tests
......................................................................

Add QualifierContext implementation and tests

QualifierContext is a context for a qualifier of a statement, deriving
from the abstract APIv2 context (just like MainSnakContext). It
shouldn’t be used yet, because I think some constraint checkers should
be updated to use more information from qualifier contexts (for
instance, I suspect the correct interpretation for “conflicts with” in a
qualifier is to look at the other snaks of the qualifier, not the other
statements of the entity), but that’s not a problem with the context
implementation itself, so I think that can be merged without problems.

Bug: T168532
Change-Id: Ibeaa9a2b0dd2c0a6269aae2922227d6ab0bf9c78
---
A includes/ConstraintCheck/Context/QualifierContext.php
A tests/phpunit/Context/QualifierContextTest.php
2 files changed, 239 insertions(+), 0 deletions(-)


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

diff --git a/includes/ConstraintCheck/Context/QualifierContext.php 
b/includes/ConstraintCheck/Context/QualifierContext.php
new file mode 100644
index 0000000..e4b92f5
--- /dev/null
+++ b/includes/ConstraintCheck/Context/QualifierContext.php
@@ -0,0 +1,65 @@
+<?php
+
+namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Context;
+
+use Wikibase\DataModel\Entity\EntityDocument;
+use Wikibase\DataModel\Snak\Snak;
+use Wikibase\DataModel\Statement\Statement;
+
+/**
+ * A constraint check context for a qualifier of a statement.
+ */
+class QualifierContext extends ApiV2Context {
+
+       /**
+        * @type Statement
+        */
+       private $statement;
+
+       public function __construct(
+               EntityDocument $entity,
+               Statement $statement,
+               Snak $snak
+       ) {
+               parent::__construct( $entity, $snak );
+               $this->statement = $statement;
+       }
+
+       public function getType() {
+               return self::TYPE_QUALIFIER;
+       }
+
+       protected function &getMainArray( array &$container ) {
+               $statementArray = &$this->getStatementArray(
+                       $container,
+                       $this->entity->getId()->getSerialization(),
+                       $this->statement->getPropertyId()->getSerialization(),
+                       $this->statement->getGuid()
+               );
+
+               if ( !array_key_exists( 'qualifiers', $statementArray ) ) {
+                       $statementArray['qualifiers'] = [];
+               }
+               $qualifiersArray = &$statementArray['qualifiers'];
+
+               $propertyId = 
$this->getSnak()->getPropertyId()->getSerialization();
+               if ( !array_key_exists( $propertyId, $qualifiersArray ) ) {
+                       $qualifiersArray[$propertyId] = [];
+               }
+               $propertyArray = &$qualifiersArray[$propertyId];
+
+               foreach ( $propertyArray as &$potentialQualifierArray ) {
+                       if ( $potentialQualifierArray['hash'] === 
$this->getSnak()->getHash() ) {
+                               $qualifierArray = &$potentialQualifierArray;
+                               break;
+                       }
+               }
+               if ( !isset( $qualifierArray ) ) {
+                       $qualifierArray = [ 'hash' => 
$this->getSnak()->getHash() ];
+                       $propertyArray[] = &$qualifierArray;
+               }
+
+               return $qualifierArray;
+       }
+
+}
diff --git a/tests/phpunit/Context/QualifierContextTest.php 
b/tests/phpunit/Context/QualifierContextTest.php
new file mode 100644
index 0000000..0b49baa
--- /dev/null
+++ b/tests/phpunit/Context/QualifierContextTest.php
@@ -0,0 +1,174 @@
+<?php
+
+namespace WikibaseQuality\ConstraintReport\Test\Context;
+
+use Wikibase\DataModel\Statement\Statement;
+use Wikibase\Repo\Tests\NewItem;
+use Wikibase\Repo\Tests\NewStatement;
+use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\Context;
+use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\QualifierContext;
+
+/**
+ * @covers 
\WikibaseQuality\ConstraintReport\ConstraintCheck\Context\AbstractContext
+ * @covers 
\WikibaseQuality\ConstraintReport\ConstraintCheck\Context\ApiV2Context
+ * @covers 
\WikibaseQuality\ConstraintReport\ConstraintCheck\Context\QualifierContext
+ * @uses \Wikibase\DataModel\Statement\Statement
+ * @uses \Wikibase\Repo\Tests\NewItem
+ * @uses \Wikibase\Repo\Tests\NewStatement
+ *
+ * @group WikibaseQualityConstraints
+ *
+ * @author Lucas Werkmeister
+ * @license GNU GPL v2+
+ */
+class QualifierContextTest extends \PHPUnit_Framework_TestCase {
+
+       public function testGetSnak() {
+               $entity = NewItem::withId( 'Q1' )->build();
+               $statement = NewStatement::noValueFor( 'P1' )->build();
+               $snak = NewStatement::someValueFor( 'P2' 
)->build()->getMainSnak();
+               $context = new QualifierContext( $entity, $statement, $snak );
+
+               $this->assertSame( $snak, $context->getSnak() );
+       }
+
+       public function testGetEntity() {
+               $entity = NewItem::withId( 'Q1' )->build();
+               $statement = NewStatement::noValueFor( 'P1' )->build();
+               $snak = NewStatement::someValueFor( 'P2' 
)->build()->getMainSnak();
+               $context = new QualifierContext( $entity, $statement, $snak );
+
+               $this->assertSame( $entity, $context->getEntity() );
+       }
+
+       public function testGetType() {
+               $entity = NewItem::withId( 'Q1' )->build();
+               $statement = NewStatement::noValueFor( 'P1' )->build();
+               $snak = NewStatement::someValueFor( 'P2' 
)->build()->getMainSnak();
+               $context = new QualifierContext( $entity, $statement, $snak );
+
+               $this->assertSame( Context::TYPE_QUALIFIER, $context->getType() 
);
+       }
+
+       public function testGetSnakRank() {
+               $entity = NewItem::withId( 'Q1' )->build();
+               $rank = Statement::RANK_DEPRECATED;
+               $statement = NewStatement::noValueFor( 'P1' )
+                       ->withRank( $rank )
+                       ->build();
+               $snak = NewStatement::someValueFor( 'P2' 
)->build()->getMainSnak();
+               $context = new QualifierContext( $entity, $statement, $snak );
+
+               $this->assertSame( null, $context->getSnakRank() );
+       }
+
+       public function testGetSnakStatement() {
+               $entity = NewItem::withId( 'Q1' )->build();
+               $statement = NewStatement::noValueFor( 'P1' )->build();
+               $snak = NewStatement::someValueFor( 'P2' 
)->build()->getMainSnak();
+               $context = new QualifierContext( $entity, $statement, $snak );
+
+               $this->assertSame( null, $context->getSnakStatement() );
+       }
+
+       public function testStoreCheckResultInArray() {
+               $entity = NewItem::withId( 'Q1' )->build();
+               $statement1 = NewStatement::noValueFor( 'P1' )
+                       ->withGuid( 'P1$13ea0742-0190-4d88-b7b0-baee67573818' )
+                       ->build();
+               $statement2 = NewStatement::noValueFor( 'P1' )
+                       ->withGuid( 'P1$9fbfae7f-6f21-4967-8e2c-ec04ca16873d' )
+                       ->build();
+               $statement3 = NewStatement::noValueFor( 'P2' )
+                       ->withGuid( 'P2$4638ca58-5128-4a1f-88a9-b379fe9f8ad9' )
+                       ->build();
+               $snak1 = NewStatement::noValueFor( 'P11' 
)->build()->getMainSnak();
+               $snak2 = NewStatement::someValueFor( 'P11' 
)->build()->getMainSnak();
+               $snak3 = NewStatement::noValueFor( 'P12' 
)->build()->getMainSnak();
+               $context1 = new QualifierContext( $entity, $statement1, $snak1 
);
+               $context2 = new QualifierContext( $entity, $statement1, $snak2 
);
+               $context3 = new QualifierContext( $entity, $statement1, $snak3 
);
+               $context4 = new QualifierContext( $entity, $statement2, $snak3 
);
+               $context5 = new QualifierContext( $entity, $statement3, $snak3 
);
+               $result1 = [ 'result1' ];
+               $result2 = [ 'status' => 'some status', 'result' => 'second 
result' ];
+               $result3 = [ 3 ];
+               $result4 = [ [ 'the fourth result' ] ];
+               $result5 = [ [ [ 5.0 ] ] ];
+
+               $actual = [];
+               $context1->storeCheckResultInArray( $result1, $actual );
+               $context2->storeCheckResultInArray( $result2, $actual );
+               $context3->storeCheckResultInArray( $result3, $actual );
+               $context3->storeCheckResultInArray( $result4, $actual );
+               $context4->storeCheckResultInArray( $result4, $actual );
+               $context5->storeCheckResultInArray( $result5, $actual );
+
+               $expected = [
+                       'Q1' => [
+                               'claims' => [
+                                       'P1' => [
+                                               [
+                                                       'id' => 
'P1$13ea0742-0190-4d88-b7b0-baee67573818',
+                                                       'qualifiers' => [
+                                                               'P11' => [
+                                                                       [
+                                                                               
'hash' => $snak1->getHash(),
+                                                                               
'results' => [
+                                                                               
        $result1,
+                                                                               
]
+                                                                       ],
+                                                                       [
+                                                                               
'hash' => $snak2->getHash(),
+                                                                               
'results' => [
+                                                                               
        $result2,
+                                                                               
]
+                                                                       ]
+                                                               ],
+                                                               'P12' => [
+                                                                       [
+                                                                               
'hash' => $snak3->getHash(),
+                                                                               
'results' => [
+                                                                               
        $result3,
+                                                                               
        $result4,
+                                                                               
]
+                                                                       ]
+                                                               ]
+                                                       ],
+                                               ],
+                                               [
+                                                       'id' => 
'P1$9fbfae7f-6f21-4967-8e2c-ec04ca16873d',
+                                                       'qualifiers' => [
+                                                               'P12' => [
+                                                                       [
+                                                                               
'hash' => $snak3->getHash(),
+                                                                               
'results' => [
+                                                                               
        $result4,
+                                                                               
]
+                                                                       ]
+                                                               ]
+                                                       ],
+                                               ],
+                                       ],
+                                       'P2' => [
+                                               [
+                                                       'id' => 
'P2$4638ca58-5128-4a1f-88a9-b379fe9f8ad9',
+                                                       'qualifiers' => [
+                                                               'P12' => [
+                                                                       [
+                                                                               
'hash' => $snak3->getHash(),
+                                                                               
'results' => [
+                                                                               
        $result5,
+                                                                               
]
+                                                                       ]
+                                                               ]
+                                                       ],
+                                               ],
+                                       ],
+                               ],
+                       ],
+               ];
+               $this->assertSame( $expected, $actual );
+       }
+
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ibeaa9a2b0dd2c0a6269aae2922227d6ab0bf9c78
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