From 9b691c288499230696f8537fbae72e5e4d03295d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Calvi=C3=B1o=20S=C3=A1nchez?= Date: Mon, 11 Aug 2025 03:12:44 +0200 Subject: [PATCH] fix: Fix getting trusted server other than the first MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit "array_filter" preserves the keys, so after the trusted servers were filtered "$server[0]" existed only if the server to get was the first one in the original array. Signed-off-by: Daniel Calviño Sánchez --- apps/federation/lib/TrustedServers.php | 9 +-- apps/federation/tests/TrustedServersTest.php | 60 ++++++++++++++++++++ 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/apps/federation/lib/TrustedServers.php b/apps/federation/lib/TrustedServers.php index a62ddfa127506..a8573003bd658 100644 --- a/apps/federation/lib/TrustedServers.php +++ b/apps/federation/lib/TrustedServers.php @@ -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); } /** diff --git a/apps/federation/tests/TrustedServersTest.php b/apps/federation/tests/TrustedServersTest.php index c3f0368858bc3..098ecd1082afb 100644 --- a/apps/federation/tests/TrustedServersTest.php +++ b/apps/federation/tests/TrustedServersTest.php @@ -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())