Skip to content

Commit ed69213

Browse files
authored
Merge pull request #54381 from nextcloud/backport/54371/stable31
[stable31] fix: Fix getting trusted server other than the first
2 parents 561f013 + 9b691c2 commit ed69213

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,66 @@ 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+
/**
198+
* @dataProvider dataTestGetServer
199+
*/
200+
public function testGetServer(int $id, ?array $expectedServer): void {
201+
$servers = [
202+
[
203+
'id' => 15,
204+
'otherData' => 'first server',
205+
],
206+
[
207+
'id' => 16,
208+
'otherData' => 'second server',
209+
],
210+
[
211+
'id' => 42,
212+
'otherData' => 'last server',
213+
],
214+
];
215+
$this->dbHandler->expects($this->once())->method('getAllServer')->willReturn($servers);
216+
217+
if ($expectedServer === null) {
218+
$this->expectException(\Exception::class);
219+
$this->expectExceptionMessage('No server found with ID: ' . $id);
220+
}
221+
222+
$this->assertEquals(
223+
$expectedServer,
224+
$this->trustedServers->getServer($id)
225+
);
226+
}
167227

168228
public function testIsTrustedServer(): void {
169229
$this->dbHandler->expects($this->once())

0 commit comments

Comments
 (0)