2015-12-02 00:08:42 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Roeland Jago Douma <rullzer@owncloud.com>
|
2016-10-03 14:57:29 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-12-02 00:08:42 +03:00
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
2016-10-03 14:57:29 +03:00
|
|
|
* @copyright Copyright (c) 2016, Lukas Reschke <lukas@statuscode.ch>
|
|
|
|
*
|
2015-12-02 00:08:42 +03:00
|
|
|
* @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/>
|
|
|
|
*
|
|
|
|
*/
|
2016-05-19 10:27:21 +03:00
|
|
|
|
2019-01-20 13:13:41 +03:00
|
|
|
namespace Test\Avatar;
|
2016-05-19 10:27:21 +03:00
|
|
|
|
2019-01-20 13:13:41 +03:00
|
|
|
use OC\Avatar\AvatarManager;
|
2021-03-22 14:08:53 +03:00
|
|
|
use OC\Avatar\GuestAvatar;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OC\Avatar\UserAvatar;
|
2018-01-13 22:36:02 +03:00
|
|
|
use OC\User\Manager;
|
2021-03-22 14:08:53 +03:00
|
|
|
use OCP\Accounts\IAccount;
|
|
|
|
use OCP\Accounts\IAccountManager;
|
|
|
|
use OCP\Accounts\IAccountProperty;
|
2016-10-04 14:11:33 +03:00
|
|
|
use OCP\Files\IAppData;
|
|
|
|
use OCP\Files\SimpleFS\ISimpleFolder;
|
2016-10-03 14:57:29 +03:00
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\IUser;
|
2021-03-22 14:08:53 +03:00
|
|
|
use OCP\IUserSession;
|
2015-12-02 00:08:42 +03:00
|
|
|
|
2016-02-15 22:49:07 +03:00
|
|
|
/**
|
|
|
|
* Class AvatarManagerTest
|
|
|
|
*/
|
2015-12-02 00:08:42 +03:00
|
|
|
class AvatarManagerTest extends \Test\TestCase {
|
2021-03-22 14:08:53 +03:00
|
|
|
/** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
|
|
|
|
private $userSession;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var Manager|\PHPUnit\Framework\MockObject\MockObject */
|
2016-10-03 14:57:29 +03:00
|
|
|
private $userManager;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */
|
2016-10-04 14:11:33 +03:00
|
|
|
private $appData;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
|
2016-10-03 14:57:29 +03:00
|
|
|
private $l10n;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ILogger|\PHPUnit\Framework\MockObject\MockObject */
|
2016-10-03 14:57:29 +03:00
|
|
|
private $logger;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
|
2016-10-03 14:57:29 +03:00
|
|
|
private $config;
|
2021-03-22 14:08:53 +03:00
|
|
|
/** @var IAccountManager|\PHPUnit\Framework\MockObject\MockObject */
|
|
|
|
private $accountManager;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var AvatarManager | \PHPUnit\Framework\MockObject\MockObject */
|
2015-12-02 00:08:42 +03:00
|
|
|
private $avatarManager;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2015-12-02 00:08:42 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2021-03-22 14:08:53 +03:00
|
|
|
$this->userSession = $this->createMock(IUserSession::class);
|
2018-01-13 22:36:02 +03:00
|
|
|
$this->userManager = $this->createMock(Manager::class);
|
2016-10-04 14:11:33 +03:00
|
|
|
$this->appData = $this->createMock(IAppData::class);
|
2016-10-03 14:57:29 +03:00
|
|
|
$this->l10n = $this->createMock(IL10N::class);
|
|
|
|
$this->logger = $this->createMock(ILogger::class);
|
|
|
|
$this->config = $this->createMock(IConfig::class);
|
2021-03-22 14:08:53 +03:00
|
|
|
$this->accountManager = $this->createMock(IAccountManager::class);
|
2016-02-15 22:49:07 +03:00
|
|
|
|
2016-10-03 14:57:29 +03:00
|
|
|
$this->avatarManager = new AvatarManager(
|
2021-03-22 14:08:53 +03:00
|
|
|
$this->userSession,
|
2016-10-03 14:57:29 +03:00
|
|
|
$this->userManager,
|
2016-10-04 14:11:33 +03:00
|
|
|
$this->appData,
|
2016-10-03 14:57:29 +03:00
|
|
|
$this->l10n,
|
|
|
|
$this->logger,
|
2021-03-22 14:08:53 +03:00
|
|
|
$this->config,
|
|
|
|
$this->accountManager
|
2016-10-03 14:57:29 +03:00
|
|
|
);
|
2015-12-02 00:08:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetAvatarInvalidUser() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Exception::class);
|
|
|
|
$this->expectExceptionMessage('user does not exist');
|
|
|
|
|
2016-10-03 14:57:29 +03:00
|
|
|
$this->userManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('invalidUser')
|
|
|
|
->willReturn(null);
|
|
|
|
|
2015-12-02 00:08:42 +03:00
|
|
|
$this->avatarManager->getAvatar('invalidUser');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetAvatarValidUser() {
|
2021-03-22 14:08:53 +03:00
|
|
|
// requesting user
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->willReturn($this->createMock(IUser::class));
|
|
|
|
|
|
|
|
// we skip account scope check for logged in user
|
|
|
|
$this->accountManager->expects($this->never())
|
|
|
|
->method('getAccount');
|
|
|
|
|
2016-10-03 14:57:29 +03:00
|
|
|
$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);
|
2016-10-04 14:11:33 +03:00
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
|
|
|
$this->appData
|
2016-10-03 14:57:29 +03:00
|
|
|
->expects($this->once())
|
2016-10-04 14:11:33 +03:00
|
|
|
->method('getFolder')
|
|
|
|
->with('valid-user')
|
2016-10-03 14:57:29 +03:00
|
|
|
->willReturn($folder);
|
2015-12-02 00:08:42 +03:00
|
|
|
|
2019-01-20 13:13:41 +03:00
|
|
|
$expected = new UserAvatar($folder, $this->l10n, $user, $this->logger, $this->config);
|
2016-10-03 14:57:29 +03:00
|
|
|
$this->assertEquals($expected, $this->avatarManager->getAvatar('valid-user'));
|
2015-12-02 00:08:42 +03:00
|
|
|
}
|
|
|
|
|
2016-10-04 13:28:41 +03:00
|
|
|
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);
|
|
|
|
|
2019-01-20 13:13:41 +03:00
|
|
|
$expected = new UserAvatar($folder, $this->l10n, $user, $this->logger, $this->config);
|
2016-10-04 13:28:41 +03:00
|
|
|
$this->assertEquals($expected, $this->avatarManager->getAvatar('vaLid-USER'));
|
|
|
|
}
|
2021-03-22 14:08:53 +03:00
|
|
|
|
|
|
|
public function testGetAvatarPrivateScope() {
|
|
|
|
$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->never())
|
|
|
|
->method('getFolder');
|
|
|
|
|
|
|
|
$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);
|
|
|
|
|
|
|
|
$expected = new GuestAvatar('valid-user', $this->createMock(ILogger::class));
|
|
|
|
$this->assertEquals($expected, $this->avatarManager->getAvatar('valid-user'));
|
|
|
|
}
|
2015-12-02 00:08:42 +03:00
|
|
|
}
|