Henning Snater has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/92299


Change subject: Validating reference snaks in SetReference API module
......................................................................

Validating reference snaks in SetReference API module

(bug 55901)
Validating the snaks of a reference in SetReference like it is done for the 
claim snaks in
the SetClaim module.

Change-Id: Iedfde729e8cecd4258f4cd1948fcd0db6a5fabc8
---
M repo/includes/api/SetReference.php
M repo/tests/phpunit/includes/api/SetReferenceTest.php
2 files changed, 61 insertions(+), 14 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/99/92299/1

diff --git a/repo/includes/api/SetReference.php 
b/repo/includes/api/SetReference.php
index 77f2846..3d294fe 100644
--- a/repo/includes/api/SetReference.php
+++ b/repo/includes/api/SetReference.php
@@ -85,6 +85,10 @@
                        array_merge( $params, $decodedParams )
                );
 
+               foreach( $newReference->getSnaks() as $snak ) {
+                       $this->snakValidation->validateSnak( $snak );
+               }
+
                $changeOp = $this->getChangeOp( $newReference );
 
                try {
@@ -173,9 +177,17 @@
                        }
                } catch( InvalidArgumentException $invalidArgumentException ) {
                        // Handle Snak instantiation failures
-                       $this->dieUsage( 'Failed to get reference from 
reference Serialization ' . $invalidArgumentException->getMessage() );
+                       $this->dieUsage(
+                               'Failed to get reference from reference 
Serialization '
+                                       . 
$invalidArgumentException->getMessage(),
+                               'snak-instantiation-failure'
+                       );
                } catch( OutOfBoundsException $outOfBoundsException ) {
-                       $this->dieUsage( 'Failed to get reference from 
reference Serialization ' . $outOfBoundsException->getMessage() );
+                       $this->dieUsage(
+                               'Failed to get reference from reference 
Serialization '
+                                       . $outOfBoundsException->getMessage(),
+                               'snak-instantiation-failure'
+                       );
                }
 
                return $snaks;
diff --git a/repo/tests/phpunit/includes/api/SetReferenceTest.php 
b/repo/tests/phpunit/includes/api/SetReferenceTest.php
index 30da997..8fd7bfe 100644
--- a/repo/tests/phpunit/includes/api/SetReferenceTest.php
+++ b/repo/tests/phpunit/includes/api/SetReferenceTest.php
@@ -35,20 +35,27 @@
        public function setUp() {
                static $hasProperties = false;
                if ( !$hasProperties ) {
-                       $prop = PropertyContent::newEmpty();
-                       $prop->getEntity()->setId( 42 );
-                       $prop->getEntity()->setDataTypeId( 'string' );
-                       $prop->save( 'testing' );
-
-                       $prop = PropertyContent::newEmpty();
-                       $prop->getEntity()->setId( 43 );
-                       $prop->getEntity()->setDataTypeId( 'string' );
-                       $prop->save( 'testing' );
+                       $this->createProperty( 1 );
+                       $this->createProperty( 2 );
+                       $this->createProperty( 3 );
+                       $this->createProperty( 42 );
+                       $this->createProperty( 43 );
 
                        $hasProperties = true;
                }
 
                parent::setUp();
+       }
+
+       /**
+        * @param int $id
+        * @param string $dataTypeId
+        */
+       public function createProperty( $id, $dataTypeId = 'string' ) {
+               $prop = PropertyContent::newEmpty();
+               $prop->getEntity()->setId( $id );
+               $prop->getEntity()->setDataTypeId( $dataTypeId );
+               $prop->save( 'testing' );
        }
 
        // TODO: clean this up so more of the input space can easily be tested
@@ -121,6 +128,26 @@
                );
        }
 
+       public function testRequestWithInvalidProperty() {
+               $item = Item::newEmpty();
+               $content = new ItemContent( $item );
+               $content->save( '', null, EDIT_NEW );
+
+               // Create a statement to act upon:
+               $statement = $item->newClaim( new PropertyNoValueSnak( 42 ) );
+               $statement->setGuid(
+                       $item->getId()->getPrefixedId() . 
'$D8505CDA-25E4-4334-AG93-A3290BCD9C0P'
+               );
+
+               $item->addClaim( $statement );
+
+               $content->save( '' );
+
+               $reference = new Reference( new SnakList( array( new 
PropertySomeValueSnak( 9999 ) ) ) );
+
+               $this->makeInvalidRequest( $statement->getGuid(), null, 
$reference, 'invalid-snak-value' );
+       }
+
        public function testSettingIndex() {
                $item = Item::newEmpty();
                $content = new ItemContent( $item );
@@ -190,7 +217,12 @@
                return $serializedReference;
        }
 
-       protected function makeInvalidRequest( $statementGuid, $referenceHash, 
Reference $reference ) {
+       protected function makeInvalidRequest(
+               $statementGuid,
+               $referenceHash,
+               Reference $reference,
+               $expectedErrorCode = 'no-such-reference'
+       ) {
                $serializedReference = $this->serializeReference( $reference );
 
                $params = $this->generateRequestParams( $statementGuid, 
$referenceHash, $serializedReference );
@@ -200,7 +232,7 @@
                        $this->assertFalse( true, 'Invalid request should raise 
an exception' );
                }
                catch ( \UsageException $e ) {
-                       $this->assertEquals( 'no-such-reference', 
$e->getCodeString(), 'Invalid request raised correct error' );
+                       $this->assertEquals( $expectedErrorCode, 
$e->getCodeString(), 'Invalid request raised correct error' );
                }
        }
 
@@ -256,11 +288,14 @@
                $params = array(
                        'action' => 'wbsetreference',
                        'statement' => $statementGuid,
-                       'reference' => $referenceHash,
                        'snaks' => \FormatJson::encode( 
$serializedReference['snaks'] ),
                        'snaks-order' => \FormatJson::encode( 
$serializedReference['snaks-order'] ),
                );
 
+               if( !is_null( $referenceHash ) ) {
+                       $params['reference'] = $referenceHash;
+               }
+
                if( !is_null( $index ) ) {
                        $params['index'] = $index;
                }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iedfde729e8cecd4258f4cd1948fcd0db6a5fabc8
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Henning Snater <[email protected]>

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

Reply via email to