diff --git a/lib/CurrentUser.php b/lib/CurrentUser.php index 8f5f59aa9..2e7649768 100644 --- a/lib/CurrentUser.php +++ b/lib/CurrentUser.php @@ -90,13 +90,15 @@ public function getUID() { * @return string|null */ public function getCloudId() { - if ($this->cloudId === false) { - $user = $this->userSession->getUser(); - if ($user instanceof IUser) { - $this->cloudId = (string)$user->getCloudId(); - } else { - $this->cloudId = $this->getCloudIDFromToken(); - } + if ($this->cloudId !== false) { + return $this->cloudId; + } + + $user = $this->userSession->getUser(); + if ($user instanceof IUser) { + $this->cloudId = (string)$user->getCloudId(); + } else { + $this->cloudId = $this->getCloudIDFromToken(); } return $this->cloudId; diff --git a/tests/CurrentUserTest.php b/tests/CurrentUserTest.php index 28a28ce6a..45644cf7f 100644 --- a/tests/CurrentUserTest.php +++ b/tests/CurrentUserTest.php @@ -194,4 +194,33 @@ public function testGetCloudIDFromToken(array $server, array $shareData, ?string $this->assertSame($expected, self::invokePrivate($instance, 'getCloudIDFromToken')); } + + public function testGetCloudIdCached(): void { + $instance = $this->getInstance(); + self::invokePrivate($instance, 'cloudId', ['cached-cloud-id']); + + $this->userSession->expects($this->never())->method('getUser'); + + $this->assertSame('cached-cloud-id', $instance->getCloudId()); + } + + public function testGetCloudIdFromUser(): void { + $user = $this->createMock(IUser::class); + $user->method('getCloudId')->willReturn('user@cloud.example.com'); + $this->userSession->method('getUser')->willReturn($user); + + $instance = $this->getInstance(); + + $this->assertSame('user@cloud.example.com', $instance->getCloudId()); + $this->assertSame('user@cloud.example.com', $instance->getCloudId(), 'second call returns cached value'); + } + + public function testGetCloudIdFallsBackToToken(): void { + $this->userSession->method('getUser')->willReturn(null); + + $instance = $this->getInstance(['getCloudIDFromToken']); + $instance->method('getCloudIDFromToken')->willReturn('token-cloud-id'); + + $this->assertSame('token-cloud-id', $instance->getCloudId()); + } }