aboutsummaryrefslogtreecommitdiffstats
path: root/apps/federation
diff options
context:
space:
mode:
Diffstat (limited to 'apps/federation')
-rw-r--r--apps/federation/lib/TrustedServers.php9
-rw-r--r--apps/federation/tests/TrustedServersTest.php58
2 files changed, 63 insertions, 4 deletions
diff --git a/apps/federation/lib/TrustedServers.php b/apps/federation/lib/TrustedServers.php
index 3b849c80f17..3d15cfac448 100644
--- a/apps/federation/lib/TrustedServers.php
+++ b/apps/federation/lib/TrustedServers.php
@@ -116,12 +116,13 @@ class TrustedServers {
$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);
}
/**
diff --git a/apps/federation/tests/TrustedServersTest.php b/apps/federation/tests/TrustedServersTest.php
index c8477f637cb..0c900f6edf7 100644
--- a/apps/federation/tests/TrustedServersTest.php
+++ b/apps/federation/tests/TrustedServersTest.php
@@ -144,6 +144,64 @@ class TrustedServersTest extends TestCase {
);
}
+ 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
+ ],
+ ];
+ }
+
+ #[\PHPUnit\Framework\Attributes\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())