|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace Test\Avatar; |
| 11 | + |
| 12 | +use OC\Avatar\RemoteAvatar; |
| 13 | +use OCP\Federation\ICloudId; |
| 14 | +use OCP\Federation\ICloudIdManager; |
| 15 | +use OCP\Files\SimpleFS\ISimpleFile; |
| 16 | +use OCP\Files\SimpleFS\ISimpleFolder; |
| 17 | +use OCP\Http\Client\IClient; |
| 18 | +use OCP\Http\Client\IClientService; |
| 19 | +use OCP\Http\Client\IResponse; |
| 20 | +use OCP\IConfig; |
| 21 | +use PHPUnit\Framework\MockObject\MockObject; |
| 22 | +use Psr\Log\LoggerInterface; |
| 23 | +use Test\TestCase; |
| 24 | + |
| 25 | +class RemoteAvatarTest extends TestCase { |
| 26 | + private const CLOUD_ID = 'user@https://remote.example.com'; |
| 27 | + |
| 28 | + private ISimpleFolder&MockObject $folder; |
| 29 | + private IConfig&MockObject $config; |
| 30 | + private LoggerInterface&MockObject $logger; |
| 31 | + private ICloudIdManager&MockObject $cloudIdManager; |
| 32 | + private IClientService&MockObject $clientService; |
| 33 | + private RemoteAvatar $avatar; |
| 34 | + |
| 35 | + #[\Override] |
| 36 | + protected function setUp(): void { |
| 37 | + parent::setUp(); |
| 38 | + |
| 39 | + $this->config = $this->createMock(IConfig::class); |
| 40 | + $this->logger = $this->createMock(LoggerInterface::class); |
| 41 | + |
| 42 | + $cloudId = $this->createMock(ICloudId::class); |
| 43 | + $cloudId->method('getUser')->willReturn('user'); |
| 44 | + $cloudId->method('getRemote')->willReturn('https://remote.example.com'); |
| 45 | + $cloudId->method('getDisplayId')->willReturn('user@remote.example.com'); |
| 46 | + |
| 47 | + $this->cloudIdManager = $this->createMock(ICloudIdManager::class); |
| 48 | + $this->cloudIdManager->method('resolveCloudId') |
| 49 | + ->with(self::CLOUD_ID) |
| 50 | + ->willReturn($cloudId); |
| 51 | + $this->overwriteService(ICloudIdManager::class, $this->cloudIdManager); |
| 52 | + |
| 53 | + $this->clientService = $this->createMock(IClientService::class); |
| 54 | + $this->overwriteService(IClientService::class, $this->clientService); |
| 55 | + |
| 56 | + $this->folder = $this->createMock(ISimpleFolder::class); |
| 57 | + $this->avatar = new RemoteAvatar($this->folder, self::CLOUD_ID, $this->config, $this->logger); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Stubs the client returned by IClientService::newClient() to respond to |
| 62 | + * a single GET request, optionally asserting the requested URL/options. |
| 63 | + * |
| 64 | + * @param string|resource|false $body |
| 65 | + */ |
| 66 | + private function mockRemoteClient(string $contentType, string $body, ?string $expectedUrl = null, ?array $expectedOptions = null): void { |
| 67 | + $response = $this->createMock(IResponse::class); |
| 68 | + $response->method('getHeader')->with('Content-Type')->willReturn($contentType); |
| 69 | + $response->method('getBody')->willReturn($body); |
| 70 | + |
| 71 | + $client = $this->createMock(IClient::class); |
| 72 | + $matcher = $client->expects($this->once())->method('get'); |
| 73 | + if ($expectedUrl !== null) { |
| 74 | + $matcher->with($expectedUrl, $expectedOptions ?? $this->anything()); |
| 75 | + } |
| 76 | + $matcher->willReturn($response); |
| 77 | + |
| 78 | + $this->clientService->method('newClient')->willReturn($client); |
| 79 | + } |
| 80 | + |
| 81 | + public function testExists(): void { |
| 82 | + $this->assertTrue($this->avatar->exists()); |
| 83 | + } |
| 84 | + |
| 85 | + public function testGetDisplayName(): void { |
| 86 | + $this->assertSame('user@remote.example.com', $this->avatar->getDisplayName()); |
| 87 | + } |
| 88 | + |
| 89 | + public function testGetFileFetchesAvatarFromRemoteInstance(): void { |
| 90 | + $this->config->method('getSystemValueBool') |
| 91 | + ->with('sharing.federation.allowSelfSignedCertificates', false) |
| 92 | + ->willReturn(false); |
| 93 | + |
| 94 | + $fileContents = 'png-bytes'; |
| 95 | + |
| 96 | + $this->mockRemoteClient( |
| 97 | + 'image/png', |
| 98 | + $fileContents, |
| 99 | + 'https://remote.example.com/index.php/avatar/user/64', |
| 100 | + ['verify' => true], |
| 101 | + ); |
| 102 | + |
| 103 | + $expectedFile = $this->createMock(ISimpleFile::class); |
| 104 | + $expectedFile->expects($this->once())->method('getContent')->willReturn($fileContents); |
| 105 | + |
| 106 | + $this->folder->expects($this->once())->method('newFile')->willReturn($expectedFile); |
| 107 | + |
| 108 | + $file = $this->avatar->getFile(64); |
| 109 | + $this->assertInstanceOf(ISimpleFile::class, $file); |
| 110 | + $this->assertSame($fileContents, $file->getContent()); |
| 111 | + } |
| 112 | + |
| 113 | + public function testGetFileFetchesAvatarFromCache(): void { |
| 114 | + $this->config->method('getSystemValueBool') |
| 115 | + ->with('sharing.federation.allowSelfSignedCertificates', false) |
| 116 | + ->willReturn(false); |
| 117 | + |
| 118 | + $fileContents = 'png-bytes'; |
| 119 | + |
| 120 | + $expectedFile = $this->createMock(ISimpleFile::class); |
| 121 | + $expectedFile->expects($this->once())->method('getContent')->willReturn($fileContents); |
| 122 | + $expectedFile->expects($this->once())->method('getMTime')->willReturn(time() + (60 * 60 * 24)); |
| 123 | + $expectedFile->expects($this->once())->method('getName')->willReturn('avatar.64.png'); |
| 124 | + $this->folder->expects($this->once())->method('getDirectoryListing')->willReturn([$expectedFile]); |
| 125 | + |
| 126 | + $this->clientService->expects($this->never())->method('newClient'); |
| 127 | + |
| 128 | + $file = $this->avatar->getFile(64); |
| 129 | + $this->assertInstanceOf(ISimpleFile::class, $file); |
| 130 | + $this->assertSame($fileContents, $file->getContent()); |
| 131 | + } |
| 132 | + |
| 133 | + public function testGetFileThrowsOnUnexpectedContentType(): void { |
| 134 | + $this->mockRemoteClient('text/html', '<html></html>'); |
| 135 | + |
| 136 | + $this->expectException(\Exception::class); |
| 137 | + $this->expectExceptionMessage('Unknown filetype'); |
| 138 | + |
| 139 | + $this->avatar->getFile(64); |
| 140 | + } |
| 141 | + |
| 142 | + public function testIsCustomAvatar(): void { |
| 143 | + $this->assertTrue($this->avatar->isCustomAvatar()); |
| 144 | + } |
| 145 | +} |
0 commit comments