Skip to content

Commit a00f5f6

Browse files
committed
Merged branch '4.6'
2 parents f851d43 + 078d9d0 commit a00f5f6

File tree

7 files changed

+87
-54
lines changed

7 files changed

+87
-54
lines changed

phpstan-baseline.neon

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9241,17 +9241,17 @@ parameters:
92419241
path: src/lib/FieldType/ISBN/Type.php
92429242

92439243
-
9244-
message: "#^Binary operation \"\\*\" between int\\<\\-3, 3\\> and string results in an error\\.$#"
9244+
message: "#^Binary operation \"\\*\" between int\\<\\-3, 3\\> and non\\-empty\\-string results in an error\\.$#"
92459245
count: 1
92469246
path: src/lib/FieldType/ISBN/Type.php
92479247

92489248
-
9249-
message: "#^Binary operation \"\\*\" between int\\<min, 3\\> and string results in an error\\.$#"
9249+
message: "#^Binary operation \"\\*\" between int\\<min, 3\\> and non\\-empty\\-string results in an error\\.$#"
92509250
count: 1
92519251
path: src/lib/FieldType/ISBN/Type.php
92529252

92539253
-
9254-
message: "#^Binary operation \"\\*\" between string and int\\<1, 10\\> results in an error\\.$#"
9254+
message: "#^Binary operation \"\\*\" between non\\-empty\\-string and int\\<1, 10\\> results in an error\\.$#"
92559255
count: 1
92569256
path: src/lib/FieldType/ISBN/Type.php
92579257

@@ -14845,21 +14845,6 @@ parameters:
1484514845
count: 1
1484614846
path: src/lib/Persistence/Legacy/Bookmark/Mapper.php
1484714847

14848-
-
14849-
message: "#^Access to an undefined property object\\:\\:\\$languageCode\\.$#"
14850-
count: 1
14851-
path: src/lib/Persistence/Legacy/Content/FieldHandler.php
14852-
14853-
-
14854-
message: "#^Cannot access offset int\\|string on Ibexa\\\\Contracts\\\\Core\\\\Persistence\\\\Content\\\\Field\\.$#"
14855-
count: 3
14856-
path: src/lib/Persistence/Legacy/Content/FieldHandler.php
14857-
14858-
-
14859-
message: "#^Cannot access offset string on Ibexa\\\\Contracts\\\\Core\\\\Persistence\\\\Content\\\\Field\\.$#"
14860-
count: 5
14861-
path: src/lib/Persistence/Legacy/Content/FieldHandler.php
14862-
1486314848
-
1486414849
message: "#^Method Ibexa\\\\Core\\\\Persistence\\\\Legacy\\\\Content\\\\FieldHandler\\:\\:copyField\\(\\) has no return type specified\\.$#"
1486514850
count: 1
@@ -14910,11 +14895,6 @@ parameters:
1491014895
count: 1
1491114896
path: src/lib/Persistence/Legacy/Content/FieldHandler.php
1491214897

14913-
-
14914-
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\\>\\>\\.$#"
14915-
count: 1
14916-
path: src/lib/Persistence/Legacy/Content/FieldHandler.php
14917-
1491814898
-
1491914899
message: "#^Method Ibexa\\\\Core\\\\Persistence\\\\Legacy\\\\Content\\\\FieldHandler\\:\\:loadExternalFieldData\\(\\) has no return type specified\\.$#"
1492014900
count: 1
@@ -14935,18 +14915,13 @@ parameters:
1493514915
count: 1
1493614916
path: src/lib/Persistence/Legacy/Content/FieldHandler.php
1493714917

14938-
-
14939-
message: "#^Parameter \\#1 \\$field of method Ibexa\\\\Core\\\\Persistence\\\\Legacy\\\\Content\\\\FieldHandler\\:\\:updateField\\(\\) expects Ibexa\\\\Contracts\\\\Core\\\\Persistence\\\\Content\\\\Field, object given\\.$#"
14940-
count: 1
14941-
path: src/lib/Persistence/Legacy/Content/FieldHandler.php
14942-
1494314918
-
1494414919
message: "#^Parameter \\#2 \\$languageCode of method Ibexa\\\\Core\\\\Persistence\\\\Legacy\\\\Content\\\\FieldHandler\\:\\:getEmptyField\\(\\) expects string, int\\|string given\\.$#"
1494514920
count: 2
1494614921
path: src/lib/Persistence/Legacy/Content/FieldHandler.php
1494714922

