Skip to content

Commit b9cecdd

Browse files
committed
fix analytics
Signed-off-by: Kostiantyn Miakshyn <molodchick@gmail.com>
1 parent cfe987b commit b9cecdd

2 files changed

Lines changed: 56 additions & 4 deletions

File tree

lib/Analytics/AnalyticsDatasource.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@
1313
use OCA\Tables\Errors\NotFoundError;
1414
use OCA\Tables\Errors\PermissionError;
1515
use OCA\Tables\Service\ColumnService;
16+
use OCA\Tables\Service\RelationService;
1617
use OCA\Tables\Service\RowService;
1718
use OCA\Tables\Service\TableService;
1819
use OCA\Tables\Service\ViewService;
1920
use OCP\AppFramework\Db\DoesNotExistException;
2021
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
2122
use OCP\IL10N;
22-
use Psr\Log\LoggerInterface;
2323

2424
class AnalyticsDatasource implements IDatasource {
2525

2626
public function __construct(
2727
private readonly IL10N $l10n,
28-
private readonly LoggerInterface $logger,
2928
private readonly TableService $tableService,
3029
private readonly ViewService $viewService,
3130
private readonly ColumnService $columnService,
3231
private readonly RowService $rowService,
32+
private readonly RelationService $relationService,
3333
protected ?string $userId,
3434
) {
3535
}
@@ -253,6 +253,7 @@ private function getData(int $nodeId, ?int $limit, ?int $offset, ?string $nodeTy
253253
private function formatValue(Column $column, mixed $value): mixed {
254254
return match ($column->getType()) {
255255
Column::TYPE_SELECTION => $this->formatSelectionValue($column, $value),
256+
Column::TYPE_RELATION => $this->formatRelationValue($column, $value),
256257
Column::TYPE_TEXT => $this->formatTextValue($column, $value),
257258
Column::TYPE_USERGROUP => $this->formatUsergroupValue($value),
258259
default => $value,
@@ -372,6 +373,17 @@ private function formatUsergroupValue(mixed $value): string {
372373
return implode(', ', array_filter($labels, static fn (string $label): bool => $label !== ''));
373374
}
374375

376+
private function formatRelationValue(Column $column, mixed $value): string {
377+
if ($value === null || $value === '') {
378+
return '';
379+
}
380+
381+
$relationData = $this->relationService->getRelationData($column);
382+
$valueId = (int)$value;
383+
384+
return $relationData[$valueId]['label'] ?? (string)$value;
385+
}
386+
375387
private function parseDefaultValue(?string $value): mixed {
376388
if ($value === null || $value === '') {
377389
return '';

tests/unit/Analytics/AnalyticsDatasourceTest.php

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,21 @@ interface IDatasource {
1717
use OCA\Tables\Db\Column;
1818
use OCA\Tables\Db\Row2;
1919
use OCA\Tables\Service\ColumnService;
20+
use OCA\Tables\Service\RelationService;
2021
use OCA\Tables\Service\RowService;
2122
use OCA\Tables\Service\TableService;
2223
use OCA\Tables\Service\ViewService;
2324
use OCP\IL10N;
2425
use PHPUnit\Framework\MockObject\MockObject;
2526
use PHPUnit\Framework\TestCase;
26-
use Psr\Log\LoggerInterface;
2727

2828
final class AnalyticsDatasourceTest extends TestCase {
2929
private IL10N|MockObject $l10n;
3030
private TableService|MockObject $tableService;
3131
private ViewService|MockObject $viewService;
3232
private ColumnService|MockObject $columnService;
3333
private RowService|MockObject $rowService;
34+
private RelationService|MockObject $relationService;
3435
private AnalyticsDatasource $datasource;
3536

3637
protected function setUp(): void {
@@ -42,14 +43,15 @@ protected function setUp(): void {
4243
$this->viewService = $this->createMock(ViewService::class);
4344
$this->columnService = $this->createMock(ColumnService::class);
4445
$this->rowService = $this->createMock(RowService::class);
46+
$this->relationService = $this->createMock(RelationService::class);
4547

4648
$this->datasource = new AnalyticsDatasource(
4749
$this->l10n,
48-
$this->createMock(LoggerInterface::class),
4950
$this->tableService,
5051
$this->viewService,
5152
$this->columnService,
5253
$this->rowService,
54+
$this->relationService,
5355
'user1',
5456
);
5557
}
@@ -148,6 +150,44 @@ public function testReadDataFormatsDisplayValuesForAnalytics(): void {
148150
], $result['data']);
149151
}
150152

153+
public function testReadDataFormatsRelationValuesForAnalytics(): void {
154+
$this->columnService
155+
->expects($this->once())
156+
->method('findAllByTable')
157+
->with(123, 'user1')
158+
->willReturn([
159+
$this->createColumn(1, 'Name', 'text'),
160+
$this->createColumn(2, 'Customer', 'relation'),
161+
]);
162+
163+
$this->relationService
164+
->expects($this->once())
165+
->method('getRelationData')
166+
->willReturn([
167+
42 => ['id' => 42, 'label' => 'Acme Corp'],
168+
43 => ['id' => 43, 'label' => 'Globex'],
169+
]);
170+
171+
$row = new Row2();
172+
$row->setData([
173+
['columnId' => 1, 'value' => 'Order 1'],
174+
['columnId' => 2, 'value' => 42],
175+
]);
176+
177+
$this->rowService
178+
->expects($this->once())
179+
->method('findAllByTable')
180+
->with(123, 'user1', null, null)
181+
->willReturn([$row]);
182+
183+
$result = $this->datasource->readData([
184+
'tableId' => '123',
185+
'user_id' => 'user1',
186+
]);
187+
188+
self::assertSame([['Order 1', 'Acme Corp', 1]], $result['data']);
189+
}
190+
151191
public function testReadDataFormatsDefaultValuesForAnalytics(): void {
152192
$numberColumn = $this->createColumn(1, 'Amount', 'number');
153193
$numberColumn->setNumberDefault(10);

0 commit comments

Comments
 (0)