Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-6833: Copying empty fields during translation copying shouldn't be skipped #390

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions eZ/Publish/Core/Repository/ContentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
alongosz marked this conversation as resolved.
Show resolved Hide resolved
continue;
}

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Integration\Core\Repository\ContentService;

use DateTime;
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinitionCreateStruct;
use eZ\Publish\Core\FieldType\TextLine;
use eZ\Publish\Core\Repository\Values\Content\ContentUpdateStruct;
use Ibexa\Tests\Integration\Core\RepositoryTestCase;

/**
* @covers \eZ\Publish\API\Repository\ContentService
*/
final class CopyTranslationsFromPublishedVersionTest extends RepositoryTestCase
{
private const ENG_LANGUAGE_CODE = 'eng-GB';
private const GER_LANGUAGE_CODE = 'ger-DE';
private const US_LANGUAGE_CODE = 'eng-US';
private const CONTENT_TYPE_IDENTIFIER = 'custom';

/**
* @throws \eZ\Publish\API\Repository\Exceptions\Exception
*/
public function testCopyTranslationsFromPublishedVersionCopiesEmptyValues(): void
{
$this->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 = '<title>';
$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);
}
}
Loading