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, 43 insertions(+), 27 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 f402b99..5e977cd 100644
--- a/repo/includes/api/ApiHelperFactory.php
+++ b/repo/includes/api/ApiHelperFactory.php
@@ -102,8 +102,7 @@
$this->newLibSerializerFactory( $defaultOptions ),
$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 ed788ea..690fdae 100644
--- a/repo/includes/api/ResultBuilder.php
+++ b/repo/includes/api/ResultBuilder.php
@@ -104,7 +104,7 @@
SerializerFactory $serializerFactory,
SiteStore $siteStore,
PropertyDataTypeLookup $dataTypeLookup,
- $isRawMode
+ $isRawMode = null
) {
if ( !$result instanceof ApiResult ) {
throw new InvalidArgumentException( 'Result builder
must be constructed with an ApiResult' );
@@ -130,11 +130,27 @@
public function getOptions() {
if ( !$this->options ) {
$this->options = new SerializationOptions();
- $this->options->setIndexTags( $this->isRawMode );
+ $this->options->setIndexTags( $this->getIsRawMode() );
$this->options->setOption(
EntitySerializer::OPT_SORT_ORDER, EntitySerializer::SORT_NONE );
}
return $this->options;
+ }
+
+ /**
+ * 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;
}
/**
@@ -181,7 +197,7 @@
$this->checkNameIsString( $name );
$this->checkTagIsString( $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'] );
@@ -247,7 +263,7 @@
$this->checkValueIsNotList( $value );
- if ( $this->isRawMode ) {
+ if ( $this->getIsRawMode() ) {
$key = null;
}
@@ -492,7 +508,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 ) {
@@ -528,7 +544,7 @@
$values = $this->getSiteLinkListArrayWithUrls( $values
);
}
- if ( $this->isRawMode ) {
+ if ( $this->getIsRawMode() ) {
$values = $this->getRawModeSiteLinkListArray( $values );
}
@@ -629,7 +645,7 @@
$this->getModCallbackToAddDataTypeToSnak()
);
- if ( $this->isRawMode ) {
+ if ( $this->getIsRawMode() ) {
$value = $this->getRawModeClaimArray( $value );
}
@@ -693,7 +709,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/228184
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I05949bbc822250eb6ff989e0b7669dedc8ef99a7
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: wmf/1.26wmf16
Gerrit-Owner: Aude <[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