Skip to content

Commit

Permalink
Fixing avatar null user folder ....
Browse files Browse the repository at this point in the history
  • Loading branch information
DeepDiver1975 committed Oct 27, 2017
1 parent f116830 commit 022145e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 32 deletions.
8 changes: 4 additions & 4 deletions core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use OC\Core\Controller\UserController;
use OC_Defaults;
use OCP\AppFramework\App;
use OCP\IServerContainer;
use OCP\Util;

/**
Expand Down Expand Up @@ -86,6 +87,8 @@ public function __construct(array $urlParams= []){
);
});
$container->registerService('AvatarController', function(SimpleContainer $c) {
/** @var IServerContainer $server */
$server = $c->query('ServerContainer');
return new AvatarController(
$c->query('AppName'),
$c->query('Request'),
Expand All @@ -94,7 +97,7 @@ public function __construct(array $urlParams= []){
$c->query('L10N'),
$c->query('UserManager'),
$c->query('UserSession'),
$c->query('UserFolder'),
$server->getRootFolder(),
$c->query('Logger')
);
});
Expand Down Expand Up @@ -169,9 +172,6 @@ public function __construct(array $urlParams= []){
$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() {
return new OC_Defaults;
});
Expand Down
30 changes: 17 additions & 13 deletions core/Controller/AvatarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,22 @@
*/
namespace OC\Core\Controller;

use OC\AppFramework\Http\Request;
use OC\Cache\File;
use OC\Files\Filesystem;
use OC\NotSquareException;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\DataDisplayResponse;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\IAvatarManager;
use OCP\ILogger;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Files\Folder;

/**
* Class AvatarController
Expand All @@ -50,7 +54,7 @@ class AvatarController extends Controller {
/** @var IAvatarManager */
protected $avatarManager;

/** @var \OC\Cache\File */
/** @var File */
protected $cache;

