From aaccb4ff14347c32f8742997840fc5101a5b1a83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20W=C3=B3js?= Date: Sat, 9 Nov 2019 13:27:22 +0100 Subject: [PATCH] EZP-31118: Introduced strict types for ContenteService --- .../Imagine/ImageAsset/AliasGeneratorTest.php | 4 +- eZ/Publish/API/Repository/ContentService.php | 94 +++---- eZ/Publish/Core/Event/ContentService.php | 10 +- .../Content/Gateway/DoctrineDatabase.php | 8 +- eZ/Publish/Core/Repository/ContentService.php | 263 ++++++++++-------- eZ/Publish/Core/Repository/SearchService.php | 3 +- .../SiteAccessAware/ContentService.php | 76 ++--- .../Tests/ContentServiceTest.php | 81 +++--- .../Tests/Service/Mock/ContentTest.php | 249 +++++++++++------ .../Tests/Service/Mock/SearchTest.php | 7 +- eZ/Publish/Core/Repository/TrashService.php | 4 +- eZ/Publish/Core/Repository/UserService.php | 10 +- .../Decorator/ContentServiceDecorator.php | 72 ++--- .../Decorator/ContentServiceDecoratorTest.php | 57 ++-- 14 files changed, 530 insertions(+), 408 deletions(-) diff --git a/eZ/Bundle/EzPublishCoreBundle/Tests/Imagine/ImageAsset/AliasGeneratorTest.php b/eZ/Bundle/EzPublishCoreBundle/Tests/Imagine/ImageAsset/AliasGeneratorTest.php index cecc5e413a0..3a412c22026 100644 --- a/eZ/Bundle/EzPublishCoreBundle/Tests/Imagine/ImageAsset/AliasGeneratorTest.php +++ b/eZ/Bundle/EzPublishCoreBundle/Tests/Imagine/ImageAsset/AliasGeneratorTest.php @@ -49,9 +49,7 @@ protected function setUp(): void public function testGetVariationOfImageAsset() { $assetField = new Field([ - 'value' => new ImageAsset\Value([ - 'destinationContentId' => 486, - ]), + 'value' => new ImageAsset\Value(486), ]); $imageField = new Field([ 'value' => new Image\Value([ diff --git a/eZ/Publish/API/Repository/ContentService.php b/eZ/Publish/API/Repository/ContentService.php index 2da9bf963f5..ba36714c46c 100644 --- a/eZ/Publish/API/Repository/ContentService.php +++ b/eZ/Publish/API/Repository/ContentService.php @@ -1,11 +1,11 @@ prepare()->execute(); - return $this->dbHandler->lastInsertId( + return (int)$this->dbHandler->lastInsertId( $this->dbHandler->getSequenceName('ezcontentobject', 'id') ); } @@ -275,7 +275,7 @@ public function insertVersion(VersionInfo $versionInfo, array $fields) $q->prepare()->execute(); - return $this->dbHandler->lastInsertId( + return (int)$this->dbHandler->lastInsertId( $this->dbHandler->getSequenceName('ezcontentobject_version', 'id') ); } @@ -689,7 +689,7 @@ public function insertNewField(Content $content, Field $field, StorageFieldValue $q->prepare()->execute(); - return $this->dbHandler->lastInsertId( + return (int)$this->dbHandler->lastInsertId( $this->dbHandler->getSequenceName('ezcontentobject_attribute', 'id') ); } @@ -2113,7 +2113,7 @@ public function insertRelation(RelationCreateStruct $createStruct) $q->prepare()->execute(); - return $this->dbHandler->lastInsertId( + return (int)$this->dbHandler->lastInsertId( $this->dbHandler->getSequenceName('ezcontentobject_link', 'id') ); } diff --git a/eZ/Publish/Core/Repository/ContentService.php b/eZ/Publish/Core/Repository/ContentService.php index b9c72063df2..bd2e3f84df3 100644 --- a/eZ/Publish/Core/Repository/ContentService.php +++ b/eZ/Publish/Core/Repository/ContentService.php @@ -1,11 +1,11 @@ internalLoadContentInfo($contentId); + $contentInfo = $this->internalLoadContentInfoById($contentId); if (!$this->permissionResolver->canUser('content', 'read', $contentInfo)) { throw new UnauthorizedException('content', 'read', ['contentId' => $contentId]); } @@ -149,29 +148,40 @@ public function loadContentInfoList(array $contentIds): iterable /** * Loads a content info object. * - * To load fields use loadContent - * * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content with the given id does not exist * - * @param mixed $id - * @param bool $isRemoteId + * @param int $id * * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo */ - public function internalLoadContentInfo($id, $isRemoteId = false) + public function internalLoadContentInfoById(int $id): ContentInfo { try { - $method = $isRemoteId ? 'loadContentInfoByRemoteId' : 'loadContentInfo'; - return $this->domainMapper->buildContentInfoDomainObject( - $this->persistenceHandler->contentHandler()->$method($id) + $this->persistenceHandler->contentHandler()->loadContentInfo($id) ); } catch (APINotFoundException $e) { - throw new NotFoundException( - 'Content', - $id, - $e + throw new NotFoundException('Content', $id, $e); + } + } + + /** + * Loads a content info object by remote id. + * + * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content with the given id does not exist + * + * @param string $remoteId + * + * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo + */ + public function internalLoadContentInfoByRemoteId(string $remoteId): ContentInfo + { + try { + return $this->domainMapper->buildContentInfoDomainObject( + $this->persistenceHandler->contentHandler()->loadContentInfoByRemoteId($remoteId) ); + } catch (APINotFoundException $e) { + throw new NotFoundException('Content', $remoteId, $e); } } @@ -187,9 +197,9 @@ public function internalLoadContentInfo($id, $isRemoteId = false) * * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo */ - public function loadContentInfoByRemoteId($remoteId) + public function loadContentInfoByRemoteId(string $remoteId): ContentInfo { - $contentInfo = $this->internalLoadContentInfo($remoteId, true); + $contentInfo = $this->internalLoadContentInfoByRemoteId($remoteId); if (!$this->permissionResolver->canUser('content', 'read', $contentInfo)) { throw new UnauthorizedException('content', 'read', ['remoteId' => $remoteId]); @@ -207,11 +217,11 @@ public function loadContentInfoByRemoteId($remoteId) * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to load this version * * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo - * @param int $versionNo the version number. If not given the current version is returned. + * @param int|null $versionNo the version number. If not given the current version is returned. * * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo */ - public function loadVersionInfo(ContentInfo $contentInfo, $versionNo = null) + public function loadVersionInfo(ContentInfo $contentInfo, ?int $versionNo = null): APIVersionInfo { return $this->loadVersionInfoById($contentInfo->id, $versionNo); } @@ -224,12 +234,12 @@ public function loadVersionInfo(ContentInfo $contentInfo, $versionNo = null) * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the version with the given number does not exist * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to load this version * - * @param mixed $contentId - * @param int $versionNo the version number. If not given the current version is returned. + * @param int $contentId + * @param int|null $versionNo the version number. If not given the current version is returned. * * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo */ - public function loadVersionInfoById($contentId, $versionNo = null) + public function loadVersionInfoById(int $contentId, ?int $versionNo = null): APIVersionInfo { try { $spiVersionInfo = $this->persistenceHandler->contentHandler()->loadVersionInfo( @@ -265,7 +275,7 @@ public function loadVersionInfoById($contentId, $versionNo = null) /** * {@inheritdoc} */ - public function loadContentByContentInfo(ContentInfo $contentInfo, array $languages = null, $versionNo = null, $useAlwaysAvailable = true) + public function loadContentByContentInfo(ContentInfo $contentInfo, array $languages = null, ?int $versionNo = null, bool $useAlwaysAvailable = true): APIContent { // Change $useAlwaysAvailable to false to avoid contentInfo lookup if we know alwaysAvailable is disabled if ($useAlwaysAvailable && !$contentInfo->alwaysAvailable) { @@ -283,7 +293,7 @@ public function loadContentByContentInfo(ContentInfo $contentInfo, array $langua /** * {@inheritdoc} */ - public function loadContentByVersionInfo(APIVersionInfo $versionInfo, array $languages = null, $useAlwaysAvailable = true) + public function loadContentByVersionInfo(APIVersionInfo $versionInfo, array $languages = null, bool $useAlwaysAvailable = true): APIContent { // Change $useAlwaysAvailable to false to avoid contentInfo lookup if we know alwaysAvailable is disabled if ($useAlwaysAvailable && !$versionInfo->getContentInfo()->alwaysAvailable) { @@ -301,9 +311,9 @@ public function loadContentByVersionInfo(APIVersionInfo $versionInfo, array $lan /** * {@inheritdoc} */ - public function loadContent($contentId, array $languages = null, $versionNo = null, $useAlwaysAvailable = true) + public function loadContent(int $contentId, array $languages = null, ?int $versionNo = null, bool $useAlwaysAvailable = true): APIContent { - $content = $this->internalLoadContent($contentId, $languages, $versionNo, false, $useAlwaysAvailable); + $content = $this->internalLoadContentById($contentId, $languages, $versionNo, $useAlwaysAvailable); if (!$this->permissionResolver->canUser('content', 'read', $content)) { throw new UnauthorizedException('content', 'read', ['contentId' => $contentId]); @@ -318,65 +328,78 @@ public function loadContent($contentId, array $languages = null, $versionNo = nu return $content; } - /** - * Loads content in a version of the given content object. - * - * If no version number is given, the method returns the current version - * - * @internal - * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the content or version with the given id and languages does not exist - * - * @param mixed $id - * @param array|null $languages A language priority, filters returned fields and is used as prioritized language code on - * returned value object. If not given all languages are returned. - * @param int|null $versionNo the version number. If not given the current version is returned - * @param bool $isRemoteId - * @param bool $useAlwaysAvailable Add Main language to \$languages if true (default) and if alwaysAvailable is true - * - * @return \eZ\Publish\API\Repository\Values\Content\Content - */ - public function internalLoadContent($id, array $languages = null, $versionNo = null, $isRemoteId = false, $useAlwaysAvailable = true) - { + public function internalLoadContentById( + int $id, + ?array $languages = null, + int $versionNo = null, + bool $useAlwaysAvailable = true + ): APIContent { try { - // Get Content ID if lookup by remote ID - if ($isRemoteId) { - $spiContentInfo = $this->persistenceHandler->contentHandler()->loadContentInfoByRemoteId($id); - $id = $spiContentInfo->id; - // Set $isRemoteId to false as the next loads will be for content id now that we have it (for exception use now) - $isRemoteId = false; - } + $spiContentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo($id); - $loadLanguages = $languages; - $alwaysAvailableLanguageCode = null; - // Set main language on $languages filter if not empty (all) and $useAlwaysAvailable being true - // @todo Move use always available logic to SPI load methods, like done in location handler in 7.x - if (!empty($loadLanguages) && $useAlwaysAvailable) { - if (!isset($spiContentInfo)) { - $spiContentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo($id); - } + return $this->internalLoadContentBySPIContentInfo( + $spiContentInfo, + $languages, + $versionNo, + $useAlwaysAvailable + ); + } catch (APINotFoundException $e) { + throw new NotFoundException( + 'Content', + [ + 'id' => $id, + 'languages' => $languages, + 'versionNo' => $versionNo, + ], + $e + ); + } + } - if ($spiContentInfo->alwaysAvailable) { - $loadLanguages[] = $alwaysAvailableLanguageCode = $spiContentInfo->mainLanguageCode; - $loadLanguages = array_unique($loadLanguages); - } - } + public function internalLoadContentByRemoteId( + string $remoteId, + array $languages = null, + int $versionNo = null, + bool $useAlwaysAvailable = true + ): APIContent { + try { + $spiContentInfo = $this->persistenceHandler->contentHandler()->loadContentInfoByRemoteId($remoteId); - $spiContent = $this->persistenceHandler->contentHandler()->load( - $id, + return $this->internalLoadContentBySPIContentInfo( + $spiContentInfo, + $languages, $versionNo, - $loadLanguages + $useAlwaysAvailable ); } catch (APINotFoundException $e) { throw new NotFoundException( 'Content', [ - $isRemoteId ? 'remoteId' : 'id' => $id, + 'remoteId' => $remoteId, 'languages' => $languages, 'versionNo' => $versionNo, ], $e ); } + } + + private function internalLoadContentBySPIContentInfo(SPIContentInfo $spiContentInfo, array $languages = null, int $versionNo = null, bool $useAlwaysAvailable = true): APIContent + { + $loadLanguages = $languages; + $alwaysAvailableLanguageCode = null; + // Set main language on $languages filter if not empty (all) and $useAlwaysAvailable being true + // @todo Move use always available logic to SPI load methods, like done in location handler in 7.x + if (!empty($loadLanguages) && $useAlwaysAvailable && $spiContentInfo->alwaysAvailable) { + $loadLanguages[] = $alwaysAvailableLanguageCode = $spiContentInfo->mainLanguageCode; + $loadLanguages = array_unique($loadLanguages); + } + + $spiContent = $this->persistenceHandler->contentHandler()->load( + $spiContentInfo->id, + $versionNo, + $loadLanguages + ); if ($languages === null) { $languages = []; @@ -408,9 +431,9 @@ public function internalLoadContent($id, array $languages = null, $versionNo = n * * @return \eZ\Publish\API\Repository\Values\Content\Content */ - public function loadContentByRemoteId($remoteId, array $languages = null, $versionNo = null, $useAlwaysAvailable = true) + public function loadContentByRemoteId(string $remoteId, array $languages = null, ?int $versionNo = null, bool $useAlwaysAvailable = true): APIContent { - $content = $this->internalLoadContent($remoteId, $languages, $versionNo, true, $useAlwaysAvailable); + $content = $this->internalLoadContentByRemoteId($remoteId, $languages, $versionNo, $useAlwaysAvailable); if (!$this->permissionResolver->canUser('content', 'read', $content)) { throw new UnauthorizedException('content', 'read', ['remoteId' => $remoteId]); @@ -444,8 +467,8 @@ public function loadContentByRemoteId($remoteId, array $languages = null, $versi public function loadContentListByContentInfo( array $contentInfoList, array $languages = [], - $useAlwaysAvailable = true - ) { + bool $useAlwaysAvailable = true + ): iterable { $loadAllLanguages = $languages === Language::ALL; $contentIds = []; $contentTypeIds = []; @@ -508,7 +531,7 @@ public function loadContentListByContentInfo( * * @return \eZ\Publish\API\Repository\Values\Content\Content - the newly created content draft */ - public function createContent(APIContentCreateStruct $contentCreateStruct, array $locationCreateStructs = []) + public function createContent(APIContentCreateStruct $contentCreateStruct, array $locationCreateStructs = []): APIContent { if ($contentCreateStruct->mainLanguageCode === null) { throw new InvalidArgumentException('$contentCreateStruct', "'mainLanguageCode' property must be set"); @@ -716,7 +739,7 @@ public function createContent(APIContentCreateStruct $contentCreateStruct, array * * @return \eZ\Publish\SPI\Persistence\Content\ObjectState[] */ - protected function getDefaultObjectStates() + protected function getDefaultObjectStates(): array { $defaultObjectStatesMap = []; $objectStateHandler = $this->persistenceHandler->objectStateHandler(); @@ -741,7 +764,7 @@ protected function getDefaultObjectStates() * * @return string[] */ - protected function getLanguageCodesForCreate(APIContentCreateStruct $contentCreateStruct) + protected function getLanguageCodesForCreate(APIContentCreateStruct $contentCreateStruct): array { $languageCodes = []; @@ -777,7 +800,7 @@ protected function getLanguageCodesForCreate(APIContentCreateStruct $contentCrea * * @return array */ - protected function mapFieldsForCreate(APIContentCreateStruct $contentCreateStruct) + protected function mapFieldsForCreate(APIContentCreateStruct $contentCreateStruct): array { $fields = []; @@ -819,7 +842,7 @@ protected function mapFieldsForCreate(APIContentCreateStruct $contentCreateStruc * * @return Field */ - private function cloneField(Field $field, array $overrides = []) + private function cloneField(Field $field, array $overrides = []): Field { $fieldData = array_merge( [ @@ -842,7 +865,7 @@ private function cloneField(Field $field, array $overrides = []) * * @return \eZ\Publish\SPI\Persistence\Content\Location\CreateStruct[] */ - protected function buildSPILocationCreateStructs(array $locationCreateStructs) + protected function buildSPILocationCreateStructs(array $locationCreateStructs): array { $spiLocationCreateStructs = []; $parentLocationIdSet = []; @@ -898,7 +921,7 @@ protected function buildSPILocationCreateStructs(array $locationCreateStructs) * * @return \eZ\Publish\API\Repository\Values\Content\Content the content with the updated attributes */ - public function updateContentMetadata(ContentInfo $contentInfo, ContentMetadataUpdateStruct $contentMetadataUpdateStruct) + public function updateContentMetadata(ContentInfo $contentInfo, ContentMetadataUpdateStruct $contentMetadataUpdateStruct): APIContent { $propertyCount = 0; foreach ($contentMetadataUpdateStruct as $propertyName => $propertyValue) { @@ -993,7 +1016,7 @@ public function updateContentMetadata(ContentInfo $contentInfo, ContentMetadataU * @param bool $updatePathIdentificationString this parameter is legacy storage specific for updating * ezcontentobject_tree.path_identification_string, it is ignored by other storage engines */ - protected function publishUrlAliasesForContent(APIContent $content, $updatePathIdentificationString = true) + protected function publishUrlAliasesForContent(APIContent $content, bool $updatePathIdentificationString = true): void { $urlAliasNames = $this->nameSchemaService->resolveUrlAliasSchema($content); $locations = $this->repository->getLocationService()->loadLocations( @@ -1029,9 +1052,9 @@ protected function publishUrlAliasesForContent(APIContent $content, $updatePathI * * @return mixed[] Affected Location Id's */ - public function deleteContent(ContentInfo $contentInfo) + public function deleteContent(ContentInfo $contentInfo): iterable { - $contentInfo = $this->internalLoadContentInfo($contentInfo->id); + $contentInfo = $this->internalLoadContentInfoById($contentInfo->id); if (!$this->permissionResolver->canUser('content', 'remove', $contentInfo)) { throw new UnauthorizedException('content', 'remove', ['contentId' => $contentInfo->id]); @@ -1063,8 +1086,8 @@ public function deleteContent(ContentInfo $contentInfo) * If no version is given, the current published version is used. * * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo - * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo - * @param \eZ\Publish\API\Repository\Values\User\User $creator if set given user is used to create the draft - otherwise the current-user is used + * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo|null $versionInfo + * @param \eZ\Publish\API\Repository\Values\User\User|null $creator if set given user is used to create the draft - otherwise the current-user is used * @param \eZ\Publish\API\Repository\Values\Content\Language|null if not set the draft is created with the initialLanguage code of the source version or if not present with the main language. * * @return \eZ\Publish\API\Repository\Values\Content\Content - the newly created content draft @@ -1075,10 +1098,10 @@ public function deleteContent(ContentInfo $contentInfo) */ public function createContentDraft( ContentInfo $contentInfo, - APIVersionInfo $versionInfo = null, - User $creator = null, + ?APIVersionInfo $versionInfo = null, + ?User $creator = null, ?Language $language = null - ) { + ): APIContent { $contentInfo = $this->loadContentInfo($contentInfo->id); if ($versionInfo !== null) { @@ -1186,7 +1209,7 @@ public function countContentDrafts(?User $user = null): int * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException */ - public function loadContentDrafts(User $user = null) + public function loadContentDrafts(?User $user = null): iterable { // throw early if user has absolutely no access to versionread if ($this->permissionResolver->hasAccess('content', 'versionread') === false) { @@ -1262,7 +1285,7 @@ public function loadContentDraftList(?User $user = null, int $offset = 0, int $l * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if a property on the struct is invalid. * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException */ - public function updateContent(APIVersionInfo $versionInfo, APIContentUpdateStruct $contentUpdateStruct) + public function updateContent(APIVersionInfo $versionInfo, APIContentUpdateStruct $contentUpdateStruct): APIContent { $contentUpdateStruct = clone $contentUpdateStruct; @@ -1457,7 +1480,7 @@ public function updateContent(APIVersionInfo $versionInfo, APIContentUpdateStruc * * @return array */ - private function getUpdatedLanguageCodes(APIContentUpdateStruct $contentUpdateStruct) + private function getUpdatedLanguageCodes(APIContentUpdateStruct $contentUpdateStruct): array { $languageCodes = [ $contentUpdateStruct->initialLanguageCode => true, @@ -1484,7 +1507,7 @@ private function getUpdatedLanguageCodes(APIContentUpdateStruct $contentUpdateSt * * @return array */ - protected function getLanguageCodesForUpdate(APIContentUpdateStruct $contentUpdateStruct, APIContent $content) + protected function getLanguageCodesForUpdate(APIContentUpdateStruct $contentUpdateStruct, APIContent $content): array { $languageCodes = array_fill_keys($content->versionInfo->languageCodes, true); $languageCodes[$contentUpdateStruct->initialLanguageCode] = true; @@ -1513,8 +1536,8 @@ protected function getLanguageCodesForUpdate(APIContentUpdateStruct $contentUpda protected function mapFieldsForUpdate( APIContentUpdateStruct $contentUpdateStruct, ContentType $contentType, - $mainLanguageCode - ) { + string $mainLanguageCode + ): array { $fields = []; foreach ($contentUpdateStruct->fields as $field) { @@ -1565,9 +1588,9 @@ protected function mapFieldsForUpdate( * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException */ - public function publishVersion(APIVersionInfo $versionInfo, array $translations = Language::ALL) + public function publishVersion(APIVersionInfo $versionInfo, array $translations = Language::ALL): APIContent { - $content = $this->internalLoadContent( + $content = $this->internalLoadContentById( $versionInfo->contentInfo->id, null, $versionInfo->versionNo @@ -1575,7 +1598,7 @@ public function publishVersion(APIVersionInfo $versionInfo, array $translations $fromContent = null; if ($content->contentInfo->currentVersionNo !== $versionInfo->versionNo) { - $fromContent = $this->internalLoadContent( + $fromContent = $this->internalLoadContentById( $content->contentInfo->id, null, $content->contentInfo->currentVersionNo @@ -1624,7 +1647,7 @@ protected function copyTranslationsFromPublishedVersion(APIVersionInfo $versionI { $contendId = $versionInfo->contentInfo->id; - $currentContent = $this->internalLoadContent($contendId); + $currentContent = $this->internalLoadContentById($contendId); $currentVersionInfo = $currentContent->versionInfo; // Copying occurs only if: @@ -1659,7 +1682,7 @@ protected function copyTranslationsFromPublishedVersion(APIVersionInfo $versionI $updateStruct = $this->newContentUpdateStruct(); $updateStruct->initialLanguageCode = $versionInfo->initialLanguageCode; - $contentToPublish = $this->internalLoadContent($contendId, null, $versionInfo->versionNo); + $contentToPublish = $this->internalLoadContentById($contendId, null, $versionInfo->versionNo); $fallbackUpdateStruct = $this->newContentUpdateStruct(); foreach ($currentContent->getFields() as $field) { @@ -1784,7 +1807,7 @@ protected function internalPublishVersion(APIVersionInfo $versionInfo, $publicat /** * @return int */ - protected function getUnixTimestamp() + protected function getUnixTimestamp(): int { return time(); } @@ -1798,7 +1821,7 @@ protected function getUnixTimestamp() * * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo */ - public function deleteVersion(APIVersionInfo $versionInfo) + public function deleteVersion(APIVersionInfo $versionInfo): void { if ($versionInfo->isPublished()) { throw new BadStateException( @@ -1852,7 +1875,7 @@ public function deleteVersion(APIVersionInfo $versionInfo) * * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo[] Sorted by creation date */ - public function loadVersions(ContentInfo $contentInfo, ?int $status = null) + public function loadVersions(ContentInfo $contentInfo, ?int $status = null): iterable { if (!$this->permissionResolver->canUser('content', 'versionread', $contentInfo)) { throw new UnauthorizedException('content', 'versionread', ['contentId' => $contentInfo->id]); @@ -1894,7 +1917,7 @@ public function loadVersions(ContentInfo $contentInfo, ?int $status = null) * * @return \eZ\Publish\API\Repository\Values\Content\Content */ - public function copyContent(ContentInfo $contentInfo, LocationCreateStruct $destinationLocationCreateStruct, APIVersionInfo $versionInfo = null) + public function copyContent(ContentInfo $contentInfo, LocationCreateStruct $destinationLocationCreateStruct, ?APIVersionInfo $versionInfo = null): APIContent { $destinationLocation = $this->repository->getLocationService()->loadLocation( $destinationLocationCreateStruct->parentLocationId @@ -1947,7 +1970,7 @@ public function copyContent(ContentInfo $contentInfo, LocationCreateStruct $dest throw $e; } - return $this->internalLoadContent($content->id); + return $this->internalLoadContentById($content->id); } /** @@ -1959,7 +1982,7 @@ public function copyContent(ContentInfo $contentInfo, LocationCreateStruct $dest * * @return \eZ\Publish\API\Repository\Values\Content\Relation[] */ - public function loadRelations(APIVersionInfo $versionInfo) + public function loadRelations(APIVersionInfo $versionInfo): iterable { if ($versionInfo->isPublished()) { $function = 'read'; @@ -1980,7 +2003,7 @@ public function loadRelations(APIVersionInfo $versionInfo) /** @var $relations \eZ\Publish\API\Repository\Values\Content\Relation[] */ $relations = []; foreach ($spiRelations as $spiRelation) { - $destinationContentInfo = $this->internalLoadContentInfo($spiRelation->destinationContentId); + $destinationContentInfo = $this->internalLoadContentInfoById($spiRelation->destinationContentId); if (!$this->permissionResolver->canUser('content', 'read', $destinationContentInfo)) { continue; } @@ -2020,7 +2043,7 @@ public function countReverseRelations(ContentInfo $contentInfo): int * * @return \eZ\Publish\API\Repository\Values\Content\Relation[] */ - public function loadReverseRelations(ContentInfo $contentInfo) + public function loadReverseRelations(ContentInfo $contentInfo): iterable { if (!$this->permissionResolver->canUser('content', 'reverserelatedlist', $contentInfo)) { throw new UnauthorizedException('content', 'reverserelatedlist', ['contentId' => $contentInfo->id]); @@ -2032,7 +2055,7 @@ public function loadReverseRelations(ContentInfo $contentInfo) $returnArray = []; foreach ($spiRelations as $spiRelation) { - $sourceContentInfo = $this->internalLoadContentInfo($spiRelation->sourceContentId); + $sourceContentInfo = $this->internalLoadContentInfoById($spiRelation->sourceContentId); if (!$this->permissionResolver->canUser('content', 'read', $sourceContentInfo)) { continue; } @@ -2067,7 +2090,7 @@ public function loadReverseRelationList(ContentInfo $contentInfo, int $offset = $limit ); foreach ($spiRelationList as $spiRelation) { - $sourceContentInfo = $this->internalLoadContentInfo($spiRelation->sourceContentId); + $sourceContentInfo = $this->internalLoadContentInfoById($spiRelation->sourceContentId); if ($this->repository->getPermissionResolver()->canUser('content', 'read', $sourceContentInfo)) { $relation = $this->domainMapper->buildRelationDomainObject( $spiRelation, @@ -2102,7 +2125,7 @@ public function loadReverseRelationList(ContentInfo $contentInfo, int $offset = * * @return \eZ\Publish\API\Repository\Values\Content\Relation the newly created relation */ - public function addRelation(APIVersionInfo $sourceVersion, ContentInfo $destinationContent) + public function addRelation(APIVersionInfo $sourceVersion, ContentInfo $destinationContent): APIRelation { $sourceVersion = $this->loadVersionInfoById( $sourceVersion->contentInfo->id, @@ -2154,7 +2177,7 @@ public function addRelation(APIVersionInfo $sourceVersion, ContentInfo $destinat * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $sourceVersion * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $destinationContent */ - public function deleteRelation(APIVersionInfo $sourceVersion, ContentInfo $destinationContent) + public function deleteRelation(APIVersionInfo $sourceVersion, ContentInfo $destinationContent): void { $sourceVersion = $this->loadVersionInfoById( $sourceVersion->contentInfo->id, @@ -2208,7 +2231,7 @@ public function deleteRelation(APIVersionInfo $sourceVersion, ContentInfo $desti /** * {@inheritdoc} */ - public function removeTranslation(ContentInfo $contentInfo, $languageCode) + public function removeTranslation(ContentInfo $contentInfo, string $languageCode): void { @trigger_error( __METHOD__ . ' is deprecated, use deleteTranslation instead', @@ -2234,7 +2257,7 @@ public function removeTranslation(ContentInfo $contentInfo, $languageCode) * * @since 6.13 */ - public function deleteTranslation(ContentInfo $contentInfo, $languageCode) + public function deleteTranslation(ContentInfo $contentInfo, string $languageCode): void { if ($contentInfo->mainLanguageCode === $languageCode) { throw new BadStateException( @@ -2330,7 +2353,7 @@ function (Location $location) { * * @since 6.12 */ - public function deleteTranslationFromDraft(APIVersionInfo $versionInfo, $languageCode) + public function deleteTranslationFromDraft(APIVersionInfo $versionInfo, string $languageCode): APIContent { if (!$versionInfo->isDraft()) { throw new BadStateException( @@ -2477,7 +2500,7 @@ public function revealContent(ContentInfo $contentInfo): void * * @return \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct */ - public function newContentCreateStruct(ContentType $contentType, $mainLanguageCode) + public function newContentCreateStruct(ContentType $contentType, string $mainLanguageCode): APIContentCreateStruct { return new ContentCreateStruct( [ @@ -2493,7 +2516,7 @@ public function newContentCreateStruct(ContentType $contentType, $mainLanguageCo * * @return \eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct */ - public function newContentMetadataUpdateStruct() + public function newContentMetadataUpdateStruct(): ContentMetadataUpdateStruct { return new ContentMetadataUpdateStruct(); } @@ -2503,7 +2526,7 @@ public function newContentMetadataUpdateStruct() * * @return \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct */ - public function newContentUpdateStruct() + public function newContentUpdateStruct(): APIContentUpdateStruct { return new ContentUpdateStruct(); } diff --git a/eZ/Publish/Core/Repository/SearchService.php b/eZ/Publish/Core/Repository/SearchService.php index 4ed0bda773f..93a4b57e70d 100644 --- a/eZ/Publish/Core/Repository/SearchService.php +++ b/eZ/Publish/Core/Repository/SearchService.php @@ -242,11 +242,10 @@ public function findSingle(Criterion $filter, array $languageFilter = [], bool $ $contentInfo = $this->searchHandler->findSingle($filter, $languageFilter); - return $this->repository->getContentService()->internalLoadContent( + return $this->repository->getContentService()->internalLoadContentById( $contentInfo->id, (!empty($languageFilter['languages']) ? $languageFilter['languages'] : null), null, - false, (isset($languageFilter['useAlwaysAvailable']) ? $languageFilter['useAlwaysAvailable'] : true) ); } diff --git a/eZ/Publish/Core/Repository/SiteAccessAware/ContentService.php b/eZ/Publish/Core/Repository/SiteAccessAware/ContentService.php index 47fdc56f5fb..e0d27ea962b 100644 --- a/eZ/Publish/Core/Repository/SiteAccessAware/ContentService.php +++ b/eZ/Publish/Core/Repository/SiteAccessAware/ContentService.php @@ -1,11 +1,11 @@ languageResolver = $languageResolver; } - public function loadContentInfo($contentId) + public function loadContentInfo(int $contentId): ContentInfo { return $this->service->loadContentInfo($contentId); } @@ -60,22 +62,22 @@ public function loadContentInfoList(array $contentIds): iterable return $this->service->loadContentInfoList($contentIds); } - public function loadContentInfoByRemoteId($remoteId) + public function loadContentInfoByRemoteId(string $remoteId): ContentInfo { return $this->service->loadContentInfoByRemoteId($remoteId); } - public function loadVersionInfo(ContentInfo $contentInfo, $versionNo = null) + public function loadVersionInfo(ContentInfo $contentInfo, ?int $versionNo = null): VersionInfo { return $this->service->loadVersionInfo($contentInfo, $versionNo); } - public function loadVersionInfoById($contentId, $versionNo = null) + public function loadVersionInfoById(int $contentId, ?int $versionNo = null): VersionInfo { return $this->service->loadVersionInfoById($contentId, $versionNo); } - public function loadContentByContentInfo(ContentInfo $contentInfo, array $languages = null, $versionNo = null, $useAlwaysAvailable = null) + public function loadContentByContentInfo(ContentInfo $contentInfo, array $languages = null, ?int $versionNo = null, bool $useAlwaysAvailable = true): Content { return $this->service->loadContentByContentInfo( $contentInfo, @@ -85,7 +87,7 @@ public function loadContentByContentInfo(ContentInfo $contentInfo, array $langua ); } - public function loadContentByVersionInfo(VersionInfo $versionInfo, array $languages = null, $useAlwaysAvailable = null) + public function loadContentByVersionInfo(VersionInfo $versionInfo, array $languages = null, bool $useAlwaysAvailable = true): Content { return $this->service->loadContentByVersionInfo( $versionInfo, @@ -94,7 +96,7 @@ public function loadContentByVersionInfo(VersionInfo $versionInfo, array $langua ); } - public function loadContent($contentId, array $languages = null, $versionNo = null, $useAlwaysAvailable = null) + public function loadContent(int $contentId, array $languages = null, ?int $versionNo = null, bool $useAlwaysAvailable = true): Content { return $this->service->loadContent( $contentId, @@ -104,7 +106,7 @@ public function loadContent($contentId, array $languages = null, $versionNo = nu ); } - public function loadContentByRemoteId($remoteId, array $languages = null, $versionNo = null, $useAlwaysAvailable = null) + public function loadContentByRemoteId(string $remoteId, array $languages = null, ?int $versionNo = null, bool $useAlwaysAvailable = true): Content { return $this->service->loadContentByRemoteId( $remoteId, @@ -114,27 +116,27 @@ public function loadContentByRemoteId($remoteId, array $languages = null, $versi ); } - public function createContent(ContentCreateStruct $contentCreateStruct, array $locationCreateStructs = []) + public function createContent(ContentCreateStruct $contentCreateStruct, array $locationCreateStructs = []): Content { return $this->service->createContent($contentCreateStruct, $locationCreateStructs); } - public function updateContentMetadata(ContentInfo $contentInfo, ContentMetadataUpdateStruct $contentMetadataUpdateStruct) + public function updateContentMetadata(ContentInfo $contentInfo, ContentMetadataUpdateStruct $contentMetadataUpdateStruct): Content { return $this->service->updateContentMetadata($contentInfo, $contentMetadataUpdateStruct); } - public function deleteContent(ContentInfo $contentInfo) + public function deleteContent(ContentInfo $contentInfo): iterable { return $this->service->deleteContent($contentInfo); } public function createContentDraft( ContentInfo $contentInfo, - VersionInfo $versionInfo = null, - User $creator = null, + ?VersionInfo $versionInfo = null, + ?User $creator = null, ?Language $language = null - ) { + ): Content { return $this->service->createContentDraft($contentInfo, $versionInfo, $creator, $language); } @@ -143,7 +145,7 @@ public function countContentDrafts(?User $user = null): int return $this->service->countContentDrafts($user); } - public function loadContentDrafts(User $user = null) + public function loadContentDrafts(?User $user = null): iterable { return $this->service->loadContentDrafts($user); } @@ -153,32 +155,32 @@ public function loadContentDraftList(?User $user = null, int $offset = 0, int $l return $this->service->loadContentDraftList($user, $offset, $limit); } - public function updateContent(VersionInfo $versionInfo, ContentUpdateStruct $contentUpdateStruct) + public function updateContent(VersionInfo $versionInfo, ContentUpdateStruct $contentUpdateStruct): Content { return $this->service->updateContent($versionInfo, $contentUpdateStruct); } - public function publishVersion(VersionInfo $versionInfo, array $translations = Language::ALL) + public function publishVersion(VersionInfo $versionInfo, array $translations = Language::ALL): Content { return $this->service->publishVersion($versionInfo, $translations); } - public function deleteVersion(VersionInfo $versionInfo) + public function deleteVersion(VersionInfo $versionInfo): void { - return $this->service->deleteVersion($versionInfo); + $this->service->deleteVersion($versionInfo); } - public function loadVersions(ContentInfo $contentInfo, ?int $status = null) + public function loadVersions(ContentInfo $contentInfo, ?int $status = null): iterable { return $this->service->loadVersions($contentInfo, $status); } - public function copyContent(ContentInfo $contentInfo, LocationCreateStruct $destinationLocationCreateStruct, VersionInfo $versionInfo = null) + public function copyContent(ContentInfo $contentInfo, LocationCreateStruct $destinationLocationCreateStruct, ?VersionInfo $versionInfo = null): Content { return $this->service->copyContent($contentInfo, $destinationLocationCreateStruct, $versionInfo); } - public function loadRelations(VersionInfo $versionInfo) + public function loadRelations(VersionInfo $versionInfo): iterable { return $this->service->loadRelations($versionInfo); } @@ -191,7 +193,7 @@ public function countReverseRelations(ContentInfo $contentInfo): int return $this->service->countReverseRelations($contentInfo); } - public function loadReverseRelations(ContentInfo $contentInfo) + public function loadReverseRelations(ContentInfo $contentInfo): iterable { return $this->service->loadReverseRelations($contentInfo); } @@ -201,32 +203,32 @@ public function loadReverseRelationList(ContentInfo $contentInfo, int $offset = return $this->service->loadReverseRelationList($contentInfo, $offset, $limit); } - public function addRelation(VersionInfo $sourceVersion, ContentInfo $destinationContent) + public function addRelation(VersionInfo $sourceVersion, ContentInfo $destinationContent): Relation { return $this->service->addRelation($sourceVersion, $destinationContent); } - public function deleteRelation(VersionInfo $sourceVersion, ContentInfo $destinationContent) + public function deleteRelation(VersionInfo $sourceVersion, ContentInfo $destinationContent): void { - return $this->service->deleteRelation($sourceVersion, $destinationContent); + $this->service->deleteRelation($sourceVersion, $destinationContent); } - public function removeTranslation(ContentInfo $contentInfo, $languageCode) + public function removeTranslation(ContentInfo $contentInfo, string $languageCode): void { - return $this->service->removeTranslation($contentInfo, $languageCode); + $this->service->removeTranslation($contentInfo, $languageCode); } - public function deleteTranslation(ContentInfo $contentInfo, $languageCode) + public function deleteTranslation(ContentInfo $contentInfo, string $languageCode): void { - return $this->service->deleteTranslation($contentInfo, $languageCode); + $this->service->deleteTranslation($contentInfo, $languageCode); } - public function deleteTranslationFromDraft(VersionInfo $versionInfo, $languageCode) + public function deleteTranslationFromDraft(VersionInfo $versionInfo, string $languageCode): Content { return $this->service->deleteTranslationFromDraft($versionInfo, $languageCode); } - public function loadContentListByContentInfo(array $contentInfoList, array $languages = null, $useAlwaysAvailable = null) + public function loadContentListByContentInfo(array $contentInfoList, array $languages = null, bool $useAlwaysAvailable = true): iterable { return $this->service->loadContentListByContentInfo( $contentInfoList, @@ -245,17 +247,17 @@ public function revealContent(ContentInfo $contentInfo): void $this->service->revealContent($contentInfo); } - public function newContentCreateStruct(ContentType $contentType, $mainLanguageCode) + public function newContentCreateStruct(ContentType $contentType, string $mainLanguageCode): ContentCreateStruct { return $this->service->newContentCreateStruct($contentType, $mainLanguageCode); } - public function newContentMetadataUpdateStruct() + public function newContentMetadataUpdateStruct(): ContentMetadataUpdateStruct { return $this->service->newContentMetadataUpdateStruct(); } - public function newContentUpdateStruct() + public function newContentUpdateStruct(): ContentUpdateStruct { return $this->service->newContentUpdateStruct(); } diff --git a/eZ/Publish/Core/Repository/SiteAccessAware/Tests/ContentServiceTest.php b/eZ/Publish/Core/Repository/SiteAccessAware/Tests/ContentServiceTest.php index 214c90e1faa..87038c12c32 100644 --- a/eZ/Publish/Core/Repository/SiteAccessAware/Tests/ContentServiceTest.php +++ b/eZ/Publish/Core/Repository/SiteAccessAware/Tests/ContentServiceTest.php @@ -9,6 +9,7 @@ use eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct; use eZ\Publish\API\Repository\Values\Content\Language; use eZ\Publish\API\Repository\Values\Content\LocationCreateStruct; +use eZ\Publish\API\Repository\Values\Content\Relation; use eZ\Publish\API\Repository\Values\Content\RelationList; use eZ\Publish\Core\Repository\Values\ContentType\ContentType; use eZ\Publish\Core\Repository\Values\User\User; @@ -34,6 +35,7 @@ public function providerForPassTroughMethods() $contentInfo = new ContentInfo(); $versionInfo = new VersionInfo(); $content = $this->createMock(Content::class); + $relation = $this->createMock(Relation::class); $relationList = new RelationList(); $contentCreateStruct = new ContentCreateStruct(); $contentUpdateStruct = new ContentUpdateStruct(); @@ -45,95 +47,96 @@ public function providerForPassTroughMethods() // string $method, array $arguments, bool $return = true return [ - ['loadContentInfo', [42]], + ['loadContentInfo', [42], $contentInfo], ['loadContentInfoList', [[42]], [$contentInfo]], - ['loadContentInfoByRemoteId', ['f348tj4gorgji4']], + ['loadContentInfoByRemoteId', ['f348tj4gorgji4'], $contentInfo], - ['loadVersionInfo', [$contentInfo]], - ['loadVersionInfo', [$contentInfo, 3]], + ['loadVersionInfo', [$contentInfo], $versionInfo], + ['loadVersionInfo', [$contentInfo, 3], $versionInfo], - ['loadVersionInfoById', [42]], - ['loadVersionInfoById', [42, 3]], + ['loadVersionInfoById', [42], $versionInfo], + ['loadVersionInfoById', [42, 3], $versionInfo], - ['createContent', [$contentCreateStruct]], - ['createContent', [$contentCreateStruct, [44]]], + ['createContent', [$contentCreateStruct], $content], + ['createContent', [$contentCreateStruct, [44]], $content], - ['updateContentMetadata', [$contentInfo, $contentMetaStruct]], + ['updateContentMetadata', [$contentInfo, $contentMetaStruct], $content], - ['deleteContent', [$contentInfo]], + ['deleteContent', [$contentInfo], null], - ['createContentDraft', [$contentInfo]], - ['createContentDraft', [$contentInfo, $versionInfo]], - ['createContentDraft', [$contentInfo, $versionInfo, $user]], - ['createContentDraft', [$contentInfo, $versionInfo, $user, $language]], + ['createContentDraft', [$contentInfo], $content], + ['createContentDraft', [$contentInfo, $versionInfo], $content], + ['createContentDraft', [$contentInfo, $versionInfo, $user], $content], + ['createContentDraft', [$contentInfo, $versionInfo, $user, $language], $content], ['countContentDrafts', [], 0], ['countContentDrafts', [$user], 0], - ['loadContentDrafts', []], - ['loadContentDrafts', [$user]], + ['loadContentDrafts', [], [$content]], + ['loadContentDrafts', [$user], [$content]], ['loadContentDraftList', [], new ContentDraftList()], ['loadContentDraftList', [$user], new ContentDraftList()], ['loadContentDraftList', [$user, 1], new ContentDraftList()], ['loadContentDraftList', [$user, 1, 25], new ContentDraftList()], - ['updateContent', [$versionInfo, $contentUpdateStruct]], + ['updateContent', [$versionInfo, $contentUpdateStruct], $content], - ['publishVersion', [$versionInfo]], + ['publishVersion', [$versionInfo], $content], - ['deleteVersion', [$versionInfo]], + ['deleteVersion', [$versionInfo], null], - ['loadVersions', [$contentInfo]], + ['loadVersions', [$contentInfo], [$versionInfo]], - ['copyContent', [$contentInfo, $locationCreateStruct]], - ['copyContent', [$contentInfo, $locationCreateStruct, $versionInfo]], + ['copyContent', [$contentInfo, $locationCreateStruct], $content], + ['copyContent', [$contentInfo, $locationCreateStruct, $versionInfo], $content], - ['loadRelations', [$versionInfo]], + ['loadRelations', [$versionInfo], [$relation]], ['countReverseRelations', [$contentInfo], 0], ['loadReverseRelations', [$contentInfo], $relationList], ['loadReverseRelationList', [$contentInfo], $relationList], - ['addRelation', [$versionInfo, $contentInfo]], + ['addRelation', [$versionInfo, $contentInfo], null], - ['deleteRelation', [$versionInfo, $contentInfo]], + ['deleteRelation', [$versionInfo, $contentInfo], null], - ['removeTranslation', [$contentInfo, 'eng-GB']], + ['removeTranslation', [$contentInfo, 'eng-GB'], null], - ['deleteTranslation', [$contentInfo, 'eng-GB']], + ['deleteTranslation', [$contentInfo, 'eng-GB'], null], - ['deleteTranslationFromDraft', [$versionInfo, 'eng-GB']], + ['deleteTranslationFromDraft', [$versionInfo, 'eng-GB'], $content], ['hideContent', [$contentInfo], null], ['revealContent', [$contentInfo], null], - ['newContentCreateStruct', [$contentType, 'eng-GB']], - ['newContentMetadataUpdateStruct', []], - ['newContentUpdateStruct', []], + ['newContentCreateStruct', [$contentType, 'eng-GB'], $contentCreateStruct], + ['newContentMetadataUpdateStruct', [], $contentMetaStruct], + ['newContentUpdateStruct', [], $contentUpdateStruct], ]; } public function providerForLanguagesLookupMethods() { + $content = $this->createMock(Content::class); $contentInfo = new ContentInfo(); $versionInfo = new VersionInfo(); // string $method, array $arguments, bool $return, int $languageArgumentIndex return [ - ['loadContentByContentInfo', [$contentInfo], true, 1], - ['loadContentByContentInfo', [$contentInfo, self::LANG_ARG, 4, false], true, 1], + ['loadContentByContentInfo', [$contentInfo], $content, 1], + ['loadContentByContentInfo', [$contentInfo, self::LANG_ARG, 4, false], $content, 1], - ['loadContentByVersionInfo', [$versionInfo], true, 1], - ['loadContentByVersionInfo', [$versionInfo, self::LANG_ARG, false], true, 1], + ['loadContentByVersionInfo', [$versionInfo], $content, 1], + ['loadContentByVersionInfo', [$versionInfo, self::LANG_ARG, false], $content, 1], - ['loadContent', [42], true, 1], - ['loadContent', [42, self::LANG_ARG, 4, false], true, 1], + ['loadContent', [42], $content, 1], + ['loadContent', [42, self::LANG_ARG, 4, false], $content, 1], - ['loadContentByRemoteId', ['f348tj4gorgji4'], true, 1], - ['loadContentByRemoteId', ['f348tj4gorgji4', self::LANG_ARG, 4, false], true, 1], + ['loadContentByRemoteId', ['f348tj4gorgji4'], $content, 1], + ['loadContentByRemoteId', ['f348tj4gorgji4', self::LANG_ARG, 4, false], $content, 1], ['loadContentListByContentInfo', [[$contentInfo]], [], 1], ['loadContentListByContentInfo', [[$contentInfo], self::LANG_ARG, false], [], 1], diff --git a/eZ/Publish/Core/Repository/Tests/Service/Mock/ContentTest.php b/eZ/Publish/Core/Repository/Tests/Service/Mock/ContentTest.php index 76250a346c8..925e1d294ae 100644 --- a/eZ/Publish/Core/Repository/Tests/Service/Mock/ContentTest.php +++ b/eZ/Publish/Core/Repository/Tests/Service/Mock/ContentTest.php @@ -195,7 +195,7 @@ public function testLoadVersionInfoByIdAndVersionNumber() */ public function testLoadVersionInfoByIdThrowsNotFoundException() { - $this->expectException(\eZ\Publish\Core\Base\Exceptions\NotFoundException::class); + $this->expectException(NotFoundException::class); $contentServiceMock = $this->getPartlyMockedContentService(['loadContentInfo']); /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ @@ -368,6 +368,8 @@ public function testLoadVersionInfoByIdNonPublishedVersion() */ public function testLoadVersionInfo() { + $expectedResult = $this->createMock(VersionInfo::class); + $contentServiceMock = $this->getPartlyMockedContentService( ['loadVersionInfoById'] ); @@ -379,7 +381,7 @@ public function testLoadVersionInfo() $this->equalTo(42), $this->equalTo(7) )->will( - $this->returnValue('result') + $this->returnValue($expectedResult) ); $result = $contentServiceMock->loadVersionInfo( @@ -387,12 +389,12 @@ public function testLoadVersionInfo() 7 ); - $this->assertEquals('result', $result); + $this->assertEquals($expectedResult, $result); } public function testLoadContent() { - $contentService = $this->getPartlyMockedContentService(['internalLoadContent']); + $contentService = $this->getPartlyMockedContentService(['internalLoadContentById']); $content = $this->createMock(APIContent::class); $versionInfo = $this->createMock(APIVersionInfo::class); $permissionResolver = $this->getPermissionResolverMock(); @@ -408,7 +410,7 @@ public function testLoadContent() $contentId = 123; $contentService ->expects($this->once()) - ->method('internalLoadContent') + ->method('internalLoadContentById') ->with($contentId) ->will($this->returnValue($content)); @@ -423,7 +425,7 @@ public function testLoadContent() public function testLoadContentNonPublished() { - $contentService = $this->getPartlyMockedContentService(['internalLoadContent']); + $contentService = $this->getPartlyMockedContentService(['internalLoadContentById']); $content = $this->createMock(APIContent::class); $versionInfo = $this->createMock(APIVersionInfo::class); $permissionResolver = $this->getPermissionResolverMock(); @@ -435,7 +437,7 @@ public function testLoadContentNonPublished() $contentId = 123; $contentService ->expects($this->once()) - ->method('internalLoadContent') + ->method('internalLoadContentById') ->with($contentId) ->will($this->returnValue($content)); @@ -460,12 +462,12 @@ public function testLoadContentUnauthorized() $permissionResolver = $this->getPermissionResolverMock(); - $contentService = $this->getPartlyMockedContentService(['internalLoadContent']); + $contentService = $this->getPartlyMockedContentService(['internalLoadContentById']); $content = $this->createMock(APIContent::class); $contentId = 123; $contentService ->expects($this->once()) - ->method('internalLoadContent') + ->method('internalLoadContentById') ->with($contentId) ->will($this->returnValue($content)); @@ -483,7 +485,7 @@ public function testLoadContentNotPublishedStatusUnauthorized() $this->expectException(\eZ\Publish\Core\Base\Exceptions\UnauthorizedException::class); $permissionResolver = $this->getPermissionResolverMock(); - $contentService = $this->getPartlyMockedContentService(['internalLoadContent']); + $contentService = $this->getPartlyMockedContentService(['internalLoadContentById']); $content = $this->createMock(APIContent::class); $versionInfo = $this ->getMockBuilder(APIVersionInfo::class) @@ -495,7 +497,7 @@ public function testLoadContentNotPublishedStatusUnauthorized() $contentId = 123; $contentService ->expects($this->once()) - ->method('internalLoadContent') + ->method('internalLoadContentById') ->with($contentId) ->will($this->returnValue($content)); @@ -515,82 +517,134 @@ public function testLoadContentNotPublishedStatusUnauthorized() } /** - * @dataProvider internalLoadContentProvider + * @dataProvider internalLoadContentProviderById */ - public function testInternalLoadContent($id, $languages, $versionNo, $isRemoteId, $useAlwaysAvailable) + public function testInternalLoadContentById(int $id, ?array $languages, ?int $versionNo, bool $useAlwaysAvailable): void { - $contentService = $this->getPartlyMockedContentService(); + if (!empty($languages) && $useAlwaysAvailable) { + $spiContentInfo = new SPIContentInfo(['id' => $id, 'alwaysAvailable' => false]); + } else { + $spiContentInfo = new SPIContentInfo(['id' => $id]); + } + + $spiContent = new SPIContent([ + 'versionInfo' => new VersionInfo([ + 'contentInfo' => new ContentInfo([ + 'id' => 42, + 'contentTypeId' => 123, + ]), + ]), + ]); + /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ $contentHandler = $this->getPersistenceMock()->contentHandler(); - $realId = $id; - - if ($isRemoteId) { - $realId = 123; - $spiContentInfo = new SPIContentInfo(['currentVersionNo' => $versionNo ?: 7, 'id' => $realId]); - $contentHandler - ->expects($this->once()) - ->method('loadContentInfoByRemoteId') - ->with($id) - ->will($this->returnValue($spiContentInfo)); - } elseif (!empty($languages) && $useAlwaysAvailable) { - $spiContentInfo = new SPIContentInfo(['alwaysAvailable' => false]); - $contentHandler - ->expects($this->once()) - ->method('loadContentInfo') - ->with($id) - ->will($this->returnValue($spiContentInfo)); - } + $contentHandler + ->expects($this->once()) + ->method('loadContentInfo') + ->with($id) + ->will($this->returnValue($spiContentInfo)); + + $contentHandler + ->expects($this->once()) + ->method('load') + ->with($id, $versionNo, $languages) + ->willReturn($spiContent); + + $contentService = $this->getPartlyMockedContentService(); + + $expectedContent = $this->mockBuildContentDomainObject($spiContent, $languages); + $actualContent = $contentService->internalLoadContentById($id, $languages, $versionNo, $useAlwaysAvailable); + + $this->assertSame($expectedContent, $actualContent); + } + + /** + * @dataProvider internalLoadContentProviderByRemoteId + */ + public function testInternalLoadContentByRemoteId(string $remoteId, ?array $languages, ?int $versionNo, bool $useAlwaysAvailable) + { + $realId = 123; + + $spiContentInfo = new SPIContentInfo([ + 'currentVersionNo' => $versionNo ?: 7, + 'id' => $realId, + ]); $spiContent = new SPIContent([ 'versionInfo' => new VersionInfo([ - 'contentInfo' => new ContentInfo(['id' => 42, 'contentTypeId' => 123]), + 'contentInfo' => new ContentInfo(['id' => 42, 'contentTypeId' => 123]), ]), ]); + + $contentService = $this->getPartlyMockedContentService(); + /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ + $contentHandler = $this->getPersistenceMock()->contentHandler(); + $contentHandler + ->expects($this->once()) + ->method('loadContentInfoByRemoteId') + ->with($remoteId) + ->will($this->returnValue($spiContentInfo)); + $contentHandler ->expects($this->once()) ->method('load') ->with($realId, $versionNo, $languages) ->willReturn($spiContent); - $content = $this->mockBuildContentDomainObject($spiContent, $languages); + $expectedContent = $this->mockBuildContentDomainObject($spiContent, $languages); - $this->assertSame( - $content, - $contentService->internalLoadContent($id, $languages, $versionNo, $isRemoteId, $useAlwaysAvailable) + $actualContent = $contentService->internalLoadContentByRemoteId( + $remoteId, + $languages, + $versionNo, + $useAlwaysAvailable ); + + $this->assertSame($expectedContent, $actualContent); } - public function internalLoadContentProvider() + public function internalLoadContentProviderById(): array { return [ - [123, null, null, false, false], - [123, null, 456, false, false], - [456, null, 123, false, true], - [456, null, 2, false, false], - [456, ['eng-GB'], 2, false, true], - [456, ['eng-GB', 'fre-FR'], null, false, false], - [456, ['eng-GB', 'fre-FR', 'nor-NO'], 2, false, false], - // With remoteId - [123, null, null, true, false], - ['someRemoteId', null, 456, true, false], - [456, null, 123, true, false], - ['someRemoteId', null, 2, true, false], - ['someRemoteId', ['eng-GB'], 2, true, false], - [456, ['eng-GB', 'fre-FR'], null, true, false], - ['someRemoteId', ['eng-GB', 'fre-FR', 'nor-NO'], 2, true, false], + [123, null, null, false], + [123, null, 456, false], + [456, null, 123, true], + [456, null, 2, false], + [456, ['eng-GB'], 2, true], + [456, ['eng-GB', 'fre-FR'], null, false], + [456, ['eng-GB', 'fre-FR', 'nor-NO'], 2, false], ]; } - public function testInternalLoadContentNotFound() + public function internalLoadContentProviderByRemoteId(): array { - $this->expectException(\eZ\Publish\Core\Base\Exceptions\NotFoundException::class); + return [ + ['123', null, null, false], + ['someRemoteId', null, 456, false], + ['456', null, 123, false], + ['someRemoteId', null, 2, false], + ['someRemoteId', ['eng-GB'], 2, false], + ['456', ['eng-GB', 'fre-FR'], null, false], + ['someRemoteId', ['eng-GB', 'fre-FR', 'nor-NO'], 2, false], + ]; + } + + public function testInternalLoadContentByIdNotFound(): void + { + $this->expectException(NotFoundException::class); - $contentService = $this->getPartlyMockedContentService(); - /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ - $contentHandler = $this->getPersistenceMock()->contentHandler(); $id = 123; $versionNo = 7; $languages = null; + + /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ + $contentHandler = $this->getPersistenceMock()->contentHandler(); + $contentHandler + ->expects($this->once()) + ->method('loadContentInfo') + ->with($id) + ->willReturn(new SPIContent\ContentInfo(['id' => $id])); + $contentHandler ->expects($this->once()) ->method('load') @@ -601,7 +655,37 @@ public function testInternalLoadContentNotFound() ) ); - $contentService->internalLoadContent($id, $languages, $versionNo); + $contentService = $this->getPartlyMockedContentService(); + $contentService->internalLoadContentById($id, $languages, $versionNo); + } + + public function testInternalLoadContentByRemoteIdNotFound(): void + { + $this->expectException(NotFoundException::class); + + $remoteId = 'dca290623518d393126d3408b45af6ee'; + $id = 123; + $versionNo = 7; + $languages = null; + + /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ + $contentHandler = $this->getPersistenceMock()->contentHandler(); + $contentHandler + ->expects($this->once()) + ->method('loadContentInfoByRemoteId') + ->with($remoteId) + ->willReturn(new SPIContent\ContentInfo(['id' => $id])); + + $contentHandler + ->expects($this->once()) + ->method('load') + ->with($id, $versionNo, $languages) + ->willThrowException( + $this->createMock(APINotFoundException::class) + ); + + $contentService = $this->getPartlyMockedContentService(); + $contentService->internalLoadContentByRemoteId($remoteId, $languages, $versionNo); } /** @@ -611,6 +695,8 @@ public function testInternalLoadContentNotFound() */ public function testLoadContentByContentInfo() { + $expectedResult = $this->createMock(Content::class); + $contentServiceMock = $this->getPartlyMockedContentService( ['loadContent'] ); @@ -624,7 +710,7 @@ public function testLoadContentByContentInfo() $this->equalTo(7), $this->equalTo(false) )->will( - $this->returnValue('result') + $this->returnValue($expectedResult) ); $result = $contentServiceMock->loadContentByContentInfo( @@ -633,7 +719,7 @@ public function testLoadContentByContentInfo() 7 ); - $this->assertEquals('result', $result); + $this->assertEquals($expectedResult, $result); } /** @@ -643,6 +729,8 @@ public function testLoadContentByContentInfo() */ public function testLoadContentByVersionInfo() { + $expectedResult = $this->createMock(Content::class); + $contentServiceMock = $this->getPartlyMockedContentService( ['loadContent'] ); @@ -656,7 +744,7 @@ public function testLoadContentByVersionInfo() $this->equalTo(7), $this->equalTo(false) )->will( - $this->returnValue('result') + $this->returnValue($expectedResult) ); $result = $contentServiceMock->loadContentByVersionInfo( @@ -669,7 +757,7 @@ public function testLoadContentByVersionInfo() ['cro-HR'] ); - $this->assertEquals('result', $result); + $this->assertEquals($expectedResult, $result); } /** @@ -682,7 +770,7 @@ public function testDeleteContentThrowsUnauthorizedException() $this->expectException(\eZ\Publish\Core\Base\Exceptions\UnauthorizedException::class); $permissionResolver = $this->getPermissionResolverMock(); - $contentService = $this->getPartlyMockedContentService(['internalLoadContentInfo']); + $contentService = $this->getPartlyMockedContentService(['internalLoadContentInfoById']); $contentInfo = $this->createMock(APIContentInfo::class); $contentInfo->expects($this->any()) @@ -691,7 +779,7 @@ public function testDeleteContentThrowsUnauthorizedException() ->will($this->returnValue(42)); $contentService->expects($this->once()) - ->method('internalLoadContentInfo') + ->method('internalLoadContentInfoById') ->with(42) ->will($this->returnValue($contentInfo)); @@ -719,7 +807,7 @@ public function testDeleteContent() ->with('content', 'remove') ->will($this->returnValue(true)); - $contentService = $this->getPartlyMockedContentService(['internalLoadContentInfo']); + $contentService = $this->getPartlyMockedContentService(['internalLoadContentInfoById']); /** @var \PHPUnit\Framework\MockObject\MockObject $urlAliasHandler */ $urlAliasHandler = $this->getPersistenceMock()->urlAliasHandler(); /** @var \PHPUnit\Framework\MockObject\MockObject $locationHandler */ @@ -730,7 +818,7 @@ public function testDeleteContent() $contentInfo = $this->createMock(APIContentInfo::class); $contentService->expects($this->once()) - ->method('internalLoadContentInfo') + ->method('internalLoadContentInfoById') ->with(42) ->will($this->returnValue($contentInfo)); @@ -783,14 +871,14 @@ public function testDeleteContentWithRollback() ->with('content', 'remove') ->will($this->returnValue(true)); - $contentService = $this->getPartlyMockedContentService(['internalLoadContentInfo']); + $contentService = $this->getPartlyMockedContentService(['internalLoadContentInfoById']); /** @var \PHPUnit\Framework\MockObject\MockObject $locationHandler */ $locationHandler = $this->getPersistenceMock()->locationHandler(); $contentInfo = $this->createMock(APIContentInfo::class); $contentService->expects($this->once()) - ->method('internalLoadContentInfo') + ->method('internalLoadContentInfoById') ->with(42) ->will($this->returnValue($contentInfo)); @@ -1020,7 +1108,7 @@ public function testCreateContentThrowsInvalidArgumentExceptionDuplicateRemoteId $mockedService->expects($this->once()) ->method('loadContentByRemoteId') ->with($contentCreateStruct->remoteId) - ->will($this->returnValue('Hello...')); + ->will($this->returnValue($this->createMock(Content::class))); $mockedService->createContent( new ContentCreateStruct( @@ -1365,7 +1453,8 @@ function (ValueStub $value) use ($emptyValue) { ->with( $this->isInstanceOf(SPIContent::class), $this->equalTo($contentType) - ); + ) + ->willReturn($this->createMock(APIContent::class)); $mockedService->createContent($contentCreateStruct, []); } @@ -2541,7 +2630,8 @@ public function testCreateContentWithLocations() ->with( $this->isInstanceOf(SPIContent::class), $this->isInstanceOf(APIContentType::class) - ); + ) + ->willReturn($this->createMock(APIContent::class)); $repositoryMock->expects($this->once())->method('commit'); @@ -2778,7 +2868,8 @@ public function testCreateContentObjectStates() ->with( $this->isInstanceOf(SPIContent::class), $this->isInstanceOf(APIContentType::class) - ); + ) + ->willReturn($this->createMock(APIContent::class)); $repositoryMock->expects($this->once())->method('commit'); @@ -3310,7 +3401,8 @@ function (ValueStub $value) use ($emptyValue) { ->with( $this->isInstanceOf(SPIContent::class), $this->isInstanceOf(APIContentType::class) - ); + ) + ->willReturn($this->createMock(APIContent::class)); $mockedService->updateContent($content->versionInfo, $contentUpdateStruct); } @@ -5452,7 +5544,7 @@ public function testCopyContent() $repositoryMock = $this->getRepositoryMock(); $contentService = $this->getPartlyMockedContentService([ 'internalLoadContentInfo', - 'internalLoadContent', + 'internalLoadContentById', 'getUnixTimestamp', ]); $locationServiceMock = $this->getLocationServiceMock(); @@ -5558,7 +5650,7 @@ public function testCopyContent() ); $contentService->expects($this->once()) - ->method('internalLoadContent') + ->method('internalLoadContentById') ->with( $content->id ) @@ -5583,8 +5675,7 @@ public function testCopyContentWithVersionInfo() { $repositoryMock = $this->getRepositoryMock(); $contentService = $this->getPartlyMockedContentService([ - 'internalLoadContentInfo', - 'internalLoadContent', + 'internalLoadContentById', 'getUnixTimestamp', ]); $locationServiceMock = $this->getLocationServiceMock(); @@ -5687,7 +5778,7 @@ public function testCopyContentWithVersionInfo() ); $contentService->expects($this->once()) - ->method('internalLoadContent') + ->method('internalLoadContentById') ->with( $content->id ) diff --git a/eZ/Publish/Core/Repository/Tests/Service/Mock/SearchTest.php b/eZ/Publish/Core/Repository/Tests/Service/Mock/SearchTest.php index d06b74450be..77b692ed20f 100644 --- a/eZ/Publish/Core/Repository/Tests/Service/Mock/SearchTest.php +++ b/eZ/Publish/Core/Repository/Tests/Service/Mock/SearchTest.php @@ -630,11 +630,12 @@ public function testFindSingle() ->will($this->returnValue(true)); $languageFilter = []; - $spiContentInfo = new SPIContentInfo(); + $spiContentInfo = new SPIContentInfo(['id' => 123]); $contentMock = $this->getMockForAbstractClass(Content::class); /* @var \PHPUnit\Framework\MockObject\MockObject $searchHandlerMock */ - $searchHandlerMock->expects($this->once()) + $searchHandlerMock + ->expects($this->once()) ->method('findSingle') ->with($this->equalTo($criterionMock), $this->equalTo($languageFilter)) ->will($this->returnValue($spiContentInfo)); @@ -644,7 +645,7 @@ public function testFindSingle() $contentServiceMock ->expects($this->once()) - ->method('internalLoadContent') + ->method('internalLoadContentById') ->will($this->returnValue($contentMock)); $result = $service->findSingle($criterionMock, $languageFilter, true); diff --git a/eZ/Publish/Core/Repository/TrashService.php b/eZ/Publish/Core/Repository/TrashService.php index dbb2cdb5e63..e6b4dcf1d87 100644 --- a/eZ/Publish/Core/Repository/TrashService.php +++ b/eZ/Publish/Core/Repository/TrashService.php @@ -102,7 +102,7 @@ public function loadTrashItem(int $trashItemId): APITrashItem $spiTrashItem = $this->persistenceHandler->trashHandler()->loadTrashItem($trashItemId); $trash = $this->buildDomainTrashItemObject( $spiTrashItem, - $this->repository->getContentService()->internalLoadContent($spiTrashItem->contentId) + $this->repository->getContentService()->internalLoadContentById($spiTrashItem->contentId) ); if (!$this->permissionResolver->canUser('content', 'read', $trash->getContentInfo())) { throw new UnauthorizedException('content', 'read'); @@ -152,7 +152,7 @@ public function trash(Location $location): ?APITrashItem return isset($spiTrashItem) ? $this->buildDomainTrashItemObject( $spiTrashItem, - $this->repository->getContentService()->internalLoadContent($spiTrashItem->contentId) + $this->repository->getContentService()->internalLoadContentById($spiTrashItem->contentId) ) : null; } catch (Exception $e) { diff --git a/eZ/Publish/Core/Repository/UserService.php b/eZ/Publish/Core/Repository/UserService.php index 63b9eea242a..b597c94cda0 100644 --- a/eZ/Publish/Core/Repository/UserService.php +++ b/eZ/Publish/Core/Repository/UserService.php @@ -226,7 +226,7 @@ public function loadSubUserGroups(APIUserGroup $userGroup, $offset = 0, $limit = $subUserGroups = []; foreach ($searchResult->searchHits as $searchHit) { $subUserGroups[] = $this->buildDomainUserGroupObject( - $this->repository->getContentService()->internalLoadContent( + $this->repository->getContentService()->internalLoadContentById( $searchHit->valueObject->contentInfo->id, $prioritizedLanguages ) @@ -466,7 +466,7 @@ public function createUser(APIUserCreateStruct $userCreateStruct, array $parentG public function loadUser($userId, array $prioritizedLanguages = []) { /** @var \eZ\Publish\API\Repository\Values\Content\Content $content */ - $content = $this->repository->getContentService()->internalLoadContent($userId, $prioritizedLanguages); + $content = $this->repository->getContentService()->internalLoadContentById($userId, $prioritizedLanguages); // Get spiUser value from Field Value foreach ($content->getFields() as $field) { if (!$field->value instanceof UserValue) { @@ -986,7 +986,7 @@ public function loadUserGroupsOfUser(APIUser $user, $offset = 0, $limit = 25, ar $userGroups = []; foreach ($searchResult->searchHits as $resultItem) { $userGroups[] = $this->buildDomainUserGroupObject( - $this->repository->getContentService()->internalLoadContent( + $this->repository->getContentService()->internalLoadContentById( $resultItem->valueObject->contentInfo->id, $prioritizedLanguages ) @@ -1044,7 +1044,7 @@ public function loadUsersOfUserGroup( foreach ($searchResult->searchHits as $resultItem) { $users[] = $this->buildDomainUserObject( $this->userHandler->load($resultItem->valueObject->contentInfo->id), - $this->repository->getContentService()->internalLoadContent( + $this->repository->getContentService()->internalLoadContentById( $resultItem->valueObject->contentInfo->id, $prioritizedLanguages ) @@ -1271,7 +1271,7 @@ protected function buildDomainUserObject( array $prioritizedLanguages = [] ) { if ($content === null) { - $content = $this->repository->getContentService()->internalLoadContent( + $content = $this->repository->getContentService()->internalLoadContentById( $spiUser->id, $prioritizedLanguages ); diff --git a/eZ/Publish/SPI/Repository/Decorator/ContentServiceDecorator.php b/eZ/Publish/SPI/Repository/Decorator/ContentServiceDecorator.php index b654f6836a9..43a1b401935 100644 --- a/eZ/Publish/SPI/Repository/Decorator/ContentServiceDecorator.php +++ b/eZ/Publish/SPI/Repository/Decorator/ContentServiceDecorator.php @@ -33,7 +33,7 @@ public function __construct(ContentService $innerService) $this->innerService = $innerService; } - public function loadContentInfo($contentId): ContentInfo + public function loadContentInfo(int $contentId): ContentInfo { return $this->innerService->loadContentInfo($contentId); } @@ -43,21 +43,21 @@ public function loadContentInfoList(array $contentIds): iterable return $this->innerService->loadContentInfoList($contentIds); } - public function loadContentInfoByRemoteId($remoteId): ContentInfo + public function loadContentInfoByRemoteId(string $remoteId): ContentInfo { return $this->innerService->loadContentInfoByRemoteId($remoteId); } public function loadVersionInfo( ContentInfo $contentInfo, - $versionNo = null + ?int $versionNo = null ): VersionInfo { return $this->innerService->loadVersionInfo($contentInfo, $versionNo); } public function loadVersionInfoById( - $contentId, - $versionNo = null + int $contentId, + ?int $versionNo = null ): VersionInfo { return $this->innerService->loadVersionInfoById($contentId, $versionNo); } @@ -65,8 +65,8 @@ public function loadVersionInfoById( public function loadContentByContentInfo( ContentInfo $contentInfo, array $languages = null, - $versionNo = null, - $useAlwaysAvailable = true + ?int $versionNo = null, + bool $useAlwaysAvailable = true ): Content { return $this->innerService->loadContentByContentInfo($contentInfo, $languages, $versionNo, $useAlwaysAvailable); } @@ -74,25 +74,25 @@ public function loadContentByContentInfo( public function loadContentByVersionInfo( VersionInfo $versionInfo, array $languages = null, - $useAlwaysAvailable = true + bool $useAlwaysAvailable = true ): Content { return $this->innerService->loadContentByVersionInfo($versionInfo, $languages, $useAlwaysAvailable); } public function loadContent( - $contentId, + int $contentId, array $languages = null, - $versionNo = null, - $useAlwaysAvailable = true + ?int $versionNo = null, + bool $useAlwaysAvailable = true ): Content { return $this->innerService->loadContent($contentId, $languages, $versionNo, $useAlwaysAvailable); } public function loadContentByRemoteId( - $remoteId, + string $remoteId, array $languages = null, - $versionNo = null, - $useAlwaysAvailable = true + ?int $versionNo = null, + bool $useAlwaysAvailable = true ): Content { return $this->innerService->loadContentByRemoteId($remoteId, $languages, $versionNo, $useAlwaysAvailable); } @@ -100,8 +100,8 @@ public function loadContentByRemoteId( public function loadContentListByContentInfo( array $contentInfoList, array $languages = [], - $useAlwaysAvailable = true - ): array { + bool $useAlwaysAvailable = true + ): iterable { return $this->innerService->loadContentListByContentInfo($contentInfoList, $languages, $useAlwaysAvailable); } @@ -119,15 +119,15 @@ public function updateContentMetadata( return $this->innerService->updateContentMetadata($contentInfo, $contentMetadataUpdateStruct); } - public function deleteContent(ContentInfo $contentInfo): array + public function deleteContent(ContentInfo $contentInfo): iterable { return $this->innerService->deleteContent($contentInfo); } public function createContentDraft( ContentInfo $contentInfo, - VersionInfo $versionInfo = null, - User $creator = null, + ?VersionInfo $versionInfo = null, + ?User $creator = null, ?Language $language = null ): Content { return $this->innerService->createContentDraft($contentInfo, $versionInfo, $creator, $language); @@ -138,7 +138,7 @@ public function countContentDrafts(User $user = null): int return $this->innerService->countContentDrafts($user); } - public function loadContentDrafts(User $user = null): array + public function loadContentDrafts(?User $user = null): iterable { return $this->innerService->loadContentDrafts($user); } @@ -160,12 +160,12 @@ public function publishVersion(VersionInfo $versionInfo, array $translations = L return $this->innerService->publishVersion($versionInfo, $translations); } - public function deleteVersion(VersionInfo $versionInfo) + public function deleteVersion(VersionInfo $versionInfo): void { - return $this->innerService->deleteVersion($versionInfo); + $this->innerService->deleteVersion($versionInfo); } - public function loadVersions(ContentInfo $contentInfo, ?int $status = null): array + public function loadVersions(ContentInfo $contentInfo, ?int $status = null): iterable { return $this->innerService->loadVersions($contentInfo, $status); } @@ -173,12 +173,12 @@ public function loadVersions(ContentInfo $contentInfo, ?int $status = null): arr public function copyContent( ContentInfo $contentInfo, LocationCreateStruct $destinationLocationCreateStruct, - VersionInfo $versionInfo = null + ?VersionInfo $versionInfo = null ): Content { return $this->innerService->copyContent($contentInfo, $destinationLocationCreateStruct, $versionInfo); } - public function loadRelations(VersionInfo $versionInfo): array + public function loadRelations(VersionInfo $versionInfo): iterable { return $this->innerService->loadRelations($versionInfo); } @@ -188,7 +188,7 @@ public function countReverseRelations(ContentInfo $contentInfo): int return $this->innerService->countReverseRelations($contentInfo); } - public function loadReverseRelations(ContentInfo $contentInfo): array + public function loadReverseRelations(ContentInfo $contentInfo): iterable { return $this->innerService->loadReverseRelations($contentInfo); } @@ -208,27 +208,27 @@ public function addRelation( public function deleteRelation( VersionInfo $sourceVersion, ContentInfo $destinationContent - ) { - return $this->innerService->deleteRelation($sourceVersion, $destinationContent); + ): void { + $this->innerService->deleteRelation($sourceVersion, $destinationContent); } public function removeTranslation( ContentInfo $contentInfo, - $languageCode - ) { - return $this->innerService->removeTranslation($contentInfo, $languageCode); + string $languageCode + ): void { + $this->innerService->removeTranslation($contentInfo, $languageCode); } public function deleteTranslation( ContentInfo $contentInfo, - $languageCode - ) { - return $this->innerService->deleteTranslation($contentInfo, $languageCode); + string $languageCode + ): void { + $this->innerService->deleteTranslation($contentInfo, $languageCode); } public function deleteTranslationFromDraft( VersionInfo $versionInfo, - $languageCode + string $languageCode ): Content { return $this->innerService->deleteTranslationFromDraft($versionInfo, $languageCode); } @@ -245,7 +245,7 @@ public function revealContent(ContentInfo $contentInfo): void public function newContentCreateStruct( ContentType $contentType, - $mainLanguageCode + string $mainLanguageCode ): ContentCreateStruct { return $this->innerService->newContentCreateStruct($contentType, $mainLanguageCode); } diff --git a/eZ/Publish/SPI/Repository/Tests/Decorator/ContentServiceDecoratorTest.php b/eZ/Publish/SPI/Repository/Tests/Decorator/ContentServiceDecoratorTest.php index b26b3c480bf..ce6a76ba039 100644 --- a/eZ/Publish/SPI/Repository/Tests/Decorator/ContentServiceDecoratorTest.php +++ b/eZ/Publish/SPI/Repository/Tests/Decorator/ContentServiceDecoratorTest.php @@ -25,6 +25,11 @@ class ContentServiceDecoratorTest extends TestCase { + private const EXAMPLE_CONTENT_ID = 1; + private const EXAMPLE_LANGUAGE_CODE = 'eng-GB'; + private const EXAMPLE_CONTENT_REMOTE_ID = 'example'; + private const EXAMPLE_VERSION_NO = 1; + protected function createDecorator(MockObject $service): ContentService { return new class($service) extends ContentServiceDecorator { @@ -41,7 +46,7 @@ public function testLoadContentInfoDecorator() $serviceMock = $this->createServiceMock(); $decoratedService = $this->createDecorator($serviceMock); - $parameters = ['random_value_5ced05ce154118.08177784']; + $parameters = [self::EXAMPLE_CONTENT_ID]; $serviceMock->expects($this->once())->method('loadContentInfo')->with(...$parameters)->willReturn($this->createMock(ContentInfo::class)); @@ -79,7 +84,7 @@ public function testLoadVersionInfoDecorator() $parameters = [ $this->createMock(ContentInfo::class), - 'random_value_5ced05ce1541e5.29503590', + self::EXAMPLE_VERSION_NO, ]; $serviceMock->expects($this->once())->method('loadVersionInfo')->with(...$parameters)->willReturn($this->createMock(VersionInfo::class)); @@ -93,8 +98,8 @@ public function testLoadVersionInfoByIdDecorator() $decoratedService = $this->createDecorator($serviceMock); $parameters = [ - 'random_value_5ced05ce154212.71705283', - 'random_value_5ced05ce154226.14877654', + self::EXAMPLE_CONTENT_ID, + self::EXAMPLE_VERSION_NO, ]; $serviceMock->expects($this->once())->method('loadVersionInfoById')->with(...$parameters)->willReturn($this->createMock(VersionInfo::class)); @@ -109,9 +114,9 @@ public function testLoadContentByContentInfoDecorator() $parameters = [ $this->createMock(ContentInfo::class), - ['random_value_5ced05ce154263.26527866'], - 'random_value_5ced05ce154274.48633738', - 'random_value_5ced05ce154288.12181629', + [self::EXAMPLE_LANGUAGE_CODE], + self::EXAMPLE_VERSION_NO, + true, ]; $serviceMock->expects($this->once())->method('loadContentByContentInfo')->with(...$parameters)->willReturn($this->createMock(Content::class)); @@ -126,8 +131,8 @@ public function testLoadContentByVersionInfoDecorator() $parameters = [ $this->createMock(VersionInfo::class), - ['random_value_5ced05ce154b93.03098248'], - 'random_value_5ced05ce154bc5.33425740', + [self::EXAMPLE_LANGUAGE_CODE], + true, ]; $serviceMock->expects($this->once())->method('loadContentByVersionInfo')->with(...$parameters)->willReturn($this->createMock(Content::class)); @@ -141,10 +146,10 @@ public function testLoadContentDecorator() $decoratedService = $this->createDecorator($serviceMock); $parameters = [ - 'random_value_5ced05ce154c25.68443488', - ['random_value_5ced05ce154c34.23275680'], - 'random_value_5ced05ce154c41.25945372', - 'random_value_5ced05ce154c55.22828466', + self::EXAMPLE_CONTENT_ID, + [self::EXAMPLE_LANGUAGE_CODE], + self::EXAMPLE_VERSION_NO, + true, ]; $serviceMock->expects($this->once())->method('loadContent')->with(...$parameters)->willReturn($this->createMock(Content::class)); @@ -158,10 +163,10 @@ public function testLoadContentByRemoteIdDecorator() $decoratedService = $this->createDecorator($serviceMock); $parameters = [ - 'random_value_5ced05ce154c88.14138266', - ['random_value_5ced05ce154c96.62330223'], - 'random_value_5ced05ce154ca4.47689455', - 'random_value_5ced05ce154cb6.74747836', + self::EXAMPLE_CONTENT_REMOTE_ID, + [self::EXAMPLE_LANGUAGE_CODE], + self::EXAMPLE_VERSION_NO, + true, ]; $serviceMock->expects($this->once())->method('loadContentByRemoteId')->with(...$parameters)->willReturn($this->createMock(Content::class)); @@ -175,9 +180,9 @@ public function testLoadContentListByContentInfoDecorator() $decoratedService = $this->createDecorator($serviceMock); $parameters = [ - ['random_value_5ced05ce154ce0.53430215'], - ['random_value_5ced05ce154cf5.73360157'], - 'random_value_5ced05ce154d01.47432661', + [$this->createMock(ContentInfo::class)], + [self::EXAMPLE_LANGUAGE_CODE], + true, ]; $serviceMock->expects($this->once())->method('loadContentListByContentInfo')->with(...$parameters)->willReturn([]); @@ -289,7 +294,7 @@ public function testDeleteVersionDecorator() $parameters = [$this->createMock(VersionInfo::class)]; - $serviceMock->expects($this->once())->method('deleteVersion')->with(...$parameters)->willReturn($this->createMock(Content::class)); + $serviceMock->expects($this->once())->method('deleteVersion')->with(...$parameters); $decoratedService->deleteVersion(...$parameters); } @@ -371,7 +376,7 @@ public function testDeleteRelationDecorator() $this->createMock(ContentInfo::class), ]; - $serviceMock->expects($this->once())->method('deleteRelation')->with(...$parameters)->willReturn($this->createMock(Content::class)); + $serviceMock->expects($this->once())->method('deleteRelation')->with(...$parameters); $decoratedService->deleteRelation(...$parameters); } @@ -383,10 +388,10 @@ public function testRemoveTranslationDecorator() $parameters = [ $this->createMock(ContentInfo::class), - 'random_value_5ced05ce156ca0.26332407', + self::EXAMPLE_LANGUAGE_CODE, ]; - $serviceMock->expects($this->once())->method('removeTranslation')->with(...$parameters)->willReturn($this->createMock(Content::class)); + $serviceMock->expects($this->once())->method('removeTranslation')->with(...$parameters); $decoratedService->removeTranslation(...$parameters); } @@ -398,10 +403,10 @@ public function testDeleteTranslationDecorator() $parameters = [ $this->createMock(ContentInfo::class), - 'random_value_5ced05ce156d02.84155908', + self::EXAMPLE_LANGUAGE_CODE, ]; - $serviceMock->expects($this->once())->method('deleteTranslation')->with(...$parameters)->willReturn($this->createMock(Content::class)); + $serviceMock->expects($this->once())->method('deleteTranslation')->with(...$parameters); $decoratedService->deleteTranslation(...$parameters); }