From bf0371429c96ac3fad637af98bceea55374cca0e Mon Sep 17 00:00:00 2001 From: Lukas Reschke Date: Mon, 3 Oct 2016 13:57:29 +0200 Subject: [PATCH] Remove database requirement from test Signed-off-by: Lukas Reschke --- lib/private/AvatarManager.php | 10 ++++- tests/lib/AvatarManagerTest.php | 74 +++++++++++++++++++++++++-------- 2 files changed, 65 insertions(+), 19 deletions(-) diff --git a/lib/private/AvatarManager.php b/lib/private/AvatarManager.php index 6f1f380a903..df3247b8f00 100644 --- a/lib/private/AvatarManager.php +++ b/lib/private/AvatarManager.php @@ -91,14 +91,20 @@ class AvatarManager implements IAvatarManager { if (is_null($user)) { throw new \Exception('user does not exist'); } + // sanitize userID - fixes casing issue (needed for the filesystem stuff that is done below) $userId = $user->getUID(); /* * Fix for #22119 - * Basically we do not want to copy the skeleton folder + * Basically we do not want to copy the skeleton folder. + * + * For unit test purposes this is ignored when run in PHPUnit. */ - \OC\Files\Filesystem::initMountPoints($userId); + if(!defined('PHPUNIT_RUN')) { + \OC\Files\Filesystem::initMountPoints($userId); + } + $dir = '/' . $userId; /** @var Folder $folder */ $folder = $this->rootFolder->get($dir); diff --git a/tests/lib/AvatarManagerTest.php b/tests/lib/AvatarManagerTest.php index 2dd6ff34923..0ad998af6d5 100644 --- a/tests/lib/AvatarManagerTest.php +++ b/tests/lib/AvatarManagerTest.php @@ -1,8 +1,11 @@ + * @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 @@ -21,33 +24,49 @@ namespace Test; +use OC\Avatar; use OC\AvatarManager; -use Test\Traits\UserTrait; -use Test\Traits\MountProviderTrait; +use OCP\Files\Folder; +use OCP\Files\IRootFolder; +use OCP\IConfig; +use OCP\IL10N; +use OCP\ILogger; +use OCP\IUser; +use OCP\IUserManager; /** * Class AvatarManagerTest - * @group DB */ class AvatarManagerTest extends \Test\TestCase { - use UserTrait; - use MountProviderTrait; - + /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */ + private $userManager; + /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */ + private $rootFolder; + /** @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 */ private $avatarManager; - /** @var \OC\Files\Storage\Temporary */ - private $storage; - public function setUp() { parent::setUp(); - $this->createUser('valid-user', 'valid-user'); + $this->userManager = $this->createMock(IUserManager::class); + $this->rootFolder = $this->createMock(IRootFolder::class); + $this->l10n = $this->createMock(IL10N::class); + $this->logger = $this->createMock(ILogger::class); + $this->config = $this->createMock(IConfig::class); - $this->storage = new \OC\Files\Storage\Temporary(); - $this->registerMount('valid-user', $this->storage, '/valid-user/'); - - $this->avatarManager = \OC::$server->getAvatarManager(); + $this->avatarManager = new AvatarManager( + $this->userManager, + $this->rootFolder, + $this->l10n, + $this->logger, + $this->config + ); } /** @@ -55,14 +74,35 @@ class AvatarManagerTest extends \Test\TestCase { * @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() { - $avatar = $this->avatarManager->getAvatar('valid-user'); + $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(Folder::class); + $this->rootFolder + ->expects($this->once()) + ->method('get') + ->with('/valid-user') + ->willReturn($folder); - $this->assertInstanceOf('\OCP\IAvatar', $avatar); - $this->assertFalse($this->storage->file_exists('files')); + $expected = new Avatar($folder, $this->l10n, $user, $this->logger, $this->config);; + $this->assertEquals($expected, $this->avatarManager->getAvatar('valid-user')); } } -- 2.39.5