Skip to content

Commit 4700e3f

Browse files
danxuliubackportbot[bot]
authored andcommitted
fix: Fix getting trusted server other than the first
"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 <danxuliu@gmail.com>
1 parent 4ccb4c2 commit 4700e3f

2 files changed

Lines changed: 63 additions & 4 deletions

File tree

apps/federation/lib/TrustedServers.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,13 @@ public function getServer(int $id): ?array {
116116
$this->trustedServersCache = $this->dbHandler->getAllServer();
117117
}
118118

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

124-
return $server[0];
125+
throw new \Exception('No server found with ID: ' . $id);
125126
}
126127

127128
/**

apps/federation/tests/TrustedServersTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,64 @@ public function testGetServers(): void {
164164
);
165165
}
166166

167+
public static function dataTestGetServer() {
168+
return [
169+
[
170+
15,
171+
[
172+
'id' => 15,
173+
'otherData' => 'first server',
174+
]
175+
],
176+
[
177+
16,
178+
[
179+
'id' => 16,
180+
'otherData' => 'second server',
181+
]
182+
],
183+
[
184+
42,
185+
[
186+
'id' => 42,
187+
'otherData' => 'last server',
188+
]
189+
],
190+
[
191+
108,
192+
null
193+
],
194+
];
195+
}
196+
197+
#[\PHPUnit\Framework\Attributes\DataProvider('dataTestGetServer')]
198+
public function testGetServer(int $id, ?array $expectedServer): void {
199+
$servers = [
200+
[
201+
'id' => 15,
202+
'otherData' => 'first server',
203+
],
204+
[
205+
'id' => 16,
206+
'otherData' => 'second server',
207+
],
208+
[
209+
'id' => 42,
210+
'otherData' => 'last server',
211+
],
212+
];
213+
$this->dbHandler->expects($this->once())->method('getAllServer')->willReturn($servers);
214+
215+
if ($expectedServer === null) {
216+
$this->expectException(\Exception::class);
217+
$this->expectExceptionMessage('No server found with ID: ' . $id);
218+
}
219+
220+
$this->assertEquals(
221+
$expectedServer,
222+
$this->trustedServers->getServer($id)
223+
);
224+
}
167225

168226
public function testIsTrustedServer(): void {
169227
$this->dbHandler->expects($this->once())

0 commit comments

Comments
 (0)