jenkins-bot has submitted this change and it was merged.
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, 48 insertions(+), 31 deletions(-)
Approvals:
Aude: Looks good to me, approved
Addshore: Looks good to me, but someone else must approve
jenkins-bot: Verified
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..9519ae3 100644
--- a/repo/includes/api/ResultBuilder.php
+++ b/repo/includes/api/ResultBuilder.php
@@ -74,7 +74,8 @@
private $modifier;
/**
- * @var bool when special elements such as '_element' are needed by the
formatter.
+ * @var bool|null when special elements such as '_element' are needed
by the formatter.
+ * @note please use $this->getIsRawMode() to access this value!
*/
private $isRawMode;
@@ -92,7 +93,7 @@
SerializerFactory $serializerFactory,
SiteStore $siteStore,
PropertyDataTypeLookup $dataTypeLookup,
- $isRawMode
+ $isRawMode = null
) {
$this->result = $result;
$this->entityTitleLookup = $entityTitleLookup;
@@ -102,6 +103,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 +161,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 +222,7 @@
Assert::parameterType( 'string', $tag, '$tag' );
$this->checkValueIsNotList( $value );
- if ( $this->isRawMode ) {
+ if ( $this->getIsRawMode() ) {
$key = null;
}
@@ -313,12 +330,12 @@
}
$serialization =
$this->filterEntitySerializationUsingLangCodes( $serialization,
$filterLangCodes );
- if ( !$this->isRawMode ) {
+ if ( !$this->getIsRawMode() ) {
// Non raw mode formats dont want empty
parts....
$serialization =
$this->filterEmptyEntitySerializationParts( $serialization );
}
- if ( $this->isRawMode ) {
+ if ( $this->getIsRawMode() ) {
$serialization =
$this->getRawModeEntitySerialization( $serialization );
}
@@ -456,7 +473,7 @@
$fallbackSerialization['source-language'] = $fallbackSerialization['source'];
}
unset( $fallbackSerialization['source']
);
- if ( $this->isRawMode &&
$requestedLanguageCode !== $fallbackSerialization['language'] ) {
+ if ( $this->getIsRawMode() &&
$requestedLanguageCode !== $fallbackSerialization['language'] ) {
$fallbackSerialization['for-language'] = $requestedLanguageCode;
}
$newSerialization[$requestedLanguageCode] = $fallbackSerialization;
@@ -664,7 +681,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 +717,7 @@
$values = $this->getSiteLinkListArrayWithUrls( $values
);
}
- if ( $this->isRawMode ) {
+ if ( $this->getIsRawMode() ) {
$values = $this->getRawModeSiteLinkListArray( $values );
}
@@ -772,7 +789,7 @@
);
}
- if ( !$this->isRawMode ) {
+ if ( !$this->getIsRawMode() ) {
$values = $this->getArrayWithAlteredClaims( $values,
false, '*/*/' );
} else {
$values = $this->getArrayWithAlteredClaims( $values,
true, '*/*/' );
@@ -810,7 +827,7 @@
$value = $this->getArrayWithAlteredClaims( $value );
- if ( $this->isRawMode ) {
+ if ( $this->getIsRawMode() ) {
$value = $this->getArrayWithRawModeClaims( $value );
}
@@ -931,7 +948,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: merged
Gerrit-Change-Id: I05949bbc822250eb6ff989e0b7669dedc8ef99a7
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Addshore <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits