diff options
Diffstat (limited to 'tests/lib/Avatar')
-rw-r--r-- | tests/lib/Avatar/AvatarManagerTest.php | 106 | ||||
-rw-r--r-- | tests/lib/Avatar/GuestAvatarTest.php | 31 | ||||
-rw-r--r-- | tests/lib/Avatar/UserAvatarTest.php | 112 |
3 files changed, 127 insertions, 122 deletions
diff --git a/tests/lib/Avatar/AvatarManagerTest.php b/tests/lib/Avatar/AvatarManagerTest.php index ce6981a2a21..495d7099d59 100644 --- a/tests/lib/Avatar/AvatarManagerTest.php +++ b/tests/lib/Avatar/AvatarManagerTest.php @@ -1,25 +1,9 @@ <?php + /** - * @author Roeland Jago Douma <rullzer@owncloud.com> - * @author Lukas Reschke <lukas@statuscode.ch> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @copyright Copyright (c) 2016, Lukas Reschke <lukas@statuscode.ch> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace Test\Avatar; @@ -29,6 +13,7 @@ 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; @@ -87,7 +72,7 @@ class AvatarManagerTest extends \Test\TestCase { ); } - public function testGetAvatarInvalidUser() { + public function testGetAvatarInvalidUser(): void { $this->expectException(\Exception::class); $this->expectExceptionMessage('user does not exist'); @@ -100,13 +85,18 @@ class AvatarManagerTest extends \Test\TestCase { $this->avatarManager->getAvatar('invalidUser'); } - public function testGetAvatarForSelf() { - $user = $this->createMock(IUser::class); + 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') @@ -135,9 +125,9 @@ class AvatarManagerTest extends \Test\TestCase { ->willReturn(IAccountManager::SCOPE_PRIVATE); $this->knownUserService->expects($this->any()) - ->method('isKnownToUser') - ->with('valid-user', 'valid-user') - ->willReturn(true); + ->method('isKnownToUser') + ->with('valid-user', 'valid-user') + ->willReturn(true); $folder = $this->createMock(ISimpleFolder::class); $this->appData @@ -150,8 +140,8 @@ class AvatarManagerTest extends \Test\TestCase { $this->assertEquals($expected, $this->avatarManager->getAvatar('valid-user')); } - public function testGetAvatarValidUserDifferentCasing() { - $user = $this->createMock(IUser::class); + public function testGetAvatarValidUserDifferentCasing(): void { + $user = $this->createMock(User::class); $this->userManager->expects($this->once()) ->method('get') ->with('vaLid-USER') @@ -161,6 +151,15 @@ class AvatarManagerTest extends \Test\TestCase { ->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()) @@ -168,28 +167,45 @@ class AvatarManagerTest extends \Test\TestCase { ->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 function knownUnknownProvider() { + public static function dataGetAvatarScopes(): array { return [ - [IAccountManager::SCOPE_LOCAL, false, false, false], - [IAccountManager::SCOPE_LOCAL, true, false, false], - // 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], ]; } - /** - * @dataProvider knownUnknownProvider - */ - public function testGetAvatarScopes($avatarScope, $isPublicCall, $isKnownUser, $expectedPlaceholder) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetAvatarScopes')] + public function testGetAvatarScopes($avatarScope, $isPublicCall, $isKnownUser, $expectedPlaceholder): void { if ($isPublicCall) { $requestingUser = null; } else { @@ -202,11 +218,17 @@ class AvatarManagerTest extends \Test\TestCase { ->method('getUser') ->willReturn($requestingUser); - $user = $this->createMock(IUser::class); + $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') @@ -238,16 +260,16 @@ class AvatarManagerTest extends \Test\TestCase { if (!$isPublicCall) { $this->knownUserService->expects($this->any()) - ->method('isKnownToUser') - ->with('requesting-user', 'valid-user') - ->willReturn($isKnownUser); + ->method('isKnownToUser') + ->with('requesting-user', 'valid-user') + ->willReturn($isKnownUser); } else { $this->knownUserService->expects($this->never()) - ->method('isKnownToUser'); + ->method('isKnownToUser'); } if ($expectedPlaceholder) { - $expected = new PlaceholderAvatar($folder, $user, $this->createMock(LoggerInterface::class)); + $expected = new PlaceholderAvatar($folder, $user, $this->config, $this->logger); } else { $expected = new UserAvatar($folder, $this->l10n, $user, $this->logger, $this->config); } diff --git a/tests/lib/Avatar/GuestAvatarTest.php b/tests/lib/Avatar/GuestAvatarTest.php index b8e6d8ae2e8..b49fcea6ed2 100644 --- a/tests/lib/Avatar/GuestAvatarTest.php +++ b/tests/lib/Avatar/GuestAvatarTest.php @@ -3,23 +3,8 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2018, Michael Weimann <mail@michael-weimann.eu> - * - * @author Michael Weimann <mail@michael-weimann.eu> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-only */ namespace Test\Avatar; @@ -49,8 +34,9 @@ class GuestAvatarTest extends TestCase { */ public function setupGuestAvatar() { /* @var MockObject|LoggerInterface $logger */ - $logger = $this->getMockBuilder(LoggerInterface::class)->getMock(); - $this->guestAvatar = new GuestAvatar('einstein', $logger); + $logger = $this->createMock(LoggerInterface::class); + $config = $this->createMock(\OCP\IConfig::class); + $this->guestAvatar = new GuestAvatar('einstein', $config, $logger); } /** @@ -58,10 +44,9 @@ class GuestAvatarTest extends TestCase { * * For the test a static name "einstein" is used and * the generated image is compared with an expected one. - * - * @return void */ - public function testGet() { + 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( @@ -75,7 +60,7 @@ class GuestAvatarTest extends TestCase { * * @return void */ - public function testIsCustomAvatar() { + public function testIsCustomAvatar(): void { self::assertFalse($this->guestAvatar->isCustomAvatar()); } } diff --git a/tests/lib/Avatar/UserAvatarTest.php b/tests/lib/Avatar/UserAvatarTest.php index dd5f25163f2..1ca3b8135cc 100644 --- a/tests/lib/Avatar/UserAvatarTest.php +++ b/tests/lib/Avatar/UserAvatarTest.php @@ -1,35 +1,32 @@ <?php + /** - * Copyright (c) 2013 Christopher Schäpers <christopher@schaepers.it> - * This file is licensed under the Affero General Public License version 3 or - * later. - * See the COPYING-README file. + * 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\Folder; 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 { - /** @var Folder | \PHPUnit\Framework\MockObject\MockObject */ - private $folder; - - /** @var \OC\Avatar\UserAvatar */ - private $avatar; - - /** @var \OC\User\User | \PHPUnit\Framework\MockObject\MockObject $user */ - private $user; - /** @var \OCP\IConfig|\PHPUnit\Framework\MockObject\MockObject */ - private $config; + private UserAvatar $avatar; + private SimpleFolder&MockObject $folder; + private IConfig&MockObject $config; + private User&MockObject $user; protected function setUp(): void { parent::setUp(); @@ -42,7 +39,7 @@ class UserAvatarTest extends \Test\TestCase { $this->avatar = $this->getUserAvatar($this->user); } - public function avatarTextData() { + public static function avatarTextData(): array { return [ ['', '?'], ['matchish', 'M'], @@ -51,13 +48,13 @@ class UserAvatarTest extends \Test\TestCase { ]; } - public function testGetNoAvatar() { + public function testGetNoAvatar(): void { $file = $this->createMock(ISimpleFile::class); $this->folder->method('newFile') ->willReturn($file); $this->folder->method('getFile') - ->willReturnCallback(function ($path) { + ->willReturnCallback(function (string $path): void { if ($path === 'avatar.64.png') { throw new NotFoundException(); } @@ -86,53 +83,57 @@ class UserAvatarTest extends \Test\TestCase { $this->assertTrue($result->valid()); } - public function testGetAvatarSizeMatch() { + public function testGetAvatarSizeMatch(): void { $this->folder->method('fileExists') ->willReturnMap([ ['avatar.jpg', true], ['avatar.128.jpg', true], + ['generated', false], ]); - $expected = new \OC_Image(); + $expected = new Image(); $expected->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png'); - $file = $this->createMock(File::class); + $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() { + public function testGetAvatarSizeMinusOne(): void { $this->folder->method('fileExists') ->willReturnMap([ ['avatar.jpg', true], + ['generated', false], ]); - $expected = new \OC_Image(); + $expected = new Image(); $expected->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png'); - $file = $this->createMock(File::class); + $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() { + public function testGetAvatarNoSizeMatch(): void { $this->folder->method('fileExists') ->willReturnMap([ + ['avatar.jpg', false], ['avatar.png', true], ['avatar.32.png', false], + ['generated', false], ]); - $expected = new \OC_Image(); + $expected = new Image(); $expected->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png'); - $expected2 = new \OC_Image(); + $expected2 = new Image(); $expected2->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png'); $expected2->resize(32); - $file = $this->createMock(File::class); + $file = $this->createMock(ISimpleFile::class); $file->method('getContent')->willReturn($expected->data()); $this->folder->method('getFile') @@ -141,12 +142,12 @@ class UserAvatarTest extends \Test\TestCase { if ($path === 'avatar.png') { return $file; } else { - throw new \OCP\Files\NotFoundException; + throw new NotFoundException; } } ); - $newFile = $this->createMock(File::class); + $newFile = $this->createMock(ISimpleFile::class); $newFile->expects($this->once()) ->method('putContent') ->with($expected2->data()); @@ -161,11 +162,11 @@ class UserAvatarTest extends \Test\TestCase { $this->assertEquals($expected2->data(), $this->avatar->get(32)->data()); } - public function testExistsNo() { + public function testExistsNo(): void { $this->assertFalse($this->avatar->exists()); } - public function testExiststJPG() { + public function testExiststJPG(): void { $this->folder->method('fileExists') ->willReturnMap([ ['avatar.jpg', true], @@ -174,7 +175,7 @@ class UserAvatarTest extends \Test\TestCase { $this->assertTrue($this->avatar->exists()); } - public function testExistsPNG() { + public function testExistsPNG(): void { $this->folder->method('fileExists') ->willReturnMap([ ['avatar.jpg', false], @@ -183,7 +184,7 @@ class UserAvatarTest extends \Test\TestCase { $this->assertTrue($this->avatar->exists()); } - public function testSetAvatar() { + public function testSetAvatar(): void { $avatarFileJPG = $this->createMock(File::class); $avatarFileJPG->method('getName') ->willReturn('avatar.jpg'); @@ -202,18 +203,18 @@ class UserAvatarTest extends \Test\TestCase { $this->folder->method('getDirectoryListing') ->willReturn([$avatarFileJPG, $avatarFilePNG, $resizedAvatarFile]); - $generated = $this->createMock(File::class); + $generated = $this->createMock(ISimpleFile::class); $this->folder->method('getFile') ->with('generated') ->willReturn($generated); - $newFile = $this->createMock(File::class); + $newFile = $this->createMock(ISimpleFile::class); $this->folder->expects($this->once()) ->method('newFile') ->with('avatar.png') ->willReturn($newFile); - $image = new \OC_Image(); + $image = new Image(); $image->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png'); $newFile->expects($this->once()) ->method('putContent') @@ -229,22 +230,19 @@ class UserAvatarTest extends \Test\TestCase { $this->avatar->set($image->data()); } - public function testGenerateSvgAvatar() { - $avatar = $this->invokePrivate($this->avatar, 'getAvatarVector', [64]); + 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="#0082c9"></rect> - <text x="50%" y="350" style="font-weight:normal;font-size:280px;font-family:\'Noto Sans\';text-anchor:middle;fill:#fff">A</text> + <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); } - - /** - * @dataProvider avatarTextData - */ - public function testGetAvatarText($displayName, $expectedAvatarText) { + #[\PHPUnit\Framework\Attributes\DataProvider('avatarTextData')] + public function testGetAvatarText($displayName, $expectedAvatarText): void { $user = $this->getUserWithDisplayName($displayName); $avatar = $this->getUserAvatar($user); @@ -252,23 +250,23 @@ class UserAvatarTest extends \Test\TestCase { $this->assertEquals($expectedAvatarText, $avatarText); } - public function testHashToInt() { + public function testHashToInt(): void { $hashToInt = $this->invokePrivate($this->avatar, 'hashToInt', ['abcdef', 18]); $this->assertTrue(gettype($hashToInt) === 'integer'); } - public function testMixPalette() { - $colorFrom = new \OC\Color(0, 0, 0); - $colorTo = new \OC\Color(6, 12, 18); + public function testMixPalette(): void { + $colorFrom = new Color(0, 0, 0); + $colorTo = new Color(6, 12, 18); $steps = 6; - $palette = $this->invokePrivate($this->avatar, 'mixPalette', [$steps, $colorFrom, $colorTo]); + $palette = Color::mixPalette($steps, $colorFrom, $colorTo); foreach ($palette as $j => $color) { // calc increment - $incR = $colorTo->r / $steps * $j; - $incG = $colorTo->g / $steps * $j; - $incB = $colorTo->b / $steps * $j; + $incR = $colorTo->red() / $steps * $j; + $incG = $colorTo->green() / $steps * $j; + $incB = $colorTo->blue() / $steps * $j; // ensure everything is equal - $this->assertEquals($color, new \OC\Color($incR, $incG, $incB)); + $this->assertEquals($color, new Color($incR, $incG, $incB)); } $hashToInt = $this->invokePrivate($this->avatar, 'hashToInt', ['abcdef', 18]); $this->assertTrue(gettype($hashToInt) === 'integer'); @@ -281,11 +279,11 @@ class UserAvatarTest extends \Test\TestCase { } private function getUserAvatar($user) { - /** @var \OCP\IL10N | \PHPUnit\Framework\MockObject\MockObject $l */ + /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject $l */ $l = $this->createMock(IL10N::class); $l->method('t')->willReturnArgument(0); - return new \OC\Avatar\UserAvatar( + return new UserAvatar( $this->folder, $l, $user, |