From 38d283814462e8ade379bc7f4cd791bc1fe22ba0 Mon Sep 17 00:00:00 2001 From: Bartek Wajda Date: Tue, 14 Nov 2023 18:39:53 +0100 Subject: [PATCH 1/3] IBX-6494: Copy nontranslatable fields to later versions --- eZ/Publish/Core/Repository/ContentService.php | 5 +- ...slatableFieldsFromPublishedVersionTest.php | 56 +++++++++++++++++++ 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/eZ/Publish/Core/Repository/ContentService.php b/eZ/Publish/Core/Repository/ContentService.php index c64d49ced0..47ef8d2424 100644 --- a/eZ/Publish/Core/Repository/ContentService.php +++ b/eZ/Publish/Core/Repository/ContentService.php @@ -1517,10 +1517,7 @@ protected function copyNonTranslatableFieldsFromPublishedVersion(APIContent $cur $publishedContent = $this->internalLoadContentById($versionInfo->getContentInfo()->getId()); $publishedVersionInfo = $publishedContent->getVersionInfo(); - if ( - !$publishedVersionInfo->isPublished() - || ($versionInfo->versionNo >= $publishedVersionInfo->versionNo) - ) { + if (!$publishedVersionInfo->isPublished()) { return; } diff --git a/tests/integration/Core/Repository/ContentService/CopyNonTranslatableFieldsFromPublishedVersionTest.php b/tests/integration/Core/Repository/ContentService/CopyNonTranslatableFieldsFromPublishedVersionTest.php index 84cb799036..612403de0d 100644 --- a/tests/integration/Core/Repository/ContentService/CopyNonTranslatableFieldsFromPublishedVersionTest.php +++ b/tests/integration/Core/Repository/ContentService/CopyNonTranslatableFieldsFromPublishedVersionTest.php @@ -84,6 +84,62 @@ public function testCopyNonTranslatableFieldsFromPublishedVersionToDraft(): void self::assertSame($expectedBodyValue, $bodyFieldValue->text); } + /** + * @throws \eZ\Publish\API\Repository\Exceptions\Exception + */ + public function testCopyNonTranslatableFieldsFromPublishedVersionToLatestVersion(): 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', 'Nontranslatable body'); + + $contentDraft = $contentService->createContent( + $contentCreateStruct, + [ + $locationService->newLocationCreateStruct(2), + ] + ); + $publishedContent = $contentService->publishVersion($contentDraft->getVersionInfo()); + + // Creating two drafts at the same time + $usDraft = $contentService->createContentDraft($publishedContent->contentInfo); + $gerDraft = $contentService->createContentDraft($publishedContent->contentInfo); + + // Publishing the draft in eng-US language + $contentUpdateStruct = new ContentUpdateStruct([ + 'initialLanguageCode' => self::ENG_US, + 'fields' => $usDraft->getFields(), + ]); + $contentUpdateStruct->setField('title', 'Title v2', self::ENG_US); + $contentUpdateStruct->setField('body', 'Nontranslatable body v2', self::ENG_US); + $usContent = $contentService->updateContent($usDraft->getVersionInfo(), $contentUpdateStruct); + $contentService->publishVersion($usContent->getVersionInfo()); + + // Publishing the draft in ger-DE language + $contentUpdateStruct = new ContentUpdateStruct([ + 'initialLanguageCode' => self::GER_DE, + 'fields' => $gerDraft->getFields(), + ]); + $contentUpdateStruct->setField('title', 'Title ger', self::GER_DE); + $gerContent = $contentService->updateContent($gerDraft->getVersionInfo(), $contentUpdateStruct); + $contentService->publishVersion($gerContent->getVersionInfo()); + + // Loading main content + $mainPublishedContent = $contentService->loadContent($gerContent->id); + $bodyFieldValue = $mainPublishedContent->getField('body')->getValue(); + + self::assertSame('Nontranslatable body v2', $bodyFieldValue->text); + } + private function createNonTranslatableContentType(): void { $permissionResolver = self::getPermissionResolver(); From a257944296d6b2eabf7c97c93d73c026a03d2d12 Mon Sep 17 00:00:00 2001 From: Bartek Wajda Date: Mon, 15 Apr 2024 22:59:13 +0200 Subject: [PATCH 2/3] 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(); From ceb8c7a83fb2b46bb27ec6c6e72568ea749ae53e Mon Sep 17 00:00:00 2001 From: Bartek Wajda Date: Mon, 15 Apr 2024 23:19:09 +0200 Subject: [PATCH 3/3] IBX-6494: CS --- ...slatableFieldsFromPublishedVersionTest.php | 65 +++++++------------ 1 file changed, 24 insertions(+), 41 deletions(-) diff --git a/tests/integration/Core/Repository/ContentService/CopyNonTranslatableFieldsFromPublishedVersionTest.php b/tests/integration/Core/Repository/ContentService/CopyNonTranslatableFieldsFromPublishedVersionTest.php index 6529551992..a38d527797 100644 --- a/tests/integration/Core/Repository/ContentService/CopyNonTranslatableFieldsFromPublishedVersionTest.php +++ b/tests/integration/Core/Repository/ContentService/CopyNonTranslatableFieldsFromPublishedVersionTest.php @@ -9,6 +9,7 @@ namespace Ibexa\Tests\Integration\Core\Repository\ContentService; use DateTime; +use eZ\Publish\API\Repository\Values\Content\Content; use eZ\Publish\API\Repository\Values\ContentType\FieldDefinitionCreateStruct; use eZ\Publish\Core\Repository\Values\Content\ContentUpdateStruct; use Ibexa\Tests\Integration\Core\RepositoryTestCase; @@ -31,21 +32,9 @@ public function testCopyNonTranslatableFieldsFromPublishedVersionToDraft(): 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'); - - $contentDraft = $contentService->createContent( - $contentCreateStruct, - [ - $locationService->newLocationCreateStruct(2), - ] - ); + $contentDraft = $this->createEngDraft(); $publishedContent = $contentService->publishVersion($contentDraft->getVersionInfo()); // Creating a draft in ger-DE language with the only field updated being 'title' @@ -92,22 +81,9 @@ public function testCopyNonTranslatableFieldsTwoParallelDrafts(): 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', 'Nontranslatable body'); - - $contentDraft = $contentService->createContent( - $contentCreateStruct, - [ - $locationService->newLocationCreateStruct(2), - ] - ); + $contentDraft = $this->createEngDraft(); $publishedContent = $contentService->publishVersion($contentDraft->getVersionInfo()); // Creating two drafts at the same time @@ -148,22 +124,9 @@ public function testCopyNonTranslatableFieldsOverridesNonMainLanguageDrafts(): v $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), - ] - ); + $contentDraft = $this->createEngDraft(); $publishedContent = $contentService->publishVersion($contentDraft->getVersionInfo()); // Creating a draft in ger-DE language with the only field updated being 'title' @@ -200,6 +163,26 @@ public function testCopyNonTranslatableFieldsOverridesNonMainLanguageDrafts(): v self::assertSame($expectedBodyValue, $bodyFieldValue->text); } + private function createEngDraft(): Content + { + $contentService = self::getContentService(); + $contentTypeService = self::getContentTypeService(); + $locationService = self::getLocationService(); + + $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'); + + return $contentService->createContent( + $contentCreateStruct, + [ + $locationService->newLocationCreateStruct(2), + ] + ); + } + private function createNonTranslatableContentType(): void { $permissionResolver = self::getPermissionResolver();