MCPcopy Create free account
hub / github.com/Rello/analytics / DatasourceControllerTest

Class DatasourceControllerTest

tests/Controller/DatasourceControllerTest.php:8–894  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6use PHPUnit\Framework\TestCase;
7
8class DatasourceControllerTest extends TestCase {
9 public function testJsonBrowseUsesCurrentUserAndReturnsOnlyContent(): void {
10 $json = $this->createMock(\OCA\Analytics\Datasource\LocalJson::class);
11 $json->expects($this->once())->method('readData')->with($this->callback(function ($options) {
12 $this->assertSame('u1', $options['user_id']);
13 $this->assertSame('', $options['path']);
14 $this->assertSame('false', $options['timestamp']);
15 $this->assertArrayNotHasKey('cacheKey', $options);
16 return true;
17 }))->willReturn(['rawdata' => '{"value":0}', 'auth' => 'secret', 'error' => 0, 'data' => []]);
18 $user = $this->createMock(\OCP\IUser::class);
19 $user->method('getUID')->willReturn('u1');
20 $session = $this->createMock(\OCP\IUserSession::class);
21 $session->method('getUser')->willReturn($user);
22 $controller = $this->createController(userSession: $session, localJson: $json);
23 $response = $controller->jsonContent(8, '{"link":"/test.json","path":"old","user_id":"other","cacheKey":"old","timestamp":"true"}');
24 $this->assertSame(200, $response->getStatus());
25 $this->assertSame(['content' => '{"value":0}'], $response->getData());
26 }
27
28 public function testJsonBrowseRejectsUnauthenticatedAndOtherDatasourceTypes(): void {
29 $json = $this->createMock(\OCA\Analytics\Datasource\LocalJson::class);
30 $json->expects($this->never())->method('readData');
31 $controller = $this->createController(localJson: $json);
32 $this->assertSame(400, $controller->jsonContent(8)->getStatus());
33 $user = $this->createMock(\OCP\IUser::class);
34 $session = $this->createMock(\OCP\IUserSession::class);
35 $session->method('getUser')->willReturn($user);
36 $controller = $this->createController(userSession: $session, localJson: $json);
37 $this->assertSame(400, $controller->jsonContent(1)->getStatus());
38 $this->assertSame(400, $controller->jsonContent(8, 'invalid')->getStatus());
39 }
40
41 public function testJsonBrowseLimitsResponseSize(): void {
42 $json = $this->createMock(\OCA\Analytics\Datasource\LocalJson::class);
43 $json->method('readData')->willReturn(['rawdata' => str_repeat(' ', 2 * 1024 * 1024 + 1), 'error' => 0, 'data' => []]);
44 $user = $this->createMock(\OCP\IUser::class);
45 $user->method('getUID')->willReturn('u1');
46 $session = $this->createMock(\OCP\IUserSession::class);
47 $session->method('getUser')->willReturn($user);
48 $response = $this->createController(userSession: $session, localJson: $json)->jsonContent(8);
49 $this->assertSame(400, $response->getStatus());
50 $this->assertArrayNotHasKey('content', $response->getData());
51 }
52
53 public function testSpreadsheetSheetsListsNamesForCurrentUsersFile(): void {
54 $spreadsheet = $this->createMock(\OCA\Analytics\Datasource\LocalSpreadsheet::class);
55 $spreadsheet->expects($this->once())->method('listWorksheetNames')
56 ->with('u1', '/reports/current.xlsx')
57 ->willReturn(['Summary', 'Details']);
58 $variableService = $this->createMock(\OCA\Analytics\Service\VariableService::class);
59 $variableService->expects($this->once())->method('replaceDatasourceText')
60 ->with('/reports/%today%.xlsx')
61 ->willReturn('/reports/current.xlsx');
62 $user = $this->createMock(\OCP\IUser::class);
63 $user->method('getUID')->willReturn('u1');
64 $userSession = $this->createMock(\OCP\IUserSession::class);
65 $userSession->method('getUser')->willReturn($user);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected