From a257944296d6b2eabf7c97c93d73c026a03d2d12 Mon Sep 17 00:00:00 2001 From: Bartek Wajda Date: Mon, 15 Apr 2024 22:59:13 +0200 Subject: [PATCH] IBX-6494: Refactored solution --- eZ/Publish/Core/Repository/ContentService.php | 13 ++-- ...slatableFieldsFromPublishedVersionTest.php | 62 ++++++++++++++++++- 2 files changed, 70 insertions(+), 5 deletions(-) diff --git a/eZ/Publish/Core/Repository/ContentService.php b/eZ/Publish/Core/Repository/ContentService.php index 47ef8d2424..3ef2ad3bdd 100644 --- a/eZ/Publish/Core/Repository/ContentService.php +++ b/eZ/Publish/Core/Repository/ContentService.php @@ -1521,9 +1521,8 @@ protected function copyNonTranslatableFieldsFromPublishedVersion(APIContent $cur return; } - $publishedContentFieldsInMainLanguage = $publishedContent->getFieldsByLanguage( - $publishedContent->getVersionInfo()->getContentInfo()->getMainLanguageCode() - ); + $mainLanguageCode = $publishedContent->getVersionInfo()->getContentInfo()->getMainLanguageCode(); + $publishedContentFieldsInMainLanguage = $publishedContent->getFieldsByLanguage($mainLanguageCode); $fieldValues = []; $persistenceFields = []; @@ -1542,7 +1541,13 @@ protected function copyNonTranslatableFieldsFromPublishedVersion(APIContent $cur $fieldDefinition->fieldTypeIdentifier ); - $newValue = $publishedContentFieldsInMainLanguage[$field->fieldDefIdentifier]->getValue(); + $newValue = ( + $versionInfo->versionNo >= $publishedVersionInfo->versionNo + && $versionInfo->initialLanguageCode === $mainLanguageCode + ) + ? $field->getValue() + : $publishedContentFieldsInMainLanguage[$field->fieldDefIdentifier]->getValue(); + $fieldValues[$fieldDefinition->identifier][$field->languageCode] = $newValue; $persistenceFields[] = new SPIField( diff --git a/tests/integration/Core/Repository/ContentService/CopyNonTranslatableFieldsFromPublishedVersionTest.php b/tests/integration/Core/Repository/ContentService/CopyNonTranslatableFieldsFromPublishedVersionTest.php index 612403de0d..6529551992 100644 --- a/tests/integration/Core/Repository/ContentService/CopyNonTranslatableFieldsFromPublishedVersionTest.php +++ b/tests/integration/Core/Repository/ContentService/CopyNonTranslatableFieldsFromPublishedVersionTest.php @@ -87,7 +87,7 @@ public function testCopyNonTranslatableFieldsFromPublishedVersionToDraft(): void /** * @throws \eZ\Publish\API\Repository\Exceptions\Exception */ - public function testCopyNonTranslatableFieldsFromPublishedVersionToLatestVersion(): void + public function testCopyNonTranslatableFieldsTwoParallelDrafts(): void { $this->createNonTranslatableContentType(); @@ -140,6 +140,66 @@ public function testCopyNonTranslatableFieldsFromPublishedVersionToLatestVersion self::assertSame('Nontranslatable body v2', $bodyFieldValue->text); } + /** + * @throws \eZ\Publish\API\Repository\Exceptions\Exception + */ + public function testCopyNonTranslatableFieldsOverridesNonMainLanguageDrafts(): void + { + $this->createNonTranslatableContentType(); + + $contentService = self::getContentService(); + $contentTypeService = self::getContentTypeService(); + $locationService = self::getLocationService(); + + // Creating start content in eng-US language + $contentType = $contentTypeService->loadContentTypeByIdentifier(self::CONTENT_TYPE_IDENTIFIER); + $mainLanguageCode = self::ENG_US; + $contentCreateStruct = $contentService->newContentCreateStruct($contentType, $mainLanguageCode); + $contentCreateStruct->setField('title', 'Test title'); + $contentCreateStruct->setField('body', 'Test body'); + + $contentDraft = $contentService->createContent( + $contentCreateStruct, + [ + $locationService->newLocationCreateStruct(2), + ] + ); + $publishedContent = $contentService->publishVersion($contentDraft->getVersionInfo()); + + // Creating a draft in ger-DE language with the only field updated being 'title' + $gerDraft = $contentService->createContentDraft($publishedContent->contentInfo); + + $contentUpdateStruct = new ContentUpdateStruct([ + 'initialLanguageCode' => self::GER_DE, + 'fields' => $contentDraft->getFields(), + ]); + + $contentUpdateStruct->setField('title', 'Folder GER', self::GER_DE); + $gerContent = $contentService->updateContent($gerDraft->getVersionInfo(), $contentUpdateStruct); + $publishedContent = $contentService->publishVersion($gerContent->getVersionInfo()); + + // Updating non-translatable field in eng-US language (allowed) and publishing it + $engContent = $contentService->createContentDraft($publishedContent->contentInfo); + + $contentUpdateStruct = new ContentUpdateStruct([ + 'initialLanguageCode' => self::ENG_US, + 'fields' => $contentDraft->getFields(), + ]); + + $expectedBodyValue = 'Non-translatable value'; + $contentUpdateStruct->setField('title', 'Title v2', self::ENG_US); + $contentUpdateStruct->setField('body', $expectedBodyValue, self::ENG_US); + + $engContent = $contentService->updateContent($engContent->getVersionInfo(), $contentUpdateStruct); + $contentService->publishVersion($engContent->getVersionInfo()); + + // Loading content in ger-DE language + $mainPublishedContent = $contentService->loadContent($engContent->id, ['ger-DE']); + $bodyFieldValue = $mainPublishedContent->getField('body')->getValue(); + + self::assertSame($expectedBodyValue, $bodyFieldValue->text); + } + private function createNonTranslatableContentType(): void { $permissionResolver = self::getPermissionResolver();