From 17e78be8a1946ee1a23f227cad176390450985d8 Mon Sep 17 00:00:00 2001 From: Bartek Wajda Date: Mon, 24 Jun 2024 16:19:45 +0200 Subject: [PATCH] IBX-6833: Fixed copying empty fields from a published version For more details see https://issues.ibexa.co/browse/IBX-6833 and https://github.com/ezsystems/ezplatform-kernel/pull/390 Key changes: * Fixed skipping copying empty fields when copying translations from published version * [Tests] Added integration coverage for the use case --- eZ/Publish/Core/Repository/ContentService.php | 7 +- ...pyTranslationsFromPublishedVersionTest.php | 118 ++++++++++++++++++ 2 files changed, 122 insertions(+), 3 deletions(-) create mode 100644 tests/integration/Core/Repository/ContentService/CopyTranslationsFromPublishedVersionTest.php diff --git a/eZ/Publish/Core/Repository/ContentService.php b/eZ/Publish/Core/Repository/ContentService.php index 3ef2ad3bdd..370bba6c90 100644 --- a/eZ/Publish/Core/Repository/ContentService.php +++ b/eZ/Publish/Core/Repository/ContentService.php @@ -1294,7 +1294,8 @@ public function updateContent(APIVersionInfo $versionInfo, APIContentUpdateStruc protected function internalUpdateContent( APIVersionInfo $versionInfo, APIContentUpdateStruct $contentUpdateStruct, - ?array $fieldIdentifiersToValidate = null + ?array $fieldIdentifiersToValidate = null, + bool $copyEmptyField = false ): Content { $contentUpdateStruct = clone $contentUpdateStruct; @@ -1385,7 +1386,7 @@ protected function internalUpdateContent( ); $fieldValues[$fieldDefinition->identifier][$languageCode] = $fieldValue; - if ($isRetained || $isCopied || ($isLanguageNew && $isEmpty) || $isProcessed) { + if ($isRetained || $isCopied || ($isLanguageNew && $isEmpty && !$copyEmptyField) || $isProcessed) { continue; } @@ -1692,7 +1693,7 @@ protected function copyTranslationsFromPublishedVersion(APIVersionInfo $versionI $updateStruct->setField($fallbackField->fieldDefIdentifier, $fallbackField->value, $fallbackField->languageCode); } - $this->internalUpdateContent($versionInfo, $updateStruct); + $this->internalUpdateContent($versionInfo, $updateStruct, null, true); } protected function fieldValuesAreEqual(FieldType $fieldType, Value $value1, Value $value2): bool diff --git a/tests/integration/Core/Repository/ContentService/CopyTranslationsFromPublishedVersionTest.php b/tests/integration/Core/Repository/ContentService/CopyTranslationsFromPublishedVersionTest.php new file mode 100644 index 0000000000..8ba05c9867 --- /dev/null +++ b/tests/integration/Core/Repository/ContentService/CopyTranslationsFromPublishedVersionTest.php @@ -0,0 +1,118 @@ +createContentType(); + + $contentService = self::getContentService(); + $contentTypeService = self::getContentTypeService(); + $locationService = self::getLocationService(); + + // Creating and publishing content in eng-GB language + $contentType = $contentTypeService->loadContentTypeByIdentifier(self::CONTENT_TYPE_IDENTIFIER); + $mainLanguageCode = self::ENG_LANGUAGE_CODE; + $contentCreateStruct = $contentService->newContentCreateStruct($contentType, $mainLanguageCode); + $contentCreateStruct->setField('title', 'Test title'); + + $contentDraft = $contentService->createContent( + $contentCreateStruct, + [ + $locationService->newLocationCreateStruct(2), + ], + ); + $publishedContent = $contentService->publishVersion($contentDraft->getVersionInfo()); + + // Creating a draft and publishing in ger-DE language with empty 'title' field + $gerDraft = $contentService->createContentDraft($publishedContent->contentInfo); + $usDraft = $contentService->createContentDraft($publishedContent->contentInfo); + + $contentUpdateStruct = new ContentUpdateStruct([ + 'initialLanguageCode' => self::GER_LANGUAGE_CODE, + ]); + $contentUpdateStruct->setField('title', null); + $gerContent = $contentService->updateContent($gerDraft->getVersionInfo(), $contentUpdateStruct); + $contentService->publishVersion($gerContent->getVersionInfo(), [self::GER_LANGUAGE_CODE]); + + // Creating a draft and publishing in eng-US language with empty 'title' field + $contentUpdateStruct = new ContentUpdateStruct([ + 'initialLanguageCode' => self::US_LANGUAGE_CODE, + ]); + $contentUpdateStruct->setField('title', null); + $usContent = $contentService->updateContent($usDraft->getVersionInfo(), $contentUpdateStruct); + $publishedUsContent = $contentService->publishVersion($usContent->getVersionInfo(), [self::US_LANGUAGE_CODE]); + + $usFieldValueInUsContent = $publishedUsContent->getField('title', self::US_LANGUAGE_CODE)->getValue(); + self::assertInstanceOf(TextLine\Value::class, $usFieldValueInUsContent); + self::assertSame('', $usFieldValueInUsContent->text); + + $gerFieldValueInUsContent = $publishedUsContent->getField('title', self::GER_LANGUAGE_CODE)->getValue(); + self::assertInstanceOf(TextLine\Value::class, $gerFieldValueInUsContent); + self::assertSame('', $gerFieldValueInUsContent->text); + } + + private function createContentType(): void + { + $permissionResolver = self::getPermissionResolver(); + $contentTypeService = self::getContentTypeService(); + + $typeCreate = $contentTypeService->newContentTypeCreateStruct(self::CONTENT_TYPE_IDENTIFIER); + + $typeCreate->mainLanguageCode = 'eng-GB'; + $typeCreate->remoteId = '1234567890abcdef'; + $typeCreate->urlAliasSchema = ''; + $typeCreate->nameSchema = '<title>'; + $typeCreate->names = [ + self::ENG_LANGUAGE_CODE => 'Some content type', + ]; + $typeCreate->descriptions = [ + self::ENG_LANGUAGE_CODE => '', + ]; + $typeCreate->creatorId = $permissionResolver->getCurrentUserReference()->getUserId(); + $typeCreate->creationDate = new DateTime(); + + $typeCreate->addFieldDefinition( + new FieldDefinitionCreateStruct( + [ + 'fieldTypeIdentifier' => 'ezstring', + 'identifier' => 'title', + 'names' => ['eng-GB' => 'Title'], + 'isRequired' => false, + 'isTranslatable' => true, + ], + ) + ); + + $contentTypeDraft = $contentTypeService->createContentType( + $typeCreate, + [$contentTypeService->loadContentTypeGroupByIdentifier('Content')], + ); + $contentTypeService->publishContentTypeDraft($contentTypeDraft); + } +}