Skip to content

Commit 99ea8d2

Browse files
authored
IBX-5068: Fixed mapping fields for update to account for language
For more details see https://issues.ibexa.co/browse/IBX-5068 and #368 Key changes: * Fixed mapping fields for update to account for language * Made `languageCode` in Field nullable
1 parent 0a108ea commit 99ea8d2

File tree

3 files changed

+119
-3
lines changed

3 files changed

+119
-3
lines changed

eZ/Publish/API/Repository/Values/Content/Field.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class Field extends ValueObject
4747
/**
4848
* the language code.
4949
*
50-
* @var string
50+
* @var string|null
5151
*/
5252
protected $languageCode;
5353

@@ -76,7 +76,7 @@ public function getValue()
7676
return $this->value;
7777
}
7878

79-
public function getLanguageCode(): string
79+
public function getLanguageCode(): ?string
8080
{
8181
return $this->languageCode;
8282
}

eZ/Publish/Core/Repository/Mapper/ContentMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public function getFieldsForUpdate(array $updatedFields, Content $content): arra
300300

301301
$fieldType = $this->fieldTypeRegistry->getFieldType($fieldDefinition->fieldTypeIdentifier);
302302

303-
$field = $content->getField($updatedField->fieldDefIdentifier);
303+
$field = $content->getField($updatedField->fieldDefIdentifier, $updatedField->getLanguageCode());
304304
$updatedFieldValue = $this->getFieldValueForUpdate(
305305
$updatedField,
306306
$field,
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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\Core\Repository\Mapper;
10+
11+
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
12+
use eZ\Publish\API\Repository\Values\Content\Field;
13+
use eZ\Publish\API\Repository\Values\Content\VersionInfo as APIVersionInfo;
14+
use eZ\Publish\Core\FieldType\FieldTypeRegistry;
15+
use eZ\Publish\Core\FieldType\TextLine;
16+
use eZ\Publish\Core\Persistence\Cache\ContentLanguageHandler;
17+
use eZ\Publish\Core\Repository\Mapper\ContentMapper;
18+
use eZ\Publish\Core\Repository\Values\Content\Content;
19+
use eZ\Publish\Core\Repository\Values\Content\VersionInfo;
20+
use eZ\Publish\Core\Repository\Values\ContentType\ContentType;
21+
use eZ\Publish\Core\Repository\Values\ContentType\FieldDefinition;
22+
use eZ\Publish\Core\Repository\Values\ContentType\FieldDefinitionCollection;
23+
use PHPUnit\Framework\TestCase;
24+
25+
final class ContentMapperTest extends TestCase
26+
{
27+
/** @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\Handler|\PHPUnit\Framework\MockObject\MockObject */
28+
private $contentLanguageHandler;
29+
30+
/** @var \eZ\Publish\Core\FieldType\FieldTypeRegistry|\PHPUnit\Framework\MockObject\MockObject */
31+
private $fieldTypeRegistry;
32+
33+
/** @var \eZ\Publish\Core\Repository\Mapper\ContentMapper */
34+
private $contentMapper;
35+
36+
protected function setUp(): void
37+
{
38+
$this->contentLanguageHandler = $this->createMock(ContentLanguageHandler::class);
39+
$this->fieldTypeRegistry = $this->createMock(FieldTypeRegistry::class);
40+
41+
$this->contentMapper = new ContentMapper(
42+
$this->contentLanguageHandler,
43+
$this->fieldTypeRegistry
44+
);
45+
}
46+
47+
/**
48+
* @covers \eZ\Publish\Core\Repository\ContentService::updateContent
49+
*
50+
* @throws \eZ\Publish\Core\Base\Exceptions\ContentValidationException
51+
*/
52+
public function testUpdateContentGetsProperFieldsToUpdate(): void
53+
{
54+
$updatedField = new Field(
55+
[
56+
'id' => 1234,
57+
'value' => new TextLine\Value('updated one'),
58+
'languageCode' => 'fre-FR',
59+
'fieldDefIdentifier' => 'name',
60+
'fieldTypeIdentifier' => 'ezstring',
61+
]
62+
);
63+
$updatedField2 = new Field(
64+
[
65+
'id' => 1235,
66+
'value' => new TextLine\Value('two'),
67+
'languageCode' => 'fre-FR',
68+
'fieldDefIdentifier' => 'name',
69+
'fieldTypeIdentifier' => 'ezstring',
70+
]
71+
);
72+
$updatedFields = [$updatedField, $updatedField2];
73+
74+
$versionInfo = new VersionInfo(
75+
[
76+
'contentInfo' => new ContentInfo(['id' => 422, 'mainLanguageCode' => 'eng-GB']),
77+
'versionNo' => 7,
78+
'status' => APIVersionInfo::STATUS_DRAFT,
79+
]
80+
);
81+
82+
$content = new Content(
83+
[
84+
'versionInfo' => $versionInfo,
85+
'internalFields' => [
86+
new Field(
87+
[
88+
'value' => new TextLine\Value('one'),
89+
'languageCode' => 'eng-GB',
90+
'fieldDefIdentifier' => 'name',
91+
'fieldTypeIdentifier' => 'ezstring',
92+
]
93+
),
94+
$updatedField2,
95+
],
96+
'contentType' => new ContentType([
97+
'fieldDefinitions' => new FieldDefinitionCollection([
98+
new FieldDefinition([
99+
'identifier' => 'name',
100+
'fieldTypeIdentifier' => 'ezstring',
101+
]),
102+
]),
103+
]),
104+
]
105+
);
106+
107+
$this->fieldTypeRegistry
108+
->expects(self::any())
109+
->method('getFieldType')
110+
->willReturn(new TextLine\Type());
111+
112+
$fieldForUpdate = $this->contentMapper->getFieldsForUpdate($updatedFields, $content);
113+
114+
self::assertSame([$updatedField], $fieldForUpdate);
115+
}
116+
}

0 commit comments

Comments
 (0)