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

Merge branch '1.3' of ezsystems/ezplatform-kernel into 4.6 #365

Merged
merged 2 commits into from
May 7, 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
17 changes: 9 additions & 8 deletions src/lib/Repository/ContentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -1517,16 +1517,12 @@ protected function copyNonTranslatableFieldsFromPublishedVersion(APIContent $cur
$publishedContent = $this->internalLoadContentById($versionInfo->getContentInfo()->getId());
$publishedVersionInfo = $publishedContent->getVersionInfo();

if (
!$publishedVersionInfo->isPublished()
|| ($versionInfo->versionNo >= $publishedVersionInfo->versionNo)
) {
if (!$publishedVersionInfo->isPublished()) {
return;
}

$publishedContentFieldsInMainLanguage = $publishedContent->getFieldsByLanguage(
$publishedContent->getVersionInfo()->getContentInfo()->getMainLanguageCode()
);
$mainLanguageCode = $publishedContent->getVersionInfo()->getContentInfo()->getMainLanguageCode();
$publishedContentFieldsInMainLanguage = $publishedContent->getFieldsByLanguage($mainLanguageCode);

$fieldValues = [];
$persistenceFields = [];
Expand All @@ -1545,7 +1541,12 @@ protected function copyNonTranslatableFieldsFromPublishedVersion(APIContent $cur
$fieldDefinition->fieldTypeIdentifier
);

$newValue = $publishedContentFieldsInMainLanguage[$field->fieldDefIdentifier]->getValue();
$newValue = (
$versionInfo->versionNo >= $publishedVersionInfo->versionNo
&& $versionInfo->initialLanguageCode === $mainLanguageCode
)
? $field->getValue()
: $publishedContentFieldsInMainLanguage[$field->fieldDefIdentifier]->getValue();
$fieldValues[$fieldDefinition->identifier][$field->languageCode] = $newValue;

$persistenceFields[] = new SPIField(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Ibexa\Tests\Integration\Core\Repository\ContentService;

use DateTime;
use Ibexa\Contracts\Core\Repository\Values\Content\Content;
use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinitionCreateStruct;
use Ibexa\Core\Repository\Values\Content\ContentUpdateStruct;
use Ibexa\Tests\Integration\Core\RepositoryTestCase;
Expand All @@ -31,21 +32,9 @@ public function testCopyNonTranslatableFieldsFromPublishedVersionToDraft(): void
$this->createNonTranslatableContentType();

$contentService = self::getContentService();
$contentTypeService = self::getContentTypeService();
$locationService = self::getLocationService();

// Creating start content in eng-US language
$contentType = $contentTypeService->loadContentTypeByIdentifier(self::CONTENT_TYPE_IDENTIFIER);
$mainLanguageCode = self::ENG_US;
$contentCreateStruct = $contentService->newContentCreateStruct($contentType, $mainLanguageCode);
$contentCreateStruct->setField('title', 'Test title');

$contentDraft = $contentService->createContent(
$contentCreateStruct,
[
$locationService->newLocationCreateStruct(2),
]
);
$contentDraft = $this->createEngDraft();
$publishedContent = $contentService->publishVersion($contentDraft->getVersionInfo());

// Creating a draft in ger-DE language with the only field updated being 'title'
Expand Down Expand Up @@ -79,11 +68,123 @@ public function testCopyNonTranslatableFieldsFromPublishedVersionToDraft(): void

// Loading main content
$mainPublishedContent = $contentService->loadContent($engContent->id);
$bodyField = $mainPublishedContent->getField('body');
$bodyFieldValue = $bodyField !== null ? $bodyField->getValue() : null;

self::assertSame($expectedBodyValue, $bodyFieldValue->text);
}

/**
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\Exception
*/
public function testCopyNonTranslatableFieldsTwoParallelDrafts(): void
{
$this->createNonTranslatableContentType();

$contentService = self::getContentService();

// Creating start content in eng-US language
$contentDraft = $this->createEngDraft();
$publishedContent = $contentService->publishVersion($contentDraft->getVersionInfo());

// Creating two drafts at the same time
$usDraft = $contentService->createContentDraft($publishedContent->contentInfo);
$gerDraft = $contentService->createContentDraft($publishedContent->contentInfo);

// Publishing the draft in eng-US language
$contentUpdateStruct = new ContentUpdateStruct([
'initialLanguageCode' => self::ENG_US,
'fields' => $usDraft->getFields(),
]);
$contentUpdateStruct->setField('title', 'Title v2', self::ENG_US);
$contentUpdateStruct->setField('body', 'Nontranslatable body v2', self::ENG_US);
$usContent = $contentService->updateContent($usDraft->getVersionInfo(), $contentUpdateStruct);
$contentService->publishVersion($usContent->getVersionInfo());

// Publishing the draft in ger-DE language
$contentUpdateStruct = new ContentUpdateStruct([
'initialLanguageCode' => self::GER_DE,
'fields' => $gerDraft->getFields(),
]);
$contentUpdateStruct->setField('title', 'Title ger', self::GER_DE);
$gerContent = $contentService->updateContent($gerDraft->getVersionInfo(), $contentUpdateStruct);
$contentService->publishVersion($gerContent->getVersionInfo());

// Loading main content
$mainPublishedContent = $contentService->loadContent($gerContent->id);
$bodyFieldValue = $mainPublishedContent->getField('body')->getValue();

self::assertSame('Nontranslatable body v2', $bodyFieldValue->text);
}

/**
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\Exception
*/
public function testCopyNonTranslatableFieldsOverridesNonMainLanguageDrafts(): void
{
$this->createNonTranslatableContentType();

$contentService = self::getContentService();

// Creating start content in eng-US language
$contentDraft = $this->createEngDraft();
$publishedContent = $contentService->publishVersion($contentDraft->getVersionInfo());

// Creating a draft in ger-DE language with the only field updated being 'title'
$gerDraft = $contentService->createContentDraft($publishedContent->contentInfo);

$contentUpdateStruct = new ContentUpdateStruct([
'initialLanguageCode' => self::GER_DE,
'fields' => $contentDraft->getFields(),
]);

$contentUpdateStruct->setField('title', 'Folder GER', self::GER_DE);
$gerContent = $contentService->updateContent($gerDraft->getVersionInfo(), $contentUpdateStruct);
$publishedContent = $contentService->publishVersion($gerContent->getVersionInfo());

// Updating non-translatable field in eng-US language (allowed) and publishing it
$engContent = $contentService->createContentDraft($publishedContent->contentInfo);

$contentUpdateStruct = new ContentUpdateStruct([
'initialLanguageCode' => self::ENG_US,
'fields' => $contentDraft->getFields(),
]);

$expectedBodyValue = 'Non-translatable value';
$contentUpdateStruct->setField('title', 'Title v2', self::ENG_US);
$contentUpdateStruct->setField('body', $expectedBodyValue, self::ENG_US);

$engContent = $contentService->updateContent($engContent->getVersionInfo(), $contentUpdateStruct);
$contentService->publishVersion($engContent->getVersionInfo());

// Loading content in ger-DE language
$mainPublishedContent = $contentService->loadContent($engContent->id, ['ger-DE']);
$bodyField = $mainPublishedContent->getField('body');
$bodyFieldValue = $bodyField !== null ? $bodyField->getValue() : null;

self::assertSame($expectedBodyValue, $bodyFieldValue->text);
}

private function createEngDraft(): Content
{
$contentService = self::getContentService();
$contentTypeService = self::getContentTypeService();
$locationService = self::getLocationService();

$contentType = $contentTypeService->loadContentTypeByIdentifier(self::CONTENT_TYPE_IDENTIFIER);
$mainLanguageCode = self::ENG_US;
$contentCreateStruct = $contentService->newContentCreateStruct($contentType, $mainLanguageCode);
$contentCreateStruct->setField('title', 'Test title');
$contentCreateStruct->setField('body', 'Test body');

return $contentService->createContent(
$contentCreateStruct,
[
$locationService->newLocationCreateStruct(2),
]
);
}

private function createNonTranslatableContentType(): void
{
$permissionResolver = self::getPermissionResolver();
Expand Down
Loading