Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 5 additions & 4 deletions apps/federation/lib/TrustedServers.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,13 @@ public function getServer(int $id): ?array {
$this->trustedServersCache = $this->dbHandler->getAllServer();
}

$server = array_filter($this->trustedServersCache, fn ($server) => $server['id'] === $id);
if (empty($server)) {
throw new \Exception('No server found with ID: ' . $id);
foreach ($this->trustedServersCache as $server) {
if ($server['id'] === $id) {
return $server;
}
}

return $server[0];
throw new \Exception('No server found with ID: ' . $id);
}

/**
Expand Down
60 changes: 60 additions & 0 deletions apps/federation/tests/TrustedServersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,66 @@ public function testGetServers(): void {
);
}

public static function dataTestGetServer() {
return [
[
15,
[
'id' => 15,
'otherData' => 'first server',
]
],
[
16,
[
'id' => 16,
'otherData' => 'second server',
]
],
[
42,
[
'id' => 42,
'otherData' => 'last server',
]
],
[
108,
null
],
];
}

/**
* @dataProvider dataTestGetServer
*/
public function testGetServer(int $id, ?array $expectedServer): void {
$servers = [
[
'id' => 15,
'otherData' => 'first server',
],
[
'id' => 16,
'otherData' => 'second server',
],
[
'id' => 42,
'otherData' => 'last server',
],
];
$this->dbHandler->expects($this->once())->method('getAllServer')->willReturn($servers);

if ($expectedServer === null) {
$this->expectException(\Exception::class);
$this->expectExceptionMessage('No server found with ID: ' . $id);
}

$this->assertEquals(
$expectedServer,
$this->trustedServers->getServer($id)
);
}

public function testIsTrustedServer(): void {
$this->dbHandler->expects($this->once())
Expand Down
Loading