From bf1253cb49a4931244a6bbde4dfa44bf084f4377 Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Sun, 20 Jan 2019 11:13:41 +0100 Subject: Implement guest avatar endpoint Signed-off-by: Michael Weimann --- tests/Core/Controller/AvatarControllerTest.php | 4 +- .../Core/Controller/GuestAvatarControllerTest.php | 90 +++++++ tests/data/guest_avatar_einstein_32.svg | 5 + tests/data/test.pdf | Bin 0 -> 7083 bytes tests/lib/Avatar/AvatarManagerTest.php | 129 ++++++++++ tests/lib/Avatar/GuestAvatarTest.php | 80 +++++++ tests/lib/Avatar/UserAvatarTest.php | 265 +++++++++++++++++++++ tests/lib/AvatarManagerTest.php | 131 ---------- tests/lib/AvatarTest.php | 265 --------------------- tests/lib/Files/SimpleFS/InMemoryFileTest.php | 145 +++++++++++ tests/lib/Repair/ClearGeneratedAvatarCacheTest.php | 2 +- tests/lib/ServerTest.php | 2 +- 12 files changed, 718 insertions(+), 400 deletions(-) create mode 100644 tests/Core/Controller/GuestAvatarControllerTest.php create mode 100644 tests/data/guest_avatar_einstein_32.svg create mode 100644 tests/data/test.pdf create mode 100644 tests/lib/Avatar/AvatarManagerTest.php create mode 100644 tests/lib/Avatar/GuestAvatarTest.php create mode 100644 tests/lib/Avatar/UserAvatarTest.php delete mode 100644 tests/lib/AvatarManagerTest.php delete mode 100644 tests/lib/AvatarTest.php create mode 100644 tests/lib/Files/SimpleFS/InMemoryFileTest.php (limited to 'tests') diff --git a/tests/Core/Controller/AvatarControllerTest.php b/tests/Core/Controller/AvatarControllerTest.php index 3369fa882c8..5fce8fc6359 100644 --- a/tests/Core/Controller/AvatarControllerTest.php +++ b/tests/Core/Controller/AvatarControllerTest.php @@ -53,7 +53,7 @@ use OCP\IUserManager; * @package OC\Core\Controller */ class AvatarControllerTest extends \Test\TestCase { - /** @var \OC\Core\Controller\AvatarController */ + /** @var AvatarController */ private $avatarController; /** @var IAvatar|\PHPUnit_Framework_MockObject_MockObject */ private $avatarMock; @@ -78,7 +78,7 @@ class AvatarControllerTest extends \Test\TestCase { private $request; /** @var TimeFactory|\PHPUnit_Framework_MockObject_MockObject */ private $timeFactory; - + protected function setUp() { parent::setUp(); diff --git a/tests/Core/Controller/GuestAvatarControllerTest.php b/tests/Core/Controller/GuestAvatarControllerTest.php new file mode 100644 index 00000000000..a7c67c684cc --- /dev/null +++ b/tests/Core/Controller/GuestAvatarControllerTest.php @@ -0,0 +1,90 @@ +logger = $this->getMockBuilder(ILogger::class)->getMock(); + $this->request = $this->getMockBuilder(IRequest::class)->getMock(); + $this->avatar = $this->getMockBuilder(IAvatar::class)->getMock(); + $this->avatarManager = $this->getMockBuilder(IAvatarManager::class)->getMock(); + $this->file = $this->getMockBuilder(ISimpleFile::class)->getMock(); + $this->guestAvatarController = new GuestAvatarController( + 'core', + $this->request, + $this->avatarManager, + $this->logger + ); + } + + /** + * Tests getAvatar returns the guest avatar. + */ + public function testGetAvatar() { + $this->avatarManager->expects($this->once()) + ->method('getGuestAvatar') + ->with('Peter') + ->willReturn($this->avatar); + + $this->avatar->expects($this->once()) + ->method('getFile') + ->with(128) + ->willReturn($this->file); + + $this->file->method('getMimeType') + ->willReturn('image/svg+xml'); + + $response = $this->guestAvatarController->getAvatar('Peter', 128); + + $this->assertGreaterThanOrEqual(201, $response->getStatus()); + $this->assertInstanceOf(FileDisplayResponse::class, $response); + $this->assertSame($this->file, $response->getFile()); + } +} diff --git a/tests/data/guest_avatar_einstein_32.svg b/tests/data/guest_avatar_einstein_32.svg new file mode 100644 index 00000000000..d007f962f24 --- /dev/null +++ b/tests/data/guest_avatar_einstein_32.svg @@ -0,0 +1,5 @@ + + + + E + diff --git a/tests/data/test.pdf b/tests/data/test.pdf new file mode 100644 index 00000000000..241e1e85d41 Binary files /dev/null and b/tests/data/test.pdf differ diff --git a/tests/lib/Avatar/AvatarManagerTest.php b/tests/lib/Avatar/AvatarManagerTest.php new file mode 100644 index 00000000000..7466f664eb5 --- /dev/null +++ b/tests/lib/Avatar/AvatarManagerTest.php @@ -0,0 +1,129 @@ + + * @author Lukas Reschke + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @copyright Copyright (c) 2016, Lukas Reschke + * + * @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 + * + */ + +namespace Test\Avatar; + +use OC\Avatar\UserAvatar; +use OC\Avatar\AvatarManager; +use OC\User\Manager; +use OCP\Files\IAppData; +use OCP\Files\SimpleFS\ISimpleFolder; +use OCP\IConfig; +use OCP\IL10N; +use OCP\ILogger; +use OCP\IUser; + +/** + * Class AvatarManagerTest + */ +class AvatarManagerTest extends \Test\TestCase { + /** @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 ILogger|\PHPUnit_Framework_MockObject_MockObject */ + private $logger; + /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ + private $config; + /** @var AvatarManager | \PHPUnit_Framework_MockObject_MockObject */ + private $avatarManager; + + public function setUp() { + parent::setUp(); + + $this->userManager = $this->createMock(Manager::class); + $this->appData = $this->createMock(IAppData::class); + $this->l10n = $this->createMock(IL10N::class); + $this->logger = $this->createMock(ILogger::class); + $this->config = $this->createMock(IConfig::class); + + $this->avatarManager = new AvatarManager( + $this->userManager, + $this->appData, + $this->l10n, + $this->logger, + $this->config + ); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage user does not exist + */ + public function testGetAvatarInvalidUser() { + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('invalidUser') + ->willReturn(null); + + $this->avatarManager->getAvatar('invalidUser'); + } + + public function testGetAvatarValidUser() { + $user = $this->createMock(IUser::class); + $user + ->expects($this->once()) + ->method('getUID') + ->willReturn('valid-user'); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('valid-user') + ->willReturn($user); + $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() { + $user = $this->createMock(IUser::class); + $this->userManager->expects($this->once()) + ->method('get') + ->with('vaLid-USER') + ->willReturn($user); + + $user->expects($this->once()) + ->method('getUID') + ->willReturn('valid-user'); + + $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')); + } +} diff --git a/tests/lib/Avatar/GuestAvatarTest.php b/tests/lib/Avatar/GuestAvatarTest.php new file mode 100644 index 00000000000..8762d063f5d --- /dev/null +++ b/tests/lib/Avatar/GuestAvatarTest.php @@ -0,0 +1,80 @@ + + * + * @author Michael Weimann + * + * @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 + */ + +namespace Test\Avatar; + +use OC\Avatar\GuestAvatar; +use OCP\Files\SimpleFS\InMemoryFile; +use OCP\ILogger; +use PHPUnit\Framework\MockObject\MockObject; +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|ILogger $logger */ + $logger = $this->getMockBuilder(ILogger::class)->getMock(); + $this->guestAvatar = new GuestAvatar('einstein', $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. + * + * @return void + */ + public function testGet() { + $avatar = $this->guestAvatar->getFile(32); + self::assertInstanceOf(InMemoryFile::class, $avatar); + $expectedFile = file_get_contents( + __DIR__ . '/../../data/guest_avatar_einstein_32.svg' + ); + self::assertEquals(trim($expectedFile), trim($avatar->getContent())); + } + + /** + * Asserts that "testIsCustomAvatar" returns false for guests. + * + * @return void + */ + public function testIsCustomAvatar() { + 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..049725c78c9 --- /dev/null +++ b/tests/lib/Avatar/UserAvatarTest.php @@ -0,0 +1,265 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\Avatar; + +use OC\Files\SimpleFS\SimpleFolder; +use OC\User\User; +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\ILogger; + +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; + + public function setUp() { + parent::setUp(); + + $this->folder = $this->createMock(SimpleFolder::class); + /** @var \OCP\IL10N | \PHPUnit_Framework_MockObject_MockObject $l */ + $l = $this->createMock(IL10N::class); + $l->method('t')->will($this->returnArgument(0)); + $this->user = $this->createMock(User::class); + $this->config = $this->createMock(IConfig::class); + + $this->avatar = new \OC\Avatar\UserAvatar( + $this->folder, + $l, + $this->user, + $this->createMock(ILogger::class), + $this->config + ); + + // abcdefghi is a convenient name that our algorithm convert to our nextcloud blue 0082c9 + $this->user->method('getDisplayName')->willReturn('abcdefghi'); + } + + public function testGetNoAvatar() { + $file = $this->createMock(ISimpleFile::class); + $this->folder->method('newFile') + ->willReturn($file); + + $this->folder->method('getFile') + ->will($this->returnCallback(function($path) { + if ($path === 'avatar.64.png') { + throw new NotFoundException(); + } + })); + $this->folder->method('fileExists') + ->will($this->returnCallback(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') + ->willReturn($data); + + $this->assertEquals($data, $this->avatar->get()->data()); + } + + public function testGetAvatarSizeMatch() { + $this->folder->method('fileExists') + ->will($this->returnValueMap([ + ['avatar.jpg', true], + ['avatar.128.jpg', true], + ])); + + $expected = new \OC_Image(); + $expected->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png'); + + $file = $this->createMock(File::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() { + $this->folder->method('fileExists') + ->will($this->returnValueMap([ + ['avatar.jpg', true], + ])); + + $expected = new \OC_Image(); + $expected->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png'); + + $file = $this->createMock(File::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() { + $this->folder->method('fileExists') + ->will($this->returnValueMap([ + ['avatar.png', true], + ['avatar.32.png', false], + ])); + + $expected = new \OC_Image(); + $expected->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png'); + $expected2 = new \OC_Image(); + $expected2->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png'); + $expected2->resize(32); + + $file = $this->createMock(File::class); + $file->method('getContent')->willReturn($expected->data()); + + $this->folder->method('getFile') + ->will($this->returnCallback( + function($path) use ($file) { + if ($path === 'avatar.png') { + return $file; + } else { + throw new \OCP\Files\NotFoundException; + } + } + )); + + $newFile = $this->createMock(File::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() { + $this->assertFalse($this->avatar->exists()); + } + + public function testExiststJPG() { + $this->folder->method('fileExists') + ->will($this->returnValueMap([ + ['avatar.jpg', true], + ['avatar.png', false], + ])); + $this->assertTrue($this->avatar->exists()); + } + + public function testExistsPNG() { + $this->folder->method('fileExists') + ->will($this->returnValueMap([ + ['avatar.jpg', false], + ['avatar.png', true], + ])); + $this->assertTrue($this->avatar->exists()); + } + + public function testSetAvatar() { + $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(File::class); + $this->folder->method('getFile') + ->with('generated') + ->willReturn($generated); + + $newFile = $this->createMock(File::class); + $this->folder->expects($this->once()) + ->method('newFile') + ->with('avatar.png') + ->willReturn($newFile); + + $image = new \OC_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'); + + // One on remove and once on setting the new avatar + $this->user->expects($this->exactly(2))->method('triggerChange'); + + $this->avatar->set($image->data()); + } + + public function testGenerateSvgAvatar() { + $avatar = $this->invokePrivate($this->avatar, 'getAvatarVector', [64]); + + $svg = ' + + + A + '; + $this->assertEquals($avatar, $svg); + } + + public function testHashToInt() { + $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); + $steps = 6; + $palette = $this->invokePrivate($this->avatar, '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; + // ensure everything is equal + $this->assertEquals($color, new \OC\Color($incR, $incG,$incB)); + } + $hashToInt = $this->invokePrivate($this->avatar, 'hashToInt', ['abcdef', 18]); + $this->assertTrue(gettype($hashToInt) === 'integer'); + } + +} diff --git a/tests/lib/AvatarManagerTest.php b/tests/lib/AvatarManagerTest.php deleted file mode 100644 index 9f2a4f4f337..00000000000 --- a/tests/lib/AvatarManagerTest.php +++ /dev/null @@ -1,131 +0,0 @@ - - * @author Lukas Reschke - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @copyright Copyright (c) 2016, Lukas Reschke - * - * @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 - * - */ - -namespace Test; - -use OC\Avatar; -use OC\AvatarManager; -use OC\Files\AppData\AppData; -use OC\User\Manager; -use OCP\Files\IAppData; -use OCP\Files\SimpleFS\ISimpleFolder; -use OCP\IConfig; -use OCP\IL10N; -use OCP\ILogger; -use OCP\IUser; -use OCP\IUserManager; - -/** - * Class AvatarManagerTest - */ -class AvatarManagerTest extends \Test\TestCase { - /** @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 ILogger|\PHPUnit_Framework_MockObject_MockObject */ - private $logger; - /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ - private $config; - /** @var AvatarManager | \PHPUnit_Framework_MockObject_MockObject */ - private $avatarManager; - - public function setUp() { - parent::setUp(); - - $this->userManager = $this->createMock(Manager::class); - $this->appData = $this->createMock(IAppData::class); - $this->l10n = $this->createMock(IL10N::class); - $this->logger = $this->createMock(ILogger::class); - $this->config = $this->createMock(IConfig::class); - - $this->avatarManager = new AvatarManager( - $this->userManager, - $this->appData, - $this->l10n, - $this->logger, - $this->config - ); - } - - /** - * @expectedException \Exception - * @expectedExceptionMessage user does not exist - */ - public function testGetAvatarInvalidUser() { - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('invalidUser') - ->willReturn(null); - - $this->avatarManager->getAvatar('invalidUser'); - } - - public function testGetAvatarValidUser() { - $user = $this->createMock(IUser::class); - $user - ->expects($this->once()) - ->method('getUID') - ->willReturn('valid-user'); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('valid-user') - ->willReturn($user); - $folder = $this->createMock(ISimpleFolder::class); - $this->appData - ->expects($this->once()) - ->method('getFolder') - ->with('valid-user') - ->willReturn($folder); - - $expected = new Avatar($folder, $this->l10n, $user, $this->logger, $this->config); - $this->assertEquals($expected, $this->avatarManager->getAvatar('valid-user')); - } - - public function testGetAvatarValidUserDifferentCasing() { - $user = $this->createMock(IUser::class); - $this->userManager->expects($this->once()) - ->method('get') - ->with('vaLid-USER') - ->willReturn($user); - - $user->expects($this->once()) - ->method('getUID') - ->willReturn('valid-user'); - - $folder = $this->createMock(ISimpleFolder::class); - $this->appData - ->expects($this->once()) - ->method('getFolder') - ->with('valid-user') - ->willReturn($folder); - - $expected = new Avatar($folder, $this->l10n, $user, $this->logger, $this->config); - $this->assertEquals($expected, $this->avatarManager->getAvatar('vaLid-USER')); - } -} diff --git a/tests/lib/AvatarTest.php b/tests/lib/AvatarTest.php deleted file mode 100644 index c8c9d3b8317..00000000000 --- a/tests/lib/AvatarTest.php +++ /dev/null @@ -1,265 +0,0 @@ - - * This file is licensed under the Affero General Public License version 3 or - * later. - * See the COPYING-README file. - */ - -namespace Test; - -use OC\Files\SimpleFS\SimpleFolder; -use OC\User\User; -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\ILogger; - -class AvatarTest extends \Test\TestCase { - /** @var Folder | \PHPUnit_Framework_MockObject_MockObject */ - private $folder; - - /** @var \OC\Avatar */ - private $avatar; - - /** @var \OC\User\User | \PHPUnit_Framework_MockObject_MockObject $user */ - private $user; - - /** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */ - private $config; - - public function setUp() { - parent::setUp(); - - $this->folder = $this->createMock(SimpleFolder::class); - /** @var \OCP\IL10N | \PHPUnit_Framework_MockObject_MockObject $l */ - $l = $this->createMock(IL10N::class); - $l->method('t')->will($this->returnArgument(0)); - $this->user = $this->createMock(User::class); - $this->config = $this->createMock(IConfig::class); - - $this->avatar = new \OC\Avatar( - $this->folder, - $l, - $this->user, - $this->createMock(ILogger::class), - $this->config - ); - - // abcdefghi is a convenient name that our algorithm convert to our nextcloud blue 0082c9 - $this->user->method('getDisplayName')->willReturn('abcdefghi'); - } - - public function testGetNoAvatar() { - $file = $this->createMock(ISimpleFile::class); - $this->folder->method('newFile') - ->willReturn($file); - - $this->folder->method('getFile') - ->will($this->returnCallback(function($path) { - if ($path === 'avatar.64.png') { - throw new NotFoundException(); - } - })); - $this->folder->method('fileExists') - ->will($this->returnCallback(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') - ->willReturn($data); - - $this->assertEquals($data, $this->avatar->get()->data()); - } - - public function testGetAvatarSizeMatch() { - $this->folder->method('fileExists') - ->will($this->returnValueMap([ - ['avatar.jpg', true], - ['avatar.128.jpg', true], - ])); - - $expected = new \OC_Image(); - $expected->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png'); - - $file = $this->createMock(File::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() { - $this->folder->method('fileExists') - ->will($this->returnValueMap([ - ['avatar.jpg', true], - ])); - - $expected = new \OC_Image(); - $expected->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png'); - - $file = $this->createMock(File::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() { - $this->folder->method('fileExists') - ->will($this->returnValueMap([ - ['avatar.png', true], - ['avatar.32.png', false], - ])); - - $expected = new \OC_Image(); - $expected->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png'); - $expected2 = new \OC_Image(); - $expected2->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png'); - $expected2->resize(32); - - $file = $this->createMock(File::class); - $file->method('getContent')->willReturn($expected->data()); - - $this->folder->method('getFile') - ->will($this->returnCallback( - function($path) use ($file) { - if ($path === 'avatar.png') { - return $file; - } else { - throw new \OCP\Files\NotFoundException; - } - } - )); - - $newFile = $this->createMock(File::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() { - $this->assertFalse($this->avatar->exists()); - } - - public function testExiststJPG() { - $this->folder->method('fileExists') - ->will($this->returnValueMap([ - ['avatar.jpg', true], - ['avatar.png', false], - ])); - $this->assertTrue($this->avatar->exists()); - } - - public function testExistsPNG() { - $this->folder->method('fileExists') - ->will($this->returnValueMap([ - ['avatar.jpg', false], - ['avatar.png', true], - ])); - $this->assertTrue($this->avatar->exists()); - } - - public function testSetAvatar() { - $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(File::class); - $this->folder->method('getFile') - ->with('generated') - ->willReturn($generated); - - $newFile = $this->createMock(File::class); - $this->folder->expects($this->once()) - ->method('newFile') - ->with('avatar.png') - ->willReturn($newFile); - - $image = new \OC_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'); - - // One on remove and once on setting the new avatar - $this->user->expects($this->exactly(2))->method('triggerChange'); - - $this->avatar->set($image->data()); - } - - public function testGenerateSvgAvatar() { - $avatar = $this->invokePrivate($this->avatar, 'getAvatarVector', [64]); - - $svg = ' - - - A - '; - $this->assertEquals($avatar, $svg); - } - - public function testHashToInt() { - $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); - $steps = 6; - $palette = $this->invokePrivate($this->avatar, '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; - // ensure everything is equal - $this->assertEquals($color, new \OC\Color($incR, $incG,$incB)); - } - $hashToInt = $this->invokePrivate($this->avatar, 'hashToInt', ['abcdef', 18]); - $this->assertTrue(gettype($hashToInt) === 'integer'); - } - -} diff --git a/tests/lib/Files/SimpleFS/InMemoryFileTest.php b/tests/lib/Files/SimpleFS/InMemoryFileTest.php new file mode 100644 index 00000000000..195a5d04a8f --- /dev/null +++ b/tests/lib/Files/SimpleFS/InMemoryFileTest.php @@ -0,0 +1,145 @@ + + * + * @author Michael Weimann + * + * @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 + */ + +namespace Test\File\SimpleFS; + +use OCP\Files\NotPermittedException; +use OCP\Files\SimpleFS\InMemoryFile; +use Test\TestCase; + +/** + * This class provide test casesf or the InMemoryFile. + * + * @package Test\File\SimpleFS + */ +class InMemoryFileTest extends TestCase { + /** + * Holds a pdf file with know attributes for tests. + * + * @var InMemoryFile + */ + private $testPdf; + + /** + * Sets the test file from "./resources/test.pdf". + * + * @before + * @return void + */ + public function setupTestPdf() { + $fileContents = file_get_contents( + __DIR__ . '/../../../data/test.pdf' + ); + $this->testPdf = new InMemoryFile('test.pdf', $fileContents); + } + + /** + * Asserts that putContent replaces the file contents. + * + * @return void + */ + public function testPutContent() { + $this->testPdf->putContent('test'); + self::assertEquals('test', $this->testPdf->getContent()); + } + + /** + * Asserts that delete() doesn't rise an exception. + * + * @return void + */ + public function testDelete() { + $this->testPdf->delete(); + // assert true, otherwise phpunit complains about not doing any assert + self::assertTrue(true); + } + + /** + * Asserts that getName returns the name passed on file creation. + * + * @return void + */ + public function testGetName() { + self::assertEquals('test.pdf', $this->testPdf->getName()); + } + + /** + * Asserts that the file size is the size of the test file. + * + * @return void + */ + public function testGetSize() { + self::assertEquals(7083, $this->testPdf->getSize()); + } + + /** + * Asserts the file contents are the same than the original file contents. + * + * @return void + */ + public function testGetContent() { + self::assertEquals( + file_get_contents(__DIR__ . '/../../../data/test.pdf'), + $this->testPdf->getContent() + ); + } + + /** + * Asserts the test file modification time is an integer. + * + * @return void + */ + public function testGetMTime() { + self::assertTrue(is_int($this->testPdf->getMTime())); + } + + /** + * Asserts the test file mime type is "application/json". + * + * @return void + */ + public function testGetMimeType() { + self::assertEquals('application/pdf', $this->testPdf->getMimeType()); + } + + + /** + * Asserts that read() raises an NotPermittedException. + * + * @return void + */ + public function testRead() { + self::expectException(NotPermittedException::class); + $this->testPdf->read(); + } + + /** + * Asserts that write() raises an NotPermittedException. + * + * @return void + */ + public function testWrite() { + self::expectException(NotPermittedException::class); + $this->testPdf->write(); + } +} diff --git a/tests/lib/Repair/ClearGeneratedAvatarCacheTest.php b/tests/lib/Repair/ClearGeneratedAvatarCacheTest.php index ec107d300d6..dd98307993f 100644 --- a/tests/lib/Repair/ClearGeneratedAvatarCacheTest.php +++ b/tests/lib/Repair/ClearGeneratedAvatarCacheTest.php @@ -25,7 +25,7 @@ namespace Test\Repair; use OCP\IConfig; use OCP\Migration\IOutput; -use OC\AvatarManager; +use OC\Avatar\AvatarManager; use OC\Repair\ClearGeneratedAvatarCache; class ClearGeneratedAvatarCacheTest extends \Test\TestCase { diff --git a/tests/lib/ServerTest.php b/tests/lib/ServerTest.php index e76b2b96db7..604e11ec11e 100644 --- a/tests/lib/ServerTest.php +++ b/tests/lib/ServerTest.php @@ -57,7 +57,7 @@ class ServerTest extends \Test\TestCase { ['AppManager', '\OCP\App\IAppManager'], ['AsyncCommandBus', '\OC\Command\AsyncBus'], ['AsyncCommandBus', '\OCP\Command\IBus'], - ['AvatarManager', '\OC\AvatarManager'], + ['AvatarManager', '\OC\Avatar\AvatarManager'], ['AvatarManager', '\OCP\IAvatarManager'], ['CategoryFetcher', CategoryFetcher::class], -- cgit v1.2.3