Skip to content

Commit 0ccf16c

Browse files
committed
IBX-10458: Fixed total count
1 parent 0577839 commit 0ccf16c

File tree

4 files changed

+39
-24
lines changed

4 files changed

+39
-24
lines changed

src/lib/Persistence/Legacy/Content/Type/Gateway.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ abstract public function insertGroup(Group $group): int;
3737

3838
abstract public function updateGroup(GroupUpdateStruct $group): void;
3939

40-
abstract public function countTypes(): int;
40+
abstract public function countTypes(?ContentTypeQuery $query = null): int;
4141

4242
abstract public function countTypesInGroup(int $groupId): int;
4343

src/lib/Persistence/Legacy/Content/Type/Gateway/DoctrineDatabase.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,38 @@ public function updateGroup(GroupUpdateStruct $group): void
207207
$query->execute();
208208
}
209209

210-
public function countTypes(): int
210+
public function countTypes(?ContentTypeQuery $query = null): int
211211
{
212-
$query = $this->connection->createQueryBuilder();
213-
$query
214-
->select($this->dbPlatform->getCountExpression('id'))
215-
->from(self::CONTENT_TYPE_TABLE);
212+
$query = $query ?: new ContentTypeQuery();
213+
$queryBuilder = $this->connection->createQueryBuilder();
214+
$expr = $queryBuilder->expr();
215+
$queryBuilder
216+
->select('COUNT(DISTINCT c.id)')
217+
->from(self::CONTENT_TYPE_TABLE, 'c')
218+
->leftJoin(
219+
'c',
220+
self::FIELD_DEFINITION_TABLE,
221+
'a',
222+
(string)$expr->and(
223+
$expr->eq('c.id', 'a.contentclass_id'),
224+
$expr->eq('c.version', 'a.version')
225+
)
226+
)
227+
->leftJoin(
228+
'c',
229+
self::CONTENT_TYPE_TO_GROUP_ASSIGNMENT_TABLE,
230+
'g',
231+
(string)$expr->and(
232+
$expr->eq('c.id', 'g.contentclass_id'),
233+
$expr->eq('c.version', 'g.contentclass_version')
234+
)
235+
);
236+
237+
if ($query->getCriterion() !== null) {
238+
$queryBuilder->andWhere($this->criterionVisitor->visitCriteria($queryBuilder, $query->getCriterion()));
239+
}
216240

217-
return (int)$query->execute()->fetchOne();
241+
return (int)$queryBuilder->execute()->fetchOne();
218242
}
219243

220244
public function countTypesInGroup(int $groupId): int
@@ -1429,7 +1453,7 @@ public function removeByUserAndVersion(int $userId, int $version): void
14291453

14301454
public function findContentTypes(?ContentTypeQuery $query = null): array
14311455
{
1432-
$totalCount = $this->countTypes();
1456+
$totalCount = $this->countTypes($query);
14331457
if ($totalCount === 0) {
14341458
return [
14351459
'count' => $totalCount,

src/lib/Persistence/Legacy/Content/Type/Gateway/ExceptionConversion.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ public function updateGroup(GroupUpdateStruct $group): void
5959
}
6060
}
6161

62-
public function countTypes(): int
62+
public function countTypes(?ContentTypeQuery $query = null): int
6363
{
6464
try {
65-
return $this->innerGateway->countTypes();
65+
return $this->innerGateway->countTypes($query);
6666
} catch (DBALException | PDOException $e) {
6767
throw DatabaseException::wrap($e);
6868
}

tests/integration/Core/Repository/ContentTypeService/FindContentTypesTest.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ public function testFindContentTypesWithNullQueryFinds25Results(): void
3232

3333
$contentTypes = $contentTypeService->findContentTypes();
3434

35+
$allContentTypes = $contentTypeService->findContentTypes(new ContentTypeQuery(null, [], 0, null));
36+
3537
self::assertCount(25, $contentTypes);
38+
self::assertSame(count($allContentTypes->getContentTypes()), $contentTypes->getTotalCount());
3639
}
3740

3841
/**
@@ -44,26 +47,14 @@ public function testFindContentTypes(ContentTypeQuery $query, array $expectedIde
4447
{
4548
$contentTypeService = self::getContentTypeService();
4649

47-
$expectedCount = $contentTypeService->findContentTypes(
48-
new ContentTypeQuery(
49-
null,
50-
[],
51-
0,
52-
null,
53-
)
54-
);
55-
$expectedCount = count($expectedCount->getContentTypes());
56-
5750
$contentTypes = $contentTypeService->findContentTypes($query);
58-
5951
$identifiers = array_map(
6052
static fn (ContentType $contentType): string => $contentType->getIdentifier(),
6153
$contentTypes->getContentTypes(),
6254
);
6355

6456
self::assertCount(count($expectedIdentifiers), $identifiers);
6557
self::assertEqualsCanonicalizing($expectedIdentifiers, $identifiers);
66-
self::assertSame($expectedCount, $contentTypes->getTotalCount());
6758
}
6859

6960
public function testFindContentTypesAscSortedByIdentifier(): void
@@ -81,7 +72,7 @@ public function testFindContentTypesAscSortedByIdentifier(): void
8172
$contentTypes->getContentTypes()
8273
);
8374

84-
self::assertCount(4, $identifiers);
75+
self::assertSame(4, $contentTypes->getTotalCount());
8576
self::assertSame(['article', 'file', 'folder', 'user'], $identifiers);
8677
}
8778

@@ -131,7 +122,7 @@ public function testFindContentTypesContainingFieldDefinitions(): void
131122
)
132123
);
133124

134-
self::assertCount(1, $contentTypes);
125+
self::assertSame(1, $contentTypes->getTotalCount());
135126
self::assertSame('folder', $contentTypes->getContentTypes()[0]->getIdentifier());
136127
}
137128

0 commit comments

Comments
 (0)