jenkins-bot has submitted this change and it was merged. Change subject: Fix and test JSON API output ......................................................................
Fix and test JSON API output In ResultBuilder::addTermList() and ResultBuilder::addRemovedTerm() set array type to 'kvp' with $kvpKeyName = 'language', and ApiResult::META_KVP_MERGE = true, like LabelsChanger and DescriptionsChanger expect it to be. Abstract some methods out of ApiXmlFormatTest into ApiFormatTestCase, extended by ApiJsonFormatTest. Change-Id: If8830bb76a685ef45a94c869e94e051db88866e7 --- M repo/includes/api/ResultBuilder.php A repo/tests/phpunit/data/api/setlabel-removed.json A repo/tests/phpunit/data/api/setlabel.json A repo/tests/phpunit/includes/api/ApiFormatTestCase.php A repo/tests/phpunit/includes/api/ApiJsonFormatTest.php M repo/tests/phpunit/includes/api/ApiXmlFormatTest.php 6 files changed, 256 insertions(+), 88 deletions(-) Approvals: Addshore: Looks good to me, approved jenkins-bot: Verified diff --git a/repo/includes/api/ResultBuilder.php b/repo/includes/api/ResultBuilder.php index c7ce9fe..707aa8b 100644 --- a/repo/includes/api/ResultBuilder.php +++ b/repo/includes/api/ResultBuilder.php @@ -646,6 +646,10 @@ private function addTermList( TermList $termList, $name, $tag, $path ) { $serializer = $this->serializerFactory->newTermListSerializer(); $value = $serializer->serialize( $termList ); + if ( $this->addMetaData ) { + ApiResult::setArrayType( $value, 'kvp', 'language' ); + $value[ApiResult::META_KVP_MERGE] = true; + } $this->setList( $path, $name, $value, $tag ); } @@ -662,6 +666,10 @@ 'removed' => '', ) ); + if ( $this->addMetaData ) { + ApiResult::setArrayType( $value, 'kvp', 'language' ); + $value[ApiResult::META_KVP_MERGE] = true; + } $this->setList( $path, $name, $value, $tag ); } diff --git a/repo/tests/phpunit/data/api/setlabel-removed.json b/repo/tests/phpunit/data/api/setlabel-removed.json new file mode 100644 index 0000000..f0f873d --- /dev/null +++ b/repo/tests/phpunit/data/api/setlabel-removed.json @@ -0,0 +1,13 @@ +{ + "entity": { + "labels": { + "en-gb": { + "language": "en-gb", + "removed": "" + } + }, + "id": "$itemIdUnderTest", + "type": "item" + }, + "success": 1 +} diff --git a/repo/tests/phpunit/data/api/setlabel.json b/repo/tests/phpunit/data/api/setlabel.json new file mode 100644 index 0000000..5f9b4fe --- /dev/null +++ b/repo/tests/phpunit/data/api/setlabel.json @@ -0,0 +1,13 @@ +{ + "entity": { + "labels": { + "en-gb": { + "language": "en-gb", + "value": "enGbLabel" + } + }, + "id": "$itemIdUnderTest", + "type": "item" + }, + "success": 1 +} diff --git a/repo/tests/phpunit/includes/api/ApiFormatTestCase.php b/repo/tests/phpunit/includes/api/ApiFormatTestCase.php new file mode 100644 index 0000000..418121f --- /dev/null +++ b/repo/tests/phpunit/includes/api/ApiFormatTestCase.php @@ -0,0 +1,115 @@ +<?php + +namespace Wikibase\Test\Repo\Api; + +use ApiBase; +use ApiMain; +use FauxRequest; +use Wikibase\DataModel\Entity\Item; +use Wikibase\DataModel\Entity\Property; +use Wikibase\DataModel\Entity\PropertyId; +use Wikibase\DataModel\Snak\PropertyNoValueSnak; +use Wikibase\EntityRevision; +use Wikibase\Repo\WikibaseRepo; + +/** + * @group API + * @group Wikibase + * @group WikibaseAPI + * @group WikibaseRepo + * @group Database + * @group medium + * + * @licence GNU GPL v2+ + * @author Katie Filbert < aude.w...@gmail.com > + * @author Adam Shorland + */ +abstract class ApiFormatTestCase extends \MediaWikiTestCase { + + /** + * @var PropertyId|null + */ + protected $lastPropertyId; + + /** + * @var PropertyId|null + */ + protected $lastItemId; + + /** + * @param string $moduleClass + * @param string $moduleName + * @param array $params + * @param bool $needsToken + * + * @return ApiMain + */ + protected function getApiModule( $moduleClass, $moduleName, array $params, $needsToken = false ) { + global $wgUser; + + if ( $needsToken ) { + $params['token'] = $wgUser->getEditToken(); + } + $request = new FauxRequest( $params, true ); + $main = new ApiMain( $request ); + + return new $moduleClass( $main, $moduleName ); + } + + protected function getNewEntityRevision( $withData = false ) { + $entityRevision = $this->storeNewItem(); + + if ( $withData ) { + $this->storeNewProperty(); + $entityRevision = $this->storePresetDataInStatement( $entityRevision, $this->lastPropertyId ); + } + + return $entityRevision; + } + + protected function storeNewProperty() { + global $wgUser; + + $store = WikibaseRepo::getDefaultInstance()->getEntityStore(); + + $property = Property::newFromType( 'string' ); + $entityRevision = $store->saveEntity( $property, 'testing', $wgUser, EDIT_NEW ); + $this->lastPropertyId = $entityRevision->getEntity()->getId(); + } + + protected function storeNewItem() { + global $wgUser; + + $store = WikibaseRepo::getDefaultInstance()->getEntityStore(); + + $item = new Item(); + $entityRevision = $store->saveEntity( $item, 'testing', $wgUser, EDIT_NEW ); + $this->lastItemId = $entityRevision->getEntity()->getId(); + + return $entityRevision; + } + + private function storePresetDataInStatement( EntityRevision $entityRevision, PropertyId $propertyId ) { + global $wgUser; + + $store = WikibaseRepo::getDefaultInstance()->getEntityStore(); + + /** @var Item $item */ + $item = $entityRevision->getEntity(); + $snak = new PropertyNoValueSnak( $propertyId ); + $guid = $item->getId()->getSerialization() . '$1111AAAA-43cb-ed6d-3adb-760e85bd17ee'; + $item->getStatements()->addNewStatement( $snak, null, null, $guid ); + + $item->setLabel( 'en', 'en-label' ); + $item->setLabel( 'de', 'de-label' ); + $item->setDescription( 'de', 'de-desc' ); + $item->setDescription( 'es', 'es-desc' ); + $item->setAliases( 'pt', array( 'AA', 'BB' ) ); + $item->setAliases( 'en', array( 'AA-en', 'BB-en' ) ); + + $entityRevision = $store->saveEntity( $item, 'testing more!', $wgUser ); + + return $entityRevision; + } + +} diff --git a/repo/tests/phpunit/includes/api/ApiJsonFormatTest.php b/repo/tests/phpunit/includes/api/ApiJsonFormatTest.php new file mode 100644 index 0000000..50651a9 --- /dev/null +++ b/repo/tests/phpunit/includes/api/ApiJsonFormatTest.php @@ -0,0 +1,106 @@ +<?php + +namespace Wikibase\Test\Repo\Api; + +use ApiBase; + +/** + * @group API + * @group Wikibase + * @group WikibaseAPI + * @group WikibaseRepo + * @group Database + * @group medium + * + * @licence GNU GPL v2+ + * @author Katie Filbert < aude.w...@gmail.com > + * @author Adam Shorland + */ +class ApiJsonFormatTest extends ApiFormatTestCase { + + private function getExpectedJson( $moduleIdentifier ) { + $json = file_get_contents( __DIR__ . '/../../data/api/' . $moduleIdentifier . '.json' ); + $json = json_decode( $json, true ); + return $this->replaceIdsInArray( $json ); + } + + private function replaceIdsInArray( array $array ) { + $replacements = array(); + if ( $this->lastPropertyId !== null ) { + $replacements['$propertyIdUnderTest'] = $this->lastPropertyId->getSerialization(); + } + if ( $this->lastItemId !== null ) { + $replacements['$itemIdUnderTest'] = $this->lastItemId->getSerialization(); + } + if ( $replacements ) { + foreach ( $array as $key => $val ) { + if ( is_string( $val ) && isset( $replacements[$val] ) ) { + $array[$key] = $replacements[$val]; + } elseif ( is_array( $val ) ) { + $array[$key] = $this->replaceIdsInArray( $val ); + } + } + } + return $array; + } + + private function removePageInfoAttributes( array $result, $entityId = null ) { + $attributesToRemove = array( 'lastrevid' ); + + foreach ( $attributesToRemove as $attributeToRemove ) { + unset( $result['entity'][$attributeToRemove] ); + } + + return $result; + } + + /** + * This mimics ApiMain::executeAction with the relevant parts, + * including setupExternalResponse where the printer is set. + * The module is then executed and results printed. + */ + private function executeApiModule( ApiBase $module ) { + $printer = $module->getMain()->createPrinterByName( 'json' ); + + $module->execute(); + + $printer->initPrinter(); + $printer->disable(); + + $printer->execute(); + + return json_decode( $printer->getBuffer(), true ); + } + + public function testSetLabelJsonFormat() { + $entityRevision = $this->getNewEntityRevision(); + $entityId = $entityRevision->getEntity()->getId()->getSerialization(); + + $params = array( + 'action' => 'wbsetlabel', + 'id' => $entityId, + 'language' => 'en-gb', + 'value' => 'enGbLabel', + ); + + $module = $this->getApiModule( '\Wikibase\Repo\Api\SetLabel', 'wbsetlabel', $params, true ); + $result = $this->executeApiModule( $module ); + $actual = $this->removePageInfoAttributes( $result, $entityId ); + + $this->assertEquals( $this->getExpectedJson( 'setlabel' ), $actual ); + + $params = array( + 'action' => 'wbsetlabel', + 'id' => $entityId, + 'language' => 'en-gb', + 'value' => '', + ); + + $module = $this->getApiModule( '\Wikibase\Repo\Api\SetLabel', 'wbsetlabel', $params, true ); + $result = $this->executeApiModule( $module ); + $actual = $this->removePageInfoAttributes( $result, $entityId ); + + $this->assertEquals( $this->getExpectedJson( 'setlabel-removed' ), $actual ); + } + +} diff --git a/repo/tests/phpunit/includes/api/ApiXmlFormatTest.php b/repo/tests/phpunit/includes/api/ApiXmlFormatTest.php index 5e7fbb5..76db3ef 100644 --- a/repo/tests/phpunit/includes/api/ApiXmlFormatTest.php +++ b/repo/tests/phpunit/includes/api/ApiXmlFormatTest.php @@ -8,7 +8,6 @@ use Wikibase\DataModel\Entity\Item; use Wikibase\DataModel\Entity\Property; use Wikibase\DataModel\Entity\PropertyId; -use Wikibase\DataModel\Snak\PropertyNoValueSnak; use Wikibase\EntityRevision; use Wikibase\Repo\Api\SetSiteLink; use Wikibase\Repo\SiteLinkTargetProvider; @@ -27,17 +26,7 @@ * @author Katie Filbert < aude.w...@gmail.com > * @author Adam Shorland */ -class ApiXmlFormatTest extends \MediaWikiTestCase { - - /** - * @var PropertyId|null - */ - private $lastPropertyId; - - /** - * @var PropertyId|null - */ - private $lastItemId; +class ApiXmlFormatTest extends ApiFormatTestCase { public function testGetEntitiesXmlFormat() { $entityRevision = $this->getNewEntityRevision( true ); @@ -330,26 +319,6 @@ } /** - * @param string $moduleClass - * @param string $moduleName - * @param array $params - * @param bool $needsToken - * - * @return ApiMain - */ - private function getApiModule( $moduleClass, $moduleName, array $params, $needsToken = false ) { - global $wgUser; - - if ( $needsToken ) { - $params['token'] = $wgUser->getEditToken(); - } - $request = new FauxRequest( $params, true ); - $main = new ApiMain( $request ); - - return new $moduleClass( $main, $moduleName ); - } - - /** * This mimics ApiMain::executeAction with the relevant parts, * including setupExternalResponse where the printer is set. * The module is then executed and results printed. @@ -366,62 +335,6 @@ $printer->execute(); return $printer->getBuffer(); - } - - private function getNewEntityRevision( $withData = false ) { - $entityRevision = $this->storeNewItem(); - - if ( $withData ) { - $this->storeNewProperty(); - $entityRevision = $this->storePresetDataInStatement( $entityRevision, $this->lastPropertyId ); - } - - return $entityRevision; - } - - private function storeNewProperty() { - global $wgUser; - - $store = WikibaseRepo::getDefaultInstance()->getEntityStore(); - - $property = Property::newFromType( 'string' ); - $entityRevision = $store->saveEntity( $property, 'testing', $wgUser, EDIT_NEW ); - $this->lastPropertyId = $entityRevision->getEntity()->getId(); - } - - private function storeNewItem() { - global $wgUser; - - $store = WikibaseRepo::getDefaultInstance()->getEntityStore(); - - $item = new Item(); - $entityRevision = $store->saveEntity( $item, 'testing', $wgUser, EDIT_NEW ); - $this->lastItemId = $entityRevision->getEntity()->getId(); - - return $entityRevision; - } - - private function storePresetDataInStatement( EntityRevision $entityRevision, PropertyId $propertyId ) { - global $wgUser; - - $store = WikibaseRepo::getDefaultInstance()->getEntityStore(); - - /** @var Item $item */ - $item = $entityRevision->getEntity(); - $snak = new PropertyNoValueSnak( $propertyId ); - $guid = $item->getId()->getSerialization() . '$1111AAAA-43cb-ed6d-3adb-760e85bd17ee'; - $item->getStatements()->addNewStatement( $snak, null, null, $guid ); - - $item->setLabel( 'en', 'en-label' ); - $item->setLabel( 'de', 'de-label' ); - $item->setDescription( 'de', 'de-desc' ); - $item->setDescription( 'es', 'es-desc' ); - $item->setAliases( 'pt', array( 'AA', 'BB' ) ); - $item->setAliases( 'en', array( 'AA-en', 'BB-en' ) ); - - $entityRevision = $store->saveEntity( $item, 'testing more!', $wgUser ); - - return $entityRevision; } } -- To view, visit https://gerrit.wikimedia.org/r/235970 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: If8830bb76a685ef45a94c869e94e051db88866e7 Gerrit-PatchSet: 15 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Ricordisamoa <ricordisa...@openmailbox.org> Gerrit-Reviewer: Addshore <addshorew...@gmail.com> Gerrit-Reviewer: Aude <aude.w...@gmail.com> Gerrit-Reviewer: JanZerebecki <jan.wikime...@zerebecki.de> Gerrit-Reviewer: Ricordisamoa <ricordisa...@openmailbox.org> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits