From 6c27874db7a563f37aff1eeb6fff7dcaa004088b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Mon, 14 Sep 2026 18:02:32 +0200 Subject: [PATCH 1/3] feat: Add IUserFolder to dependency injection container MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of having Folder only in application containers, make IUserFolder directly available on server DI. Folder is now a deprecated alias. Signed-off-by: Côme Chilliet --- .../AppFramework/DependencyInjection/DIContainer.php | 10 ---------- lib/private/Server.php | 11 +++++++++++ 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/private/AppFramework/DependencyInjection/DIContainer.php b/lib/private/AppFramework/DependencyInjection/DIContainer.php index f5680374add02..2604346413fa0 100644 --- a/lib/private/AppFramework/DependencyInjection/DIContainer.php +++ b/lib/private/AppFramework/DependencyInjection/DIContainer.php @@ -50,9 +50,7 @@ use OCP\AppFramework\Services\IAppConfig; use OCP\AppFramework\Services\IInitialState; use OCP\Files\AppData\IAppDataFactory; -use OCP\Files\Folder; use OCP\Files\IAppData; -use OCP\Files\IRootFolder; use OCP\Group\ISubAdmin; use OCP\IConfig; use OCP\IDBConnection; @@ -106,14 +104,6 @@ public function __construct( /* Cannot be an alias because Output is not in OCA */ $this->registerService(IOutput::class, fn (ContainerInterface $c): IOutput => new Output($c->get('webRoot'))); - $this->registerService(Folder::class, function () { - $user = $this->get(IUserSession::class)->getUser(); - if ($user === null) { - return null; - } - return $this->getServer()->get(IRootFolder::class)->getUserFolder($user->getUID()); - }); - $this->registerService(IAppData::class, function (ContainerInterface $c): IAppData { return $c->get(IAppDataFactory::class)->get($c->get('appName')); }); diff --git a/lib/private/Server.php b/lib/private/Server.php index ab4f6c93cb68a..6dbd4efd92c22 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -196,11 +196,13 @@ use OCP\Files\Config\IMountProviderCollection; use OCP\Files\Config\IUserMountCache; use OCP\Files\Conversion\IConversionManager; +use OCP\Files\Folder; use OCP\Files\IFilenameValidator; use OCP\Files\IMimeTypeDetector; use OCP\Files\IMimeTypeLoader; use OCP\Files\IRootFolder; use OCP\Files\ISetupManager; +use OCP\Files\IUserFolder; use OCP\Files\Lock\ILockManager; use OCP\Files\Mount\IMountManager; use OCP\Files\Storage\IStorageFactory; @@ -404,6 +406,15 @@ public function __construct( }); }); + $this->registerService(IUserFolder::class, static function (ContainerInterface $c): ?IUserFolder { + $user = $c->get(IUserSession::class)->getUser(); + if ($user === null) { + return null; + } + return $c->get(IRootFolder::class)->getUserFolder($user->getUID()); + }); + $this->registerDeprecatedAlias(Folder::class, IUserFolder::class); + $this->registerAlias(IUserManager::class, \OC\User\Manager::class); $this->registerService(DisplayNameCache::class, static function (ContainerInterface $c) { From 3b2ebd52aa4f10edcd998680ced1b8d22f08c444 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 15 Sep 2026 11:35:59 +0200 Subject: [PATCH 2/3] fix: Inject IUserFolder where it makes sense MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- .../lib/Listener/CommentsEntityEventListener.php | 9 ++++----- apps/dav/lib/BulkUpload/BulkUploadPlugin.php | 4 ++-- apps/dav/lib/Connector/Sabre/FilesReportPlugin.php | 14 ++------------ apps/dav/lib/Controller/DirectController.php | 10 ++++------ apps/dav/lib/DAV/ViewOnlyPlugin.php | 4 ++-- apps/dav/lib/Server.php | 1 - core/Controller/AvatarController.php | 7 +++---- core/Controller/PreviewController.php | 11 ++++------- 8 files changed, 21 insertions(+), 39 deletions(-) diff --git a/apps/comments/lib/Listener/CommentsEntityEventListener.php b/apps/comments/lib/Listener/CommentsEntityEventListener.php index 3c5900f220a57..9bc71e0b39222 100644 --- a/apps/comments/lib/Listener/CommentsEntityEventListener.php +++ b/apps/comments/lib/Listener/CommentsEntityEventListener.php @@ -12,13 +12,12 @@ use OCP\Comments\CommentsEntityEvent; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; -use OCP\Files\IRootFolder; +use OCP\Files\IUserFolder; /** @template-implements IEventListener */ class CommentsEntityEventListener implements IEventListener { public function __construct( - private IRootFolder $rootFolder, - private ?string $userId = null, + private ?IUserFolder $userFolder, ) { } @@ -29,12 +28,12 @@ public function handle(Event $event): void { return; } - if ($this->userId === null) { + if ($this->userFolder === null) { return; } $event->addEntityCollection('files', function ($name): bool { - $node = $this->rootFolder->getUserFolder($this->userId)->getFirstNodeById((int)$name); + $node = $this->userFolder->getFirstNodeById((int)$name); return $node !== null; }); } diff --git a/apps/dav/lib/BulkUpload/BulkUploadPlugin.php b/apps/dav/lib/BulkUpload/BulkUploadPlugin.php index 58e2275f1655a..b51baf7bc926f 100644 --- a/apps/dav/lib/BulkUpload/BulkUploadPlugin.php +++ b/apps/dav/lib/BulkUpload/BulkUploadPlugin.php @@ -10,7 +10,7 @@ use OCA\DAV\Connector\Sabre\MtimeSanitizer; use OCP\AppFramework\Http; use OCP\Files\DavUtil; -use OCP\Files\Folder; +use OCP\Files\IUserFolder; use Psr\Log\LoggerInterface; use Sabre\DAV\Server; use Sabre\DAV\ServerPlugin; @@ -19,7 +19,7 @@ class BulkUploadPlugin extends ServerPlugin { public function __construct( - private Folder $userFolder, + private IUserFolder $userFolder, private LoggerInterface $logger, ) { } diff --git a/apps/dav/lib/Connector/Sabre/FilesReportPlugin.php b/apps/dav/lib/Connector/Sabre/FilesReportPlugin.php index dfff081330300..ae7e681621445 100644 --- a/apps/dav/lib/Connector/Sabre/FilesReportPlugin.php +++ b/apps/dav/lib/Connector/Sabre/FilesReportPlugin.php @@ -13,6 +13,7 @@ use OCP\App\IAppManager; use OCP\AppFramework\Http; use OCP\Files\Folder; +use OCP\Files\IUserFolder; use OCP\Files\Node as INode; use OCP\IGroupManager; use OCP\ITagManager; @@ -43,17 +44,6 @@ class FilesReportPlugin extends ServerPlugin { */ private $server; - /** - * @param Tree $tree - * @param View $fileView - * @param ISystemTagManager $tagManager - * @param ISystemTagObjectMapper $tagMapper - * @param ITagManager $fileTagger manager for private tags - * @param IUserSession $userSession - * @param IGroupManager $groupManager - * @param Folder $userFolder - * @param IAppManager $appManager - */ public function __construct( private Tree $tree, private View $fileView, @@ -65,7 +55,7 @@ public function __construct( private ITagManager $fileTagger, private IUserSession $userSession, private IGroupManager $groupManager, - private Folder $userFolder, + private IUserFolder $userFolder, private IAppManager $appManager, ) { } diff --git a/apps/dav/lib/Controller/DirectController.php b/apps/dav/lib/Controller/DirectController.php index d209800ccabc5..7ccdcdcc34dd9 100644 --- a/apps/dav/lib/Controller/DirectController.php +++ b/apps/dav/lib/Controller/DirectController.php @@ -22,7 +22,7 @@ use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\Events\BeforeDirectFileDownloadEvent; use OCP\Files\File; -use OCP\Files\IRootFolder; +use OCP\Files\IUserFolder; use OCP\IRequest; use OCP\IURLGenerator; use OCP\Security\ISecureRandom; @@ -33,7 +33,7 @@ class DirectController extends OCSController { public function __construct( string $appName, IRequest $request, - private IRootFolder $rootFolder, + private IUserFolder $userFolder, private string $userId, private DirectMapper $mapper, private ISecureRandom $random, @@ -63,9 +63,7 @@ public function getUrl(int $fileId, int $expirationTime = 60 * 60 * 8): DataResp throw new OCSForbiddenException('Creating direct links is disabled'); } - $userFolder = $this->rootFolder->getUserFolder($this->userId); - - $file = $userFolder->getFirstNodeById($fileId); + $file = $this->userFolder->getFirstNodeById($fileId); if (!$file) { throw new OCSNotFoundException(); @@ -79,7 +77,7 @@ public function getUrl(int $fileId, int $expirationTime = 60 * 60 * 8): DataResp throw new OCSBadRequestException('Direct download only works for files'); } - $event = new BeforeDirectFileDownloadEvent($userFolder->getRelativePath($file->getPath())); + $event = new BeforeDirectFileDownloadEvent($this->userFolder->getRelativePath($file->getPath())); $this->eventDispatcher->dispatchTyped($event); if ($event->isSuccessful() === false) { diff --git a/apps/dav/lib/DAV/ViewOnlyPlugin.php b/apps/dav/lib/DAV/ViewOnlyPlugin.php index 7ee165c0cc4d7..e41514d7e21ce 100644 --- a/apps/dav/lib/DAV/ViewOnlyPlugin.php +++ b/apps/dav/lib/DAV/ViewOnlyPlugin.php @@ -12,7 +12,7 @@ use OCA\DAV\Connector\Sabre\Exception\Forbidden; use OCA\DAV\Connector\Sabre\File as DavFile; use OCA\Files_Versions\Sabre\VersionFile; -use OCP\Files\Folder; +use OCP\Files\IUserFolder; use OCP\Files\NotFoundException; use OCP\Files\Storage\ISharedStorage; use Sabre\DAV\Exception\NotFound; @@ -27,7 +27,7 @@ class ViewOnlyPlugin extends ServerPlugin { private ?Server $server = null; public function __construct( - private ?Folder $userFolder, + private ?IUserFolder $userFolder, ) { } diff --git a/apps/dav/lib/Server.php b/apps/dav/lib/Server.php index 4e286f408b2bc..ce5964e1c10f7 100644 --- a/apps/dav/lib/Server.php +++ b/apps/dav/lib/Server.php @@ -338,7 +338,6 @@ public function __construct( ) ); - // TODO: switch to LazyUserFolder $shareManager = \OCP\Server::get(\OCP\Share\IManager::class); $this->server->addPlugin(new SharesPlugin( $this->server->tree, diff --git a/core/Controller/AvatarController.php b/core/Controller/AvatarController.php index c232dd140fc37..196850068aa6c 100644 --- a/core/Controller/AvatarController.php +++ b/core/Controller/AvatarController.php @@ -21,7 +21,7 @@ use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\Response; use OCP\Files\File; -use OCP\Files\IRootFolder; +use OCP\Files\IUserFolder; use OCP\Files\NotPermittedException; use OCP\IAvatarManager; use OCP\IL10N; @@ -42,7 +42,7 @@ public function __construct( protected IAvatarManager $avatarManager, protected IL10N $l10n, protected IUserManager $userManager, - protected IRootFolder $rootFolder, + protected ?IUserFolder $userFolder, protected LoggerInterface $logger, protected ?string $userId, protected TimeFactory $timeFactory, @@ -158,9 +158,8 @@ public function postAvatar(?string $path = null): JSONResponse { if (isset($path)) { $path = stripslashes($path); - $userFolder = $this->rootFolder->getUserFolder($this->userId); /** @var File $node */ - $node = $userFolder->get($path); + $node = $this->userFolder->get($path); if (!($node instanceof File)) { return new JSONResponse(['data' => ['message' => $this->l10n->t('Please select a file.')]]); } diff --git a/core/Controller/PreviewController.php b/core/Controller/PreviewController.php index 310d7f85f74a1..b335b5a957506 100644 --- a/core/Controller/PreviewController.php +++ b/core/Controller/PreviewController.php @@ -21,7 +21,7 @@ use OCP\AppFramework\Http\RedirectResponse; use OCP\AppFramework\Http\Response; use OCP\Files\File; -use OCP\Files\IRootFolder; +use OCP\Files\IUserFolder; use OCP\Files\Node; use OCP\Files\NotFoundException; use OCP\Files\Storage\ISharedStorage; @@ -34,8 +34,7 @@ public function __construct( string $appName, IRequest $request, private IPreview $preview, - private IRootFolder $root, - private ?string $userId, + private ?IUserFolder $userFolder, private IMimeIconProvider $mimeIconProvider, ) { parent::__construct($appName, $request); @@ -76,8 +75,7 @@ public function getPreview( } try { - $userFolder = $this->root->getUserFolder($this->userId); - $node = $userFolder->get($file); + $node = $this->userFolder->get($file); } catch (NotFoundException $e) { return new DataResponse([], Http::STATUS_NOT_FOUND); } @@ -119,8 +117,7 @@ public function getPreviewByFileId( return new DataResponse([], Http::STATUS_BAD_REQUEST); } - $userFolder = $this->root->getUserFolder($this->userId); - $node = $userFolder->getFirstNodeById($fileId); + $node = $this->userFolder->getFirstNodeById($fileId); if (!$node) { return new DataResponse([], Http::STATUS_NOT_FOUND); From e295d2be936f6e2ce312a96c3683bedbf53e147e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 17 Sep 2026 15:19:21 +0200 Subject: [PATCH 3/3] fix(tests): Adapt tests to IUserFolder injection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- .../Connector/Sabre/FilesReportPluginTest.php | 3 +- .../unit/Controller/DirectControllerTest.php | 37 ++------- .../dav/tests/unit/DAV/ViewOnlyPluginTest.php | 3 +- .../Core/Controller/AvatarControllerTest.php | 82 ++++++++----------- .../Core/Controller/PreviewControllerTest.php | 72 +++------------- 5 files changed, 56 insertions(+), 141 deletions(-) diff --git a/apps/dav/tests/unit/Connector/Sabre/FilesReportPluginTest.php b/apps/dav/tests/unit/Connector/Sabre/FilesReportPluginTest.php index cb05fe3c30d22..f3dbe89fdc9dc 100644 --- a/apps/dav/tests/unit/Connector/Sabre/FilesReportPluginTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/FilesReportPluginTest.php @@ -19,6 +19,7 @@ use OCP\Files\FileInfo; use OCP\Files\Folder; use OCP\Files\IFilenameValidator; +use OCP\Files\IUserFolder; use OCP\IConfig; use OCP\IGroupManager; use OCP\IPreview; @@ -73,7 +74,7 @@ protected function setUp(): void { ->willReturn('http://example.com/owncloud/remote.php/dav'); $this->groupManager = $this->createMock(IGroupManager::class); - $this->userFolder = $this->createMock(Folder::class); + $this->userFolder = $this->createMock(IUserFolder::class); $this->previewManager = $this->createMock(IPreview::class); $this->appManager = $this->createMock(IAppManager::class); $this->tagManager = $this->createMock(ISystemTagManager::class); diff --git a/apps/dav/tests/unit/Controller/DirectControllerTest.php b/apps/dav/tests/unit/Controller/DirectControllerTest.php index 26b7edb1170a2..aa195406779e6 100644 --- a/apps/dav/tests/unit/Controller/DirectControllerTest.php +++ b/apps/dav/tests/unit/Controller/DirectControllerTest.php @@ -20,7 +20,6 @@ use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\File; use OCP\Files\Folder; -use OCP\Files\IRootFolder; use OCP\Files\IUserFolder; use OCP\IRequest; use OCP\IURLGenerator; @@ -30,7 +29,7 @@ use Test\TestCase; class DirectControllerTest extends TestCase { - private IRootFolder&MockObject $rootFolder; + private IUserFolder&MockObject $userFolder; private DirectMapper&MockObject $directMapper; private ISecureRandom&MockObject $random; private ITimeFactory&MockObject $timeFactory; @@ -43,7 +42,7 @@ class DirectControllerTest extends TestCase { protected function setUp(): void { parent::setUp(); - $this->rootFolder = $this->createMock(IRootFolder::class); + $this->userFolder = $this->createMock(IUserFolder::class); $this->directMapper = $this->createMock(DirectMapper::class); $this->random = $this->createMock(ISecureRandom::class); $this->timeFactory = $this->createMock(ITimeFactory::class); @@ -54,7 +53,7 @@ protected function setUp(): void { $this->controller = new DirectController( 'dav', $this->createMock(IRequest::class), - $this->rootFolder, + $this->userFolder, 'awesomeUser', $this->directMapper, $this->random, @@ -69,12 +68,7 @@ public function testGetUrlNonExistingFileId(): void { $this->shareManager->method('shareApiAllowLinks') ->willReturn(true); - $userFolder = $this->createMock(IUserFolder::class); - $this->rootFolder->method('getUserFolder') - ->with('awesomeUser') - ->willReturn($userFolder); - - $userFolder->method('getById') + $this->userFolder->method('getById') ->with(101) ->willReturn([]); @@ -86,14 +80,9 @@ public function testGetUrlForFolder(): void { $this->shareManager->method('shareApiAllowLinks') ->willReturn(true); - $userFolder = $this->createMock(IUserFolder::class); - $this->rootFolder->method('getUserFolder') - ->with('awesomeUser') - ->willReturn($userFolder); - $folder = $this->createMock(Folder::class); - $userFolder->method('getFirstNodeById') + $this->userFolder->method('getFirstNodeById') ->with(101) ->willReturn($folder); @@ -105,21 +94,16 @@ public function testGetUrlValid(): void { $this->shareManager->method('shareApiAllowLinks') ->willReturn(true); - $userFolder = $this->createMock(IUserFolder::class); - $this->rootFolder->method('getUserFolder') - ->with('awesomeUser') - ->willReturn($userFolder); - $file = $this->createMock(File::class); $this->timeFactory->method('getTime') ->willReturn(42); - $userFolder->method('getFirstNodeById') + $this->userFolder->method('getFirstNodeById') ->with(101) ->willReturn($file); - $userFolder->method('getRelativePath') + $this->userFolder->method('getRelativePath') ->willReturn('/path'); $this->random->method('generate') @@ -156,14 +140,9 @@ public function testGetUrlNoLinkShares(): void { $this->shareManager->method('shareApiAllowLinks') ->willReturn(false); - $userFolder = $this->createMock(IUserFolder::class); - $this->rootFolder->method('getUserFolder') - ->with('awesomeUser') - ->willReturn($userFolder); - $file = $this->createMock(File::class); - $userFolder->method('getFirstNodeById') + $this->userFolder->method('getFirstNodeById') ->with(101) ->willReturn($file); diff --git a/apps/dav/tests/unit/DAV/ViewOnlyPluginTest.php b/apps/dav/tests/unit/DAV/ViewOnlyPluginTest.php index 52f2c553a5112..a0070793b3bb7 100644 --- a/apps/dav/tests/unit/DAV/ViewOnlyPluginTest.php +++ b/apps/dav/tests/unit/DAV/ViewOnlyPluginTest.php @@ -17,6 +17,7 @@ use OCA\Files_Versions\Versions\IVersion; use OCP\Files\File; use OCP\Files\Folder; +use OCP\Files\IUserFolder; use OCP\Files\Storage\ISharedStorage; use OCP\Files\Storage\IStorage; use OCP\IUser; @@ -36,7 +37,7 @@ class ViewOnlyPluginTest extends TestCase { public function setUp(): void { parent::setUp(); - $this->userFolder = $this->createMock(Folder::class); + $this->userFolder = $this->createMock(IUserFolder::class); $this->request = $this->createMock(RequestInterface::class); $this->tree = $this->createMock(Tree::class); $server = $this->createMock(Server::class); diff --git a/tests/Core/Controller/AvatarControllerTest.php b/tests/Core/Controller/AvatarControllerTest.php index 125c015a30335..38718eab67786 100644 --- a/tests/Core/Controller/AvatarControllerTest.php +++ b/tests/Core/Controller/AvatarControllerTest.php @@ -22,7 +22,6 @@ function is_uploaded_file($filename) { use OC\Core\Controller\AvatarController; use OC\Core\Controller\GuestAvatarController; use OCP\AppFramework\Http; -use OCP\Files\IRootFolder; use OCP\Files\IUserFolder; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; @@ -33,6 +32,7 @@ function is_uploaded_file($filename) { use OCP\IRequest; use OCP\IUser; use OCP\IUserManager; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; /** @@ -41,47 +41,35 @@ function is_uploaded_file($filename) { * @package OC\Core\Controller */ class AvatarControllerTest extends \Test\TestCase { - /** @var AvatarController */ - private $avatarController; - /** @var GuestAvatarController */ - private $guestAvatarController; - - /** @var IAvatar|\PHPUnit\Framework\MockObject\MockObject */ - private $avatarMock; - /** @var IUser|\PHPUnit\Framework\MockObject\MockObject */ - private $userMock; - /** @var ISimpleFile|\PHPUnit\Framework\MockObject\MockObject */ - private $avatarFile; - /** @var IAvatarManager|\PHPUnit\Framework\MockObject\MockObject */ - private $avatarManager; - /** @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 LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */ - private $logger; - /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */ - private $request; - /** @var TimeFactory|\PHPUnit\Framework\MockObject\MockObject */ - private $timeFactory; + private AvatarController $avatarController; + private GuestAvatarController $guestAvatarController; + + private IAvatar&MockObject $avatarMock; + private IUser&MockObject $userMock; + private ISimpleFile&MockObject $avatarFile; + private IAvatarManager&MockObject $avatarManager; + private IL10N&MockObject $l; + private IUserManager&MockObject $userManager; + private IUserFolder&MockObject $userFolder; + private LoggerInterface&MockObject $logger; + private IRequest&MockObject $request; + private TimeFactory&MockObject $timeFactory; #[\Override] protected function setUp(): void { parent::setUp(); - $this->avatarManager = $this->getMockBuilder('OCP\IAvatarManager')->getMock(); - $this->l = $this->getMockBuilder(IL10N::class)->getMock(); + $this->avatarManager = $this->createMock(IAvatarManager::class); + $this->l = $this->createMock(IL10N::class); $this->l->method('t')->willReturnArgument(0); - $this->userManager = $this->getMockBuilder(IUserManager::class)->getMock(); - $this->request = $this->getMockBuilder(IRequest::class)->getMock(); - $this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->getMock(); - $this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock(); - $this->timeFactory = $this->getMockBuilder('OC\AppFramework\Utility\TimeFactory')->getMock(); + $this->userManager = $this->createMock(IUserManager::class); + $this->request = $this->createMock(IRequest::class); + $this->userFolder = $this->createMock(IUserFolder::class); + $this->logger = $this->createMock(LoggerInterface::class); + $this->timeFactory = $this->createMock(TimeFactory::class); - $this->avatarMock = $this->getMockBuilder('OCP\IAvatar')->getMock(); - $this->userMock = $this->getMockBuilder(IUser::class)->getMock(); + $this->avatarMock = $this->createMock(IAvatar::class); + $this->userMock = $this->createMock(IUser::class); $this->guestAvatarController = new GuestAvatarController( 'core', @@ -96,7 +84,7 @@ protected function setUp(): void { $this->avatarManager, $this->l, $this->userManager, - $this->rootFolder, + $this->userFolder, $this->logger, 'userid', $this->timeFactory, @@ -109,7 +97,7 @@ protected function setUp(): void { $this->userManager->method('get') ->willReturnMap([['userId', $this->userMock]]); - $this->avatarFile = $this->getMockBuilder(ISimpleFile::class)->getMock(); + $this->avatarFile = $this->createMock(ISimpleFile::class); $this->avatarFile->method('getContent')->willReturn('image data'); $this->avatarFile->method('getMimeType')->willReturn('image type'); $this->avatarFile->method('getEtag')->willReturn('my etag'); @@ -372,9 +360,7 @@ public function testPostAvatarFromFile(): void { $file->expects($this->once()) ->method('getMimeType') ->willReturn('image/jpeg'); - $userFolder = $this->createMock(IUserFolder::class); - $this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder); - $userFolder->method('get')->willReturn($file); + $this->userFolder->method('get')->willReturn($file); //Create request return $response = $this->avatarController->postAvatar('avatar.jpg'); @@ -387,10 +373,8 @@ public function testPostAvatarFromFile(): void { * Test posting avatar from existing folder */ public function testPostAvatarFromNoFile(): void { - $file = $this->getMockBuilder('OCP\Files\Node')->getMock(); - $userFolder = $this->createMock(IUserFolder::class); - $this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder); - $userFolder + $file = $this->createMock('OCP\Files\Node'); + $this->userFolder ->method('get') ->with('folder') ->willReturn($file); @@ -410,9 +394,8 @@ public function testPostAvatarInvalidType(): void { $file->expects($this->exactly(2)) ->method('getMimeType') ->willReturn('text/plain'); - $userFolder = $this->createMock(IUserFolder::class); - $this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder); - $userFolder->method('get')->willReturn($file); + + $this->userFolder->method('get')->willReturn($file); $expectedResponse = new Http\JSONResponse(['data' => ['message' => 'The selected file is not an image.']], Http::STATUS_BAD_REQUEST); $this->assertEquals($expectedResponse, $this->avatarController->postAvatar('avatar.jpg')); @@ -427,9 +410,8 @@ public function testPostAvatarNotPermittedException(): void { $file->expects($this->once()) ->method('getMimeType') ->willReturn('image/jpeg'); - $userFolder = $this->createMock(IUserFolder::class); - $this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder); - $userFolder->method('get')->willReturn($file); + + $this->userFolder->method('get')->willReturn($file); $expectedResponse = new Http\JSONResponse(['data' => ['message' => 'The selected file cannot be read.']], Http::STATUS_BAD_REQUEST); $this->assertEquals($expectedResponse, $this->avatarController->postAvatar('avatar.jpg')); diff --git a/tests/Core/Controller/PreviewControllerTest.php b/tests/Core/Controller/PreviewControllerTest.php index 19676af3256ad..ec5da44765966 100644 --- a/tests/Core/Controller/PreviewControllerTest.php +++ b/tests/Core/Controller/PreviewControllerTest.php @@ -12,7 +12,6 @@ use OCP\AppFramework\Http\DataResponse; use OCP\Files\File; use OCP\Files\Folder; -use OCP\Files\IRootFolder; use OCP\Files\IUserFolder; use OCP\Files\NotFoundException; use OCP\Files\SimpleFS\ISimpleFile; @@ -29,7 +28,7 @@ class PreviewControllerTest extends \Test\TestCase { private string $userId; private PreviewController $controller; - private IRootFolder&MockObject $rootFolder; + private IUserFolder&MockObject $userFolder; private IPreview&MockObject $previewManager; private IRequest&MockObject $request; @@ -37,8 +36,7 @@ class PreviewControllerTest extends \Test\TestCase { protected function setUp(): void { parent::setUp(); - $this->userId = 'user'; - $this->rootFolder = $this->createMock(IRootFolder::class); + $this->userFolder = $this->createMock(IUserFolder::class); $this->previewManager = $this->createMock(IPreview::class); $this->request = $this->createMock(IRequest::class); @@ -46,8 +44,7 @@ protected function setUp(): void { 'core', $this->request, $this->previewManager, - $this->rootFolder, - $this->userId, + $this->userFolder, $this->createMock(IMimeIconProvider::class) ); } @@ -74,12 +71,7 @@ public function testInvalidHeight(): void { } public function testFileNotFound(): void { - $userFolder = $this->createMock(IUserFolder::class); - $this->rootFolder->method('getUserFolder') - ->with($this->equalTo($this->userId)) - ->willReturn($userFolder); - - $userFolder->method('get') + $this->userFolder->method('get') ->with($this->equalTo('file')) ->willThrowException(new NotFoundException()); @@ -90,13 +82,8 @@ public function testFileNotFound(): void { } public function testNotAFile(): void { - $userFolder = $this->createMock(IUserFolder::class); - $this->rootFolder->method('getUserFolder') - ->with($this->equalTo($this->userId)) - ->willReturn($userFolder); - $folder = $this->createMock(Folder::class); - $userFolder->method('get') + $this->userFolder->method('get') ->with($this->equalTo('file')) ->willReturn($folder); @@ -107,13 +94,8 @@ public function testNotAFile(): void { } public function testNoPreviewAndNoIcon(): void { - $userFolder = $this->createMock(IUserFolder::class); - $this->rootFolder->method('getUserFolder') - ->with($this->equalTo($this->userId)) - ->willReturn($userFolder); - $file = $this->createMock(File::class); - $userFolder->method('get') + $this->userFolder->method('get') ->with($this->equalTo('file')) ->willReturn($file); @@ -128,13 +110,8 @@ public function testNoPreviewAndNoIcon(): void { } public function testNoPreview() { - $userFolder = $this->createMock(IUserFolder::class); - $this->rootFolder->method('getUserFolder') - ->with($this->equalTo($this->userId)) - ->willReturn($userFolder); - $file = $this->createMock(File::class); - $userFolder->method('get') + $this->userFolder->method('get') ->with($this->equalTo('file')) ->willReturn($file); @@ -159,13 +136,8 @@ public function testNoPreview() { $this->assertEquals($expected, $res); } public function testFileWithoutReadPermission() { - $userFolder = $this->createMock(IUserFolder::class); - $this->rootFolder->method('getUserFolder') - ->with($this->equalTo($this->userId)) - ->willReturn($userFolder); - $file = $this->createMock(File::class); - $userFolder->method('get') + $this->userFolder->method('get') ->with($this->equalTo('file')) ->willReturn($file); @@ -183,14 +155,9 @@ public function testFileWithoutReadPermission() { } public function testFileWithoutDownloadPermission() { - $userFolder = $this->createMock(IUserFolder::class); - $this->rootFolder->method('getUserFolder') - ->with($this->equalTo($this->userId)) - ->willReturn($userFolder); - $file = $this->createMock(File::class); $file->method('getId')->willReturn(123); - $userFolder->method('get') + $this->userFolder->method('get') ->with($this->equalTo('file')) ->willReturn($file); @@ -223,14 +190,9 @@ public function testFileWithoutDownloadPermission() { } public function testFileWithoutDownloadPermissionButHeader() { - $userFolder = $this->createMock(IUserFolder::class); - $this->rootFolder->method('getUserFolder') - ->with($this->equalTo($this->userId)) - ->willReturn($userFolder); - $file = $this->createMock(File::class); $file->method('getId')->willReturn(123); - $userFolder->method('get') + $this->userFolder->method('get') ->with($this->equalTo('file')) ->willReturn($file); @@ -276,14 +238,9 @@ public function testFileWithoutDownloadPermissionButHeader() { } public function testValidPreview(): void { - $userFolder = $this->createMock(IUserFolder::class); - $this->rootFolder->method('getUserFolder') - ->with($this->equalTo($this->userId)) - ->willReturn($userFolder); - $file = $this->createMock(File::class); $file->method('getId')->willReturn(123); - $userFolder->method('get') + $this->userFolder->method('get') ->with($this->equalTo('file')) ->willReturn($file); @@ -315,14 +272,9 @@ public function testValidPreview(): void { } public function testValidPreviewOfShare() { - $userFolder = $this->createMock(IUserFolder::class); - $this->rootFolder->method('getUserFolder') - ->with($this->equalTo($this->userId)) - ->willReturn($userFolder); - $file = $this->createMock(File::class); $file->method('getId')->willReturn(123); - $userFolder->method('get') + $this->userFolder->method('get') ->with($this->equalTo('file')) ->willReturn($file);