diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 43e60a3254..8c33e839fe 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -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 @@ -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\\ but returns array\\\\>\\.$#" - 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 @@ -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 diff --git a/src/lib/Persistence/Legacy/Content/FieldHandler.php b/src/lib/Persistence/Legacy/Content/FieldHandler.php index 36f72d3d82..892608c25c 100644 --- a/src/lib/Persistence/Legacy/Content/FieldHandler.php +++ b/src/lib/Persistence/Legacy/Content/FieldHandler.php @@ -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 @@ -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> */ protected function getFieldMap(array $fields, &$languageCodes = null) { diff --git a/src/lib/Persistence/Legacy/Content/Handler.php b/src/lib/Persistence/Legacy/Content/Handler.php index 88cef6dd00..56871b0d27 100644 --- a/src/lib/Persistence/Legacy/Content/Handler.php +++ b/src/lib/Persistence/Legacy/Content/Handler.php @@ -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; @@ -85,6 +85,8 @@ class Handler implements BaseContentHandler */ protected $treeHandler; + protected LanguageHandler $languageHandler; + /** @var \Psr\Log\LoggerInterface */ private $logger; @@ -110,6 +112,7 @@ public function __construct( UrlAliasGateway $urlAliasGateway, ContentTypeHandler $contentTypeHandler, TreeHandler $treeHandler, + LanguageHandler $languageHandler, LoggerInterface $logger = null ) { $this->contentGateway = $contentGateway; @@ -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(); } @@ -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) { @@ -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) { @@ -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); @@ -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) { diff --git a/src/lib/Persistence/Legacy/Content/Mapper.php b/src/lib/Persistence/Legacy/Content/Mapper.php index 3113470b28..aa6a670a66 100644 --- a/src/lib/Persistence/Legacy/Content/Mapper.php +++ b/src/lib/Persistence/Legacy/Content/Mapper.php @@ -203,13 +203,15 @@ public function convertToStorageValue(Field $field) * @param array> $rows * @param array> $nameRows * @param string $prefix + * @param array|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 = []; @@ -226,7 +228,8 @@ public function extractContentFromRows( $fieldDefinitions = $this->loadCachedVersionFieldDefinitionsPerLanguage( $rows, - $prefix + $prefix, + $translations ); foreach ($rows as $row) { @@ -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( @@ -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 = []; @@ -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; } } diff --git a/src/lib/Resources/settings/storage_engines/legacy/content.yml b/src/lib/Resources/settings/storage_engines/legacy/content.yml index 713346f146..fa9b13d1df 100644 --- a/src/lib/Resources/settings/storage_engines/legacy/content.yml +++ b/src/lib/Resources/settings/storage_engines/legacy/content.yml @@ -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 diff --git a/tests/lib/Persistence/Legacy/Content/ContentHandlerTest.php b/tests/lib/Persistence/Legacy/Content/ContentHandlerTest.php index 1e68bbbbe0..bde1f77c6e 100644 --- a/tests/lib/Persistence/Legacy/Content/ContentHandlerTest.php +++ b/tests/lib/Persistence/Legacy/Content/ContentHandlerTest.php @@ -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; @@ -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! */ @@ -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') @@ -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( @@ -1509,7 +1524,8 @@ protected function getContentHandler() $this->getSlugConverterMock(), $this->getUrlAliasGatewayMock(), $this->getContentTypeHandlerMock(), - $this->getTreeHandlerMock() + $this->getTreeHandlerMock(), + $this->getLanguageHandlerMock(), ); } @@ -1537,6 +1553,7 @@ protected function getPartlyMockedHandler(array $methods) $this->getUrlAliasGatewayMock(), $this->getContentTypeHandlerMock(), $this->getTreeHandlerMock(), + $this->getLanguageHandlerMock(), ] ) ->getMock(); @@ -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. * diff --git a/tests/lib/Persistence/Legacy/Content/FieldHandlerTest.php b/tests/lib/Persistence/Legacy/Content/FieldHandlerTest.php index a9214ebe59..a938b8b850 100644 --- a/tests/lib/Persistence/Legacy/Content/FieldHandlerTest.php +++ b/tests/lib/Persistence/Legacy/Content/FieldHandlerTest.php @@ -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, @@ -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, @@ -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; }