Skip to content

Commit

Permalink
IBX-6017: Implemented loadVersionInfoListByContentInfo PAPI method
Browse files Browse the repository at this point in the history
  • Loading branch information
barw4 committed Jun 28, 2023
1 parent 0b8bad6 commit 37bab3b
Show file tree
Hide file tree
Showing 17 changed files with 302 additions and 1 deletion.
13 changes: 13 additions & 0 deletions eZ/Publish/API/Repository/ContentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -577,4 +577,17 @@ 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: 29 additions & 0 deletions eZ/Publish/API/Repository/Tests/ContentServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6685,4 +6685,33 @@ 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);
}
}
}
2 changes: 1 addition & 1 deletion eZ/Publish/Core/Helper/TranslationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function getTranslatedContentName(Content $content, $forcedLanguage = nul
*
* @return string
*/
private function getTranslatedContentNameByVersionInfo(VersionInfo $versionInfo, $forcedLanguage = null)
public function getTranslatedContentNameByVersionInfo(VersionInfo $versionInfo, $forcedLanguage = null)
{
foreach ($this->getLanguages($forcedLanguage) as $lang) {
$translatedName = $versionInfo->getName($lang);
Expand Down
33 changes: 33 additions & 0 deletions eZ/Publish/Core/Persistence/Cache/ContentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -580,4 +580,37 @@ 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(
$contentIds,
$this->cacheIdentifierGenerator->generateKey(
self::CONTENT_VERSION_INFO_IDENTIFIER,
[],
true
) . '-',
function (array $cacheMissIds): array {
return $this->persistenceHandler->contentHandler()->loadVersionInfoList($cacheMissIds);
},
function (VersionInfo $versionInfo): array {
return $this->getCacheTagsForVersion($versionInfo);
},
function (VersionInfo $versionInfo) {
return [
$this->cacheIdentifierGenerator->generateKey(
self::CONTENT_VERSION_INFO_IDENTIFIER,
[$versionInfo->contentInfo->id],
true
),
];
},
'',
['content' => $contentIds]
);
}
}
17 changes: 17 additions & 0 deletions eZ/Publish/Core/Persistence/Cache/Tests/ContentHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function providerForCachedLoadMethodsHit(): array
['loadVersionInfo', [2, 1], 'ibx-cvi-2-1', null, null, [['content_version_info', [2], true]], ['ibx-cvi-2'], $version],
['loadVersionInfo', [2], 'ibx-cvi-2', null, null, [['content_version_info', [2], true]], ['ibx-cvi-2'], $version],
['listVersions', [2], 'ibx-c-2-vl', null, null, [['content_version_list', [2], true]], ['ibx-c-2-vl'], [$version]],
['loadVersionInfoList', [[2]], 'ibx-cvi-2', null, null, [['content_version_info', [], true]], ['ibx-cvi'], [2 => $version], true],
];
}

Expand Down Expand Up @@ -300,6 +301,22 @@ public function providerForCachedLoadMethodsMiss(): array
['ibx-c-2-vl'],
[$version],
],
[
'loadVersionInfoList',
[[2]],
'ibx-cvi-2',
[
['content_version', [2, 1], false],
['content', [2], false],
],
['c-2-v-1', 'c-2'],
[
['content_version_info', [], true],
],
['ibx-cvi'],
[2 => $version],
true,
],
];
}

Expand Down
8 changes: 8 additions & 0 deletions eZ/Publish/Core/Persistence/Legacy/Content/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,4 +459,12 @@ abstract public function deleteTranslationFromVersion(
int $versionNo,
string $languageCode
): void;

/**
* @param array<int> $contentIds
*
* @throws \Doctrine\DBAL\Driver\Exception
* @throws \Doctrine\DBAL\Exception
*/
abstract public function loadVersionInfoList(array $contentIds): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -1980,4 +1980,27 @@ private function setLanguageMaskForUpdateQuery(

return $query;
}

/**
* @throws \Doctrine\DBAL\Driver\Exception
* @throws \Doctrine\DBAL\Exception
*/
public function loadVersionInfoList(array $contentIds): array
{
$queryBuilder = $this->queryBuilder->createVersionInfoFindQueryBuilder();
$expr = $queryBuilder->expr();

$queryBuilder
->andWhere(
$expr->in(
'c.id',
$queryBuilder->createNamedParameter($contentIds, Connection::PARAM_INT_ARRAY)
)
)
->andWhere(
$expr->eq('v.version', 'c.current_version')
);

return $queryBuilder->execute()->fetchAllAssociative();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -516,4 +516,13 @@ public function deleteTranslationFromVersion(
throw DatabaseException::wrap($e);
}
}

public function loadVersionInfoList(array $contentIds): array
{
try {
return $this->innerGateway->loadVersionInfoList($contentIds);
} catch (DBALException | PDOException $e) {
throw DatabaseException::wrap($e);
}
}
}
27 changes: 27 additions & 0 deletions eZ/Publish/Core/Persistence/Legacy/Content/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -904,4 +904,31 @@ static function ($lang) use ($languageCode) {
// reload entire Version w/o removed Translation
return $this->load($contentId, $versionNo);
}

