Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/contracts/Persistence/Content/Type/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function loadGroupByIdentifier($identifier);
/**
* @return \Ibexa\Contracts\Core\Persistence\Content\Type\Group[]
*/
public function loadAllGroups();
public function loadAllGroups(bool $includeSystem = false);

/**
* @param mixed $groupId
Expand Down
3 changes: 2 additions & 1 deletion src/contracts/Repository/ContentTypeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ public function loadContentTypeGroupByIdentifier(string $contentTypeGroupIdentif
* Get all content type groups.
*
* @param string[] $prioritizedLanguages Used as prioritized language code on translated properties of returned object.
* @param bool $includeSystem When true also returns system ContentTypeGroups. Default false.
*
* @return \Ibexa\Contracts\Core\Repository\Values\ContentType\ContentTypeGroup[]
*/
public function loadContentTypeGroups(array $prioritizedLanguages = []): iterable;
public function loadContentTypeGroups(array $prioritizedLanguages = [], bool $includeSystem = false): iterable;

/**
* Update a content type group object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public function loadContentTypeGroupByIdentifier(
return $this->innerService->loadContentTypeGroupByIdentifier($contentTypeGroupIdentifier, $prioritizedLanguages);
}

public function loadContentTypeGroups(array $prioritizedLanguages = []): iterable
public function loadContentTypeGroups(array $prioritizedLanguages = [], bool $includeSystem = false): iterable
{
return $this->innerService->loadContentTypeGroups($prioritizedLanguages);
return $this->innerService->loadContentTypeGroups($prioritizedLanguages, $includeSystem);
}

public function updateContentTypeGroup(
Expand Down
14 changes: 10 additions & 4 deletions src/lib/Persistence/Cache/ContentTypeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public function createGroup(GroupCreateStruct $struct)
$this->logger->logCall(__METHOD__, ['struct' => $struct]);
$this->cache->deleteItems([
$this->cacheIdentifierGenerator->generateKey(self::CONTENT_TYPE_GROUP_LIST_IDENTIFIER, [], true),
$this->cacheIdentifierGenerator->generateKey(self::CONTENT_TYPE_GROUP_LIST_IDENTIFIER, [true], true),
]);

return $this->persistenceHandler->contentTypeHandler()->createGroup($struct);
Expand All @@ -115,6 +116,7 @@ public function updateGroup(GroupUpdateStruct $struct)

$this->cache->deleteItems([
$this->cacheIdentifierGenerator->generateKey(self::CONTENT_TYPE_GROUP_LIST_IDENTIFIER, [], true),
$this->cacheIdentifierGenerator->generateKey(self::CONTENT_TYPE_GROUP_LIST_IDENTIFIER, [true], true),
$this->cacheIdentifierGenerator->generateKey(self::CONTENT_TYPE_GROUP_IDENTIFIER, [$struct->id], true),
$this->cacheIdentifierGenerator->generateKey(
self::CONTENT_TYPE_GROUP_WITH_ID_SUFFIX_IDENTIFIER,
Expand All @@ -137,6 +139,10 @@ public function deleteGroup($groupId)
$this->cache->invalidateTags([
$this->cacheIdentifierGenerator->generateTag(self::TYPE_GROUP_IDENTIFIER, [$groupId]),
]);
$this->cache->deleteItems([
$this->cacheIdentifierGenerator->generateKey(self::CONTENT_TYPE_GROUP_LIST_IDENTIFIER, [], true),
$this->cacheIdentifierGenerator->generateKey(self::CONTENT_TYPE_GROUP_LIST_IDENTIFIER, [true], true),
]);

return $return;
}
Expand Down Expand Up @@ -193,12 +199,12 @@ function () use ($identifier) {
/**
* {@inheritdoc}
*/
public function loadAllGroups()
public function loadAllGroups(bool $includeSystem = false)
{
return $this->getListCacheValue(
$this->cacheIdentifierGenerator->generateKey(self::CONTENT_TYPE_GROUP_LIST_IDENTIFIER, [], true),
function () {
return $this->persistenceHandler->contentTypeHandler()->loadAllGroups();
$this->cacheIdentifierGenerator->generateKey(self::CONTENT_TYPE_GROUP_LIST_IDENTIFIER, [$includeSystem], true),
function () use ($includeSystem) {
return $this->persistenceHandler->contentTypeHandler()->loadAllGroups($includeSystem);
},
$this->getGroupTags,
$this->getGroupKeys
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Persistence/Legacy/Content/Type/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ abstract public function loadGroupData(array $groupIds): array;

abstract public function loadGroupDataByIdentifier(string $identifier): array;

abstract public function loadAllGroupsData(): array;
abstract public function loadAllGroupsData(bool $includeSystem = false): array;

/**
* Load data for all content types of the given status, belonging to the given Group.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -547,16 +547,18 @@ public function loadGroupDataByIdentifier(string $identifier): array
return $query->execute()->fetchAll(FetchMode::ASSOCIATIVE);
}

public function loadAllGroupsData(): array
public function loadAllGroupsData(bool $includeSystem = false): array
{
$query = $this->createGroupLoadQuery();

$query->andWhere(
$query->expr()->eq(
'is_system',
$query->createPositionalParameter(false, ParameterType::BOOLEAN)
)
);
if (!$includeSystem) {
$query->andWhere(
$query->expr()->eq(
'is_system',
$query->createPositionalParameter(false, ParameterType::BOOLEAN)
)
);
}

return $query->execute()->fetchAll(FetchMode::ASSOCIATIVE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ public function loadGroupDataByIdentifier(string $identifier): array
}
}

public function loadAllGroupsData(): array
public function loadAllGroupsData(bool $includeSystem = false): array
{
try {
return $this->innerGateway->loadAllGroupsData();
return $this->innerGateway->loadAllGroupsData($includeSystem);
} catch (DBALException | PDOException $e) {
throw DatabaseException::wrap($e);
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/Persistence/Legacy/Content/Type/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ public function loadGroupByIdentifier($identifier)
/**
* @return \Ibexa\Contracts\Core\Persistence\Content\Type\Group[]
*/
public function loadAllGroups()
public function loadAllGroups(bool $includeSystem = false)
{
return $this->mapper->extractGroupsFromRows(
$this->contentTypeGateway->loadAllGroupsData()
$this->contentTypeGateway->loadAllGroupsData($includeSystem)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function createGroup(GroupCreateStruct $createStruct): Group
$this->storeGroupCache([$group]);
$this->cache->deleteMulti([
$this->generator->generateKey(self::CONTENT_TYPE_GROUP_LIST, [], true),
$this->generator->generateKey(self::CONTENT_TYPE_GROUP_LIST, [true], true),
]);

return $group;
Expand All @@ -64,6 +65,7 @@ public function updateGroup(GroupUpdateStruct $struct): Group
$this->storeGroupCache([$group]);
$this->cache->deleteMulti([
$this->generator->generateKey(self::CONTENT_TYPE_GROUP_LIST, [], true),
$this->generator->generateKey(self::CONTENT_TYPE_GROUP_LIST, [true], true),
]);

return $group;
Expand All @@ -76,6 +78,7 @@ public function deleteGroup($groupId): void
$this->cache->deleteMulti([
$this->generator->generateKey(self::CONTENT_TYPE_GROUP, [$groupId], true),
$this->generator->generateKey(self::CONTENT_TYPE_GROUP_LIST, [], true),
$this->generator->generateKey(self::CONTENT_TYPE_GROUP_LIST, [true], true),
]);
}

Expand Down Expand Up @@ -139,13 +142,13 @@ public function loadGroupByIdentifier($identifier): Group
/**
* @return \Ibexa\Contracts\Core\Persistence\Content\Type\Group[]
*/
public function loadAllGroups(): array
public function loadAllGroups(bool $includeSystem = false): array
{
$contentTypeGroupListKey = $this->generator->generateKey(self::CONTENT_TYPE_GROUP_LIST, [], true);
$contentTypeGroupListKey = $this->generator->generateKey(self::CONTENT_TYPE_GROUP_LIST, [$includeSystem], true);
$groups = $this->cache->get($contentTypeGroupListKey);

if ($groups === null) {
$groups = $this->innerHandler->loadAllGroups();
$groups = $this->innerHandler->loadAllGroups($includeSystem);
$this->storeGroupCache($groups, $contentTypeGroupListKey);
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib/Repository/ContentTypeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ public function loadContentTypeGroupByIdentifier(string $contentTypeGroupIdentif
/**
* {@inheritdoc}
*/
public function loadContentTypeGroups(array $prioritizedLanguages = []): iterable
public function loadContentTypeGroups(array $prioritizedLanguages = [], bool $includeSystem = false): iterable
{
$spiGroups = $this->contentTypeHandler->loadAllGroups();
$spiGroups = $this->contentTypeHandler->loadAllGroups($includeSystem);

$groups = [];
foreach ($spiGroups as $spiGroup) {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/Repository/SiteAccessAware/ContentTypeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ public function loadContentTypeGroupByIdentifier(string $contentTypeGroupIdentif
return $this->service->loadContentTypeGroupByIdentifier($contentTypeGroupIdentifier, $prioritizedLanguages);
}

public function loadContentTypeGroups(?array $prioritizedLanguages = null): iterable
public function loadContentTypeGroups(?array $prioritizedLanguages = null, bool $includeSystem = false): iterable
{
$prioritizedLanguages = $this->languageResolver->getPrioritizedLanguages($prioritizedLanguages);

return $this->service->loadContentTypeGroups($prioritizedLanguages);
return $this->service->loadContentTypeGroups($prioritizedLanguages, $includeSystem);
}

public function updateContentTypeGroup(ContentTypeGroup $contentTypeGroup, ContentTypeGroupUpdateStruct $contentTypeGroupUpdateStruct): void
Expand Down
35 changes: 35 additions & 0 deletions tests/integration/Core/Repository/ContentTypeServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,41 @@ public function testLoadContentTypeGroupsIdentifiers($groups)
);
}

/**
* Test that system ContentTypeGroups are returned only when explicitly requested.
*
* @covers \Ibexa\Contracts\Core\Repository\ContentTypeService::loadContentTypeGroups()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't add @covers annotation to specific methods. We add it only for a class the top of a test case class. It makes coverage report somehow incomplete AFAIR.

*/
public function testLoadContentTypeGroupsIncludeSystem(): void
{
$repository = $this->getRepository();
$contentTypeService = $repository->getContentTypeService();

$systemGroupIdentifier = 'SystemGroup';
$contentTypeGroupCreateStruct = $contentTypeService->newContentTypeGroupCreateStruct($systemGroupIdentifier);
$contentTypeGroupCreateStruct->isSystem = true;

$systemGroup = $contentTypeService->createContentTypeGroup($contentTypeGroupCreateStruct);

try {
$defaultGroupsIterable = $contentTypeService->loadContentTypeGroups();
$defaultGroups = is_array($defaultGroupsIterable) ? $defaultGroupsIterable : iterator_to_array($defaultGroupsIterable);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIR our polyfill iterator_to_array should be able to handle that for you.

/** @var \Ibexa\Contracts\Core\Repository\Values\ContentType\ContentTypeGroup[] $defaultGroups */
$defaultIdentifiers = array_map(static fn (ContentTypeGroup $group): string => $group->identifier, $defaultGroups);

self::assertNotContains($systemGroupIdentifier, $defaultIdentifiers);

$allGroupsIterable = $contentTypeService->loadContentTypeGroups([], true);
$allGroups = is_array($allGroupsIterable) ? $allGroupsIterable : iterator_to_array($allGroupsIterable);
/** @var \Ibexa\Contracts\Core\Repository\Values\ContentType\ContentTypeGroup[] $allGroups */
$allIdentifiers = array_map(static fn (ContentTypeGroup $group): string => $group->identifier, $allGroups);

self::assertContains($systemGroupIdentifier, $allIdentifiers);
} finally {
$contentTypeService->deleteContentTypeGroup($systemGroup);
}
}

/**
* Test for the newContentTypeGroupUpdateStruct() method.
*
Expand Down
42 changes: 37 additions & 5 deletions tests/lib/Persistence/Cache/ContentTypeHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,41 @@ public function providerForUnCachedMethods(): array

// string $method, array $arguments, array? $tagGeneratingArguments, array? $keyGeneratingArguments, array? $tags, array? $key, ?mixed $returnValue
return [
['createGroup', [new SPITypeGroupCreateStruct()], null, [['content_type_group_list', [], true]], null, ['ibx-ctgl']],
[
'createGroup',
[new SPITypeGroupCreateStruct()],
null,
[
['content_type_group_list', [], true],
['content_type_group_list', [true], true],
],
null,
['ibx-ctgl', 'ibx-ctgl-true'],
],
[
'updateGroup',
[$groupUpdate],
null,
[
['content_type_group_list', [], true],
['content_type_group_list', [true], true],
['content_type_group', [3], true],
['content_type_group_with_id_suffix', ['media'], true],
],
null,
['ibx-ctgl', 'ibx-ctg-3', 'ibx-ctg-media-bi'],
['ibx-ctgl', 'ibx-ctgl-true', 'ibx-ctg-3', 'ibx-ctg-media-bi'],
],
[
'deleteGroup',
[3],
[['type_group', [3], false]],
[
['content_type_group_list', [], true],
['content_type_group_list', [true], true],
],
['tg-3'],
['ibx-ctgl', 'ibx-ctgl-true'],
],
['deleteGroup', [3], [['type_group', [3], false]], null, ['tg-3']],
['loadContentTypes', [3, 1]], // also listed for cached cases in providerForCachedLoadMethods
['load', [5, 1]], // also listed for cached case in providerForCachedLoadMethods
[
Expand Down Expand Up @@ -202,7 +223,18 @@ public function providerForCachedLoadMethodsHit(): array
['ibx-ctg', 'bi'],
$group,
],
['loadAllGroups', [], 'ibx-ctgl', null, null, [['content_type_group_list', [], true]], ['ibx-ctgl'], [3 => $group]],
[
'loadAllGroups',
[],
'ibx-ctgl',
null,
null,
[
['content_type_group_list', [false], true],
],
['ibx-ctgl'],
[3 => $group],
],
['loadContentTypes', [3, 0], 'ibx-ctlbg-3', null, null, [['content_type_list_by_group', [3], true]], ['ibx-ctlbg-3'], [$type]],
['loadContentTypesByFieldDefinitionIdentifier', [3, 0], 'ibx-ctlbfdi-3', null, null, [['content_type_list_by_field_definition_identifier', [3], true]], ['ibx-ctlbfdi-3'], [$type]],
['loadContentTypeList', [[5]], 'ibx-ct-5', null, null, [['content_type', [], true]], ['ibx-ct'], [5 => $type], true],
Expand Down Expand Up @@ -297,7 +329,7 @@ public function providerForCachedLoadMethodsMiss(): array
],
['tg-3'],
[
['content_type_group_list', [], true],
['content_type_group_list', [false], true],
],
['ibx-ctgl'],
[3 => $group],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ public function testLoadAllGroups()
$gatewayMock = $this->getGatewayMock();
$gatewayMock->expects($this->once())
->method('loadAllGroupsData')
->with($this->equalTo(false))
->will($this->returnValue([]));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was legacy style.
Rather:

Suggested change
->with($this->equalTo(false))
->will($this->returnValue([]));
->with(false)
->willReturnValue([]);


$mapperMock = $this->getMapperMock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function testLoadContentTypeGroupsDecorator()
$serviceMock = $this->createServiceMock();
$decoratedService = $this->createDecorator($serviceMock);

$parameters = [['prioritized_language_value']];
$parameters = [['prioritized_language_value'], false];

$serviceMock->expects($this->once())->method('loadContentTypeGroups')->with(...$parameters);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function providerForLanguagesLookupMethods()

['loadContentTypeGroupByIdentifier', ['content', self::LANG_ARG], $contentTypeGroup, 1],

['loadContentTypeGroups', [self::LANG_ARG], [$contentTypeGroup], 0],
['loadContentTypeGroups', [self::LANG_ARG, false], [$contentTypeGroup], 0],

['loadContentType', [22, self::LANG_ARG], $contentType, 1],

Expand Down
Loading