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-6495: Fixed saving non-translatable fields in main language #74

Merged
merged 3 commits into from
Sep 28, 2023
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
6 changes: 5 additions & 1 deletion src/lib/Data/Mapper/ContentUpdateMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,18 @@ public function mapToFormData(ValueObject $contentDraft, array $params = [])
$data->initialLanguageCode = $languageCode;

$fields = $contentDraft->getFieldsByLanguage($languageCode);
$mainLanguageCode = $contentDraft->getVersionInfo()->getContentInfo()->getMainLanguage()->getLanguageCode();

foreach ($params['contentType']->fieldDefinitions as $fieldDef) {
$isNonTranslatable = $fieldDef->isTranslatable === false;
$field = $fields[$fieldDef->identifier];
$shouldUseCurrentFieldValue = $isNonTranslatable
&& isset($mappedCurrentFields[$fieldDef->identifier])
&& $mainLanguageCode !== $languageCode;
$data->addFieldData(new FieldData([
'fieldDefinition' => $fieldDef,
'field' => $field,
'value' => $isNonTranslatable && isset($mappedCurrentFields[$fieldDef->identifier])
'value' => $shouldUseCurrentFieldValue
? $mappedCurrentFields[$fieldDef->identifier]->value
: $field->value,
]));
Expand Down
103 changes: 74 additions & 29 deletions tests/lib/Data/Mapper/ContentUpdateMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,58 @@ public function testMapToFormData(): void
'value' => $expectedShortName = 'Nontranslateable short name',
]),
];
$content = new Content([

$newName = 'GER name';
$newShortName = '';
$content = $this->getContent($newName, $newShortName, 'ger-DE');

$data = (new ContentUpdateMapper())->mapToFormData($content, [
'languageCode' => 'ger-DE',
'contentType' => $this->getContentType(),
'currentFields' => $currentFields,
]);

$fieldsData = $data->fieldsData;

self::assertSame($newName, $fieldsData['name']->value);
self::assertSame($expectedShortName, $fieldsData['short_name']->value);
}

public function testMapToFormDataWithTheSameLanguage(): void
{
$currentFields = [
new APIField([
'fieldDefIdentifier' => 'name',
'fieldTypeIdentifier' => 'ezstring',
'languageCode' => 'eng-GB',
'value' => 'Name',
]),
new APIField([
'fieldDefIdentifier' => 'short_name',
'fieldTypeIdentifier' => 'ezstring',
'languageCode' => 'eng-GB',
'value' => 'Short name',
]),
];

$newName = 'New name';
$newShortName = 'New short name';
$content = $this->getContent($newName, $newShortName, 'eng-GB');

$data = (new ContentUpdateMapper())->mapToFormData($content, [
'languageCode' => 'eng-GB',
'contentType' => $this->getContentType(),
'currentFields' => $currentFields,
]);

$fieldsData = $data->fieldsData;

self::assertSame($newName, $fieldsData['name']->value);
self::assertSame($newShortName, $fieldsData['short_name']->value);
}

private function getContent(string $name, string $shortName, string $languageCode): Content {
return new Content([
'versionInfo' => new VersionInfo([
'contentInfo' => new ContentInfo([
'remoteId' => 'foo',
Expand All @@ -63,46 +114,40 @@ public function testMapToFormData(): void
]),
]),
]),
'contentType' => $contentType = new ContentType([
'identifier' => 'folder',
'fieldDefinitions' => new FieldDefinitionCollection([
new FieldDefinition([
'identifier' => 'name',
'isTranslatable' => true,
'defaultValue' => '',
]),
new FieldDefinition([
'identifier' => 'short_name',
'isTranslatable' => false,
'defaultValue' => '',
]),
]),
]),
'contentType' => $this->getContentType(),
'internalFields' => [
new APIField([
'fieldDefIdentifier' => 'name',
'fieldTypeIdentifier' => 'ezstring',
'languageCode' => 'ger-DE',
'value' => $expectedName = 'GER name',
'languageCode' => $languageCode,
'value' => $name,
]),
new APIField([
'fieldDefIdentifier' => 'short_name',
'fieldTypeIdentifier' => 'ezstring',
'languageCode' => 'ger-DE',
'value' => '',
'languageCode' => $languageCode,
'value' => $shortName,
]),
],
]);
}

$data = (new ContentUpdateMapper())->mapToFormData($content, [
'languageCode' => 'ger-DE',
'contentType' => $contentType,
'currentFields' => $currentFields,
private function getContentType(): ContentType
{
return new ContentType([
'identifier' => 'folder',
'fieldDefinitions' => new FieldDefinitionCollection([
new FieldDefinition([
'identifier' => 'name',
'isTranslatable' => true,
'defaultValue' => '',
]),
new FieldDefinition([
'identifier' => 'short_name',
'isTranslatable' => false,
'defaultValue' => '',
]),
]),
]);

$fieldsData = $data->fieldsData;

self::assertSame($expectedName, $fieldsData['name']->value);
self::assertSame($expectedShortName, $fieldsData['short_name']->value);
}
}