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