Addshore has uploaded a new change for review.
https://gerrit.wikimedia.org/r/228841
Change subject: Fix ApiXmlFormatTest and raw mode handling in api
......................................................................
Fix ApiXmlFormatTest and raw mode handling in api
In the test, raw mode needs to be set after the api module
is initiated and before executed for the test to work as
intended.
In ResultBuilder, isRawMode needs to be lazy initialized.
It is always false when the api modules are constructed and
ResultBuilder constructed. It is then set later in the
api module execution process.
Bug: T59529
Change-Id: I05949bbc822250eb6ff989e0b7669dedc8ef99a7
---
M repo/includes/api/ApiHelperFactory.php
M repo/includes/api/ResultBuilder.php
M repo/tests/phpunit/includes/api/ApiXmlFormatTest.php
3 files changed, 42 insertions(+), 26 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase
refs/changes/41/228841/1
diff --git a/repo/includes/api/ApiHelperFactory.php
b/repo/includes/api/ApiHelperFactory.php
index 8eb8a01..8e0e2e9 100644
--- a/repo/includes/api/ApiHelperFactory.php
+++ b/repo/includes/api/ApiHelperFactory.php
@@ -98,8 +98,7 @@
$this->titleLookup,
$this->newSerializerFactory(),
$this->siteStore,
- $this->dataTypeLookup,
- $api->getResult()->getIsRawMode()
+ $this->dataTypeLookup
);
}
diff --git a/repo/includes/api/ResultBuilder.php
b/repo/includes/api/ResultBuilder.php
index aa0e2e4..a0b3da7 100644
--- a/repo/includes/api/ResultBuilder.php
+++ b/repo/includes/api/ResultBuilder.php
@@ -92,7 +92,7 @@
SerializerFactory $serializerFactory,
SiteStore $siteStore,
PropertyDataTypeLookup $dataTypeLookup,
- $isRawMode
+ $isRawMode = null
) {
$this->result = $result;
$this->entityTitleLookup = $entityTitleLookup;
@@ -102,6 +102,22 @@
$this->siteStore = $siteStore;
$this->dataTypeLookup = $dataTypeLookup;
$this->modifier = new SerializationModifier();
+ }
+
+ /**
+ * isRawMode needs to be lazy initialized since ApiMain may set it
+ * on the ApiResult after the module is constructed.
+ *
+ * isRawMode can be set in the constructor, such as for testing
purposes.
+ *
+ * @return bool
+ */
+ private function getIsRawMode() {
+ if ( $this->isRawMode === null ) {
+ $this->isRawMode = $this->result->getIsRawMode();
+ }
+
+ return $this->isRawMode;
}
/**
@@ -144,7 +160,7 @@
Assert::parameterType( 'string', $name, '$name' );
Assert::parameterType( 'string', $tag, '$tag' );
- if ( $this->isRawMode ) {
+ if ( $this->getIsRawMode() ) {
// Unset first, so we don't make the tag name an actual
value.
// We'll be setting this to $tag by calling
setIndexedTagName().
unset( $values['_element'] );
@@ -205,7 +221,7 @@
Assert::parameterType( 'string', $tag, '$tag' );
$this->checkValueIsNotList( $value );
- if ( $this->isRawMode ) {
+ if ( $this->getIsRawMode() ) {
$key = null;
}
@@ -664,7 +680,7 @@
* @param array|string $path where the data is located
*/
public function addAliasGroupList( AliasGroupList $aliasGroupList,
$path ) {
- if ( $this->isRawMode ) {
+ if ( $this->getIsRawMode() ) {
$serializer =
$this->serializerFactory->newAliasGroupSerializer();
$values = array();
foreach ( $aliasGroupList->toArray() as $aliasGroup ) {
@@ -700,7 +716,7 @@
$values = $this->getSiteLinkListArrayWithUrls( $values
);
}
- if ( $this->isRawMode ) {
+ if ( $this->getIsRawMode() ) {
$values = $this->getRawModeSiteLinkListArray( $values );
}
@@ -810,7 +826,7 @@
$value = $this->getArrayWithAlteredClaims( $value );
- if ( $this->isRawMode ) {
+ if ( $this->getIsRawMode() ) {
$value = $this->getArrayWithRawModeClaims( $value );
}
@@ -931,7 +947,7 @@
$value = $this->getArrayWithDataTypesInGroupedSnakListAtPath(
$value, 'snaks' );
- if ( $this->isRawMode ) {
+ if ( $this->getIsRawMode() ) {
$value = $this->getRawModeReferenceArray( $value );
}
diff --git a/repo/tests/phpunit/includes/api/ApiXmlFormatTest.php
b/repo/tests/phpunit/includes/api/ApiXmlFormatTest.php
index ee90ddf..be0ab70 100644
--- a/repo/tests/phpunit/includes/api/ApiXmlFormatTest.php
+++ b/repo/tests/phpunit/includes/api/ApiXmlFormatTest.php
@@ -49,7 +49,7 @@
);
$module = $this->getApiModule(
'\Wikibase\Repo\Api\GetEntities', 'wbgetentities', $params );
- $result = $this->doApiRequest( $module );
+ $result = $this->executeApiModule( $module );
$actual = $this->removePageInfoAttributes( $result, $entityId );
$this->assertXmlStringEqualsXmlString( $this->getExpectedXml(
'getentities' ), $actual );
@@ -65,7 +65,7 @@
);
$module = $this->getApiModule( '\Wikibase\Repo\Api\GetClaims',
'wbgetclaims', $params );
- $actual = $this->doApiRequest( $module );
+ $actual = $this->executeApiModule( $module );
$this->assertXmlStringEqualsXmlString( $this->getExpectedXml(
'getclaims' ), $actual );
}
@@ -82,7 +82,7 @@
);
$module = $this->getApiModule( '\Wikibase\Repo\Api\SetLabel',
'wbsetlabel', $params, true );
- $result = $this->doApiRequest( $module );
+ $result = $this->executeApiModule( $module );
$actual = $this->removePageInfoAttributes( $result, $entityId );
$this->assertXmlStringEqualsXmlString( $this->getExpectedXml(
'setlabel' ), $actual );
@@ -100,7 +100,7 @@
);
$module = $this->getApiModule(
'\Wikibase\Repo\Api\SetDescription', 'wbsetdescription', $params, true );
- $result = $this->doApiRequest( $module );
+ $result = $this->executeApiModule( $module );
$actual = $this->removePageInfoAttributes( $result, $entityId );
$this->assertXmlStringEqualsXmlString( $this->getExpectedXml(
'setdescription' ), $actual );
@@ -118,7 +118,7 @@
);
$module = $this->getApiModule( '\Wikibase\Repo\Api\SetAliases',
'wbsetaliases', $params, true );
- $result = $this->doApiRequest( $module );
+ $result = $this->executeApiModule( $module );
$actual = $this->removePageInfoAttributes( $result, $entityId );
$this->assertXmlStringEqualsXmlString( $this->getExpectedXml(
'setaliases' ), $actual );
@@ -140,7 +140,7 @@
$module = $this->getApiModule(
'\Wikibase\Repo\Api\SetSiteLink', 'wbsetsitelink', $params, true );
$siteTaregtProvider = new SiteLinkTargetProvider(
MockSiteStore::newFromTestSites(), array() );
$module->setServices( $siteTaregtProvider );
- $result = $this->doApiRequest( $module );
+ $result = $this->executeApiModule( $module );
$actual = $this->removePageInfoAttributes( $result, $entityId );
//If a URL has been added just remove it as it is not always
present
$actual = str_replace(
'url="https://en.wikipedia.org/wiki/Japan"', '', $actual );
@@ -160,7 +160,7 @@
/** @var SetSiteLink $module */
$module = $this->getApiModule( '\Wikibase\Repo\Api\SetClaim',
'wbsetclaim', $params, true );
- $result = $this->doApiRequest( $module );
+ $result = $this->executeApiModule( $module );
$actual = $this->removePageInfoAttributes( $result );
$this->assertXmlStringEqualsXmlString( $this->getExpectedXml(
'setclaim' ), $actual );
@@ -180,7 +180,7 @@
/** @var SetSiteLink $module */
$module = $this->getApiModule(
'\Wikibase\Repo\Api\SetReference', 'wbsetreference', $params, true );
- $result = $this->doApiRequest( $module );
+ $result = $this->executeApiModule( $module );
$actual = $this->removePageInfoAttributes( $result );
$actual = $this->replaceHashWithMock( $actual );
@@ -201,7 +201,7 @@
/** @var SetSiteLink $module */
$module = $this->getApiModule(
'\Wikibase\Repo\Api\SetQualifier', 'wbsetqualifier', $params, true );
- $result = $this->doApiRequest( $module );
+ $result = $this->executeApiModule( $module );
$actual = $this->removePageInfoAttributes( $result );
$actual = $this->replaceHashWithMock( $actual );
@@ -223,7 +223,7 @@
);
$module = $this->getApiModule( '\Wikibase\Repo\Api\EditEntity',
'wbeditEntity', $params, true );
- $result = $this->doApiRequest( $module );
+ $result = $this->executeApiModule( $module );
$actual = $this->removePageInfoAttributes( $result, $entityId );
$actual = $this->replaceHashWithMock( $actual );
@@ -295,17 +295,18 @@
$request = new FauxRequest( $params, true );
$main = new ApiMain( $request );
- /**
- * This has to be set before the Wikibase Api module is
instansiated due to the ApiHelperFactory
- * using $api->getResult()->getIsRawMode().
- */
- $main->getResult()->setRawMode( true );
-
return new $moduleClass( $main, $moduleName );
}
- private function doApiRequest( ApiBase $module ) {
+ /**
+ * This mimics ApiMain::executeAction with the relevant parts,
+ * including setupExternalResponse where the printer is set. and
+ * Then raw mode is set the api format requires it. (always for xml)
+ * The module is then executed and results printed.
+ */
+ private function executeApiModule( ApiBase $module ) {
$printer = $module->getMain()->createPrinterByName( 'xml' );
+ $module->getResult()->setRawMode( true );
$module->execute();
--
To view, visit https://gerrit.wikimedia.org/r/228841
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I05949bbc822250eb6ff989e0b7669dedc8ef99a7
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Addshore <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits