jenkins-bot has submitted this change and it was merged.

Change subject: Added SetReference test with invalid property
......................................................................


Added SetReference test with invalid property

(bug 55901)
Validation of reference snaks has been fixed. This change sets adds a test 
regarding invalid
property usage.

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

Approvals:
  Tobias Gritschacher: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/repo/includes/api/SetReference.php 
b/repo/includes/api/SetReference.php
index ffda27f..ad1822e 100644
--- a/repo/includes/api/SetReference.php
+++ b/repo/includes/api/SetReference.php
@@ -154,13 +154,15 @@
                } catch( InvalidArgumentException $invalidArgumentException ) {
                        // Handle Snak instantiation failures
                        $this->dieUsage(
-                               'Failed to get reference from reference 
Serialization ' . $invalidArgumentException->getMessage(),
-                               'invalid-json'
+                               '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(),
-                               'invalid-json'
+                               'Failed to get reference from reference 
Serialization '
+                                       . $outOfBoundsException->getMessage(),
+                               'snak-instantiation-failure'
                        );
                }
 
diff --git a/repo/tests/phpunit/includes/api/SetReferenceTest.php 
b/repo/tests/phpunit/includes/api/SetReferenceTest.php
index 900874b..928d99e 100644
--- a/repo/tests/phpunit/includes/api/SetReferenceTest.php
+++ b/repo/tests/phpunit/includes/api/SetReferenceTest.php
@@ -42,25 +42,28 @@
        public function setUp() {
                static $hasProperties = false;
                if ( !$hasProperties ) {
-                       $prop = PropertyContent::newEmpty();
-                       $prop->getEntity()->setId( new PropertyId( 'P42' ) );
-                       $prop->getEntity()->setDataTypeId( 'string' );
-                       $prop->save( 'testing' );
-
-                       $prop = PropertyContent::newEmpty();
-                       $prop->getEntity()->setId( new PropertyId( 'P43' ) );
-                       $prop->getEntity()->setDataTypeId( 'string' );
-                       $prop->save( 'testing' );
-
-                       $prop = PropertyContent::newEmpty();
-                       $prop->getEntity()->setId( new PropertyId( 'P66' ) );
-                       $prop->getEntity()->setDataTypeId( 'string' );
-                       $prop->save( 'testing' );
+                       $this->createProperty( 1 );
+                       $this->createProperty( 2 );
+                       $this->createProperty( 3 );
+                       $this->createProperty( 42 );
+                       $this->createProperty( 43 );
+                       $this->createProperty( 66 );
 
                        $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
@@ -133,6 +136,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 );
@@ -202,7 +225,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 );
@@ -212,7 +240,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' );
                }
        }
 
@@ -268,11 +296,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: merged
Gerrit-Change-Id: Iedfde729e8cecd4258f4cd1948fcd0db6a5fabc8
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Henning Snater <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Henning Snater <[email protected]>
Gerrit-Reviewer: Tobias Gritschacher <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to