Skip to content

Commit b4302fe

Browse files
authored
Merge pull request #54371 from nextcloud/fix-getting-trusted-server-other-than-the-first
fix: Fix getting trusted server other than the first
2 parents cf0b709 + b42d125 commit b4302fe

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-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: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,64 @@ public function testGetServers(): void {
144144
);
145145
}
146146

147+
public static function dataTestGetServer() {
148+
return [
149+
[
150+
15,
151+
[
152+
'id' => 15,
153+
'otherData' => 'first server',
154+
]
155+
],
156+
[
157+
16,
158+
[
159+
'id' => 16,
160+
'otherData' => 'second server',
161+
]
162+
],
163+
[
164+
42,
165+
[
166+
'id' => 42,
167+
'otherData' => 'last server',
168+
]
169+
],
170+
[
171+
108,
172+
null
173+
],
174+
];
175+
}
176+
177+
#[\PHPUnit\Framework\Attributes\DataProvider('dataTestGetServer')]
178+
public function testGetServer(int $id, ?array $expectedServer): void {
179+
$servers = [
180+
[
181+
'id' => 15,
182+
'otherData' => 'first server',
183+
],
184+
[
185+
'id' => 16,
186+
'otherData' => 'second server',
187+
],
188+
[
189+
'id' => 42,
190+
'otherData' => 'last server',
191+
],
192+
];
193+
$this->dbHandler->expects($this->once())->method('getAllServer')->willReturn($servers);
194+
195+
if ($expectedServer === null) {
196+
$this->expectException(\Exception::class);
197+
$this->expectExceptionMessage('No server found with ID: ' . $id);
198+
}
199+
200+
$this->assertEquals(
201+
$expectedServer,
202+
$this->trustedServers->getServer($id)
203+
);
204+
}
147205

148206
public function testIsTrustedServer(): void {
149207
$this->dbHandler->expects($this->once())

0 commit comments

Comments
 (0)