Skip to content

Commit b5fe9ad

Browse files
authored
IBX-6494: Fixed copying of non-translatable fields to later versions
For more details see https://issues.ibexa.co/browse/IBX-6494 and #394 Key changes: * Fixed copying of non-translatable fields to later versions * [Tests] Added integration test coverage for the use case
1 parent 245d02e commit b5fe9ad

File tree

2 files changed

+122
-21
lines changed

2 files changed

+122
-21
lines changed

eZ/Publish/Core/Repository/ContentService.php

+10-8
Original file line numberDiff line numberDiff line change
@@ -1517,16 +1517,12 @@ protected function copyNonTranslatableFieldsFromPublishedVersion(APIContent $cur
15171517
$publishedContent = $this->internalLoadContentById($versionInfo->getContentInfo()->getId());
15181518
$publishedVersionInfo = $publishedContent->getVersionInfo();
15191519

1520-
if (
1521-
!$publishedVersionInfo->isPublished()
1522-
|| ($versionInfo->versionNo >= $publishedVersionInfo->versionNo)
1523-
) {
1520+
if (!$publishedVersionInfo->isPublished()) {
15241521
return;
15251522
}
15261523

1527-
$publishedContentFieldsInMainLanguage = $publishedContent->getFieldsByLanguage(
1528-
$publishedContent->getVersionInfo()->getContentInfo()->getMainLanguageCode()
1529-
);
1524+
$mainLanguageCode = $publishedContent->getVersionInfo()->getContentInfo()->getMainLanguageCode();
1525+
$publishedContentFieldsInMainLanguage = $publishedContent->getFieldsByLanguage($mainLanguageCode);
15301526

15311527
$fieldValues = [];
15321528
$persistenceFields = [];
@@ -1545,7 +1541,13 @@ protected function copyNonTranslatableFieldsFromPublishedVersion(APIContent $cur
15451541
$fieldDefinition->fieldTypeIdentifier
15461542
);
15471543

1548-
$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+
15491551
$fieldValues[$fieldDefinition->identifier][$field->languageCode] = $newValue;
15501552

15511553
$persistenceFields[] = new SPIField(

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

+112-13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Ibexa\Tests\Integration\Core\Repository\ContentService;
1010

1111
use DateTime;
12+
use eZ\Publish\API\Repository\Values\Content\Content;
1213
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinitionCreateStruct;
1314
use eZ\Publish\Core\Repository\Values\Content\ContentUpdateStruct;
1415
use Ibexa\Tests\Integration\Core\RepositoryTestCase;
@@ -31,21 +32,9 @@ public function testCopyNonTranslatableFieldsFromPublishedVersionToDraft(): void
3132
$this->createNonTranslatableContentType();
3233

3334
$contentService = self::getContentService();
34-
$contentTypeService = self::getContentTypeService();
35-
$locationService = self::getLocationService();
3635

3736
// Creating start content in eng-US language
38-
$contentType = $contentTypeService->loadContentTypeByIdentifier(self::CONTENT_TYPE_IDENTIFIER);
39-
$mainLanguageCode = self::ENG_US;
40-
$contentCreateStruct = $contentService->newContentCreateStruct($contentType, $mainLanguageCode);
41-
$contentCreateStruct->setField('title', 'Test title');
42-
43-
$contentDraft = $contentService->createContent(
44-
$contentCreateStruct,
45-
[
46-
$locationService->newLocationCreateStruct(2),
47-
]
48-
);
37+
$contentDraft = $this->createEngDraft();
4938
$publishedContent = $contentService->publishVersion($contentDraft->getVersionInfo());
5039

5140
// Creating a draft in ger-DE language with the only field updated being 'title'
@@ -84,6 +73,116 @@ public function testCopyNonTranslatableFieldsFromPublishedVersionToDraft(): void
8473
self::assertSame($expectedBodyValue, $bodyFieldValue->text);
8574
}
8675

76+
/**
77+
* @throws \eZ\Publish\API\Repository\Exceptions\Exception
78+
*/
79+
public function testCopyNonTranslatableFieldsTwoParallelDrafts(): void
80+
{
81+
$this->createNonTranslatableContentType();
82+
83+
$contentService = self::getContentService();
84+
85+
// Creating start content in eng-US language
86+
$contentDraft = $this->createEngDraft();
87+
$publishedContent = $contentService->publishVersion($contentDraft->getVersionInfo());
88+
89+
// Creating two drafts at the same time
90+
$usDraft = $contentService->createContentDraft($publishedContent->contentInfo);
91+
$gerDraft = $contentService->createContentDraft($publishedContent->contentInfo);
92+
93+
// Publishing the draft in eng-US language
94+
$contentUpdateStruct = new ContentUpdateStruct([
95+
'initialLanguageCode' => self::ENG_US,
96+
'fields' => $usDraft->getFields(),
97+
]);
98+
$contentUpdateStruct->setField('title', 'Title v2', self::ENG_US);
99+
$contentUpdateStruct->setField('body', 'Nontranslatable body v2', self::ENG_US);
100+
$usContent = $contentService->updateContent($usDraft->getVersionInfo(), $contentUpdateStruct);
101+
$contentService->publishVersion($usContent->getVersionInfo());
102+
103+
// Publishing the draft in ger-DE language
104+
$contentUpdateStruct = new ContentUpdateStruct([
105+
'initialLanguageCode' => self::GER_DE,
106+
'fields' => $gerDraft->getFields(),
107+
]);
108+
$contentUpdateStruct->setField('title', 'Title ger', self::GER_DE);
109+
$gerContent = $contentService->updateContent($gerDraft->getVersionInfo(), $contentUpdateStruct);
110+
$contentService->publishVersion($gerContent->getVersionInfo());
111+
112+
// Loading main content
113+
$mainPublishedContent = $contentService->loadContent($gerContent->id);
114+
$bodyFieldValue = $mainPublishedContent->getField('body')->getValue();
115+
116+
self::assertSame('Nontranslatable body v2', $bodyFieldValue->text);
117+
}
118+
119+
/**
120+
* @throws \eZ\Publish\API\Repository\Exceptions\Exception
121+
*/
122+
public function testCopyNonTranslatableFieldsOverridesNonMainLanguageDrafts(): void
123+
{
124+
$this->createNonTranslatableContentType();
125+
126+
$contentService = self::getContentService();
127+
128+
// Creating start content in eng-US language
129+
$contentDraft = $this->createEngDraft();
130+
$publishedContent = $contentService->publishVersion($contentDraft->getVersionInfo());
131+
132+
// Creating a draft in ger-DE language with the only field updated being 'title'
133+
$gerDraft = $contentService->createContentDraft($publishedContent->contentInfo);
134+
135+
$contentUpdateStruct = new ContentUpdateStruct([
136+
'initialLanguageCode' => self::GER_DE,
137+
'fields' => $contentDraft->getFields(),
138+
]);
139+
140+
$contentUpdateStruct->setField('title', 'Folder GER', self::GER_DE);
141+
$gerContent = $contentService->updateContent($gerDraft->getVersionInfo(), $contentUpdateStruct);
142+
$publishedContent = $contentService->publishVersion($gerContent->getVersionInfo());
143+
144+
// Updating non-translatable field in eng-US language (allowed) and publishing it
145+
$engContent = $contentService->createContentDraft($publishedContent->contentInfo);
146+
147+
$contentUpdateStruct = new ContentUpdateStruct([
148+
'initialLanguageCode' => self::ENG_US,
149+
'fields' => $contentDraft->getFields(),
150+
]);
151+
152+
$expectedBodyValue = 'Non-translatable value';
153+
$contentUpdateStruct->setField('title', 'Title v2', self::ENG_US);
154+
$contentUpdateStruct->setField('body', $expectedBodyValue, self::ENG_US);
155+
156+
$engContent = $contentService->updateContent($engContent->getVersionInfo(), $contentUpdateStruct);
157+
$contentService->publishVersion($engContent->getVersionInfo());
158+
159+
// Loading content in ger-DE language
160+
$mainPublishedContent = $contentService->loadContent($engContent->id, ['ger-DE']);
161+
$bodyFieldValue = $mainPublishedContent->getField('body')->getValue();
162+
163+
self::assertSame($expectedBodyValue, $bodyFieldValue->text);
164+
}
165+
166+
private function createEngDraft(): Content
167+
{
168+
$contentService = self::getContentService();
169+
$contentTypeService = self::getContentTypeService();
170+
$locationService = self::getLocationService();
171+
172+
$contentType = $contentTypeService->loadContentTypeByIdentifier(self::CONTENT_TYPE_IDENTIFIER);
173+
$mainLanguageCode = self::ENG_US;
174+
$contentCreateStruct = $contentService->newContentCreateStruct($contentType, $mainLanguageCode);
175+
$contentCreateStruct->setField('title', 'Test title');
176+
$contentCreateStruct->setField('body', 'Test body');
177+
178+
return $contentService->createContent(
179+
$contentCreateStruct,
180+
[
181+
$locationService->newLocationCreateStruct(2),
182+
]
183+
);
184+
}
185+
87186
private function createNonTranslatableContentType(): void
88187
{
89188
$permissionResolver = self::getPermissionResolver();

0 commit comments

Comments
 (0)