Skip to content

Commit 17e78be

Browse files
authored
IBX-6833: Fixed copying empty fields from a published version
For more details see https://issues.ibexa.co/browse/IBX-6833 and #390 Key changes: * Fixed skipping copying empty fields when copying translations from published version * [Tests] Added integration coverage for the use case
1 parent c88c397 commit 17e78be

File tree

2 files changed

+122
-3
lines changed

2 files changed

+122
-3
lines changed

eZ/Publish/Core/Repository/ContentService.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,8 @@ public function updateContent(APIVersionInfo $versionInfo, APIContentUpdateStruc
12941294
protected function internalUpdateContent(
12951295
APIVersionInfo $versionInfo,
12961296
APIContentUpdateStruct $contentUpdateStruct,
1297-
?array $fieldIdentifiersToValidate = null
1297+
?array $fieldIdentifiersToValidate = null,
1298+
bool $copyEmptyField = false
12981299
): Content {
12991300
$contentUpdateStruct = clone $contentUpdateStruct;
13001301

@@ -1385,7 +1386,7 @@ protected function internalUpdateContent(
13851386
);
13861387
$fieldValues[$fieldDefinition->identifier][$languageCode] = $fieldValue;
13871388

1388-
if ($isRetained || $isCopied || ($isLanguageNew && $isEmpty) || $isProcessed) {
1389+
if ($isRetained || $isCopied || ($isLanguageNew && $isEmpty && !$copyEmptyField) || $isProcessed) {
13891390
continue;
13901391
}
13911392

@@ -1692,7 +1693,7 @@ protected function copyTranslationsFromPublishedVersion(APIVersionInfo $versionI
16921693
$updateStruct->setField($fallbackField->fieldDefIdentifier, $fallbackField->value, $fallbackField->languageCode);
16931694
}
16941695

1695-
$this->internalUpdateContent($versionInfo, $updateStruct);
1696+
$this->internalUpdateContent($versionInfo, $updateStruct, null, true);
16961697
}
16971698

16981699
protected function fieldValuesAreEqual(FieldType $fieldType, Value $value1, Value $value2): bool
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Tests\Integration\Core\Repository\ContentService;
10+
11+
use DateTime;
12+
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinitionCreateStruct;
13+
use eZ\Publish\Core\FieldType\TextLine;
14+
use eZ\Publish\Core\Repository\Values\Content\ContentUpdateStruct;
15+
use Ibexa\Tests\Integration\Core\RepositoryTestCase;
16+
17+
/**
18+
* @covers \eZ\Publish\API\Repository\ContentService
19+
*/
20+
final class CopyTranslationsFromPublishedVersionTest extends RepositoryTestCase
21+
{
22+
private const ENG_LANGUAGE_CODE = 'eng-GB';
23+
private const GER_LANGUAGE_CODE = 'ger-DE';
24+
private const US_LANGUAGE_CODE = 'eng-US';
25+
private const CONTENT_TYPE_IDENTIFIER = 'custom';
26+
27+
/**
28+
* @throws \eZ\Publish\API\Repository\Exceptions\Exception
29+
*/
30+
public function testCopyTranslationsFromPublishedVersionCopiesEmptyValues(): void
31+
{
32+
$this->createContentType();
33+
34+
$contentService = self::getContentService();
35+
$contentTypeService = self::getContentTypeService();
36+
$locationService = self::getLocationService();
37+
38+
// Creating and publishing content in eng-GB language
39+
$contentType = $contentTypeService->loadContentTypeByIdentifier(self::CONTENT_TYPE_IDENTIFIER);
40+
$mainLanguageCode = self::ENG_LANGUAGE_CODE;
41+
$contentCreateStruct = $contentService->newContentCreateStruct($contentType, $mainLanguageCode);
42+
$contentCreateStruct->setField('title', 'Test title');
43+
44+
$contentDraft = $contentService->createContent(
45+
$contentCreateStruct,
46+
[
47+
$locationService->newLocationCreateStruct(2),
48+
],
49+
);
50+
$publishedContent = $contentService->publishVersion($contentDraft->getVersionInfo());
51+
52+
// Creating a draft and publishing in ger-DE language with empty 'title' field
53+
$gerDraft = $contentService->createContentDraft($publishedContent->contentInfo);
54+
$usDraft = $contentService->createContentDraft($publishedContent->contentInfo);
55+
56+
$contentUpdateStruct = new ContentUpdateStruct([
57+
'initialLanguageCode' => self::GER_LANGUAGE_CODE,
58+
]);
59+
$contentUpdateStruct->setField('title', null);
60+
$gerContent = $contentService->updateContent($gerDraft->getVersionInfo(), $contentUpdateStruct);
61+
$contentService->publishVersion($gerContent->getVersionInfo(), [self::GER_LANGUAGE_CODE]);
62+
63+
// Creating a draft and publishing in eng-US language with empty 'title' field
64+
$contentUpdateStruct = new ContentUpdateStruct([
65+
'initialLanguageCode' => self::US_LANGUAGE_CODE,
66+
]);
67+
$contentUpdateStruct->setField('title', null);
68+
$usContent = $contentService->updateContent($usDraft->getVersionInfo(), $contentUpdateStruct);
69+
$publishedUsContent = $contentService->publishVersion($usContent->getVersionInfo(), [self::US_LANGUAGE_CODE]);
70+
71+
$usFieldValueInUsContent = $publishedUsContent->getField('title', self::US_LANGUAGE_CODE)->getValue();
72+
self::assertInstanceOf(TextLine\Value::class, $usFieldValueInUsContent);
73+
self::assertSame('', $usFieldValueInUsContent->text);
74+
75+
$gerFieldValueInUsContent = $publishedUsContent->getField('title', self::GER_LANGUAGE_CODE)->getValue();
76+
self::assertInstanceOf(TextLine\Value::class, $gerFieldValueInUsContent);
77+
self::assertSame('', $gerFieldValueInUsContent->text);
78+
}
79+
80+
private function createContentType(): void
81+
{
82+
$permissionResolver = self::getPermissionResolver();
83+
$contentTypeService = self::getContentTypeService();
84+
85+
$typeCreate = $contentTypeService->newContentTypeCreateStruct(self::CONTENT_TYPE_IDENTIFIER);
86+
87+
$typeCreate->mainLanguageCode = 'eng-GB';
88+
$typeCreate->remoteId = '1234567890abcdef';
89+
$typeCreate->urlAliasSchema = '<title>';
90+
$typeCreate->nameSchema = '<title>';
91+
$typeCreate->names = [
92+
self::ENG_LANGUAGE_CODE => 'Some content type',
93+
];
94+
$typeCreate->descriptions = [
95+
self::ENG_LANGUAGE_CODE => '',
96+
];
97+
$typeCreate->creatorId = $permissionResolver->getCurrentUserReference()->getUserId();
98+
$typeCreate->creationDate = new DateTime();
99+
100+
$typeCreate->addFieldDefinition(
101+
new FieldDefinitionCreateStruct(
102+
[
103+
'fieldTypeIdentifier' => 'ezstring',
104+
'identifier' => 'title',
105+
'names' => ['eng-GB' => 'Title'],
106+
'isRequired' => false,
107+
'isTranslatable' => true,
108+
],
109+
)
110+
);
111+
112+
$contentTypeDraft = $contentTypeService->createContentType(
113+
$typeCreate,
114+
[$contentTypeService->loadContentTypeGroupByIdentifier('Content')],
115+
);
116+
$contentTypeService->publishContentTypeDraft($contentTypeDraft);
117+
}
118+
}

0 commit comments

Comments
 (0)