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 #413

Merged
merged 2 commits into from
Aug 14, 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
27 changes: 1 addition & 26 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -14905,21 +14905,6 @@ parameters:
count: 1
path: src/lib/Persistence/Legacy/Bookmark/Mapper.php

-
message: "#^Access to an undefined property object\\:\\:\\$languageCode\\.$#"
count: 1
path: src/lib/Persistence/Legacy/Content/FieldHandler.php

-
message: "#^Cannot access offset int\\|string on Ibexa\\\\Contracts\\\\Core\\\\Persistence\\\\Content\\\\Field\\.$#"
count: 3
path: src/lib/Persistence/Legacy/Content/FieldHandler.php

-
message: "#^Cannot access offset string on Ibexa\\\\Contracts\\\\Core\\\\Persistence\\\\Content\\\\Field\\.$#"
count: 5
path: src/lib/Persistence/Legacy/Content/FieldHandler.php

-
message: "#^Method Ibexa\\\\Core\\\\Persistence\\\\Legacy\\\\Content\\\\FieldHandler\\:\\:copyField\\(\\) has no return type specified\\.$#"
count: 1
Expand Down Expand Up @@ -14970,11 +14955,6 @@ parameters:
count: 1
path: src/lib/Persistence/Legacy/Content/FieldHandler.php

-
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\\>\\>\\.$#"
count: 1
path: src/lib/Persistence/Legacy/Content/FieldHandler.php

-
message: "#^Method Ibexa\\\\Core\\\\Persistence\\\\Legacy\\\\Content\\\\FieldHandler\\:\\:loadExternalFieldData\\(\\) has no return type specified\\.$#"
count: 1
Expand All @@ -14995,18 +14975,13 @@ parameters:
count: 1
path: src/lib/Persistence/Legacy/Content/FieldHandler.php

-
message: "#^Parameter \\#1 \\$field of method Ibexa\\\\Core\\\\Persistence\\\\Legacy\\\\Content\\\\FieldHandler\\:\\:updateField\\(\\) expects Ibexa\\\\Contracts\\\\Core\\\\Persistence\\\\Content\\\\Field, object given\\.$#"
count: 1
path: src/lib/Persistence/Legacy/Content/FieldHandler.php

-
message: "#^Parameter \\#2 \\$languageCode of method Ibexa\\\\Core\\\\Persistence\\\\Legacy\\\\Content\\\\FieldHandler\\:\\:getEmptyField\\(\\) expects string, int\\|string given\\.$#"
count: 2
path: src/lib/Persistence/Legacy/Content/FieldHandler.php

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

Expand Down
9 changes: 8 additions & 1 deletion src/lib/Persistence/Legacy/Content/FieldHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,13 @@ public function updateFields(Content $content, UpdateStruct $updateStruct, Type
// also update copied field data
// Register for processing after all given fields are updated
$nonTranslatableCopiesUpdateSet[$fieldDefinition->id][] = $languageCode;
} elseif (isset($contentFieldMap[$fieldDefinition->id][$languageCode])) {
$field = clone $contentFieldMap[$fieldDefinition->id][$languageCode];
$field->versionNo = $content->versionInfo->versionNo;
// Persist virtual field
if (null === $field->id) {
$this->createNewField($field, $content);
}
}

