Skip to content

Commit

Permalink
Merge branch '6.12' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Longosz committed Oct 31, 2017
2 parents cb64ec5 + 6672409 commit 28c8cdb
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 4 deletions.
128 changes: 128 additions & 0 deletions eZ/Publish/API/Repository/Tests/FieldType/BaseIntegrationTest.php
Expand Up @@ -534,6 +534,42 @@ protected function createContent($fieldData, $contentType = null)
return $contentService->createContent($createStruct);
}

/**
* Create multilingual content of given name and FT-specific data.
*
* @param array $names Content names in the form of <code>[languageCode => name]</code>
* @param array $fieldData FT-specific data in the form of <code>[languageCode => data]</code>
*
* @return \eZ\Publish\API\Repository\Values\Content\Content
*/
protected function createMultilingualContent(array $names, array $fieldData)
{
self::assertEquals(array_keys($names), array_keys($fieldData), 'Languages passed to names and data differ');

$contentType = $this->createContentType(
$this->getValidFieldSettings(),
$this->getValidValidatorConfiguration(),
[],
['isTranslatable' => true]
);

$repository = $this->getRepository();
$contentService = $repository->getContentService();

$createStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
foreach ($names as $languageCode => $name) {
$createStruct->setField('name', $name, $languageCode);
}
foreach ($fieldData as $languageCode => $value) {
$createStruct->setField('data', $value, $languageCode);
}

$createStruct->remoteId = md5(uniqid('', true) . microtime());
$createStruct->alwaysAvailable = true;

return $contentService->createContent($createStruct);
}

/**
* @depends testCreateContent
*/
Expand Down Expand Up @@ -1092,4 +1128,96 @@ public function testUpdateContentWithNewTranslationOnEmptyField()
$updatedContentDraft = $contentService->updateContent($contentDraft->versionInfo, $updateStruct);
$contentService->publishVersion($updatedContentDraft->versionInfo);
}

/**
* Get proper multilingual FT-specific Values. It Can be overridden by a Field Type test case.
*
* @param string[] $languageCodes List of languages to create data for
*
* @return array an array in the form of <code>[languageCode => data]</code>
*/
public function getValidMultilingualFieldData(array $languageCodes)
{
$data = [];
foreach ($languageCodes as $languageCode) {
$data[$languageCode] = $this->getValidCreationFieldData();
}

return $data;
}

/**
* Test that removing Translation from all Versions works for data from a Field Type.
*
* @covers \eZ\Publish\API\Repository\ContentService::removeTranslation
*/
public function testRemoveTranslation()
{
$repository = $this->getRepository();
$contentService = $repository->getContentService();

$languageCodes = ['eng-US', 'ger-DE'];

$fieldName = $this->getFieldName();
$names = [];
foreach ($languageCodes as $languageCode) {
$names[$languageCode] = "{$languageCode} {$fieldName}";
}

$fieldData = $this->getValidMultilingualFieldData($languageCodes);

$content = $contentService->publishVersion(
$this->createMultilingualContent($names, $fieldData)->versionInfo
);

// create one more Version
$publishedContent = $contentService->publishVersion(
$contentService->createContentDraft($content->contentInfo)->versionInfo
);

// create Draft
$contentService->createContentDraft($content->contentInfo);

// create copy of content in all Versions to use it for comparision later on
$contentByVersion = [];
foreach ($contentService->loadVersions($content->contentInfo) as $versionInfo) {
$contentByVersion[$versionInfo->versionNo] = $contentService->loadContent(
$content->id,
null,
$versionInfo->versionNo
);
}

// delete Translation from all available Versions
$contentService->removeTranslation($publishedContent->contentInfo, 'ger-DE');

// check if are Versions have valid Translation
foreach ($contentService->loadVersions($publishedContent->contentInfo) as $versionInfo) {
// check if deleted Translation does not exist
self::assertEquals(['eng-US'], array_keys($versionInfo->getNames()));
self::assertEquals(['eng-US'], $versionInfo->languageCodes);

// load Content of a Version to access other fields data
$versionContent = $contentService->loadContent(
$content->id,
null,
$versionInfo->versionNo
);
// check if deleted Translation for Field Type data does not exist
self::assertEmpty($versionContent->getFieldsByLanguage('ger-DE'));
self::assertEmpty($versionContent->getField('data', 'ger-DE'));

// check if the remaining Translation is still valid
$expectedContent = $contentByVersion[$versionContent->versionInfo->versionNo];
self::assertNotEmpty($versionContent->getFieldsByLanguage('eng-US'));
self::assertEquals(
$expectedContent->getField('name', 'eng-US'),
$versionContent->getField('name', 'eng-US')
);
self::assertEquals(
$expectedContent->getField('data', 'eng-US'),
$versionContent->getField('data', 'eng-US')
);
}
}
}
Expand Up @@ -104,6 +104,25 @@ public function getValidCreationFieldData()
return new KeywordValue(array('foo', 'bar', 'sindelfingen'));
}

