Skip to content

Commit 4e7be1e

Browse files
committed
Merge remote-tracking branch 'ezsystems/1.3' into temp_1.3_to_4.6
2 parents 1208a1e + 9a62410 commit 4e7be1e

File tree

7 files changed

+84
-51
lines changed

7 files changed

+84
-51
lines changed

phpstan-baseline.neon

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14905,21 +14905,6 @@ parameters:
1490514905
count: 1
1490614906
path: src/lib/Persistence/Legacy/Bookmark/Mapper.php
1490714907

14908-
-
14909-
message: "#^Access to an undefined property object\\:\\:\\$languageCode\\.$#"
14910-
count: 1
14911-
path: src/lib/Persistence/Legacy/Content/FieldHandler.php
14912-
14913-
-
14914-
message: "#^Cannot access offset int\\|string on Ibexa\\\\Contracts\\\\Core\\\\Persistence\\\\Content\\\\Field\\.$#"
14915-
count: 3
14916-
path: src/lib/Persistence/Legacy/Content/FieldHandler.php
14917-
14918-
-
14919-
message: "#^Cannot access offset string on Ibexa\\\\Contracts\\\\Core\\\\Persistence\\\\Content\\\\Field\\.$#"
14920-
count: 5
14921-
path: src/lib/Persistence/Legacy/Content/FieldHandler.php
14922-
1492314908
-
1492414909
message: "#^Method Ibexa\\\\Core\\\\Persistence\\\\Legacy\\\\Content\\\\FieldHandler\\:\\:copyField\\(\\) has no return type specified\\.$#"
1492514910
count: 1
@@ -14970,11 +14955,6 @@ parameters:
1497014955
count: 1
1497114956
path: src/lib/Persistence/Legacy/Content/FieldHandler.php
1497214957

14973-
-
14974-
message: "#^Method Ibexa\\\\Core\\\\Persistence\\\\Legacy\\\\Content\\\\FieldHandler\\:\\:getFieldMap\\(\\) should return array\\<Ibexa\\\\Contracts\\\\Core\\\\Persistence\\\\Content\\\\Field\\> but returns array\\<int, array\\<string, Ibexa\\\\Contracts\\\\Core\\\\Persistence\\\\Content\\\\Field\\>\\>\\.$#"
14975-
count: 1
14976-
path: src/lib/Persistence/Legacy/Content/FieldHandler.php
14977-
1497814958
-
1497914959
message: "#^Method Ibexa\\\\Core\\\\Persistence\\\\Legacy\\\\Content\\\\FieldHandler\\:\\:loadExternalFieldData\\(\\) has no return type specified\\.$#"
1498014960
count: 1
@@ -14995,18 +14975,13 @@ parameters:
1499514975
count: 1
1499614976
path: src/lib/Persistence/Legacy/Content/FieldHandler.php
1499714977

14998-
-
14999-
message: "#^Parameter \\#1 \\$field of method Ibexa\\\\Core\\\\Persistence\\\\Legacy\\\\Content\\\\FieldHandler\\:\\:updateField\\(\\) expects Ibexa\\\\Contracts\\\\Core\\\\Persistence\\\\Content\\\\Field, object given\\.$#"
15000-
count: 1
15001-
path: src/lib/Persistence/Legacy/Content/FieldHandler.php
15002-
1500314978
-
1500414979
message: "#^Parameter \\#2 \\$languageCode of method Ibexa\\\\Core\\\\Persistence\\\\Legacy\\\\Content\\\\FieldHandler\\:\\:getEmptyField\\(\\) expects string, int\\|string given\\.$#"
1500514980
count: 2
1500614981
path: src/lib/Persistence/Legacy/Content/FieldHandler.php
1500714982

