aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/Avatar
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lib/Avatar')
-rw-r--r--tests/lib/Avatar/AvatarManagerTest.php278
-rw-r--r--tests/lib/Avatar/GuestAvatarTest.php66
-rw-r--r--tests/lib/Avatar/UserAvatarTest.php294
3 files changed, 638 insertions, 0 deletions
diff --git a/tests/lib/Avatar/AvatarManagerTest.php b/tests/lib/Avatar/AvatarManagerTest.php
new file mode 100644
index 00000000000..495d7099d59
--- /dev/null
+++ b/tests/lib/Avatar/AvatarManagerTest.php
@@ -0,0 +1,278 @@
+<?php
+
+/**
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+
+namespace Test\Avatar;
+
+use OC\Avatar\AvatarManager;
+use OC\Avatar\PlaceholderAvatar;
+use OC\Avatar\UserAvatar;
+use OC\KnownUser\KnownUserService;
+use OC\User\Manager;
+use OC\User\User;
+use OCP\Accounts\IAccount;
+use OCP\Accounts\IAccountManager;
+use OCP\Accounts\IAccountProperty;
+use OCP\Files\IAppData;
+use OCP\Files\SimpleFS\ISimpleFolder;
+use OCP\IConfig;
+use OCP\IL10N;
+use OCP\IUser;
+use OCP\IUserSession;
+use Psr\Log\LoggerInterface;
+
+/**
+ * Class AvatarManagerTest
+ */
+class AvatarManagerTest extends \Test\TestCase {
+ /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
+ private $userSession;
+ /** @var Manager|\PHPUnit\Framework\MockObject\MockObject */
+ private $userManager;
+ /** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */
+ private $appData;
+ /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
+ private $l10n;
+ /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
+ private $logger;
+ /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
+ private $config;
+ /** @var IAccountManager|\PHPUnit\Framework\MockObject\MockObject */
+ private $accountManager;
+ /** @var AvatarManager | \PHPUnit\Framework\MockObject\MockObject */
+ private $avatarManager;
+ /** @var KnownUserService | \PHPUnit\Framework\MockObject\MockObject */
+ private $knownUserService;
+
+ protected function setUp(): void {
+ parent::setUp();
+
+ $this->userSession = $this->createMock(IUserSession::class);
+ $this->userManager = $this->createMock(Manager::class);
+ $this->appData = $this->createMock(IAppData::class);
+ $this->l10n = $this->createMock(IL10N::class);
+ $this->logger = $this->createMock(LoggerInterface::class);
+ $this->config = $this->createMock(IConfig::class);
+ $this->accountManager = $this->createMock(IAccountManager::class);
+ $this->knownUserService = $this->createMock(KnownUserService::class);
+
+ $this->avatarManager = new AvatarManager(
+ $this->userSession,
+ $this->userManager,
+ $this->appData,
+ $this->l10n,
+ $this->logger,
+ $this->config,
+ $this->accountManager,
+ $this->knownUserService
+ );
+ }
+
+ public function testGetAvatarInvalidUser(): void {
+ $this->expectException(\Exception::class);
+ $this->expectExceptionMessage('user does not exist');
+
+ $this->userManager
+ ->expects($this->once())
+ ->method('get')
+ ->with('invalidUser')
+ ->willReturn(null);
+
+ $this->avatarManager->getAvatar('invalidUser');
+ }
+
+ public function testGetAvatarForSelf(): void {
+ $user = $this->createMock(User::class);
+ $user
+ ->expects($this->any())
+ ->method('getUID')
+ ->willReturn('valid-user');
+
+ $user
+ ->expects($this->any())
+ ->method('isEnabled')
+ ->willReturn(true);
+
+ // requesting user
+ $this->userSession->expects($this->once())
+ ->method('getUser')
+ ->willReturn($user);
+
+ $this->userManager
+ ->expects($this->once())
+ ->method('get')
+ ->with('valid-user')
+ ->willReturn($user);
+
+ $account = $this->createMock(IAccount::class);
+ $this->accountManager->expects($this->once())
+ ->method('getAccount')
+ ->with($user)
+ ->willReturn($account);
+
+ $property = $this->createMock(IAccountProperty::class);
+ $account->expects($this->once())
+ ->method('getProperty')
+ ->with(IAccountManager::PROPERTY_AVATAR)
+ ->willReturn($property);
+
+ $property->expects($this->once())
+ ->method('getScope')
+ ->willReturn(IAccountManager::SCOPE_PRIVATE);
+
+ $this->knownUserService->expects($this->any())
+ ->method('isKnownToUser')
+ ->with('valid-user', 'valid-user')
+ ->willReturn(true);
+
+ $folder = $this->createMock(ISimpleFolder::class);
+ $this->appData
+ ->expects($this->once())
+ ->method('getFolder')
+ ->with('valid-user')
+ ->willReturn($folder);
+
+ $expected = new UserAvatar($folder, $this->l10n, $user, $this->logger, $this->config);
+ $this->assertEquals($expected, $this->avatarManager->getAvatar('valid-user'));
+ }
+
+ public function testGetAvatarValidUserDifferentCasing(): void {
+ $user = $this->createMock(User::class);
+ $this->userManager->expects($this->once())
+ ->method('get')
+ ->with('vaLid-USER')
+ ->willReturn($user);
+
+ $user->expects($this->once())
+ ->method('getUID')
+ ->willReturn('valid-user');
+
+ $user
+ ->expects($this->any())
+ ->method('isEnabled')
+ ->willReturn(true);
+
+ $this->userSession->expects($this->once())
+ ->method('getUser')
+ ->willReturn($user);
+
+ $folder = $this->createMock(ISimpleFolder::class);
+ $this->appData
+ ->expects($this->once())
+ ->method('getFolder')
+ ->with('valid-user')
+ ->willReturn($folder);
+
+ $account = $this->createMock(IAccount::class);
+ $this->accountManager->expects($this->once())
+ ->method('getAccount')
+ ->with($user)
+ ->willReturn($account);
+
+ $property = $this->createMock(IAccountProperty::class);
+ $account->expects($this->once())
+ ->method('getProperty')
+ ->with(IAccountManager::PROPERTY_AVATAR)
+ ->willReturn($property);
+
+ $property->expects($this->once())
+ ->method('getScope')
+ ->willReturn(IAccountManager::SCOPE_FEDERATED);
+
+ $expected = new UserAvatar($folder, $this->l10n, $user, $this->logger, $this->config);
+ $this->assertEquals($expected, $this->avatarManager->getAvatar('vaLid-USER'));
+ }
+
+ public static function dataGetAvatarScopes(): array {
+ return [
+ // public access cannot see real avatar
+ [IAccountManager::SCOPE_PRIVATE, true, false, true],
+ // unknown users cannot see real avatar
+ [IAccountManager::SCOPE_PRIVATE, false, false, true],
+ // known users can see real avatar
+ [IAccountManager::SCOPE_PRIVATE, false, true, false],
+ [IAccountManager::SCOPE_LOCAL, false, false, false],
+ [IAccountManager::SCOPE_LOCAL, true, false, false],
+ [IAccountManager::SCOPE_FEDERATED, false, false, false],
+ [IAccountManager::SCOPE_FEDERATED, true, false, false],
+ [IAccountManager::SCOPE_PUBLISHED, false, false, false],
+ [IAccountManager::SCOPE_PUBLISHED, true, false, false],
+ ];
+ }
+
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataGetAvatarScopes')]
+ public function testGetAvatarScopes($avatarScope, $isPublicCall, $isKnownUser, $expectedPlaceholder): void {
+ if ($isPublicCall) {
+ $requestingUser = null;
+ } else {
+ $requestingUser = $this->createMock(IUser::class);
+ $requestingUser->method('getUID')->willReturn('requesting-user');
+ }
+
+ // requesting user
+ $this->userSession->expects($this->once())
+ ->method('getUser')
+ ->willReturn($requestingUser);
+
+ $user = $this->createMock(User::class);
+ $user
+ ->expects($this->once())
+ ->method('getUID')
+ ->willReturn('valid-user');
+
+ $user
+ ->expects($this->any())
+ ->method('isEnabled')
+ ->willReturn(true);
+
+ $this->userManager
+ ->expects($this->once())
+ ->method('get')
+ ->with('valid-user')
+ ->willReturn($user);
+
+ $account = $this->createMock(IAccount::class);
+ $this->accountManager->expects($this->once())
+ ->method('getAccount')
+ ->with($user)
+ ->willReturn($account);
+
+ $property = $this->createMock(IAccountProperty::class);
+ $account->expects($this->once())
+ ->method('getProperty')
+ ->with(IAccountManager::PROPERTY_AVATAR)
+ ->willReturn($property);
+
+ $property->expects($this->once())
+ ->method('getScope')
+ ->willReturn($avatarScope);
+
+ $folder = $this->createMock(ISimpleFolder::class);
+ $this->appData
+ ->expects($this->once())
+ ->method('getFolder')
+ ->with('valid-user')
+ ->willReturn($folder);
+
+ if (!$isPublicCall) {
+ $this->knownUserService->expects($this->any())
+ ->method('isKnownToUser')
+ ->with('requesting-user', 'valid-user')
+ ->willReturn($isKnownUser);
+ } else {
+ $this->knownUserService->expects($this->never())
+ ->method('isKnownToUser');
+ }
+
+ if ($expectedPlaceholder) {
+ $expected = new PlaceholderAvatar($folder, $user, $this->config, $this->logger);
+ } else {
+ $expected = new UserAvatar($folder, $this->l10n, $user, $this->logger, $this->config);
+ }
+ $this->assertEquals($expected, $this->avatarManager->getAvatar('valid-user'));
+ }
+}
diff --git a/tests/lib/Avatar/GuestAvatarTest.php b/tests/lib/Avatar/GuestAvatarTest.php
new file mode 100644
index 00000000000..b49fcea6ed2
--- /dev/null
+++ b/tests/lib/Avatar/GuestAvatarTest.php
@@ -0,0 +1,66 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+
+namespace Test\Avatar;
+
+use OC\Avatar\GuestAvatar;
+use OCP\Files\SimpleFS\InMemoryFile;
+use PHPUnit\Framework\MockObject\MockObject;
+use Psr\Log\LoggerInterface;
+use Test\TestCase;
+
+/**
+ * This class provides test cases for the GuestAvatar class.
+ *
+ * @package Test\Avatar
+ */
+class GuestAvatarTest extends TestCase {
+ /**
+ * @var GuestAvatar
+ */
+ private $guestAvatar;
+
+ /**
+ * Setups a guest avatar instance for tests.
+ *
+ * @before
+ * @return void
+ */
+ public function setupGuestAvatar() {
+ /* @var MockObject|LoggerInterface $logger */
+ $logger = $this->createMock(LoggerInterface::class);
+ $config = $this->createMock(\OCP\IConfig::class);
+ $this->guestAvatar = new GuestAvatar('einstein', $config, $logger);
+ }
+
+ /**
+ * Asserts that testGet() returns the expected avatar.
+ *
+ * For the test a static name "einstein" is used and
+ * the generated image is compared with an expected one.
+ */
+ public function testGet(): void {
+ $this->markTestSkipped('TODO: Disable because fails on drone');
+ $avatar = $this->guestAvatar->getFile(32);
+ self::assertInstanceOf(InMemoryFile::class, $avatar);
+ $expectedFile = file_get_contents(
+ __DIR__ . '/../../data/guest_avatar_einstein_32.png'
+ );
+ self::assertEquals(trim($expectedFile), trim($avatar->getContent()));
+ }
+
+ /**
+ * Asserts that "testIsCustomAvatar" returns false for guests.
+ *
+ * @return void
+ */
+ public function testIsCustomAvatar(): void {
+ self::assertFalse($this->guestAvatar->isCustomAvatar());
+ }
+}
diff --git a/tests/lib/Avatar/UserAvatarTest.php b/tests/lib/Avatar/UserAvatarTest.php
new file mode 100644
index 00000000000..1ca3b8135cc
--- /dev/null
+++ b/tests/lib/Avatar/UserAvatarTest.php
@@ -0,0 +1,294 @@
+<?php
+
+/**
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace Test\Avatar;
+
+use OC\Avatar\UserAvatar;
+use OC\Files\SimpleFS\SimpleFolder;
+use OC\User\User;
+use OCP\Color;
+use OCP\Files\File;
+use OCP\Files\NotFoundException;
+use OCP\Files\SimpleFS\ISimpleFile;
+use OCP\IConfig;
+use OCP\IL10N;
+use OCP\Image;
+use PHPUnit\Framework\MockObject\MockObject;
+use Psr\Log\LoggerInterface;
+
+class UserAvatarTest extends \Test\TestCase {
+
+ private UserAvatar $avatar;
+ private SimpleFolder&MockObject $folder;
+ private IConfig&MockObject $config;
+ private User&MockObject $user;
+
+ protected function setUp(): void {
+ parent::setUp();
+
+ $this->folder = $this->createMock(SimpleFolder::class);
+ // abcdefghi is a convenient name that our algorithm convert to our nextcloud blue 0082c9
+ $this->user = $this->getUserWithDisplayName('abcdefghi');
+ $this->config = $this->createMock(IConfig::class);
+
+ $this->avatar = $this->getUserAvatar($this->user);
+ }
+
+ public static function avatarTextData(): array {
+ return [
+ ['', '?'],
+ ['matchish', 'M'],
+ ['Firstname Lastname', 'FL'],
+ ['Firstname Lastname Rest', 'FL'],
+ ];
+ }
+
+ public function testGetNoAvatar(): void {
+ $file = $this->createMock(ISimpleFile::class);
+ $this->folder->method('newFile')
+ ->willReturn($file);
+
+ $this->folder->method('getFile')
+ ->willReturnCallback(function (string $path): void {
+ if ($path === 'avatar.64.png') {
+ throw new NotFoundException();
+ }
+ });
+ $this->folder->method('fileExists')
+ ->willReturnCallback(function ($path) {
+ if ($path === 'generated') {
+ return true;
+ }
+ return false;
+ });
+
+ $data = null;
+ $file->method('putContent')
+ ->with($this->callback(function ($d) use (&$data) {
+ $data = $d;
+ return true;
+ }));
+
+ $file->method('getContent')
+ ->willReturnCallback(function () use (&$data) {
+ return $data;
+ });
+
+ $result = $this->avatar->get();
+ $this->assertTrue($result->valid());
+ }
+
+ public function testGetAvatarSizeMatch(): void {
+ $this->folder->method('fileExists')
+ ->willReturnMap([
+ ['avatar.jpg', true],
+ ['avatar.128.jpg', true],
+ ['generated', false],
+ ]);
+
+ $expected = new Image();
+ $expected->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png');
+
+ $file = $this->createMock(ISimpleFile::class);
+ $file->method('getContent')->willReturn($expected->data());
+ $this->folder->method('getFile')->with('avatar.128.jpg')->willReturn($file);
+
+ $this->assertEquals($expected->data(), $this->avatar->get(128)->data());
+ }
+
+ public function testGetAvatarSizeMinusOne(): void {
+ $this->folder->method('fileExists')
+ ->willReturnMap([
+ ['avatar.jpg', true],
+ ['generated', false],
+ ]);
+
+ $expected = new Image();
+ $expected->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png');
+
+ $file = $this->createMock(ISimpleFile::class);
+ $file->method('getContent')->willReturn($expected->data());
+ $this->folder->method('getFile')->with('avatar.jpg')->willReturn($file);
+
+ $this->assertEquals($expected->data(), $this->avatar->get(-1)->data());
+ }
+
+ public function testGetAvatarNoSizeMatch(): void {
+ $this->folder->method('fileExists')
+ ->willReturnMap([
+ ['avatar.jpg', false],
+ ['avatar.png', true],
+ ['avatar.32.png', false],
+ ['generated', false],
+ ]);
+
+ $expected = new Image();
+ $expected->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png');
+ $expected2 = new Image();
+ $expected2->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png');
+ $expected2->resize(32);
+
+ $file = $this->createMock(ISimpleFile::class);
+ $file->method('getContent')->willReturn($expected->data());
+
+ $this->folder->method('getFile')
+ ->willReturnCallback(
+ function ($path) use ($file) {
+ if ($path === 'avatar.png') {
+ return $file;
+ } else {
+ throw new NotFoundException;
+ }
+ }
+ );
+
+ $newFile = $this->createMock(ISimpleFile::class);
+ $newFile->expects($this->once())
+ ->method('putContent')
+ ->with($expected2->data());
+ $newFile->expects($this->once())
+ ->method('getContent')
+ ->willReturn($expected2->data());
+ $this->folder->expects($this->once())
+ ->method('newFile')
+ ->with('avatar.32.png')
+ ->willReturn($newFile);
+
+ $this->assertEquals($expected2->data(), $this->avatar->get(32)->data());
+ }
+
+ public function testExistsNo(): void {
+ $this->assertFalse($this->avatar->exists());
+ }
+
+ public function testExiststJPG(): void {
+ $this->folder->method('fileExists')
+ ->willReturnMap([
+ ['avatar.jpg', true],
+ ['avatar.png', false],
+ ]);
+ $this->assertTrue($this->avatar->exists());
+ }
+
+ public function testExistsPNG(): void {
+ $this->folder->method('fileExists')
+ ->willReturnMap([
+ ['avatar.jpg', false],
+ ['avatar.png', true],
+ ]);
+ $this->assertTrue($this->avatar->exists());
+ }
+
+ public function testSetAvatar(): void {
+ $avatarFileJPG = $this->createMock(File::class);
+ $avatarFileJPG->method('getName')
+ ->willReturn('avatar.jpg');
+ $avatarFileJPG->expects($this->once())->method('delete');
+
+ $avatarFilePNG = $this->createMock(File::class);
+ $avatarFilePNG->method('getName')
+ ->willReturn('avatar.png');
+ $avatarFilePNG->expects($this->once())->method('delete');
+
+ $resizedAvatarFile = $this->createMock(File::class);
+ $resizedAvatarFile->method('getName')
+ ->willReturn('avatar.32.jpg');
+ $resizedAvatarFile->expects($this->once())->method('delete');
+
+ $this->folder->method('getDirectoryListing')
+ ->willReturn([$avatarFileJPG, $avatarFilePNG, $resizedAvatarFile]);
+
+ $generated = $this->createMock(ISimpleFile::class);
+ $this->folder->method('getFile')
+ ->with('generated')
+ ->willReturn($generated);
+
+ $newFile = $this->createMock(ISimpleFile::class);
+ $this->folder->expects($this->once())
+ ->method('newFile')
+ ->with('avatar.png')
+ ->willReturn($newFile);
+
+ $image = new Image();
+ $image->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png');
+ $newFile->expects($this->once())
+ ->method('putContent')
+ ->with($image->data());
+
+ $this->config->expects($this->exactly(3))
+ ->method('setUserValue');
+ $this->config->expects($this->once())
+ ->method('getUserValue');
+
+ $this->user->expects($this->exactly(1))->method('triggerChange');
+
+ $this->avatar->set($image->data());
+ }
+
+ public function testGenerateSvgAvatar(): void {
+ $avatar = $this->invokePrivate($this->avatar, 'getAvatarVector', [$this->user->getDisplayName(), 64, false]);
+
+ $svg = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+ <svg width="64" height="64" version="1.1" viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg">
+ <rect width="100%" height="100%" fill="#e5f2f9"></rect>
+ <text x="50%" y="350" style="font-weight:normal;font-size:280px;font-family:\'Noto Sans\';text-anchor:middle;fill:#0082c9">A</text>
+ </svg>';
+ $this->assertEquals($avatar, $svg);
+ }
+
+ #[\PHPUnit\Framework\Attributes\DataProvider('avatarTextData')]
+ public function testGetAvatarText($displayName, $expectedAvatarText): void {
+ $user = $this->getUserWithDisplayName($displayName);
+ $avatar = $this->getUserAvatar($user);
+
+ $avatarText = $this->invokePrivate($avatar, 'getAvatarText');
+ $this->assertEquals($expectedAvatarText, $avatarText);
+ }
+
+ public function testHashToInt(): void {
+ $hashToInt = $this->invokePrivate($this->avatar, 'hashToInt', ['abcdef', 18]);
+ $this->assertTrue(gettype($hashToInt) === 'integer');
+ }
+
+ public function testMixPalette(): void {
+ $colorFrom = new Color(0, 0, 0);
+ $colorTo = new Color(6, 12, 18);
+ $steps = 6;
+ $palette = Color::mixPalette($steps, $colorFrom, $colorTo);
+ foreach ($palette as $j => $color) {
+ // calc increment
+ $incR = $colorTo->red() / $steps * $j;
+ $incG = $colorTo->green() / $steps * $j;
+ $incB = $colorTo->blue() / $steps * $j;
+ // ensure everything is equal
+ $this->assertEquals($color, new Color($incR, $incG, $incB));
+ }
+ $hashToInt = $this->invokePrivate($this->avatar, 'hashToInt', ['abcdef', 18]);
+ $this->assertTrue(gettype($hashToInt) === 'integer');
+ }
+
+ private function getUserWithDisplayName($name) {
+ $user = $this->createMock(User::class);
+ $user->method('getDisplayName')->willReturn($name);
+ return $user;
+ }
+
+ private function getUserAvatar($user) {
+ /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject $l */
+ $l = $this->createMock(IL10N::class);
+ $l->method('t')->willReturnArgument(0);
+
+ return new UserAvatar(
+ $this->folder,
+ $l,
+ $user,
+ $this->createMock(LoggerInterface::class),
+ $this->config
+ );
+ }
+}