Skip to content

Commit

Permalink
IBX-6017: Applied review remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
barw4 committed Jul 18, 2023
1 parent 7e6f77e commit 5b5b605
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 104 deletions.
26 changes: 13 additions & 13 deletions eZ/Publish/API/Repository/ContentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ public function loadVersionInfo(ContentInfo $contentInfo, ?int $versionNo = null
*/
public function loadVersionInfoById(int $contentId, ?int $versionNo = null): VersionInfo;

/**
* Bulk-load VersionInfo items by the list of ContentInfo Value Objects.
*
* @param array<\eZ\Publish\API\Repository\Values\Content\ContentInfo> $contentInfoList
*
* @return array<int, \eZ\Publish\API\Repository\Values\Content\VersionInfo> List of VersionInfo items with Content Ids as keys
*
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
*/
public function loadVersionInfoListByContentInfo(array $contentInfoList): array;

/**
* Loads content in a version for the given content info object.
*
Expand Down Expand Up @@ -577,17 +590,4 @@ public function find(Filter $filter, ?array $languages = null): ContentList;
* for a SiteAccess in a current context will be used.
*/
public function count(Filter $filter, ?array $languages = null): int;

/**
* Bulk-load VersionInfo items by the list of ContentInfo Value Objects.
*
* @param array<\eZ\Publish\API\Repository\Values\Content\ContentInfo> $contentInfoList
*
* @return array<int, \eZ\Publish\API\Repository\Values\Content\VersionInfo> List of VersionInfo items with Content Ids as keys
*
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
*/
public function loadVersionInfoListByContentInfo(array $contentInfoList): array;
}
29 changes: 0 additions & 29 deletions eZ/Publish/API/Repository/Tests/ContentServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6685,33 +6685,4 @@ private function createContentWithReverseRelations(array $drafts)

return $contentWithReverseRelations;
}

/**
* @covers \eZ\Publish\API\Repository\ContentService::LoadVersionInfoListByContentInfo
*
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
public function testLoadVersionInfoListByContentInfo(): void
{
$folder1 = $this->createFolder(['eng-GB' => 'Folder1'], 2);
$folder2 = $this->createFolder(['eng-GB' => 'Folder2'], 2);

$versionInfoList = $this->contentService->loadVersionInfoListByContentInfo([
$folder1->contentInfo,
$folder2->contentInfo,
]);

self::assertCount(2, $versionInfoList);

foreach ($versionInfoList as $versionInfo) {
$loadedVersionInfo = $this->contentService->loadVersionInfo(
$versionInfo->getContentInfo(),
$versionInfo->versionNo
);
self::assertEquals($loadedVersionInfo, $versionInfo);
}
}
}
6 changes: 1 addition & 5 deletions eZ/Publish/Core/Persistence/Cache/ContentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -580,11 +580,7 @@ private function getCacheTagsForVersion(VersionInfo $versionInfo, array $tags =

return $getContentInfoTagsFn($contentInfo, $tags);
}

/**
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
*/

