Merge pull request #1058 from nextcloud/avatarController_cleanup

Avatar controller cleanup
This commit is contained in:
Lukas Reschke 2016-08-27 22:25:34 +02:00 committed by GitHub
commit 8bf9be2167
3 changed files with 106 additions and 116 deletions

View file

@ -31,13 +31,11 @@ namespace OC\Core;
use OC\AppFramework\Utility\SimpleContainer; use OC\AppFramework\Utility\SimpleContainer;
use OC\AppFramework\Utility\TimeFactory; use OC\AppFramework\Utility\TimeFactory;
use OC\Core\Controller\AvatarController;
use OC\Core\Controller\LoginController; use OC\Core\Controller\LoginController;
use OC\Core\Controller\LostController; use OC\Core\Controller\LostController;
use OC\Core\Controller\TokenController; use OC\Core\Controller\TokenController;
use OC\Core\Controller\TwoFactorChallengeController; use OC\Core\Controller\TwoFactorChallengeController;
use OC\Core\Controller\UserController; use OC\Core\Controller\UserController;
use OCP\Defaults;
use OCP\AppFramework\App; use OCP\AppFramework\App;
use OCP\Util; use OCP\Util;
@ -83,19 +81,6 @@ class Application extends App {
$c->query('Defaults') $c->query('Defaults')
); );
}); });
$container->registerService('AvatarController', function(SimpleContainer $c) {
return new AvatarController(
$c->query('AppName'),
$c->query('Request'),
$c->query('AvatarManager'),
$c->query('Cache'),
$c->query('L10N'),
$c->query('UserManager'),
$c->query('UserSession'),
$c->query('UserFolder'),
$c->query('Logger')
);
});
$container->registerService('LoginController', function(SimpleContainer $c) { $container->registerService('LoginController', function(SimpleContainer $c) {
return new LoginController( return new LoginController(
$c->query('AppName'), $c->query('AppName'),
@ -150,33 +135,18 @@ class Application extends App {
$container->registerService('SecureRandom', function(SimpleContainer $c) { $container->registerService('SecureRandom', function(SimpleContainer $c) {
return $c->query('ServerContainer')->getSecureRandom(); return $c->query('ServerContainer')->getSecureRandom();
}); });
$container->registerService('AvatarManager', function(SimpleContainer $c) {
return $c->query('ServerContainer')->getAvatarManager();
});
$container->registerService('Session', function(SimpleContainer $c) { $container->registerService('Session', function(SimpleContainer $c) {
return $c->query('ServerContainer')->getSession(); return $c->query('ServerContainer')->getSession();
}); });
$container->registerService('UserSession', function(SimpleContainer $c) { $container->registerService('UserSession', function(SimpleContainer $c) {
return $c->query('ServerContainer')->getUserSession(); return $c->query('ServerContainer')->getUserSession();
}); });
$container->registerService('Session', function(SimpleContainer $c) {
return $c->query('ServerContainer')->getSession();
});
$container->registerService('Cache', function(SimpleContainer $c) {
return $c->query('ServerContainer')->getCache();
});
$container->registerService('UserFolder', function(SimpleContainer $c) {
return $c->query('ServerContainer')->getUserFolder();
});
$container->registerService('Defaults', function(SimpleContainer $c) { $container->registerService('Defaults', function(SimpleContainer $c) {
return $c->query('ServerContainer')->getThemingDefaults(); return $c->query('ServerContainer')->getThemingDefaults();
}); });
$container->registerService('Mailer', function(SimpleContainer $c) { $container->registerService('Mailer', function(SimpleContainer $c) {
return $c->query('ServerContainer')->getMailer(); return $c->query('ServerContainer')->getMailer();
}); });
$container->registerService('Logger', function(SimpleContainer $c) {
return $c->query('ServerContainer')->getLogger();
});
$container->registerService('TimeFactory', function(SimpleContainer $c) { $container->registerService('TimeFactory', function(SimpleContainer $c) {
return new TimeFactory(); return new TimeFactory();
}); });
@ -186,9 +156,6 @@ class Application extends App {
$container->registerService('TwoFactorAuthManager', function(SimpleContainer $c) { $container->registerService('TwoFactorAuthManager', function(SimpleContainer $c) {
return $c->query('ServerContainer')->getTwoFactorAuthManager(); return $c->query('ServerContainer')->getTwoFactorAuthManager();
}); });
$container->registerService('OC\CapabilitiesManager', function(SimpleContainer $c) {
return $c->query('ServerContainer')->getCapabilitiesManager();
});
} }
} }

View file

@ -31,14 +31,16 @@ use OCP\AppFramework\Controller;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\DataDisplayResponse; use OCP\AppFramework\Http\DataDisplayResponse;
use OCP\Files\File;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\IAvatarManager; use OCP\IAvatarManager;
use OCP\ICache;
use OCP\ILogger; use OCP\ILogger;
use OCP\IL10N; use OCP\IL10N;
use OCP\IRequest; use OCP\IRequest;
use OCP\IUserManager; use OCP\IUserManager;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\Files\Folder;
/** /**
* Class AvatarController * Class AvatarController
@ -50,7 +52,7 @@ class AvatarController extends Controller {
/** @var IAvatarManager */ /** @var IAvatarManager */
protected $avatarManager; protected $avatarManager;
/** @var \OC\Cache\File */ /** @var ICache */
protected $cache; protected $cache;
/** @var IL10N */ /** @var IL10N */
@ -62,41 +64,44 @@ class AvatarController extends Controller {
/** @var IUserSession */ /** @var IUserSession */
protected $userSession; protected $userSession;
/** @var Folder */ /** @var IRootFolder */
protected $userFolder; protected $rootFolder;
/** @var ILogger */ /** @var ILogger */
protected $logger; protected $logger;
/** @var string */
protected $userId;
/** /**
* @param string $appName * @param string $appName
* @param IRequest $request * @param IRequest $request
* @param IAvatarManager $avatarManager * @param IAvatarManager $avatarManager
* @param \OC\Cache\File $cache * @param ICache $cache
* @param IL10N $l10n * @param IL10N $l10n
* @param IUserManager $userManager * @param IUserManager $userManager
* @param IUserSession $userSession * @param IRootFolder $rootFolder
* @param Folder $userFolder
* @param ILogger $logger * @param ILogger $logger
* @param string $userId
*/ */
public function __construct($appName, public function __construct($appName,
IRequest $request, IRequest $request,
IAvatarManager $avatarManager, IAvatarManager $avatarManager,
\OC\Cache\File $cache, ICache $cache,
IL10N $l10n, IL10N $l10n,
IUserManager $userManager, IUserManager $userManager,
IUserSession $userSession, IRootFolder $rootFolder,
Folder $userFolder = null, ILogger $logger,
ILogger $logger) { $userId) {
parent::__construct($appName, $request); parent::__construct($appName, $request);
$this->avatarManager = $avatarManager; $this->avatarManager = $avatarManager;
$this->cache = $cache; $this->cache = $cache;
$this->l = $l10n; $this->l = $l10n;
$this->userManager = $userManager; $this->userManager = $userManager;
$this->userSession = $userSession; $this->rootFolder = $rootFolder;
$this->userFolder = $userFolder;
$this->logger = $logger; $this->logger = $logger;
$this->userId = $userId;
} }
/** /**
@ -156,8 +161,9 @@ class AvatarController extends Controller {
if (isset($path)) { if (isset($path)) {
$path = stripslashes($path); $path = stripslashes($path);
$node = $this->userFolder->get($path); $userFolder = $this->rootFolder->getUserFolder($this->userId);
if (!($node instanceof \OCP\Files\File)) { $node = $userFolder->get($path);
if (!($node instanceof File)) {
return new DataResponse(['data' => ['message' => $this->l->t('Please select a file.')]], Http::STATUS_OK, $headers); return new DataResponse(['data' => ['message' => $this->l->t('Please select a file.')]], Http::STATUS_OK, $headers);
} }
if ($node->getSize() > 20*1024*1024) { if ($node->getSize() > 20*1024*1024) {
@ -240,10 +246,8 @@ class AvatarController extends Controller {
* @return DataResponse * @return DataResponse
*/ */
public function deleteAvatar() { public function deleteAvatar() {
$userId = $this->userSession->getUser()->getUID();
try { try {
$avatar = $this->avatarManager->getAvatar($userId); $avatar = $this->avatarManager->getAvatar($this->userId);
$avatar->remove(); $avatar->remove();
return new DataResponse(); return new DataResponse();
} catch (\Exception $e) { } catch (\Exception $e) {
@ -285,8 +289,6 @@ class AvatarController extends Controller {
* @return DataResponse * @return DataResponse
*/ */
public function postCroppedAvatar($crop) { public function postCroppedAvatar($crop) {
$userId = $this->userSession->getUser()->getUID();
if (is_null($crop)) { if (is_null($crop)) {
return new DataResponse(['data' => ['message' => $this->l->t("No crop data provided")]], return new DataResponse(['data' => ['message' => $this->l->t("No crop data provided")]],
Http::STATUS_BAD_REQUEST); Http::STATUS_BAD_REQUEST);
@ -308,7 +310,7 @@ class AvatarController extends Controller {
$image = new \OC_Image($tmpAvatar); $image = new \OC_Image($tmpAvatar);
$image->crop($crop['x'], $crop['y'], round($crop['w']), round($crop['h'])); $image->crop($crop['x'], $crop['y'], round($crop['w']), round($crop['h']));
try { try {
$avatar = $this->avatarManager->getAvatar($userId); $avatar = $this->avatarManager->getAvatar($this->userId);
$avatar->set($image); $avatar->set($image);
// Clean up // Clean up
$this->cache->remove('tmpAvatar'); $this->cache->remove('tmpAvatar');

View file

@ -31,67 +31,83 @@ function is_uploaded_file($filename) {
namespace Tests\Core\Controller; namespace Tests\Core\Controller;
use OC\Core\Application; use OC\Core\Controller\AvatarController;
use OCP\AppFramework\IAppContainer;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\Files\Cache\ICache;
use OCP\Files\File; use OCP\Files\File;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\IUser;
use OCP\IAvatar; use OCP\IAvatar;
use Punic\Exception; use OCP\IAvatarManager;
use Test\Traits\UserTrait; use OCP\IL10N;
use OCP\ILogger;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserManager;
/** /**
* Class AvatarControllerTest * Class AvatarControllerTest
* *
* @group DB
*
* @package OC\Core\Controller * @package OC\Core\Controller
*/ */
class AvatarControllerTest extends \Test\TestCase { class AvatarControllerTest extends \Test\TestCase {
use UserTrait;
/** @var IAppContainer */
private $container;
/** @var \OC\Core\Controller\AvatarController */ /** @var \OC\Core\Controller\AvatarController */
private $avatarController; private $avatarController;
/** @var IAvatar */ /** @var IAvatar|\PHPUnit_Framework_MockObject_MockObject */
private $avatarMock; private $avatarMock;
/** @var IUser */ /** @var IUser|\PHPUnit_Framework_MockObject_MockObject */
private $userMock; private $userMock;
/** @var File */ /** @var File|\PHPUnit_Framework_MockObject_MockObject */
private $avatarFile; private $avatarFile;
/** @var IAvatarManager|\PHPUnit_Framework_MockObject_MockObject */
private $avatarManager;
/** @var ICache|\PHPUnit_Framework_MockObject_MockObject */
private $cache;
/** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
private $l;
/** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
private $userManager;
/** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */
private $rootFolder;
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
private $logger;
/** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
private $request;
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$this->createUser('userid', 'pass');
$this->loginAsUser('userid');
$app = new Application; $this->avatarManager = $this->getMockBuilder('OCP\IAvatarManager')->getMock();
$this->container = $app->getContainer(); $this->cache = $this->getMockBuilder('OCP\ICache')
$this->container['AppName'] = 'core';
$this->container['AvatarManager'] = $this->getMockBuilder('OCP\IAvatarManager')->getMock();
$this->container['Cache'] = $this->getMockBuilder('OC\Cache\File')
->disableOriginalConstructor()->getMock(); ->disableOriginalConstructor()->getMock();
$this->container['L10N'] = $this->getMockBuilder('OCP\IL10N')->getMock(); $this->l = $this->getMockBuilder('OCP\IL10N')->getMock();
$this->container['L10N']->method('t')->will($this->returnArgument(0)); $this->l->method('t')->will($this->returnArgument(0));
$this->container['UserManager'] = $this->getMockBuilder('OCP\IUserManager')->getMock(); $this->userManager = $this->getMockBuilder('OCP\IUserManager')->getMock();
$this->container['UserSession'] = $this->getMockBuilder('OCP\IUserSession')->getMock(); $this->request = $this->getMockBuilder('OCP\IRequest')->getMock();
$this->container['Request'] = $this->getMockBuilder('OCP\IRequest')->getMock(); $this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->getMock();
$this->container['UserFolder'] = $this->getMockBuilder('OCP\Files\Folder')->getMock(); $this->logger = $this->getMockBuilder('OCP\ILogger')->getMock();
$this->container['Logger'] = $this->getMockBuilder('OCP\ILogger')->getMock();
$this->avatarMock = $this->getMockBuilder('OCP\IAvatar')->getMock(); $this->avatarMock = $this->getMockBuilder('OCP\IAvatar')->getMock();
$this->userMock = $this->getMockBuilder('OCP\IUser')->getMock(); $this->userMock = $this->getMockBuilder('OCP\IUser')->getMock();
$this->avatarController = $this->container['AvatarController']; $this->avatarController = new AvatarController(
'core',
$this->request,
$this->avatarManager,
$this->cache,
$this->l,
$this->userManager,
$this->rootFolder,
$this->logger,
'userid'
);
// Configure userMock // Configure userMock
$this->userMock->method('getDisplayName')->willReturn('displayName'); $this->userMock->method('getDisplayName')->willReturn('displayName');
$this->userMock->method('getUID')->willReturn('userId'); $this->userMock->method('getUID')->willReturn('userId');
$this->container['UserManager']->method('get') $this->userManager->method('get')
->willReturnMap([['userId', $this->userMock]]); ->willReturnMap([['userId', $this->userMock]]);
$this->container['UserSession']->method('getUser')->willReturn($this->userMock);
$this->avatarFile = $this->getMockBuilder('OCP\Files\File')->getMock(); $this->avatarFile = $this->getMockBuilder('OCP\Files\File')->getMock();
$this->avatarFile->method('getContent')->willReturn('image data'); $this->avatarFile->method('getContent')->willReturn('image data');
@ -100,7 +116,6 @@ class AvatarControllerTest extends \Test\TestCase {
} }
public function tearDown() { public function tearDown() {
$this->logout();
parent::tearDown(); parent::tearDown();
} }
@ -108,7 +123,7 @@ class AvatarControllerTest extends \Test\TestCase {
* Fetch an avatar if a user has no avatar * Fetch an avatar if a user has no avatar
*/ */
public function testGetAvatarNoAvatar() { public function testGetAvatarNoAvatar() {
$this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock); $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
$this->avatarMock->method('getFile')->will($this->throwException(new NotFoundException())); $this->avatarMock->method('getFile')->will($this->throwException(new NotFoundException()));
$response = $this->avatarController->getAvatar('userId', 32); $response = $this->avatarController->getAvatar('userId', 32);
@ -123,7 +138,7 @@ class AvatarControllerTest extends \Test\TestCase {
*/ */
public function testGetAvatar() { public function testGetAvatar() {
$this->avatarMock->method('getFile')->willReturn($this->avatarFile); $this->avatarMock->method('getFile')->willReturn($this->avatarFile);
$this->container['AvatarManager']->method('getAvatar')->with('userId')->willReturn($this->avatarMock); $this->avatarManager->method('getAvatar')->with('userId')->willReturn($this->avatarMock);
$response = $this->avatarController->getAvatar('userId', 32); $response = $this->avatarController->getAvatar('userId', 32);
@ -131,14 +146,14 @@ class AvatarControllerTest extends \Test\TestCase {
$this->assertArrayHasKey('Content-Type', $response->getHeaders()); $this->assertArrayHasKey('Content-Type', $response->getHeaders());
$this->assertEquals('image type', $response->getHeaders()['Content-Type']); $this->assertEquals('image type', $response->getHeaders()['Content-Type']);
$this->assertEquals('my etag', $response->getEtag()); $this->assertEquals('my etag', $response->getETag());
} }
/** /**
* Fetch the avatar of a non-existing user * Fetch the avatar of a non-existing user
*/ */
public function testGetAvatarNoUser() { public function testGetAvatarNoUser() {
$this->container['AvatarManager'] $this->avatarManager
->method('getAvatar') ->method('getAvatar')
->with('userDoesNotExist') ->with('userDoesNotExist')
->will($this->throwException(new \Exception('user does not exist'))); ->will($this->throwException(new \Exception('user does not exist')));
@ -160,7 +175,7 @@ class AvatarControllerTest extends \Test\TestCase {
->with($this->equalTo(32)) ->with($this->equalTo(32))
->willReturn($this->avatarFile); ->willReturn($this->avatarFile);
$this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock); $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
$this->avatarController->getAvatar('userId', 32); $this->avatarController->getAvatar('userId', 32);
} }
@ -174,7 +189,7 @@ class AvatarControllerTest extends \Test\TestCase {
->with($this->equalTo(64)) ->with($this->equalTo(64))
->willReturn($this->avatarFile); ->willReturn($this->avatarFile);
$this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock); $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
$this->avatarController->getAvatar('userId', 0); $this->avatarController->getAvatar('userId', 0);
} }
@ -188,7 +203,7 @@ class AvatarControllerTest extends \Test\TestCase {
->with($this->equalTo(2048)) ->with($this->equalTo(2048))
->willReturn($this->avatarFile); ->willReturn($this->avatarFile);
$this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock); $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
$this->avatarController->getAvatar('userId', 2049); $this->avatarController->getAvatar('userId', 2049);
} }
@ -197,7 +212,7 @@ class AvatarControllerTest extends \Test\TestCase {
* Remove an avatar * Remove an avatar
*/ */
public function testDeleteAvatar() { public function testDeleteAvatar() {
$this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock); $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
$response = $this->avatarController->deleteAvatar(); $response = $this->avatarController->deleteAvatar();
$this->assertEquals(Http::STATUS_OK, $response->getStatus()); $this->assertEquals(Http::STATUS_OK, $response->getStatus());
@ -208,9 +223,9 @@ class AvatarControllerTest extends \Test\TestCase {
*/ */
public function testDeleteAvatarException() { public function testDeleteAvatarException() {
$this->avatarMock->method('remove')->will($this->throwException(new \Exception("foo"))); $this->avatarMock->method('remove')->will($this->throwException(new \Exception("foo")));
$this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock); $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
$this->container['Logger']->expects($this->once()) $this->logger->expects($this->once())
->method('logException') ->method('logException')
->with(new \Exception("foo")); ->with(new \Exception("foo"));
$expectedResponse = new Http\DataResponse(['data' => ['message' => 'An error occurred. Please contact your admin.']], Http::STATUS_BAD_REQUEST); $expectedResponse = new Http\DataResponse(['data' => ['message' => 'An error occurred. Please contact your admin.']], Http::STATUS_BAD_REQUEST);
@ -229,7 +244,7 @@ class AvatarControllerTest extends \Test\TestCase {
* Fetch tmp avatar * Fetch tmp avatar
*/ */
public function testTmpAvatarValid() { public function testTmpAvatarValid() {
$this->container['Cache']->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg')); $this->cache->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
$response = $this->avatarController->getTmpAvatar(); $response = $this->avatarController->getTmpAvatar();
$this->assertEquals(Http::STATUS_OK, $response->getStatus()); $this->assertEquals(Http::STATUS_OK, $response->getStatus());
@ -255,11 +270,11 @@ class AvatarControllerTest extends \Test\TestCase {
$this->assertTrue($copyRes); $this->assertTrue($copyRes);
//Create file in cache //Create file in cache
$this->container['Cache']->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg')); $this->cache->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
//Create request return //Create request return
$reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [filesize(\OC::$SERVERROOT.'/tests/data/testimage.jpg')]]; $reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [filesize(\OC::$SERVERROOT.'/tests/data/testimage.jpg')]];
$this->container['Request']->method('getUploadedFile')->willReturn($reqRet); $this->request->method('getUploadedFile')->willReturn($reqRet);
$response = $this->avatarController->postAvatar(null); $response = $this->avatarController->postAvatar(null);
@ -276,7 +291,7 @@ class AvatarControllerTest extends \Test\TestCase {
public function testPostAvatarInvalidFile() { public function testPostAvatarInvalidFile() {
//Create request return //Create request return
$reqRet = ['error' => [1], 'tmp_name' => ['foo']]; $reqRet = ['error' => [1], 'tmp_name' => ['foo']];
$this->container['Request']->method('getUploadedFile')->willReturn($reqRet); $this->request->method('getUploadedFile')->willReturn($reqRet);
$response = $this->avatarController->postAvatar(null); $response = $this->avatarController->postAvatar(null);
@ -293,11 +308,11 @@ class AvatarControllerTest extends \Test\TestCase {
$this->assertTrue($copyRes); $this->assertTrue($copyRes);
//Create file in cache //Create file in cache
$this->container['Cache']->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.gif')); $this->cache->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.gif'));
//Create request return //Create request return
$reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => filesize(\OC::$SERVERROOT.'/tests/data/testimage.gif')]; $reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => filesize(\OC::$SERVERROOT.'/tests/data/testimage.gif')];
$this->container['Request']->method('getUploadedFile')->willReturn($reqRet); $this->request->method('getUploadedFile')->willReturn($reqRet);
$response = $this->avatarController->postAvatar(null); $response = $this->avatarController->postAvatar(null);
@ -315,7 +330,9 @@ class AvatarControllerTest extends \Test\TestCase {
$file = $this->getMockBuilder('OCP\Files\File') $file = $this->getMockBuilder('OCP\Files\File')
->disableOriginalConstructor()->getMock(); ->disableOriginalConstructor()->getMock();
$file->method('getContent')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg')); $file->method('getContent')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
$this->container['UserFolder']->method('get')->willReturn($file); $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
$this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder);
$userFolder->method('get')->willReturn($file);
//Create request return //Create request return
$response = $this->avatarController->postAvatar('avatar.jpg'); $response = $this->avatarController->postAvatar('avatar.jpg');
@ -329,7 +346,9 @@ class AvatarControllerTest extends \Test\TestCase {
*/ */
public function testPostAvatarFromNoFile() { public function testPostAvatarFromNoFile() {
$file = $this->getMockBuilder('OCP\Files\Node')->getMock(); $file = $this->getMockBuilder('OCP\Files\Node')->getMock();
$this->container['UserFolder'] $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
$this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder);
$userFolder
->method('get') ->method('get')
->with('folder') ->with('folder')
->willReturn($file); ->willReturn($file);
@ -345,15 +364,17 @@ class AvatarControllerTest extends \Test\TestCase {
* Test what happens if the upload of the avatar fails * Test what happens if the upload of the avatar fails
*/ */
public function testPostAvatarException() { public function testPostAvatarException() {
$this->container['Cache']->expects($this->once()) $this->cache->expects($this->once())
->method('set') ->method('set')
->will($this->throwException(new \Exception("foo"))); ->will($this->throwException(new \Exception("foo")));
$file = $this->getMockBuilder('OCP\Files\File') $file = $this->getMockBuilder('OCP\Files\File')
->disableOriginalConstructor()->getMock(); ->disableOriginalConstructor()->getMock();
$file->method('getContent')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg')); $file->method('getContent')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
$this->container['UserFolder']->method('get')->willReturn($file); $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
$this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder);
$userFolder->method('get')->willReturn($file);
$this->container['Logger']->expects($this->once()) $this->logger->expects($this->once())
->method('logException') ->method('logException')
->with(new \Exception("foo")); ->with(new \Exception("foo"));
$expectedResponse = new Http\DataResponse(['data' => ['message' => 'An error occurred. Please contact your admin.']], Http::STATUS_OK); $expectedResponse = new Http\DataResponse(['data' => ['message' => 'An error occurred. Please contact your admin.']], Http::STATUS_OK);
@ -383,10 +404,10 @@ class AvatarControllerTest extends \Test\TestCase {
* Test with non square crop * Test with non square crop
*/ */
public function testPostCroppedAvatarNoSquareCrop() { public function testPostCroppedAvatarNoSquareCrop() {
$this->container['Cache']->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg')); $this->cache->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
$this->avatarMock->method('set')->will($this->throwException(new \OC\NotSquareException)); $this->avatarMock->method('set')->will($this->throwException(new \OC\NotSquareException));
$this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock); $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
$response = $this->avatarController->postCroppedAvatar(['x' => 0, 'y' => 0, 'w' => 10, 'h' => 11]); $response = $this->avatarController->postCroppedAvatar(['x' => 0, 'y' => 0, 'w' => 10, 'h' => 11]);
$this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus()); $this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus());
@ -396,8 +417,8 @@ class AvatarControllerTest extends \Test\TestCase {
* Check for proper reply on proper crop argument * Check for proper reply on proper crop argument
*/ */
public function testPostCroppedAvatarValidCrop() { public function testPostCroppedAvatarValidCrop() {
$this->container['Cache']->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg')); $this->cache->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
$this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock); $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
$response = $this->avatarController->postCroppedAvatar(['x' => 0, 'y' => 0, 'w' => 10, 'h' => 10]); $response = $this->avatarController->postCroppedAvatar(['x' => 0, 'y' => 0, 'w' => 10, 'h' => 10]);
$this->assertEquals(Http::STATUS_OK, $response->getStatus()); $this->assertEquals(Http::STATUS_OK, $response->getStatus());
@ -408,12 +429,12 @@ class AvatarControllerTest extends \Test\TestCase {
* Test what happens if the cropping of the avatar fails * Test what happens if the cropping of the avatar fails
*/ */
public function testPostCroppedAvatarException() { public function testPostCroppedAvatarException() {
$this->container['Cache']->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg')); $this->cache->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
$this->avatarMock->method('set')->will($this->throwException(new \Exception('foo'))); $this->avatarMock->method('set')->will($this->throwException(new \Exception('foo')));
$this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock); $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
$this->container['Logger']->expects($this->once()) $this->logger->expects($this->once())
->method('logException') ->method('logException')
->with(new \Exception('foo')); ->with(new \Exception('foo'));
$expectedResponse = new Http\DataResponse(['data' => ['message' => 'An error occurred. Please contact your admin.']], Http::STATUS_BAD_REQUEST); $expectedResponse = new Http\DataResponse(['data' => ['message' => 'An error occurred. Please contact your admin.']], Http::STATUS_BAD_REQUEST);
@ -428,7 +449,7 @@ class AvatarControllerTest extends \Test\TestCase {
$fileName = \OC::$SERVERROOT.'/tests/data/testimage.jpg'; $fileName = \OC::$SERVERROOT.'/tests/data/testimage.jpg';
//Create request return //Create request return
$reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [21*1024*1024]]; $reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [21*1024*1024]];
$this->container['Request']->method('getUploadedFile')->willReturn($reqRet); $this->request->method('getUploadedFile')->willReturn($reqRet);
$response = $this->avatarController->postAvatar(null); $response = $this->avatarController->postAvatar(null);