1500814983
-
15009-
message: "#^Parameter \\#3 \\$originalField of method Ibexa\\\\Core\\\\Persistence\\\\Legacy\\\\Content\\\\FieldHandler\\:\\:updateCopiedField\\(\\) expects Ibexa\\\\Contracts\\\\Core\\\\Persistence\\\\Content\\\\Field, object given\\.$#"
14984+
message: "#^Property Ibexa\\\\Contracts\\\\Core\\\\Persistence\\\\Content\\\\Field\\:\\:\\$id \\(int\\) in isset\\(\\) is not nullable\\.$#"
1501014985
count: 1
1501114986
path: src/lib/Persistence/Legacy/Content/FieldHandler.php
1501214987

src/lib/Persistence/Legacy/Content/FieldHandler.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,13 @@ public function updateFields(Content $content, UpdateStruct $updateStruct, Type
358358
// also update copied field data
359359
// Register for processing after all given fields are updated
360360
$nonTranslatableCopiesUpdateSet[$fieldDefinition->id][] = $languageCode;
361+
} elseif (isset($contentFieldMap[$fieldDefinition->id][$languageCode])) {
362+
$field = clone $contentFieldMap[$fieldDefinition->id][$languageCode];
363+
$field->versionNo = $content->versionInfo->versionNo;
364+
// Persist virtual field
365+
if (null === $field->id) {
366+
$this->createNewField($field, $content);
367+
}
361368
}
362369

