Skip to content

Commit a257944

Browse files
committed
IBX-6494: Refactored solution
1 parent 38d2838 commit a257944

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

eZ/Publish/Core/Repository/ContentService.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -1521,9 +1521,8 @@ protected function copyNonTranslatableFieldsFromPublishedVersion(APIContent $cur
15211521
return;
15221522
}
15231523

1524-
$publishedContentFieldsInMainLanguage = $publishedContent->getFieldsByLanguage(
1525-
$publishedContent->getVersionInfo()->getContentInfo()->getMainLanguageCode()
1526-
);
1524+
$mainLanguageCode = $publishedContent->getVersionInfo()->getContentInfo()->getMainLanguageCode();
1525+
$publishedContentFieldsInMainLanguage = $publishedContent->getFieldsByLanguage($mainLanguageCode);
15271526

15281527
$fieldValues = [];
15291528
$persistenceFields = [];
@@ -1542,7 +1541,13 @@ protected function copyNonTranslatableFieldsFromPublishedVersion(APIContent $cur
15421541
$fieldDefinition->fieldTypeIdentifier
15431542
);
15441543

1545-
$newValue = $publishedContentFieldsInMainLanguage[$field->fieldDefIdentifier]->getValue();
1544+
$newValue = (
1545+
$versionInfo->versionNo >= $publishedVersionInfo->versionNo
1546+
&& $versionInfo->initialLanguageCode === $mainLanguageCode
1547+
)
1548+
? $field->getValue()
1549+
: $publishedContentFieldsInMainLanguage[$field->fieldDefIdentifier]->getValue();
1550+
15461551
$fieldValues[$fieldDefinition->identifier][$field->languageCode] = $newValue;
15471552

15481553
$persistenceFields[] = new SPIField(

tests/integration/Core/Repository/ContentService/CopyNonTranslatableFieldsFromPublishedVersionTest.php

+61-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function testCopyNonTranslatableFieldsFromPublishedVersionToDraft(): void
8787
/**
8888
* @throws \eZ\Publish\API\Repository\Exceptions\Exception
8989
*/
90-
public function testCopyNonTranslatableFieldsFromPublishedVersionToLatestVersion(): void
90+
public function testCopyNonTranslatableFieldsTwoParallelDrafts(): void
9191
{
9292
$this->createNonTranslatableContentType();
9393

@@ -140,6 +140,66 @@ public function testCopyNonTranslatableFieldsFromPublishedVersionToLatestVersion
140140
self::assertSame('Nontranslatable body v2', $bodyFieldValue->text);
141141
}
142142

143+
/**
144+
* @throws \eZ\Publish\API\Repository\Exceptions\Exception
145+
*/
146+
public function testCopyNonTranslatableFieldsOverridesNonMainLanguageDrafts(): void
147+
{
148+
$this->createNonTranslatableContentType();
149+
150+
$contentService = self::getContentService();
151+
$contentTypeService = self::getContentTypeService();
152+
$locationService = self::getLocationService();
153+
154+
// Creating start content in eng-US language
155+
$contentType = $contentTypeService->loadContentTypeByIdentifier(self::CONTENT_TYPE_IDENTIFIER);
156+
$mainLanguageCode = self::ENG_US;
157+
$contentCreateStruct = $contentService->newContentCreateStruct($contentType, $mainLanguageCode);
158+
$contentCreateStruct->setField('title', 'Test title');
159+
$contentCreateStruct->setField('body', 'Test body');
160+
161+
$contentDraft = $contentService->createContent(
162+
$contentCreateStruct,
163+
[
164+
$locationService->newLocationCreateStruct(2),
165+
]
166+
);
167+
$publishedContent = $contentService->publishVersion($contentDraft->getVersionInfo());
168+
169+
// Creating a draft in ger-DE language with the only field updated being 'title'
170+
$gerDraft = $contentService->createContentDraft($publishedContent->contentInfo);
171+
172+
$contentUpdateStruct = new ContentUpdateStruct([
173+
'initialLanguageCode' => self::GER_DE,
174+
'fields' => $contentDraft->getFields(),
175+
]);
176+
177+
$contentUpdateStruct->setField('title', 'Folder GER', self::GER_DE);
178+
$gerContent = $contentService->updateContent($gerDraft->getVersionInfo(), $contentUpdateStruct);
179+
$publishedContent = $contentService->publishVersion($gerContent->getVersionInfo());
180+
181+
// Updating non-translatable field in eng-US language (allowed) and publishing it
182+
$engContent = $contentService->createContentDraft($publishedContent->contentInfo);
183+
184+
$contentUpdateStruct = new ContentUpdateStruct([
185+
'initialLanguageCode' => self::ENG_US,
186+
'fields' => $contentDraft->getFields(),
187+
]);
188+
189+
$expectedBodyValue = 'Non-translatable value';
190+
$contentUpdateStruct->setField('title', 'Title v2', self::ENG_US);
191+
$contentUpdateStruct->setField('body', $expectedBodyValue, self::ENG_US);
192+
193+
$engContent = $contentService->updateContent($engContent->getVersionInfo(), $contentUpdateStruct);
194+
$contentService->publishVersion($engContent->getVersionInfo());
195+
196+
// Loading content in ger-DE language
197+
$mainPublishedContent = $contentService->loadContent($engContent->id, ['ger-DE']);
198+
$bodyFieldValue = $mainPublishedContent->getField('body')->getValue();
199+
200+
self::assertSame($expectedBodyValue, $bodyFieldValue->text);
201+
}
202+
143203
private function createNonTranslatableContentType(): void
144204
{
145205
$permissionResolver = self::getPermissionResolver();

0 commit comments

Comments
 (0)