From 2887d29c58f38015309395eae8c7e285b028f0ca Mon Sep 17 00:00:00 2001 From: Andrew Longosz Date: Tue, 31 Oct 2017 14:37:29 +0100 Subject: [PATCH] EZP-27949: [PAPI] ContentService::removeTranslation does not cleanup Field Type External Storage (#2128) * EZP-27949: Created base integration test for multilingual FT data FTs supporting multilingual external storage data have to override getValidMultilingualFieldData method * EZP-27949: Fixed handling of FT Ext. Storage when deleting Translation Note: Applies to deleting Translation from all Versions * EZP-27949: Enabled multilingual FT data testing for ezkeyword --- .../Tests/FieldType/BaseIntegrationTest.php | 128 ++++++++++++++++++ .../FieldType/KeywordIntegrationTest.php | 19 +++ 2 files changed, 147 insertions(+) diff --git a/Repository/Tests/FieldType/BaseIntegrationTest.php b/Repository/Tests/FieldType/BaseIntegrationTest.php index 2e5e91db1..de7b7ef79 100644 --- a/Repository/Tests/FieldType/BaseIntegrationTest.php +++ b/Repository/Tests/FieldType/BaseIntegrationTest.php @@ -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 [languageCode => name] + * @param array $fieldData FT-specific data in the form of [languageCode => data] + * + * @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 */ @@ -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 [languageCode => data] + */ + 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') + ); + } + } } diff --git a/Repository/Tests/FieldType/KeywordIntegrationTest.php b/Repository/Tests/FieldType/KeywordIntegrationTest.php index 4ec8c7cc0..701d12090 100644 --- a/Repository/Tests/FieldType/KeywordIntegrationTest.php +++ b/Repository/Tests/FieldType/KeywordIntegrationTest.php @@ -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()). *