1494814923
-
14949-
message: "#^Parameter \\#3 \\$originalField of method Ibexa\\\\Core\\\\Persistence\\\\Legacy\\\\Content\\\\FieldHandler\\:\\:updateCopiedField\\(\\) expects Ibexa\\\\Contracts\\\\Core\\\\Persistence\\\\Content\\\\Field, object given\\.$#"
14924+
message: "#^Property Ibexa\\\\Contracts\\\\Core\\\\Persistence\\\\Content\\\\Field\\:\\:\\$id \\(int\\) in isset\\(\\) is not nullable\\.$#"
1495014925
count: 1
1495114926
path: src/lib/Persistence/Legacy/Content/FieldHandler.php
1495214927

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

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

364371
// If no above conditions were met - do nothing
@@ -418,7 +425,7 @@ protected function updateCopiedField(Field $field, Field $updateField, Field $or
418425
* @param \Ibexa\Contracts\Core\Persistence\Content\Field[] $fields
419426
* @param array $languageCodes
420427
*
421-
* @return \Ibexa\Contracts\Core\Persistence\Content\Field[]
428+
* @return array<int, array<string, \Ibexa\Contracts\Core\Persistence\Content\Field>>
422429
*/
423430
protected function getFieldMap(array $fields, &$languageCodes = null)
424431
{

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
use Exception;
1111
use Ibexa\Contracts\Core\Persistence\Content;
1212
use Ibexa\Contracts\Core\Persistence\Content\CreateStruct;
13-
use Ibexa\Contracts\Core\Persistence\Content\Field;
1413
use Ibexa\Contracts\Core\Persistence\Content\Handler as BaseContentHandler;
14+
use Ibexa\Contracts\Core\Persistence\Content\Language\Handler as LanguageHandler;
1515
use Ibexa\Contracts\Core\Persistence\Content\MetadataUpdateStruct;
1616
use Ibexa\Contracts\Core\Persistence\Content\Relation;
1717
use Ibexa\Contracts\Core\Persistence\Content\Relation\CreateStruct as RelationCreateStruct;
@@ -86,6 +86,8 @@ class Handler implements BaseContentHandler
8686
*/
8787
protected $treeHandler;
8888

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

@@ -111,6 +113,7 @@ public function __construct(
111113
UrlAliasGateway $urlAliasGateway,
112114
ContentTypeHandler $contentTypeHandler,
113115
TreeHandler $treeHandler,
116+
LanguageHandler $languageHandler,
114117
LoggerInterface $logger = null
115118
) {
116119
$this->contentGateway = $contentGateway;
@@ -121,6 +124,7 @@ public function __construct(
121124
$this->urlAliasGateway = $urlAliasGateway;
122125
$this->contentTypeHandler = $contentTypeHandler;
123126
$this->treeHandler = $treeHandler;
127+
$this->languageHandler = $languageHandler;
124128
$this->logger = null !== $logger ? $logger : new NullLogger();
125129
}
126130

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

286+
// Persist virtual fields
287+
$contentType = $this->contentTypeHandler->load($content->versionInfo->contentInfo->contentTypeId);
288+
$this->fieldHandler->updateFields($content, new UpdateStruct([
289+
'initialLanguageId' => $this->languageHandler->loadByLanguageCode(
290+
$content->versionInfo->initialLanguageCode
291+
)->id,
292+
]), $contentType);
293+
280294
// Create relations for new version
281295
$relations = $this->contentGateway->loadRelations($contentId, $srcVersion);
282296
foreach ($relations as $relation) {
@@ -322,7 +336,9 @@ public function load($id, $version = null, array $translations = null)
322336
$this->contentGateway->loadVersionedNameData([[
323337
'id' => $id,
324338
'version' => $rows[0]['ezcontentobject_version_version'],
325-
]])
339+
]]),
340+
'ezcontentobject_',
341+
$translations
326342
);
327343
$content = $contentObjects[0];
328344
unset($rows, $contentObjects);
@@ -373,7 +389,9 @@ public function loadContentList(array $contentIds, array $translations = null):
373389
try {
374390
$contentList = $this->mapper->extractContentFromRows(
375391
$contentItemsRow,
376-
$contentItemNameData[$contentId]
392+
$contentItemNameData[$contentId],
393+
'ezcontentobject_',
394+
$translations
377395
);
378396
$contentItems[$contentId] = $contentList[0];
379397
} catch (Exception $e) {

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

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

@@ -227,7 +229,8 @@ public function extractContentFromRows(
227229

228230
$fieldDefinitions = $this->loadCachedVersionFieldDefinitionsPerLanguage(
229231
$rows,
230-
$prefix
232+
$prefix,
233+
$translations
231234
);
232235

233236
foreach ($rows as $row) {
@@ -295,9 +298,10 @@ private function buildContentObjects(
295298
$content->versionInfo = $versionInfo;
296299
$content->versionInfo->names = $names;
297300
$content->versionInfo->contentInfo = $contentInfo;
298-
$content->fields = array_values($fields[$contentId][$versionId]);
301+
$content->fields = array_values($fields[$contentId][$versionId] ?? []);
302+
303+
$missingVersionFieldDefinitions = $missingFieldDefinitions[$contentId][$versionId] ?? [];
299304

300-
$missingVersionFieldDefinitions = $missingFieldDefinitions[$contentId][$versionId];
301305
foreach ($missingVersionFieldDefinitions as $languageCode => $versionFieldDefinitions) {
302306
foreach ($versionFieldDefinitions as $fieldDefinition) {
303307
$event = $this->eventDispatcher->dispatch(
@@ -325,13 +329,16 @@ private function buildContentObjects(
325329
/**
326330
* @phpstan-param TRawContentRow[] $rows
327331
*
332+
* @param string[]|null $translations
333+
*
328334
* @phpstan-return TVersionedLanguageFieldDefinitionsMap
329335
*
330336
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
331337
*/
332338
private function loadCachedVersionFieldDefinitionsPerLanguage(
333339
array $rows,
334-
string $prefix
340+
string $prefix,
341+
?array $translations = null
335342
): array {
336343
$fieldDefinitions = [];
337344
$contentTypes = [];
@@ -347,12 +354,14 @@ private function loadCachedVersionFieldDefinitionsPerLanguage(
347354
continue;
348355
}
349356

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

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
@@ -24,6 +24,7 @@
2424
use Ibexa\Core\Persistence\Legacy\Content\FieldHandler;
2525
use Ibexa\Core\Persistence\Legacy\Content\Gateway as ContentGateway;
2626
use Ibexa\Core\Persistence\Legacy\Content\Handler;
27+
use Ibexa\Core\Persistence\Legacy\Content\Language\Handler as LanguageHandler;
2728
use Ibexa\Core\Persistence\Legacy\Content\Location\Gateway as LocationGateway;
2829
use Ibexa\Core\Persistence\Legacy\Content\Mapper;
2930
use Ibexa\Core\Persistence\Legacy\Content\TreeHandler;
@@ -111,6 +112,11 @@ class ContentHandlerTest extends TestCase
111112
*/
112113
protected $contentTypeHandlerMock;
113114

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

380388
$handler->expects(self::once())
381389
->method('load')
@@ -394,11 +402,18 @@ public function testCreateDraftFromVersion()
394402
[
395403
'names' => [],
396404
'versionNo' => 3,
405+
'contentInfo' => new ContentInfo(),
397406
]
398407
)
399408
)
400409
);
401410

411+
$languageHandlerMock->method('loadByLanguageCode')
412+
->willReturn(new Content\Language());
413+
414+
$contentTypeHandlerMock->method('load')
415+
->willReturn(new Type());
416+
402417
$gatewayMock->expects(self::once())
403418
->method('insertVersion')
404419
->with(
@@ -1516,7 +1531,8 @@ protected function getContentHandler()
15161531
$this->getSlugConverterMock(),
15171532
$this->getUrlAliasGatewayMock(),
15181533
$this->getContentTypeHandlerMock(),
1519-
$this->getTreeHandlerMock()
1534+
$this->getTreeHandlerMock(),
1535+
$this->getLanguageHandlerMock(),
15201536
);
15211537
}
15221538

@@ -1544,6 +1560,7 @@ protected function getPartlyMockedHandler(array $methods)
15441560
$this->getUrlAliasGatewayMock(),
15451561
$this->getContentTypeHandlerMock(),
15461562
$this->getTreeHandlerMock(),
1563+
$this->getLanguageHandlerMock(),
15471564
]
15481565
)
15491566
->getMock();
@@ -1577,6 +1594,18 @@ protected function getContentTypeHandlerMock()
15771594
return $this->contentTypeHandlerMock;
15781595
}
15791596

1597+
/**
1598+
* @return \PHPUnit\Framework\MockObject\MockObject&\Ibexa\Core\Persistence\Legacy\Content\Language\Handler
1599+
*/
1600+
protected function getLanguageHandlerMock(): LanguageHandler
1601+
{
1602+
if (!isset($this->languageHandlerMock)) {
1603+
$this->languageHandlerMock = $this->createMock(LanguageHandler::class);
1604+
}
1605+
1606+
return $this->languageHandlerMock;
1607+
}
1608+
15801609
/**
15811610
* Returns a FieldHandler mock.
15821611
*

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

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

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

816-
$content->fields = [
817-
$firstField,
818-
$secondField,
819-
$thirdField,
820-
];
813+
$content->fields[] = $contentField;
814+
}
821815

822816
return $content;
823817
}

0 commit comments

Comments
 (0)