/**
* @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException
*/
public function loadVersionInfoList(array $contentIds): array
{
$rows = $this->contentGateway->loadVersionInfoList($contentIds);
$mappedRows = array_map(
static fn ($row) => [

Check failure on line 915 in eZ/Publish/Core/Persistence/Legacy/Content/Handler.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (7.3)

Syntax error, unexpected T_DOUBLE_ARROW on line 915

Check failure on line 915 in eZ/Publish/Core/Persistence/Legacy/Content/Handler.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (7.3)

Syntax error, unexpected T_STRING, expecting T_PAAMAYIM_NEKUDOTAYIM on line 915
'id' => $row['ezcontentobject_id'],
'version' => $row['ezcontentobject_version_version'],
],

Check failure on line 918 in eZ/Publish/Core/Persistence/Legacy/Content/Handler.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (7.3)

Syntax error, unexpected ',' on line 918
$rows,
);

$versionInfoList = $this->mapper->extractVersionInfoListFromRows(
$rows,
$this->contentGateway->loadVersionedNameData($mappedRows)
);

$versionInfoListById = [];
foreach ($versionInfoList as $versionInfo) {
$versionInfoListById[$versionInfo->contentInfo->id] = $versionInfo;
}

return $versionInfoListById;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,63 @@ public function testSetStatus()
);
}

/**
* @covers \eZ\Publish\Core\Persistence\Legacy\Content\Handler::loadVersionInfoList
*
* @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException
*/
public function testLoadVersionInfoList(): void
{
$handler = $this->getContentHandler();
$gatewayMock = $this->getGatewayMock();
$mapperMock = $this->getMapperMock();

$contentIds = [2, 3];
$versionInfo1 = new VersionInfo([
'contentInfo' => new ContentInfo(['id' => 2]),
]);
$versionInfo2 = new VersionInfo([
'contentInfo' => new ContentInfo(['id' => 3]),
]);

$versionRows = [
['ezcontentobject_id' => 2, 'ezcontentobject_version_version' => 2],
['ezcontentobject_id' => 3, 'ezcontentobject_version_version' => 1],
];

$gatewayMock->expects(self::once())
->method('loadVersionInfoList')
->with($contentIds)
->willReturn($versionRows);

$nameDataRows = [
['ezcontentobject_name_contentobject_id' => 2, 'ezcontentobject_name_content_version' => 2],
['ezcontentobject_name_contentobject_id' => 3, 'ezcontentobject_name_content_version' => 1],
];

$gatewayMock->expects(self::once())
->method('loadVersionedNameData')
->with([['id' => 2, 'version' => 2], ['id' => 3, 'version' => 1]])
->willReturn($nameDataRows);

$mapperMock->expects(self::once())
->method('extractVersionInfoListFromRows')
->with($versionRows)
->willReturn([
$versionInfo1,
$versionInfo2,
]);

$expected = [
2 => $versionInfo1,
3 => $versionInfo2,
];

$result = $handler->loadVersionInfoList($contentIds);

self::assertEquals($expected, $result);
}

/**
* Returns the handler to test.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2116,4 +2116,28 @@ 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.'
);
}
}
18 changes: 18 additions & 0 deletions eZ/Publish/Core/Repository/ContentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2526,4 +2526,22 @@ 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;
}
}
5 changes: 5 additions & 0 deletions eZ/Publish/Core/Repository/SiteAccessAware/ContentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,9 @@ public function count(Filter $filter, ?array $languages = null): int
$this->languageResolver->getPrioritizedLanguages($languages)
);
}

public function loadVersionInfoListByContentInfo(array $contentInfoList): array
{
return $this->service->loadVersionInfoListByContentInfo($contentInfoList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ public function providerForPassTroughMethods(): array
['newContentMetadataUpdateStruct', [], $contentMetaStruct],
['newContentUpdateStruct', [], $contentUpdateStruct],
['validate', [$contentUpdateStruct, []], []],

['loadVersionInfoListByContentInfo', [[$contentInfo]], [$versionInfo]],
];
}

Expand Down
10 changes: 10 additions & 0 deletions eZ/Publish/SPI/Persistence/Content/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,4 +364,14 @@ public function deleteTranslationFromContent($contentId, $languageCode);
* @return \eZ\Publish\SPI\Persistence\Content The Content Draft w/o removed Translation
*/
public function deleteTranslationFromDraft($contentId, $versionNo, $languageCode);

/**
* @param array<int> $contentIds
*
* @return array<\eZ\Publish\SPI\Persistence\Content\VersionInfo>
*
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
* @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException
*/
public function loadVersionInfoList(array $contentIds): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,9 @@ 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);
}
}
Loading

0 comments on commit 37bab3b

Please sign in to comment.