Aude has submitted this change and it was merged. Change subject: ReAdd validation for wbsetreference.... ......................................................................
ReAdd validation for wbsetreference.... The call to the method was accidently removed, this re adds it as well as doing general cleanup Bug: 56623 Bug: 56662 Change-Id: I8b8f8930fc47a530165fc51caaf52ccb9efa1495 (cherry picked from commit c0e0156e990f9b86607696f315d7f8241dde9b8b) --- M repo/includes/api/SetReference.php M repo/tests/phpunit/includes/api/SetReferenceTest.php 2 files changed, 81 insertions(+), 58 deletions(-) Approvals: Tobias Gritschacher: Looks good to me, approved Aude: Verified; Looks good to me, approved diff --git a/repo/includes/api/SetReference.php b/repo/includes/api/SetReference.php index 77f2846..0db58dd 100644 --- a/repo/includes/api/SetReference.php +++ b/repo/includes/api/SetReference.php @@ -3,9 +3,9 @@ namespace Wikibase\Api; use ApiBase; +use FormatJson; use InvalidArgumentException; use OutOfBoundsException; -use Wikibase\Repo\WikibaseRepo; use Wikibase\ChangeOp\ChangeOpReference; use Wikibase\ChangeOp\ChangeOpException; use Wikibase\SnakList; @@ -16,25 +16,7 @@ /** * API module for creating a reference or setting the value of an existing one. * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * http://www.gnu.org/copyleft/gpl.html - * * @since 0.3 - * - * @ingroup WikibaseRepo - * @ingroup API * * @licence GNU GPL v2+ * @author Jeroen De Dauw < [email protected] > @@ -70,19 +52,11 @@ $this->validateReferenceHash( $claim, $params['reference'] ); } - $serializerFactory = new SerializerFactory(); - $unserializer = $serializerFactory->newUnserializerForClass( 'Wikibase\Reference' ); - - $decodedParams = array( - 'snaks' => $this->getArrayFromParam( $params['snaks'] ) - ); - - if( isset( $params['snaks-order' ] ) ) { - $decodedParams['snaks-order'] = $this->getArrayFromParam( $params['snaks-order'] ); - } - - $newReference = $unserializer->newFromSerialization( - array_merge( $params, $decodedParams ) + $newReference = new Reference( + $this->getSnaks( + $this->getArrayFromParam( $params['snaks'] ), + $this->getArrayFromParam( $params['snaks-order'] ) + ) ); $changeOp = $this->getChangeOp( $newReference ); @@ -131,7 +105,7 @@ * @return array */ protected function getArrayFromParam( $arrayParam ) { - $rawArray = \FormatJson::decode( $arrayParam, true ); + $rawArray = FormatJson::decode( $arrayParam, true ); if ( !is_array( $rawArray ) || !count( $rawArray ) ) { $this->dieUsage( 'No array or invalid JSON given', 'invalid-json' ); @@ -143,8 +117,8 @@ /** * @since 0.3 * - * @param array $rawSnaks - * @param array $snakOrder List of property ids the snaks are supposed to be ordered by. + * @param array $rawSnaks array of snaks + * @param array $snakOrder array of property ids the snaks are supposed to be ordered by. * * @return SnakList */ @@ -173,9 +147,15 @@ } } 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(), + 'invalid-json' + ); } 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(), + 'invalid-json' + ); } return $snaks; @@ -201,10 +181,10 @@ /** * @since 0.3 * - * @param \Wikibase\Reference $reference + * @param Reference $reference */ protected function outputReference( Reference $reference ) { - $serializerFactory = new \Wikibase\Lib\Serializers\SerializerFactory(); + $serializerFactory = new SerializerFactory(); $serializer = $serializerFactory->newSerializerForObject( $reference ); $serializer->getOptions()->setIndexTags( $this->getResult()->getIsRawMode() ); diff --git a/repo/tests/phpunit/includes/api/SetReferenceTest.php b/repo/tests/phpunit/includes/api/SetReferenceTest.php index 30da997..1c506b2 100644 --- a/repo/tests/phpunit/includes/api/SetReferenceTest.php +++ b/repo/tests/phpunit/includes/api/SetReferenceTest.php @@ -2,16 +2,21 @@ namespace Wikibase\Test\Api; +use DataValues\StringValue; +use FormatJson; +use UsageException; use Wikibase\Item; use Wikibase\ItemContent; +use Wikibase\Lib\Serializers\SerializerFactory; use Wikibase\PropertyContent; use Wikibase\PropertyNoValueSnak; use Wikibase\PropertySomeValueSnak; +use Wikibase\PropertyValueSnak; use Wikibase\Reference; use Wikibase\SnakList; /** - * Unit tests for the Wikibase\ApiSetReference class. + * @covers Wikibase\Api\SetReference * * @since 0.3 * @@ -29,6 +34,7 @@ * @author Katie Filbert < [email protected] > * @author Daniel Kinzler * @author H. Snater < [email protected] > + * @author Adam Shorland */ class SetReferenceTest extends WikibaseApiTestCase { @@ -45,6 +51,11 @@ $prop->getEntity()->setDataTypeId( 'string' ); $prop->save( 'testing' ); + $prop = PropertyContent::newEmpty(); + $prop->getEntity()->setId( 3 ); + $prop->getEntity()->setDataTypeId( 'string' ); + $prop->save( 'testing' ); + $hasProperties = true; } @@ -55,15 +66,15 @@ // semi-blocked by cleanup of GUID handling in claims // can perhaps tseal from RemoveReferencesTest public function testRequests() { - $item = \Wikibase\Item::newEmpty(); - $content = new \Wikibase\ItemContent( $item ); + $item = Item::newEmpty(); + $content = new ItemContent( $item ); $content->save( '', null, EDIT_NEW ); - $statement = $item->newClaim( new \Wikibase\PropertyNoValueSnak( 42 ) ); + $statement = $item->newClaim( new PropertyNoValueSnak( 42 ) ); $statement->setGuid( $item->getId()->getPrefixedId() . '$D8505CDA-25E4-4334-AG93-A3290BCD9C0P' ); - $reference = new \Wikibase\Reference( new \Wikibase\SnakList( - array( new \Wikibase\PropertySomeValueSnak( 1 ) ) + $reference = new Reference( new SnakList( + array( new PropertySomeValueSnak( 1 ) ) ) ); $statement->getReferences()->addReference( $reference ); @@ -74,8 +85,8 @@ $referenceHash = $reference->getHash(); - $reference = new \Wikibase\Reference( new \Wikibase\SnakList( - array( new \Wikibase\PropertyNoValueSnak( 42 ) ) + $reference = new Reference( new SnakList( + array( new PropertyNoValueSnak( 42 ) ) ) ); $serializedReference = $this->makeValidRequest( @@ -93,10 +104,10 @@ $referenceHash = $serializedReference['hash']; - $reference = new \Wikibase\Reference( new \Wikibase\SnakList( + $reference = new Reference( new SnakList( array( - new \Wikibase\PropertyNoValueSnak( 42 ), - new \Wikibase\PropertyNoValueSnak( 43 ), + new PropertyNoValueSnak( 42 ), + new PropertyNoValueSnak( 43 ), ) ) ); @@ -199,7 +210,7 @@ $this->doApiRequestWithToken( $params ); $this->assertFalse( true, 'Invalid request should raise an exception' ); } - catch ( \UsageException $e ) { + catch ( UsageException $e ) { $this->assertEquals( 'no-such-reference', $e->getCodeString(), 'Invalid request raised correct error' ); } } @@ -214,7 +225,7 @@ if( !is_a( $reference, '\Wikibase\Reference' ) ) { return $reference; } else { - $serializerFactory = new \Wikibase\Lib\Serializers\SerializerFactory(); + $serializerFactory = new SerializerFactory(); $serializer = $serializerFactory->newSerializerForObject( $reference ); return $serializer->getSerialized( $reference ); } @@ -231,7 +242,7 @@ return $reference; } else { unset( $reference['hash'] ); - $serializerFactory = new \Wikibase\Lib\Serializers\SerializerFactory(); + $serializerFactory = new SerializerFactory(); $unserializer = $serializerFactory->newUnserializerForClass( '\Wikibase\Reference' ); return $unserializer->newFromSerialization( $reference ); } @@ -257,8 +268,8 @@ 'action' => 'wbsetreference', 'statement' => $statementGuid, 'reference' => $referenceHash, - 'snaks' => \FormatJson::encode( $serializedReference['snaks'] ), - 'snaks-order' => \FormatJson::encode( $serializedReference['snaks-order'] ), + 'snaks' => FormatJson::encode( $serializedReference['snaks'] ), + 'snaks-order' => FormatJson::encode( $serializedReference['snaks-order'] ), ); if( !is_null( $index ) ) { @@ -282,16 +293,16 @@ try { $this->doApiRequestWithToken( $params ); $this->fail( "Exception with code $expectedError expected" ); - } catch ( \UsageException $e ) { + } catch ( UsageException $e ) { $this->assertEquals( $expectedError, $e->getCodeString(), 'Error code' ); } } public function invalidClaimProvider() { - $snak = new \Wikibase\PropertyValueSnak( 42, new \DataValues\StringValue( 'abc') ); + $snak = new PropertyValueSnak( 42, new StringValue( 'abc') ); $snakHash = $snak->getHash(); - $reference = new \Wikibase\PropertyValueSnak( 42, new \DataValues\StringValue( 'def' ) ); + $reference = new PropertyValueSnak( 42, new StringValue( 'def' ) ); $refHash = $reference->getHash(); return array( @@ -300,4 +311,36 @@ ); } + /** + * Currently tests bad calender model + * @todo test more bad serializations... + */ + public function testInvalidSerialization() { + $this->setExpectedException( 'UsageException' ); + $params = array( + 'action' => 'wbsetreference', + 'statement' => 'Foo$Guid', + 'snaks' => '{ + "P813":[ + { + "snaktype":"value", + "property":"P813", + "datavalue":{ + "value":{ + "time":"+00000002013-10-05T00:00:00Z", + "timezone":0, + "before":0, + "after":0, + "precision":11, + "calendarmodel":"FOOBAR :D" + }, + "type":"time" + } + } + ] +}', + ); + $this->doApiRequestWithToken( $params ); + } + } -- To view, visit https://gerrit.wikimedia.org/r/94141 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I8b8f8930fc47a530165fc51caaf52ccb9efa1495 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: mw1.23-wmf3 Gerrit-Owner: Addshore <[email protected]> Gerrit-Reviewer: Aude <[email protected]> Gerrit-Reviewer: Tobias Gritschacher <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
