1010
1111use OC \Avatar \AvatarManager ;
1212use OC \Avatar \PlaceholderAvatar ;
13+ use OC \Avatar \RemoteAvatar ;
1314use OC \Avatar \UserAvatar ;
1415use OC \KnownUser \KnownUserService ;
1516use OC \User \Manager ;
1617use OC \User \User ;
1718use OCP \Accounts \IAccount ;
1819use OCP \Accounts \IAccountManager ;
1920use OCP \Accounts \IAccountProperty ;
21+ use OCP \Federation \ICloudId ;
22+ use OCP \Federation \ICloudIdManager ;
2023use OCP \Files \IAppData ;
2124use OCP \Files \SimpleFS \ISimpleFolder ;
2225use OCP \IConfig ;
@@ -47,6 +50,7 @@ class AvatarManagerTest extends \Test\TestCase {
4750 private $ avatarManager ;
4851 /** @var KnownUserService | \PHPUnit\Framework\MockObject\MockObject */
4952 private $ knownUserService ;
53+ private ICloudIdManager &\PHPUnit \Framework \MockObject \MockObject $ cloudIdManager ;
5054
5155 #[\Override]
5256 protected function setUp (): void {
@@ -60,6 +64,7 @@ protected function setUp(): void {
6064 $ this ->config = $ this ->createMock (IConfig::class);
6165 $ this ->accountManager = $ this ->createMock (IAccountManager::class);
6266 $ this ->knownUserService = $ this ->createMock (KnownUserService::class);
67+ $ this ->cloudIdManager = $ this ->createMock (ICloudIdManager::class);
6368
6469 $ this ->avatarManager = new AvatarManager (
6570 $ this ->userSession ,
@@ -69,23 +74,11 @@ protected function setUp(): void {
6974 $ this ->logger ,
7075 $ this ->config ,
7176 $ this ->accountManager ,
72- $ this ->knownUserService
77+ $ this ->knownUserService ,
78+ $ this ->cloudIdManager
7379 );
7480 }
7581
76- public function testGetAvatarInvalidUser (): void {
77- $ this ->expectException (\Exception::class);
78- $ this ->expectExceptionMessage ('user does not exist ' );
79-
80- $ this ->userManager
81- ->expects ($ this ->once ())
82- ->method ('get ' )
83- ->with ('invalidUser ' )
84- ->willReturn (null );
85-
86- $ this ->avatarManager ->getAvatar ('invalidUser ' );
87- }
88-
8982 public function testGetAvatarForSelf (): void {
9083 $ user = $ this ->createMock (User::class);
9184 $ user
@@ -276,4 +269,67 @@ public function testGetAvatarScopes($avatarScope, $isPublicCall, $isKnownUser, $
276269 }
277270 $ this ->assertEquals ($ expected , $ this ->avatarManager ->getAvatar ('valid-user ' ));
278271 }
272+
273+ public function testGetAvatarInvalidUser (): void {
274+ $ this ->expectException (\Exception::class);
275+ $ this ->expectExceptionMessage ('user does not exist ' );
276+
277+ $ this ->userManager
278+ ->expects ($ this ->once ())
279+ ->method ('get ' )
280+ ->with ('invalidUser ' )
281+ ->willReturn (null );
282+
283+ $ this ->avatarManager ->getAvatar ('invalidUser ' );
284+ }
285+
286+ public function testGetAvatarForRemoteUser (): void {
287+ $ cloudId = 'user@https://remote.example.com ' ;
288+
289+ $ this ->userManager
290+ ->expects ($ this ->never ())
291+ ->method ('get ' );
292+
293+ $ resolvedCloudId = $ this ->createMock (ICloudId::class);
294+ $ resolvedCloudId ->method ('getUser ' )->willReturn ('user ' );
295+ $ resolvedCloudId ->method ('getRemote ' )->willReturn ('https://remote.example.com ' );
296+ $ resolvedCloudId ->method ('getDisplayId ' )->willReturn ('user@remote.example.com ' );
297+
298+ $ this ->cloudIdManager ->expects ($ this ->once ())
299+ ->method ('isValidCloudId ' )
300+ ->with ($ cloudId )
301+ ->willReturn (true );
302+ $ this ->cloudIdManager ->method ('resolveCloudId ' )
303+ ->with ($ cloudId )
304+ ->willReturn ($ resolvedCloudId );
305+ $ this ->overwriteService (ICloudIdManager::class, $ this ->cloudIdManager );
306+
307+ $ this ->appData ->expects ($ this ->once ())->method ('getFolder ' );
308+ $ this ->accountManager ->expects ($ this ->never ())->method ('getAccount ' );
309+
310+ $ avatar = $ this ->avatarManager ->getAvatar ($ cloudId );
311+
312+ $ this ->assertInstanceOf (RemoteAvatar::class, $ avatar );
313+ $ this ->assertTrue ($ avatar ->exists ());
314+ $ this ->assertTrue ($ avatar ->isCustomAvatar ());
315+ $ this ->assertSame ('user@remote.example.com ' , $ avatar ->getDisplayName ());
316+ }
317+
318+ public function testGetAvatarThrowsForUnknownUserThatIsNotACloudId (): void {
319+ $ this ->expectException (\Exception::class);
320+ $ this ->expectExceptionMessage ('user does not exist ' );
321+
322+ $ this ->userManager
323+ ->expects ($ this ->once ())
324+ ->method ('get ' )
325+ ->with ('invalidUser ' )
326+ ->willReturn (null );
327+
328+ $ this ->cloudIdManager ->expects ($ this ->once ())
329+ ->method ('isValidCloudId ' )
330+ ->with ('invalidUser ' )
331+ ->willReturn (false );
332+
333+ $ this ->avatarManager ->getAvatar ('invalidUser ' );
334+ }
279335}
0 commit comments