public function loadVersionInfoList(array $contentIds): array
{
return $this->getMultipleCacheValues(
Expand Down
3 changes: 1 addition & 2 deletions eZ/Publish/Core/Persistence/Legacy/Content/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,7 @@ abstract public function deleteTranslationFromVersion(
/**
* @param array<int> $contentIds
*
* @throws \Doctrine\DBAL\Driver\Exception
* @throws \Doctrine\DBAL\Exception
* @throws \eZ\Publish\Core\Base\Exceptions\DatabaseException
*/
abstract public function loadVersionInfoList(array $contentIds): array;
}
4 changes: 2 additions & 2 deletions eZ/Publish/Core/Persistence/Legacy/Content/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -906,13 +906,13 @@ static function ($lang) use ($languageCode) {
}

/**
* @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
*/
public function loadVersionInfoList(array $contentIds): array
{
$rows = $this->contentGateway->loadVersionInfoList($contentIds);
$mappedRows = array_map(
static function ($row) {
static function (array $row): array {
return [
'id' => $row['ezcontentobject_id'],
'version' => $row['ezcontentobject_version_version'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1468,7 +1468,7 @@ public function testSetStatus()
/**
* @covers \eZ\Publish\Core\Persistence\Legacy\Content\Handler::loadVersionInfoList
*
* @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
*/
public function testLoadVersionInfoList(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2116,28 +2116,4 @@ private function assertContentVersionAttributesLanguages(
->orderBy('id')
);
}

/**
* @throws \Doctrine\DBAL\Driver\Exception
* @throws \Doctrine\DBAL\DBALException
* @throws \Doctrine\DBAL\Exception
*/
public function testLoadVersionInfoList(): void
{
$this->insertDatabaseFixture(
__DIR__ . '/../_fixtures/contentobjects.php'
);

$gateway = $this->getDatabaseGateway();

$results = $gateway->loadVersionInfoList([11]);

$orig = include __DIR__ . '/../_fixtures/extract_version_info_from_rows_multiple_versions.php';

$this->assertEquals(
[$orig[1]],
$results,
'Fixtures differ between what was previously stored(expected) and what it now generates(actual), this hints either some mistake in impl or that the fixture (../_fixtures/extract_content_from_rows_multiple_versions.php) and tests needs to be adapted.'
);
}
}
55 changes: 37 additions & 18 deletions eZ/Publish/Core/Repository/ContentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,43 @@ public function loadVersionInfoById(int $contentId, ?int $versionNo = null): API
return $versionInfo;
}

public function loadVersionInfoListByContentInfo(array $contentInfoList): array
{
foreach ($contentInfoList as $idx => $contentInfo) {
if (!$contentInfo instanceof ContentInfo) {
throw new InvalidArgumentException(
'$contentInfoList',
sprintf(
'Element at position %d is not an instance of %s',
$idx,
$contentInfo
)
);
}
}

$contentIds = array_map(
static function (ContentInfo $contentInfo): int {
return $contentInfo->getId();
},
$contentInfoList
);

$persistenceVersionInfos = $this->persistenceHandler
->contentHandler()
->loadVersionInfoList($contentIds);

$versionInfoList = [];
foreach ($persistenceVersionInfos as $persistenceVersionInfo) {
$versionInfo = $this->contentDomainMapper->buildVersionInfoDomainObject($persistenceVersionInfo);
if ($this->permissionResolver->canUser('content', 'read', $versionInfo)) {
$versionInfoList[$versionInfo->getContentInfo()->getId()] = $versionInfo;
}
}

return $versionInfoList;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -2526,22 +2563,4 @@ public function count(Filter $filter, ?array $languages = null): int

return $this->contentFilteringHandler->count($filter);
}

public function loadVersionInfoListByContentInfo(array $contentInfoList): array
{
$contentIds = array_column($contentInfoList, 'id');
$persistenceVersionInfos = $this->persistenceHandler
->contentHandler()
->loadVersionInfoList($contentIds);

$versionInfoList = [];
foreach ($persistenceVersionInfos as $persistenceVersionInfo) {
$versionInfo = $this->contentDomainMapper->buildVersionInfoDomainObject($persistenceVersionInfo);
if ($this->permissionResolver->canUser('content', 'read', $versionInfo)) {
$versionInfoList[$versionInfo->getContentInfo()->getId()] = $versionInfo;
}
}

return $versionInfoList;
}
}
10 changes: 5 additions & 5 deletions eZ/Publish/Core/Repository/SiteAccessAware/ContentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public function loadVersionInfoById(int $contentId, ?int $versionNo = null): Ver
return $this->service->loadVersionInfoById($contentId, $versionNo);
}

public function loadVersionInfoListByContentInfo(array $contentInfoList): array
{
return $this->service->loadVersionInfoListByContentInfo($contentInfoList);
}

public function loadContentByContentInfo(ContentInfo $contentInfo, array $languages = null, ?int $versionNo = null, bool $useAlwaysAvailable = true): Content
{
return $this->service->loadContentByContentInfo(
Expand Down Expand Up @@ -286,9 +291,4 @@ public function count(Filter $filter, ?array $languages = null): int
$this->languageResolver->getPrioritizedLanguages($languages)
);
}

public function loadVersionInfoListByContentInfo(array $contentInfoList): array
{
return $this->service->loadVersionInfoListByContentInfo($contentInfoList);
}
}
10 changes: 5 additions & 5 deletions eZ/Publish/SPI/Repository/Decorator/ContentServiceDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public function loadVersionInfoById(
return $this->innerService->loadVersionInfoById($contentId, $versionNo);
}

public function loadVersionInfoListByContentInfo(array $contentInfoList): array
{
return $this->innerService->loadVersionInfoListByContentInfo($contentInfoList);
}

public function loadContentByContentInfo(
ContentInfo $contentInfo,
array $languages = null,
Expand Down Expand Up @@ -275,9 +280,4 @@ public function count(Filter $filter, ?array $languages = null): int
{
return $this->innerService->count($filter, $languages);
}

public function loadVersionInfoListByContentInfo(array $contentInfoList): array
{
return $this->innerService->loadVersionInfoListByContentInfo($contentInfoList);
}
}
1 change: 1 addition & 0 deletions src/contracts/Test/IbexaTestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public function registerContainerConfiguration(LoaderInterface $loader): void
{
$loader->load(static function (ContainerBuilder $container): void {
$container->setParameter('ibexa.core.test.resource_dir', self::getResourcesPath());
$container->setParameter('ezpublish.kernel.root_dir', dirname(__DIR__, 3));
});

$this->loadConfiguration($loader);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Integration\Core\Repository\ContentService;

use Ibexa\Tests\Integration\Core\RepositoryTestCase;

/**
* @covers \eZ\Publish\API\Repository\ContentService
*/
final class LoadVersionInfoTest extends RepositoryTestCase
{
/**
* @throws \eZ\Publish\API\Repository\Exceptions\Exception
*/
public function testLoadVersionInfoListByContentInfo(): void
{
$contentService = self::getContentService();

$folder1 = $this->createFolder(['eng-GB' => 'Folder1'], 2);
$folder2 = $this->createFolder(['eng-GB' => 'Folder2'], 2);

$versionInfoList = $contentService->loadVersionInfoListByContentInfo(
[
$folder1->getVersionInfo()->getContentInfo(),
$folder2->getVersionInfo()->getContentInfo(),
]
);

self::assertCount(2, $versionInfoList);

foreach ($versionInfoList as $versionInfo) {
$loadedVersionInfo = $contentService->loadVersionInfo(
$versionInfo->getContentInfo(),
$versionInfo->versionNo
);
self::assertEquals($loadedVersionInfo, $versionInfo);
}
}
}
73 changes: 73 additions & 0 deletions tests/integration/Core/RepositoryTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Integration\Core;

use eZ\Publish\API\Repository\Values\Content\Content;
use Ibexa\Contracts\Core\Test\IbexaKernelTestCase;
use InvalidArgumentException;

abstract class RepositoryTestCase extends IbexaKernelTestCase
{
public const CONTENT_TREE_ROOT_ID = 2;

private const CONTENT_TYPE_FOLDER_IDENTIFIER = 'folder';

protected function setUp(): void
{
parent::setUp();

self::loadSchema();
self::loadFixtures();

self::setAdministratorUser();
}

/**
* @param array<string, string> $names
*
* @throws \eZ\Publish\API\Repository\Exceptions\Exception
*/
public function createFolder(array $names, int $parentLocationId = self::CONTENT_TREE_ROOT_ID): Content
{
$contentService = self::getContentService();
$draft = $this->createFolderDraft($names, $parentLocationId);

return $contentService->publishVersion($draft->getVersionInfo());
}

/**
* @param array<string, string> $names
*
* @throws \eZ\Publish\API\Repository\Exceptions\Exception
*/
public function createFolderDraft(array $names, int $parentLocationId = self::CONTENT_TREE_ROOT_ID): Content
{
if (empty($names)) {
throw new InvalidArgumentException(__METHOD__ . ' requires $names to be not empty');
}

$contentService = self::getContentService();
$contentTypeService = self::getContentTypeService();
$locationService = self::getLocationService();

$folderType = $contentTypeService->loadContentTypeByIdentifier(self::CONTENT_TYPE_FOLDER_IDENTIFIER);
$mainLanguageCode = array_keys($names)[0];
$contentCreateStruct = $contentService->newContentCreateStruct($folderType, $mainLanguageCode);
foreach ($names as $languageCode => $name) {
$contentCreateStruct->setField('name', $name, $languageCode);
}

return $contentService->createContent(
$contentCreateStruct,
[
$locationService->newLocationCreateStruct($parentLocationId),
]
);
}
}

0 comments on commit 5b5b605

Please sign in to comment.