/** @var IL10N */
Expand All @@ -62,8 +66,8 @@ class AvatarController extends Controller {
/** @var IUserSession */
protected $userSession;

/** @var Folder */
protected $userFolder;
/** @var IRootFolder */
protected $rootFolder;

/** @var ILogger */
protected $logger;
Expand All @@ -72,21 +76,21 @@ class AvatarController extends Controller {
* @param string $appName
* @param IRequest $request
* @param IAvatarManager $avatarManager
* @param \OC\Cache\File $cache
* @param File $cache
* @param IL10N $l10n
* @param IUserManager $userManager
* @param IUserSession $userSession
* @param Folder $userFolder
* @param IRootFolder $rootFolder
* @param ILogger $logger
*/
public function __construct($appName,
IRequest $request,
IAvatarManager $avatarManager,
\OC\Cache\File $cache,
File $cache,
IL10N $l10n,
IUserManager $userManager,
IUserSession $userSession,
Folder $userFolder,
IRootFolder $rootFolder,
ILogger $logger) {
parent::__construct($appName, $request);

Expand All @@ -95,7 +99,7 @@ public function __construct($appName,
$this->l = $l10n;
$this->userManager = $userManager;
$this->userSession = $userSession;
$this->userFolder = $userFolder;
$this->rootFolder = $rootFolder;
$this->logger = $logger;
}

Expand Down Expand Up @@ -153,14 +157,14 @@ public function postAvatar($path) {
$files = $this->request->getUploadedFile('files');

$headers = [];
if ($this->request->isUserAgent([\OC\AppFramework\Http\Request::USER_AGENT_IE_8])) {
if ($this->request->isUserAgent([Request::USER_AGENT_IE_8])) {
// due to upload iframe workaround, need to set content-type to text/plain
$headers['Content-Type'] = 'text/plain';
}

if (isset($path)) {
$path = stripslashes($path);
$node = $this->userFolder->get($path);
$node = $this->rootFolder->getUserFolder($userId)->get($path);
if (!($node instanceof \OCP\Files\File)) {
return new DataResponse(['data' => ['message' => $this->l->t('Please select a file.')]], Http::STATUS_OK, $headers);
}
Expand All @@ -176,7 +180,7 @@ public function postAvatar($path) {
if (
$files['error'][0] === 0 &&
$this->isUploadFile($files['tmp_name'][0]) &&
!\OC\Files\Filesystem::isForbiddenFileOrDir($files['tmp_name'][0])
!Filesystem::isForbiddenFileOrDir($files['tmp_name'][0])
) {
if ($files['size'][0] > 20*1024*1024) {
return new DataResponse(
Expand Down Expand Up @@ -317,7 +321,7 @@ public function postCroppedAvatar($crop) {
// Clean up
$this->cache->remove('tmpAvatar');
return new DataResponse(['status' => 'success']);
} catch (\OC\NotSquareException $e) {
} catch (NotSquareException $e) {
return new DataResponse(['data' => ['message' => $this->l->t('Crop is not square')]],
Http::STATUS_BAD_REQUEST);
} catch (\Exception $e) {
Expand Down
38 changes: 23 additions & 15 deletions tests/Core/Controller/AvatarControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use OCP\AppFramework\Http;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\IAvatarManager;
use OCP\IL10N;
Expand Down Expand Up @@ -69,7 +70,7 @@ class AvatarControllerTest extends TestCase {
/** @var IUserSession | \PHPUnit_Framework_MockObject_MockObject */
private $userSession;
/** @var Folder | \PHPUnit_Framework_MockObject_MockObject */
private $userFolder;
private $rootFolder;
/** @var ILogger | \PHPUnit_Framework_MockObject_MockObject */
private $logger;

Expand All @@ -78,18 +79,18 @@ protected function setUp() {
$this->createUser('userid', 'pass');
$this->loginAsUser('userid');

$this->avatarManager = $this->createMock('OCP\IAvatarManager');
$this->cache = $this->getMockBuilder('OC\Cache\File')->disableOriginalConstructor()->getMock();
$this->l10N = $this->createMock('OCP\IL10N');
$this->avatarManager = $this->createMock(IAvatarManager::class);
$this->cache = $this->getMockBuilder(\OC\Cache\File::class)->disableOriginalConstructor()->getMock();
$this->l10N = $this->createMock(IL10N::class);
$this->l10N->expects($this->any())->method('t')->will($this->returnArgument(0));
$this->userManager = $this->createMock('OCP\IUserManager');
$this->userSession = $this->createMock('OCP\IUserSession');
$this->request = $this->createMock('OCP\IRequest');
$this->userFolder = $this->createMock('OCP\Files\Folder');
$this->logger = $this->createMock('OCP\ILogger');
$this->userManager = $this->createMock(IUserManager::class);
$this->userSession = $this->createMock(IUserSession::class);
$this->request = $this->createMock(IRequest::class);
$this->rootFolder = $this->createMock(IRootFolder::class);
$this->logger = $this->createMock(ILogger::class);

$this->avatarMock = $this->createMock('OCP\IAvatar');
$this->userMock = $this->createMock('OCP\IUser');
$this->avatarMock = $this->createMock(IAvatar::class);
$this->userMock = $this->createMock(IUser::class);

$this->avatarController = $this->getMockBuilder(AvatarController::class)
->setMethods(['isUploadFile'])
Expand All @@ -101,7 +102,7 @@ protected function setUp() {
$this->l10N,
$this->userManager,
$this->userSession,
$this->userFolder,
$this->rootFolder,
$this->logger])
->getMock();
$this->avatarController
Expand Down Expand Up @@ -338,10 +339,13 @@ public function testPostAvatarFileGif() {
*/
public function testPostAvatarFromFile() {
//Mock node API call
$userFolder = $this->createMock(Folder::class);
$file = $this->getMockBuilder('OCP\Files\File')
->disableOriginalConstructor()->getMock();
$file->expects($this->any())->method('getContent')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
$this->userFolder->expects($this->once())->method('get')->willReturn($file);
$userFolder->expects($this->once())->method('get')->willReturn($file);

$this->rootFolder->expects($this->once())->method('getUserFolder')->willReturn($userFolder);

//Create request return
$response = $this->avatarController->postAvatar('avatar.jpg');
Expand All @@ -354,12 +358,14 @@ public function testPostAvatarFromFile() {
* Test posting avatar from existing folder
*/
public function testPostAvatarFromNoFile() {
$userFolder = $this->createMock(Folder::class);
$file = $this->createMock('OCP\Files\Node');
$this->userFolder
$userFolder
->expects($this->once())
->method('get')
->with('folder')
->willReturn($file);
$this->rootFolder->expects($this->once())->method('getUserFolder')->willReturn($userFolder);

//Create request return
$response = $this->avatarController->postAvatar('folder');
Expand All @@ -375,10 +381,12 @@ public function testPostAvatarException() {
$this->cache->expects($this->once())
->method('set')
->will($this->throwException(new \Exception("foo")));
$userFolder = $this->createMock(Folder::class);
$file = $this->getMockBuilder('OCP\Files\File')
->disableOriginalConstructor()->getMock();
$file->expects($this->any())->method('getContent')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
$this->userFolder->expects($this->once())->method('get')->willReturn($file);
$userFolder->expects($this->once())->method('get')->willReturn($file);
$this->rootFolder->expects($this->once())->method('getUserFolder')->willReturn($userFolder);

$this->logger->expects($this->once())
->method('logException')
Expand Down

0 comments on commit 022145e

Please sign in to comment.