363370
// If no above conditions were met - do nothing
@@ -417,7 +424,7 @@ protected function updateCopiedField(Field $field, Field $updateField, Field $or
417424
* @param \Ibexa\Contracts\Core\Persistence\Content\Field[] $fields
418425
* @param array $languageCodes
419426
*
420-
* @return \Ibexa\Contracts\Core\Persistence\Content\Field[]
427+
* @return array<int, array<string, \Ibexa\Contracts\Core\Persistence\Content\Field>>
421428
*/
422429
protected function getFieldMap(array $fields, &$languageCodes = null)
423430
{

src/lib/Persistence/Legacy/Content/Handler.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
use Exception;
1010
use Ibexa\Contracts\Core\Persistence\Content;
1111
use Ibexa\Contracts\Core\Persistence\Content\CreateStruct;
12-
use Ibexa\Contracts\Core\Persistence\Content\Field;
1312
use Ibexa\Contracts\Core\Persistence\Content\Handler as BaseContentHandler;
13+
use Ibexa\Contracts\Core\Persistence\Content\Language\Handler as LanguageHandler;
1414
use Ibexa\Contracts\Core\Persistence\Content\MetadataUpdateStruct;
1515
use Ibexa\Contracts\Core\Persistence\Content\Relation;
1616
use Ibexa\Contracts\Core\Persistence\Content\Relation\CreateStruct as RelationCreateStruct;
@@ -85,6 +85,8 @@ class Handler implements BaseContentHandler
8585
*/
8686
protected $treeHandler;
8787

88+
protected LanguageHandler $languageHandler;
89+
8890
/** @var \Psr\Log\LoggerInterface */
8991
private $logger;
9092

@@ -110,6 +112,7 @@ public function __construct(
110112
UrlAliasGateway $urlAliasGateway,
111113
ContentTypeHandler $contentTypeHandler,
112114
TreeHandler $treeHandler,
115+
LanguageHandler $languageHandler,
113116
LoggerInterface $logger = null
114117
) {
115118
$this->contentGateway = $contentGateway;
@@ -120,6 +123,7 @@ public function __construct(
120123
$this->urlAliasGateway = $urlAliasGateway;
121124
$this->contentTypeHandler = $contentTypeHandler;
122125
$this->treeHandler = $treeHandler;
126+
$this->languageHandler = $languageHandler;
123127
$this->logger = null !== $logger ? $logger : new NullLogger();
124128
}
125129

@@ -256,6 +260,8 @@ public function publish($contentId, $versionNo, MetadataUpdateStruct $metaDataUp
256260
* @param string|null $languageCode
257261
*
258262
* @return \Ibexa\Contracts\Core\Persistence\Content
263+
*
264+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
259265
*/
260266
public function createDraftFromVersion($contentId, $srcVersion, $userId, ?string $languageCode = null)
261267
{
@@ -276,6 +282,14 @@ public function createDraftFromVersion($contentId, $srcVersion, $userId, ?string
276282
// Clone fields from previous version and append them to the new one
277283
$this->fieldHandler->createExistingFieldsInNewVersion($content);
278284

285+
// Persist virtual fields
286+
$contentType = $this->contentTypeHandler->load($content->versionInfo->contentInfo->contentTypeId);
287+
$this->fieldHandler->updateFields($content, new UpdateStruct([
288+
'initialLanguageId' => $this->languageHandler->loadByLanguageCode(
289+
$content->versionInfo->initialLanguageCode
290+
)->id,
291+
]), $contentType);
292+
279293
// Create relations for new version
280294
$relations = $this->contentGateway->loadRelations($contentId, $srcVersion);
281295
foreach ($relations as $relation) {
@@ -321,7 +335,9 @@ public function load($id, $version = null, array $translations = null)
321335
$this->contentGateway->loadVersionedNameData([[
322336
'id' => $id,
323337
'version' => $rows[0]['ezcontentobject_version_version'],
324-
]])
338+
]]),
339+
'ezcontentobject_',
340+
$translations
325341
);
326342
$content = $contentObjects[0];
327343
unset($rows, $contentObjects);
@@ -372,7 +388,9 @@ public function loadContentList(array $contentIds, array $translations = null):
372388
try {
373389
$contentList = $this->mapper->extractContentFromRows(
374390
$contentItemsRow,
375-
$contentItemNameData[$contentId]
391+
$contentItemNameData[$contentId],
392+
'ezcontentobject_',
393+
$translations
376394
);
377395
$contentItems[$contentId] = $contentList[0];
378396
} catch (Exception $e) {

src/lib/Persistence/Legacy/Content/Mapper.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,15 @@ public function convertToStorageValue(Field $field)
203203
* @param array<array<string, scalar>> $rows
204204
* @param array<array<string, scalar>> $nameRows
205205
* @param string $prefix
206+
* @param array<string>|null $translations
206207
*
207208
* @return \Ibexa\Contracts\Core\Persistence\Content[]
208209
*/
209210
public function extractContentFromRows(
210211
array $rows,
211212
array $nameRows,
212-
string $prefix = 'ezcontentobject_'
213+
string $prefix = 'ezcontentobject_',
214+
?array $translations = null
213215
): array {
214216
$versionedNameData = [];
215217

@@ -226,7 +228,8 @@ public function extractContentFromRows(
226228

227229
$fieldDefinitions = $this->loadCachedVersionFieldDefinitionsPerLanguage(
228230
$rows,
229-
$prefix
231+
$prefix,
232+
$translations
230233
);
231234

232235
foreach ($rows as $row) {
@@ -294,9 +297,10 @@ private function buildContentObjects(
294297
$content->versionInfo = $versionInfo;
295298
$content->versionInfo->names = $names;
296299
$content->versionInfo->contentInfo = $contentInfo;
297-
$content->fields = array_values($fields[$contentId][$versionId]);
300+
$content->fields = array_values($fields[$contentId][$versionId] ?? []);
301+
302+
$missingVersionFieldDefinitions = $missingFieldDefinitions[$contentId][$versionId] ?? [];
298303

299-
$missingVersionFieldDefinitions = $missingFieldDefinitions[$contentId][$versionId];
300304
foreach ($missingVersionFieldDefinitions as $languageCode => $versionFieldDefinitions) {
301305
foreach ($versionFieldDefinitions as $fieldDefinition) {
302306
$event = $this->eventDispatcher->dispatch(
@@ -324,13 +328,16 @@ private function buildContentObjects(
324328
/**
325329
* @phpstan-param TRawContentRow[] $rows
326330
*
331+
* @param string[]|null $translations
332+
*
327333
* @phpstan-return TVersionedLanguageFieldDefinitionsMap
328334
*
329335
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
330336
*/
331337
private function loadCachedVersionFieldDefinitionsPerLanguage(
332338
array $rows,
333-
string $prefix
339+
string $prefix,
340+
?array $translations = null
334341
): array {
335342
$fieldDefinitions = [];
336343
$contentTypes = [];
@@ -346,12 +353,14 @@ private function loadCachedVersionFieldDefinitionsPerLanguage(
346353
continue;
347354
}
348355

349-
$languageCodes = $this->extractLanguageCodesFromMask($languageMask, $allLanguages);
356+
$allLanguagesCodes = $this->extractLanguageCodesFromMask($languageMask, $allLanguages);
357+
$languageCodes = empty($translations) ? $allLanguagesCodes : array_intersect($translations, $allLanguagesCodes);
350358
$contentTypes[$contentTypeId] = $contentTypes[$contentTypeId] ?? $this->contentTypeHandler->load($contentTypeId);
351359
$contentType = $contentTypes[$contentTypeId];
352360
foreach ($contentType->fieldDefinitions as $fieldDefinition) {
353361
foreach ($languageCodes as $languageCode) {
354362
$id = (int)$fieldDefinition->id;
363+
$languageCode = (string)$languageCode;
355364
$fieldDefinitions[$contentId][$versionId][$languageCode][$id] = $fieldDefinition;
356365
}
357366
}

src/lib/Resources/settings/storage_engines/legacy/content.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,6 @@ services:
6868
- '@ibexa.persistence.legacy.url_alias.gateway'
6969
- '@ibexa.spi.persistence.legacy.content_type.handler'
7070
- '@Ibexa\Core\Persistence\Legacy\Content\TreeHandler'
71+
- '@ibexa.spi.persistence.legacy.language.handler'
7172
- "@logger"
7273
lazy: true

tests/lib/Persistence/Legacy/Content/ContentHandlerTest.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Ibexa\Core\Persistence\Legacy\Content\FieldHandler;
2424
use Ibexa\Core\Persistence\Legacy\Content\Gateway as ContentGateway;
2525
use Ibexa\Core\Persistence\Legacy\Content\Handler;
26+
use Ibexa\Core\Persistence\Legacy\Content\Language\Handler as LanguageHandler;
2627
use Ibexa\Core\Persistence\Legacy\Content\Location\Gateway as LocationGateway;
2728
use Ibexa\Core\Persistence\Legacy\Content\Mapper;
2829
use Ibexa\Core\Persistence\Legacy\Content\TreeHandler;
@@ -110,6 +111,11 @@ class ContentHandlerTest extends TestCase
110111
*/
111112
protected $contentTypeHandlerMock;
112113

114+
/**
115+
* @var \PHPUnit\Framework\MockObject\MockObject&\Ibexa\Core\Persistence\Legacy\Content\Language\Handler
116+
*/
117+
private LanguageHandler $languageHandlerMock;
118+
113119
/**
114120
* @todo Current method way to complex to test, refactor!
115121
*/
@@ -375,6 +381,8 @@ public function testCreateDraftFromVersion()
375381
$mapperMock = $this->getMapperMock();
376382
$gatewayMock = $this->getGatewayMock();
377383
$fieldHandlerMock = $this->getFieldHandlerMock();
384+
$languageHandlerMock = $this->getLanguageHandlerMock();
385+
$contentTypeHandlerMock = $this->getContentTypeHandlerMock();
378386

379387
$handler->expects($this->once())
380388
->method('load')
@@ -393,11 +401,18 @@ public function testCreateDraftFromVersion()
393401
[
394402
'names' => [],
395403
'versionNo' => 3,
404+
'contentInfo' => new ContentInfo(),
396405
]
397406
)
398407
)
399408
);
400409

410+
$languageHandlerMock->method('loadByLanguageCode')
411+
->willReturn(new Content\Language());
412+
413+
$contentTypeHandlerMock->method('load')
414+
->willReturn(new Type());
415+
401416
$gatewayMock->expects($this->once())
402417
->method('insertVersion')
403418
->with(
@@ -1509,7 +1524,8 @@ protected function getContentHandler()
15091524
$this->getSlugConverterMock(),
15101525
$this->getUrlAliasGatewayMock(),
15111526
$this->getContentTypeHandlerMock(),
1512-
$this->getTreeHandlerMock()
1527+
$this->getTreeHandlerMock(),
1528+
$this->getLanguageHandlerMock(),
15131529
);
15141530
}
15151531

@@ -1537,6 +1553,7 @@ protected function getPartlyMockedHandler(array $methods)
15371553
$this->getUrlAliasGatewayMock(),
15381554
$this->getContentTypeHandlerMock(),
15391555
$this->getTreeHandlerMock(),
1556+
$this->getLanguageHandlerMock(),
15401557
]
15411558
)
15421559
->getMock();
@@ -1570,6 +1587,18 @@ protected function getContentTypeHandlerMock()
15701587
return $this->contentTypeHandlerMock;
15711588
}
15721589

1590+
/**
1591+
* @return \PHPUnit\Framework\MockObject\MockObject&\Ibexa\Core\Persistence\Legacy\Content\Language\Handler
1592+
*/
1593+
protected function getLanguageHandlerMock(): LanguageHandler
1594+
{
1595+
if (!isset($this->languageHandlerMock)) {
1596+
$this->languageHandlerMock = $this->createMock(LanguageHandler::class);
1597+
}
1598+
1599+
return $this->languageHandlerMock;
1600+
}
1601+
15731602
/**
15741603
* Returns a FieldHandler mock.
15751604
*

tests/lib/Persistence/Legacy/Content/FieldHandlerTest.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ public function assertUpdateFieldsForInitialLanguage($storageHandlerUpdatesField
600600
$callNo = 0;
601601
$fieldValue = new FieldValue();
602602
$fieldsToCopy = [];
603-
foreach ([1, 2, 3] as $fieldDefinitionId) {
603+
foreach ([1, 2, 3] as $id => $fieldDefinitionId) {
604604
$field = new Field(
605605
[
606606
'fieldDefinitionId' => $fieldDefinitionId,
@@ -613,6 +613,7 @@ public function assertUpdateFieldsForInitialLanguage($storageHandlerUpdatesField
613613
// These fields are copied from main language
614614
if ($fieldDefinitionId == 2 || $fieldDefinitionId == 3) {
615615
$originalField = clone $field;
616+
$originalField->id = $fieldDefinitionId;
616617
$originalField->languageCode = 'eng-GB';
617618
$fieldsToCopy[] = [
618619
'copy' => clone $field,
@@ -803,20 +804,13 @@ protected function getContentSingleLanguageFixture()
803804
$field->value = new FieldValue();
804805
$field->languageCode = 'eng-GB';
805806

806-
$firstField = clone $field;
807-
$firstField->fieldDefinitionId = 1;
808-
809-
$secondField = clone $field;
810-
$secondField->fieldDefinitionId = 2;
811-
812-
$thirdField = clone $field;
813-
$thirdField->fieldDefinitionId = 3;
807+
foreach ([1, 2, 3] as $id) {
808+
$contentField = clone $field;
809+
$contentField->id = $id;
810+
$contentField->fieldDefinitionId = $id;
814811

815-
$content->fields = [
816-
$firstField,
817-
$secondField,
818-
$thirdField,
819-
];
812+
$content->fields[] = $contentField;
813+
}
820814

821815
return $content;
822816
}

0 commit comments

Comments
 (0)