// If no above conditions were met - do nothing
Expand Down Expand Up @@ -417,7 +424,7 @@ protected function updateCopiedField(Field $field, Field $updateField, Field $or
* @param \Ibexa\Contracts\Core\Persistence\Content\Field[] $fields
* @param array $languageCodes
*
* @return \Ibexa\Contracts\Core\Persistence\Content\Field[]
* @return array<int, array<string, \Ibexa\Contracts\Core\Persistence\Content\Field>>
*/
protected function getFieldMap(array $fields, &$languageCodes = null)
{
Expand Down
24 changes: 21 additions & 3 deletions src/lib/Persistence/Legacy/Content/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use Exception;
use Ibexa\Contracts\Core\Persistence\Content;
use Ibexa\Contracts\Core\Persistence\Content\CreateStruct;
use Ibexa\Contracts\Core\Persistence\Content\Field;
use Ibexa\Contracts\Core\Persistence\Content\Handler as BaseContentHandler;
use Ibexa\Contracts\Core\Persistence\Content\Language\Handler as LanguageHandler;
use Ibexa\Contracts\Core\Persistence\Content\MetadataUpdateStruct;
use Ibexa\Contracts\Core\Persistence\Content\Relation;
use Ibexa\Contracts\Core\Persistence\Content\Relation\CreateStruct as RelationCreateStruct;
Expand Down Expand Up @@ -85,6 +85,8 @@ class Handler implements BaseContentHandler
*/
protected $treeHandler;

protected LanguageHandler $languageHandler;

/** @var \Psr\Log\LoggerInterface */
private $logger;

Expand All @@ -110,6 +112,7 @@ public function __construct(
UrlAliasGateway $urlAliasGateway,
ContentTypeHandler $contentTypeHandler,
TreeHandler $treeHandler,
LanguageHandler $languageHandler,
LoggerInterface $logger = null
) {
$this->contentGateway = $contentGateway;
Expand All @@ -120,6 +123,7 @@ public function __construct(
$this->urlAliasGateway = $urlAliasGateway;
$this->contentTypeHandler = $contentTypeHandler;
$this->treeHandler = $treeHandler;
$this->languageHandler = $languageHandler;
$this->logger = null !== $logger ? $logger : new NullLogger();
}

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

// Persist virtual fields
$contentType = $this->contentTypeHandler->load($content->versionInfo->contentInfo->contentTypeId);
$this->fieldHandler->updateFields($content, new UpdateStruct([
'initialLanguageId' => $this->languageHandler->loadByLanguageCode(
$content->versionInfo->initialLanguageCode
)->id,
]), $contentType);

// Create relations for new version
$relations = $this->contentGateway->loadRelations($contentId, $srcVersion);
foreach ($relations as $relation) {
Expand Down Expand Up @@ -321,7 +335,9 @@ public function load($id, $version = null, array $translations = null)
$this->contentGateway->loadVersionedNameData([[
'id' => $id,
'version' => $rows[0]['ezcontentobject_version_version'],
]])
]]),
'ezcontentobject_',
$translations
);
$content = $contentObjects[0];
unset($rows, $contentObjects);
Expand Down Expand Up @@ -372,7 +388,9 @@ public function loadContentList(array $contentIds, array $translations = null):
try {
$contentList = $this->mapper->extractContentFromRows(
$contentItemsRow,
$contentItemNameData[$contentId]
$contentItemNameData[$contentId],
'ezcontentobject_',
$translations
);
$contentItems[$contentId] = $contentList[0];
} catch (Exception $e) {
Expand Down
21 changes: 15 additions & 6 deletions src/lib/Persistence/Legacy/Content/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,15 @@ public function convertToStorageValue(Field $field)
* @param array<array<string, scalar>> $rows
* @param array<array<string, scalar>> $nameRows
* @param string $prefix
* @param array<string>|null $translations
*
* @return \Ibexa\Contracts\Core\Persistence\Content[]
*/
public function extractContentFromRows(
array $rows,
array $nameRows,
string $prefix = 'ezcontentobject_'
string $prefix = 'ezcontentobject_',
?array $translations = null
): array {
$versionedNameData = [];

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

$fieldDefinitions = $this->loadCachedVersionFieldDefinitionsPerLanguage(
$rows,
$prefix
$prefix,
$translations
);

foreach ($rows as $row) {
Expand Down Expand Up @@ -294,9 +297,10 @@ private function buildContentObjects(
$content->versionInfo = $versionInfo;
$content->versionInfo->names = $names;
$content->versionInfo->contentInfo = $contentInfo;
$content->fields = array_values($fields[$contentId][$versionId]);
$content->fields = array_values($fields[$contentId][$versionId] ?? []);

$missingVersionFieldDefinitions = $missingFieldDefinitions[$contentId][$versionId] ?? [];

$missingVersionFieldDefinitions = $missingFieldDefinitions[$contentId][$versionId];
foreach ($missingVersionFieldDefinitions as $languageCode => $versionFieldDefinitions) {
foreach ($versionFieldDefinitions as $fieldDefinition) {
$event = $this->eventDispatcher->dispatch(
Expand Down Expand Up @@ -324,13 +328,16 @@ private function buildContentObjects(
/**
* @phpstan-param TRawContentRow[] $rows
*
* @param string[]|null $translations
*
* @phpstan-return TVersionedLanguageFieldDefinitionsMap
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
*/
private function loadCachedVersionFieldDefinitionsPerLanguage(
array $rows,
string $prefix
string $prefix,
?array $translations = null
): array {
$fieldDefinitions = [];
$contentTypes = [];
Expand All @@ -346,12 +353,14 @@ private function loadCachedVersionFieldDefinitionsPerLanguage(
continue;
}

$languageCodes = $this->extractLanguageCodesFromMask($languageMask, $allLanguages);
$allLanguagesCodes = $this->extractLanguageCodesFromMask($languageMask, $allLanguages);
$languageCodes = empty($translations) ? $allLanguagesCodes : array_intersect($translations, $allLanguagesCodes);
$contentTypes[$contentTypeId] = $contentTypes[$contentTypeId] ?? $this->contentTypeHandler->load($contentTypeId);
$contentType = $contentTypes[$contentTypeId];
foreach ($contentType->fieldDefinitions as $fieldDefinition) {
foreach ($languageCodes as $languageCode) {
$id = (int)$fieldDefinition->id;
$languageCode = (string)$languageCode;
$fieldDefinitions[$contentId][$versionId][$languageCode][$id] = $fieldDefinition;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,6 @@ services:
- '@ibexa.persistence.legacy.url_alias.gateway'
- '@ibexa.spi.persistence.legacy.content_type.handler'
- '@Ibexa\Core\Persistence\Legacy\Content\TreeHandler'
- '@ibexa.spi.persistence.legacy.language.handler'
- "@logger"
lazy: true
31 changes: 30 additions & 1 deletion tests/lib/Persistence/Legacy/Content/ContentHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Ibexa\Core\Persistence\Legacy\Content\FieldHandler;
use Ibexa\Core\Persistence\Legacy\Content\Gateway as ContentGateway;
use Ibexa\Core\Persistence\Legacy\Content\Handler;
use Ibexa\Core\Persistence\Legacy\Content\Language\Handler as LanguageHandler;
use Ibexa\Core\Persistence\Legacy\Content\Location\Gateway as LocationGateway;
use Ibexa\Core\Persistence\Legacy\Content\Mapper;
use Ibexa\Core\Persistence\Legacy\Content\TreeHandler;
Expand Down Expand Up @@ -110,6 +111,11 @@ class ContentHandlerTest extends TestCase
*/
protected $contentTypeHandlerMock;

/**
* @var \PHPUnit\Framework\MockObject\MockObject&\Ibexa\Core\Persistence\Legacy\Content\Language\Handler
*/
private LanguageHandler $languageHandlerMock;

/**
* @todo Current method way to complex to test, refactor!
*/
Expand Down Expand Up @@ -375,6 +381,8 @@ public function testCreateDraftFromVersion()
$mapperMock = $this->getMapperMock();
$gatewayMock = $this->getGatewayMock();
$fieldHandlerMock = $this->getFieldHandlerMock();
$languageHandlerMock = $this->getLanguageHandlerMock();
$contentTypeHandlerMock = $this->getContentTypeHandlerMock();

$handler->expects($this->once())
->method('load')
Expand All @@ -393,11 +401,18 @@ public function testCreateDraftFromVersion()
[
'names' => [],
'versionNo' => 3,
'contentInfo' => new ContentInfo(),
]
)
)
);

$languageHandlerMock->method('loadByLanguageCode')
->willReturn(new Content\Language());

$contentTypeHandlerMock->method('load')
->willReturn(new Type());

$gatewayMock->expects($this->once())
->method('insertVersion')
->with(
Expand Down Expand Up @@ -1509,7 +1524,8 @@ protected function getContentHandler()
$this->getSlugConverterMock(),
$this->getUrlAliasGatewayMock(),
$this->getContentTypeHandlerMock(),
$this->getTreeHandlerMock()
$this->getTreeHandlerMock(),
$this->getLanguageHandlerMock(),
);
}

Expand Down Expand Up @@ -1537,6 +1553,7 @@ protected function getPartlyMockedHandler(array $methods)
$this->getUrlAliasGatewayMock(),
$this->getContentTypeHandlerMock(),
$this->getTreeHandlerMock(),
$this->getLanguageHandlerMock(),
]
)
->getMock();
Expand Down Expand Up @@ -1570,6 +1587,18 @@ protected function getContentTypeHandlerMock()
return $this->contentTypeHandlerMock;
}

/**
* @return \PHPUnit\Framework\MockObject\MockObject&\Ibexa\Core\Persistence\Legacy\Content\Language\Handler
*/
protected function getLanguageHandlerMock(): LanguageHandler
{
if (!isset($this->languageHandlerMock)) {
$this->languageHandlerMock = $this->createMock(LanguageHandler::class);
}

return $this->languageHandlerMock;
}

/**
* Returns a FieldHandler mock.
*
Expand Down
22 changes: 8 additions & 14 deletions tests/lib/Persistence/Legacy/Content/FieldHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ public function assertUpdateFieldsForInitialLanguage($storageHandlerUpdatesField
$callNo = 0;
$fieldValue = new FieldValue();
$fieldsToCopy = [];
foreach ([1, 2, 3] as $fieldDefinitionId) {
foreach ([1, 2, 3] as $id => $fieldDefinitionId) {
$field = new Field(
[
'fieldDefinitionId' => $fieldDefinitionId,
Expand All @@ -613,6 +613,7 @@ public function assertUpdateFieldsForInitialLanguage($storageHandlerUpdatesField
// These fields are copied from main language
if ($fieldDefinitionId == 2 || $fieldDefinitionId == 3) {
$originalField = clone $field;
$originalField->id = $fieldDefinitionId;
$originalField->languageCode = 'eng-GB';
$fieldsToCopy[] = [
'copy' => clone $field,
Expand Down Expand Up @@ -803,20 +804,13 @@ protected function getContentSingleLanguageFixture()
$field->value = new FieldValue();
$field->languageCode = 'eng-GB';

$firstField = clone $field;
$firstField->fieldDefinitionId = 1;

$secondField = clone $field;
$secondField->fieldDefinitionId = 2;

$thirdField = clone $field;
$thirdField->fieldDefinitionId = 3;
foreach ([1, 2, 3] as $id) {
$contentField = clone $field;
$contentField->id = $id;
$contentField->fieldDefinitionId = $id;

$content->fields = [
$firstField,
$secondField,
$thirdField,
];
$content->fields[] = $contentField;
}

return $content;
}
Expand Down
Loading