mirror of
https://github.com/nextcloud/server.git
synced 2026-06-09 08:44:07 -04:00
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>
This commit is contained in:
parent
2427b864ec
commit
b42d125950
2 changed files with 63 additions and 4 deletions
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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())
|
||||
|
|
|
|||
Loading…
Reference in a new issue