diff --git a/core/Application.php b/core/Application.php index 4ce9d9376262..df624c2de2f6 100644 --- a/core/Application.php +++ b/core/Application.php @@ -40,6 +40,7 @@ use OC\Core\Controller\UserController; use OC_Defaults; use OCP\AppFramework\App; +use OCP\IServerContainer; use OCP\Util; /** @@ -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'), @@ -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') ); }); @@ -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; }); diff --git a/core/Controller/AvatarController.php b/core/Controller/AvatarController.php index 5e28ad9cfb72..764edbea498f 100644 --- a/core/Controller/AvatarController.php +++ b/core/Controller/AvatarController.php @@ -27,10 +27,15 @@ */ 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; @@ -38,7 +43,6 @@ use OCP\IRequest; use OCP\IUserManager; use OCP\IUserSession; -use OCP\Files\Folder; /** * Class AvatarController @@ -50,7 +54,7 @@ class AvatarController extends Controller { /** @var IAvatarManager */ protected $avatarManager; - /** @var \OC\Cache\File */ + /** @var File */ protected $cache; /** @var IL10N */ @@ -62,8 +66,8 @@ class AvatarController extends Controller { /** @var IUserSession */ protected $userSession; - /** @var Folder */ - protected $userFolder; + /** @var IRootFolder */ + protected $rootFolder; /** @var ILogger */ protected $logger; @@ -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); @@ -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; } @@ -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); } @@ -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( @@ -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) { diff --git a/tests/Core/Controller/AvatarControllerTest.php b/tests/Core/Controller/AvatarControllerTest.php index 14ce14f17a03..43af90bd44e8 100644 --- a/tests/Core/Controller/AvatarControllerTest.php +++ b/tests/Core/Controller/AvatarControllerTest.php @@ -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; @@ -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; @@ -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']) @@ -101,7 +102,7 @@ protected function setUp() { $this->l10N, $this->userManager, $this->userSession, - $this->userFolder, + $this->rootFolder, $this->logger]) ->getMock(); $this->avatarController @@ -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'); @@ -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'); @@ -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')