/**
* {@inheritdoc}
*/
public function getValidMultilingualFieldData(array $languageCodes)
{
$data = [];
foreach ($languageCodes as $languageCode) {
$data[$languageCode] = new KeywordValue(
[
"{$languageCode} bar",
"{$languageCode} foo",
"$languageCode sindelfingen",
]
);
}

return $data;
}

/**
* Get name generated by the given field type (either via Nameable or fieldType->getName()).
*
Expand Down
25 changes: 25 additions & 0 deletions eZ/Publish/Core/Persistence/Legacy/Content/FieldHandler.php
Expand Up @@ -446,6 +446,31 @@ public function deleteFields($contentId, VersionInfo $versionInfo)
$this->contentGateway->deleteFields($contentId, $versionInfo->versionNo);
}

/**
* Deletes translated fields and their external storage data for the given Content Versions.
*
* @param int $contentId
* @param \eZ\Publish\SPI\Persistence\Content\VersionInfo[] $versions
* @param string $languageCode
*/
public function deleteTranslationFromContentFields($contentId, array $versions, $languageCode)
{
foreach ($versions as $versionInfo) {
// FT-specific implementations require VersionInfo to delete data
$fieldTypeIdsMap = $this->contentGateway->getFieldIdsByType(
$versionInfo->contentInfo->id,
$versionInfo->versionNo,
$languageCode
);

foreach ($fieldTypeIdsMap as $fieldType => $ids) {
$this->storageHandler->deleteFieldData($fieldType, $versionInfo, $ids);
}
}

$this->contentGateway->deleteTranslatedFields($languageCode, $contentId);
}

/**
* Deletes translated fields and their external storage data for the given $versionInfo.
*
Expand Down
Expand Up @@ -1970,11 +1970,8 @@ public function removeTranslationFromContent($contentId, $languageCode)
$this->connection->beginTransaction();
try {
$this->deleteTranslationFromContentVersions($contentId, $language->id);
$this->deleteTranslationFromContentObject($contentId, $language->id);

// @todo: move this to field handler when fixing EZP-27949
$this->deleteTranslatedFields($languageCode, $contentId);
$this->deleteTranslationFromContentNames($contentId, $languageCode);
$this->deleteTranslationFromContentObject($contentId, $language->id);

$this->connection->commit();
} catch (DBALException $e) {
Expand Down
5 changes: 5 additions & 0 deletions eZ/Publish/Core/Persistence/Legacy/Content/Handler.php
Expand Up @@ -718,6 +718,11 @@ public function loadReverseRelations($destinationContentId, $type = null)
*/
public function removeTranslationFromContent($contentId, $languageCode)
{
$this->fieldHandler->deleteTranslationFromContentFields(
$contentId,
$this->listVersions($contentId),
$languageCode
);
$this->contentGateway->removeTranslationFromContent($contentId, $languageCode);
}

Expand Down

0 comments on commit 28c8cdb

Please sign in to comment.