diff --git a/composer.lock b/composer.lock index 91bf5a9e2..b6a67bdc6 100644 --- a/composer.lock +++ b/composer.lock @@ -13,17 +13,17 @@ "source": { "type": "git", "url": "https://github.com/ChristophWurst/nextcloud_composer.git", - "reference": "6aec9c3521c554466bb7d92559782cf61f391c79" + "reference": "577103cb24552d134a6338014fe665cabfd9c2c8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/ChristophWurst/nextcloud_composer/zipball/6aec9c3521c554466bb7d92559782cf61f391c79", - "reference": "6aec9c3521c554466bb7d92559782cf61f391c79", + "url": "https://api.github.com/repos/ChristophWurst/nextcloud_composer/zipball/577103cb24552d134a6338014fe665cabfd9c2c8", + "reference": "577103cb24552d134a6338014fe665cabfd9c2c8", "shasum": "" }, "require": { "php": "^7.4 || ~8.0 || ~8.1", - "psr/container": "^1.0", + "psr/container": "^1.1.1", "psr/event-dispatcher": "^1.0", "psr/log": "^1.1" }, @@ -31,7 +31,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "24.0.0-dev" + "dev-master": "25.0.0-dev" } }, "notification-url": "https://packagist.org/downloads/", @@ -49,7 +49,7 @@ "issues": "https://github.com/ChristophWurst/nextcloud_composer/issues", "source": "https://github.com/ChristophWurst/nextcloud_composer/tree/master" }, - "time": "2022-02-07T08:09:24+00:00" + "time": "2022-08-09T02:21:50+00:00" }, { "name": "composer/pcre", diff --git a/lib/Controller/APIv2Controller.php b/lib/Controller/APIv2Controller.php index 0591b3528..b241a1079 100644 --- a/lib/Controller/APIv2Controller.php +++ b/lib/Controller/APIv2Controller.php @@ -24,7 +24,6 @@ namespace OCA\Activity\Controller; -use OC\Files\View; use OCA\Activity\Data; use OCA\Activity\Exception\InvalidFilterException; use OCA\Activity\GroupHelper; @@ -93,28 +92,9 @@ class APIv2Controller extends OCSController { /** @var IMimeTypeDetector */ protected $mimeTypeDetector; - /** @var View */ - protected $view; - /** @var ViewInfoCache */ protected $infoCache; - /** - * OCSEndPoint constructor. - * - * @param string $appName - * @param IRequest $request - * @param IManager $activityManager - * @param Data $data - * @param GroupHelper $helper - * @param UserSettings $settings - * @param IURLGenerator $urlGenerator - * @param IUserSession $userSession - * @param IPreview $preview - * @param IMimeTypeDetector $mimeTypeDetector - * @param View $view - * @param ViewInfoCache $infoCache - */ public function __construct($appName, IRequest $request, IManager $activityManager, @@ -125,7 +105,6 @@ public function __construct($appName, IUserSession $userSession, IPreview $preview, IMimeTypeDetector $mimeTypeDetector, - View $view, ViewInfoCache $infoCache) { parent::__construct($appName, $request); $this->activityManager = $activityManager; @@ -136,7 +115,6 @@ public function __construct($appName, $this->userSession = $userSession; $this->preview = $preview; $this->mimeTypeDetector = $mimeTypeDetector; - $this->view = $view; $this->infoCache = $infoCache; } @@ -357,7 +335,7 @@ protected function getPreview(string $owner, int $fileId, string $filePath): arr } $preview = [ - 'link' => $this->getPreviewLink($info['path'], $info['is_dir'], $info['view']), + 'link' => $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $fileId]), 'source' => '', 'mimeType' => 'application/octet-stream', 'isMimeTypeIcon' => true, @@ -371,8 +349,7 @@ protected function getPreview(string $owner, int $fileId, string $filePath): arr $preview['source'] = $this->getPreviewPathFromMimeType('dir'); $preview['mimeType'] = 'dir'; } else { - $this->view->chroot('/' . $owner . '/files'); - $fileInfo = $this->view->getFileInfo($info['path']); + $fileInfo = $info['node'] ?? null; if (!($fileInfo instanceof FileInfo)) { $preview = $this->getPreviewFromPath($fileId, $filePath, $info); } elseif ($this->preview->isAvailable($fileInfo)) { @@ -382,7 +359,7 @@ protected function getPreview(string $owner, int $fileId, string $filePath): arr 'x' => 250, 'y' => 250, 'fileId' => $fileId, - 'c' => $fileInfo->getEtag() + 'c' => $fileInfo->getEtag(), ]; $preview['source'] = $this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', $params); @@ -400,7 +377,7 @@ protected function getPreview(string $owner, int $fileId, string $filePath): arr protected function getPreviewFromPath(int $fileId, string $filePath, array $info): array { $mimeType = $info['is_dir'] ? 'dir' : $this->mimeTypeDetector->detectPath($filePath); return [ - 'link' => $this->getPreviewLink($info['path'], $info['is_dir'], $info['view']), + 'link' => $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $fileId]), 'source' => $this->getPreviewPathFromMimeType($mimeType), 'mimeType' => $mimeType, 'isMimeTypeIcon' => true, @@ -418,18 +395,4 @@ protected function getPreviewPathFromMimeType(string $mimeType): string { return $this->urlGenerator->getAbsoluteURL($mimeTypeIcon); } - - protected function getPreviewLink(string $path, bool $isDir, string $view): string { - $params = [ - 'dir' => $path, - ]; - if (!$isDir) { - $params['dir'] = (substr_count($path, '/') === 1) ? '/' : \dirname($path); - $params['scrollto'] = basename($path); - } - if ($view !== '') { - $params['view'] = $view; - } - return $this->urlGenerator->linkToRouteAbsolute('files.view.index', $params); - } } diff --git a/lib/ViewInfoCache.php b/lib/ViewInfoCache.php index 865f4fb15..cc7f9f23e 100644 --- a/lib/ViewInfoCache.php +++ b/lib/ViewInfoCache.php @@ -22,7 +22,9 @@ namespace OCA\Activity; -use OC\Files\View; +use OCP\Files\Folder; +use OCP\Files\IRootFolder; +use OCP\Files\Node; use OCP\Files\NotFoundException; class ViewInfoCache { @@ -32,27 +34,11 @@ class ViewInfoCache { /** @var array */ protected $cacheId; - /** @var \OC\Files\View */ - protected $view; + /** @var IRootFolder */ + protected $rootFolder; - /** - * @param View $view - */ - public function __construct(View $view) { - $this->view = $view; - } - - /** - * @param string $user - * @param string $path - * @return array - */ - public function getInfoByPath($user, $path) { - if (isset($this->cachePath[$user][$path])) { - return $this->cachePath[$user][$path]; - } - - return $this->findInfoByPath($user, $path); + public function __construct(IRootFolder $rootFolder) { + $this->rootFolder = $rootFolder; } /** @@ -73,26 +59,6 @@ public function getInfoById($user, $fileId, $path) { return $this->findInfoById($user, $fileId, $path); } - /** - * @param string $user - * @param string $path - * @return array - */ - protected function findInfoByPath($user, $path) { - $this->view->chroot('/' . $user . '/files'); - - $exists = $this->view->file_exists($path); - - $this->cachePath[$user][$path] = [ - 'path' => $path, - 'exists' => $exists, - 'is_dir' => $exists ? (bool)$this->view->is_dir($path) : false, - 'view' => '', - ]; - - return $this->cachePath[$user][$path]; - } - /** * @param string $user * @param int $fileId @@ -100,8 +66,6 @@ protected function findInfoByPath($user, $path) { * @return array */ protected function findInfoById($user, $fileId, $filePath) { - $this->view->chroot('/' . $user . '/files'); - $cache = [ 'path' => $filePath, 'exists' => false, @@ -111,24 +75,38 @@ protected function findInfoById($user, $fileId, $filePath) { $notFound = false; try { - $path = $this->view->getPath($fileId); + $userFolder = $this->rootFolder->getUserFolder($user); + $entries = $userFolder->getById($fileId); + if (empty($entries)) { + throw new NotFoundException('No entries returned'); + } + /** @var Node $entry */ + $entry = array_shift($entries); - $cache['path'] = $path; - $cache['is_dir'] = $this->view->is_dir($path); + $cache['path'] = $userFolder->getRelativePath($entry->getPath()); + $cache['is_dir'] = $entry instanceof Folder; $cache['exists'] = true; + $cache['node'] = $entry; } catch (NotFoundException $e) { - // The file was not found in the normal view, maybe it is in - // the trashbin? - $this->view->chroot('/' . $user . '/files_trashbin'); - + // The file was not found in the normal view, + // maybe it is in the trashbin? try { - $path = $this->view->getPath($fileId); + /** @var Folder $userTrashBin */ + $userTrashBin = $this->rootFolder->get('/' . $user . '/files_trashbin'); + $entries = $userTrashBin->getById($fileId); + if (empty($entries)) { + throw new NotFoundException('No entries returned'); + } + + /** @var Node $entry */ + $entry = array_shift($entries); $cache = [ - 'path' => substr($path, strlen('/files')), + 'path' => $userTrashBin->getRelativePath($entry->getPath()), 'exists' => true, - 'is_dir' => (bool)$this->view->is_dir($path), + 'is_dir' => $entry instanceof Folder, 'view' => 'trashbin', + 'node' => $entry, ]; } catch (NotFoundException $e) { $notFound = true; diff --git a/psalm.xml b/psalm.xml index 4b8a420a9..1d0019548 100644 --- a/psalm.xml +++ b/psalm.xml @@ -43,4 +43,7 @@ + + + diff --git a/tests/Controller/APIv2ControllerTest.php b/tests/Controller/APIv2ControllerTest.php index fac88ab60..90d9f7d24 100644 --- a/tests/Controller/APIv2ControllerTest.php +++ b/tests/Controller/APIv2ControllerTest.php @@ -33,10 +33,8 @@ use OCP\AppFramework\Http\DataResponse; use OCP\IL10N; use OCP\IUser; -use OCP\Files\FileInfo; use OCP\IRequest; use OCA\Activity\ViewInfoCache; -use OC\Files\View; use OCP\Files\IMimeTypeDetector; use OCP\IUserSession; use OCP\IURLGenerator; @@ -80,9 +78,6 @@ class APIv2ControllerTest extends TestCase { /** @var IMimeTypeDetector|MockObject */ protected $mimeTypeDetector; - /** @var View|MockObject */ - protected $view; - /** @var ViewInfoCache|MockObject */ protected $infoCache; @@ -103,7 +98,6 @@ protected function setUp(): void { $this->urlGenerator = $this->createMock(IURLGenerator::class); $this->userSession = $this->createMock(IUserSession::class); $this->mimeTypeDetector = $this->createMock(IMimeTypeDetector::class); - $this->view = $this->createMock(View::class); $this->infoCache = $this->createMock(ViewInfoCache::class); $this->request = $this->createMock(IRequest::class); @@ -127,7 +121,6 @@ protected function getController(array $methods = []): APIv2Controller { $this->userSession, $this->preview, $this->mimeTypeDetector, - $this->view, $this->infoCache ); } @@ -144,7 +137,6 @@ protected function getController(array $methods = []): APIv2Controller { $this->userSession, $this->preview, $this->mimeTypeDetector, - $this->view, $this->infoCache, ]) ->onlyMethods($methods) @@ -653,7 +645,7 @@ public function dataGetPreview(): array { return [ ['author', 42, '/path', '/currentPath', true, true, false, '/preview/dir', true, 'dir'], ['author', 42, '/file.txt', '/currentFile.txt', false, true, false, '/preview/mpeg', true, 'audio/mp3'], - ['author', 42, '/file.txt', '/currentFile.txt', false, true, true, '/preview/currentFile.txt', false, 'text/plain'], + ['author', 42, '/file.txt', '/currentFile.txt', false, true, true, 'core.Preview.getPreviewByFileId#42', false, 'text/plain'], ['author', 42, '/file.txt', '/currentFile.txt', false, false, true, 'source::getPreviewFromPath', true, 'text/plain'], ]; } @@ -674,26 +666,15 @@ public function dataGetPreview(): array { */ public function testGetPreview(string $author, int $fileId, string $path, string $returnedPath, bool $isDir, bool $validFileInfo, bool $isMimeSup, string $source, bool $isMimeTypeIcon, string $mimeType): void { $controller = $this->getController([ - 'getPreviewLink', 'getPreviewFromPath', 'getPreviewPathFromMimeType', ]); - $this->infoCache->expects($this->once()) - ->method('getInfoById') - ->with($author, $fileId, $path) - ->willReturn([ - 'path' => $returnedPath, - 'exists' => true, - 'is_dir' => $isDir, - 'view' => '', - ]); - - $controller->expects($this->once()) - ->method('getPreviewLink') - ->with($returnedPath, $isDir) - ->willReturnCallback(function ($path) { - return '/preview' . $path; + $node = $this->createMock(\OCP\Files\File::class); + $this->urlGenerator->expects($this->any()) + ->method('linkToRouteAbsolute') + ->willReturnCallback(function ($url, $params) { + return $url . '#' . ($params['fileid'] ?? $params['fileId']); }); if ($isDir) { @@ -702,23 +683,13 @@ public function testGetPreview(string $author, int $fileId, string $path, string ->with('dir') ->willReturn('/preview/dir'); } elseif ($validFileInfo) { - $fileInfo = $this->createMock(FileInfo::class); - - $this->view->expects($this->once()) - ->method('chroot') - ->with('/' . $author . '/files'); - $this->view->expects($this->once()) - ->method('getFileInfo') - ->with($returnedPath) - ->willReturn($fileInfo); - $this->preview->expects($this->once()) ->method('isAvailable') - ->with($fileInfo) + ->with($node) ->willReturn($isMimeSup); if (!$isMimeSup) { - $fileInfo->expects($this->atLeastOnce()) + $node->expects($this->atLeastOnce()) ->method('getMimetype') ->willReturn('audio/mp3'); @@ -727,31 +698,18 @@ public function testGetPreview(string $author, int $fileId, string $path, string ->with('audio/mp3') ->willReturn('/preview/mpeg'); } else { - $fileInfo->expects($this->atLeastOnce()) + $node->expects($this->atLeastOnce()) ->method('getMimetype') ->willReturn('text/plain'); - - $this->urlGenerator->expects($this->once()) - ->method('linkToRouteAbsolute') - ->with('core.Preview.getPreviewByFileId', $this->anything()) - ->willReturnCallback(function () use ($returnedPath) { - return '/preview' . $returnedPath; - }); } } else { - $this->view->expects($this->once()) - ->method('chroot') - ->with('/' . $author . '/files'); - $this->view->expects($this->once()) - ->method('getFileInfo') - ->with($returnedPath) - ->willReturn(false); + $node = null; $controller->expects($this->once()) ->method('getPreviewFromPath') ->with($fileId, $path, $this->anything()) ->willReturn([ - 'link' => '/preview' . $returnedPath, + 'link' => 'files.viewcontroller.showFile#' . $fileId, 'source' => 'source::getPreviewFromPath', 'mimeType' => $mimeType, 'isMimeTypeIcon' => $isMimeTypeIcon, @@ -761,8 +719,19 @@ public function testGetPreview(string $author, int $fileId, string $path, string ]); } + $this->infoCache->expects($this->once()) + ->method('getInfoById') + ->with($author, $fileId, $path) + ->willReturn([ + 'path' => $returnedPath, + 'exists' => true, + 'is_dir' => $isDir, + 'view' => '', + 'node' => $node, + ]); + $this->assertSame([ - 'link' => '/preview' . $returnedPath, + 'link' => 'files.viewcontroller.showFile#' . $fileId, 'source' => $source, 'mimeType' => $mimeType, 'isMimeTypeIcon' => $isMimeTypeIcon, @@ -791,24 +760,24 @@ public function dataGetPreviewFromPath(): array { public function testGetPreviewFromPath(int $fileId, string $filePath, string $mimeType, bool $isDir, string $view): void { $controller = $this->getController([ 'getPreviewPathFromMimeType', - 'getPreviewLink', ]); $controller->expects($this->once()) ->method('getPreviewPathFromMimeType') ->with($mimeType) ->willReturn('mime-type-icon'); - $controller->expects($this->once()) - ->method('getPreviewLink') - ->with($filePath, $isDir, $view) - ->willReturn('target-link'); + $this->urlGenerator->expects($this->any()) + ->method('linkToRouteAbsolute') + ->willReturnCallback(function ($url, $params) { + return $url . '#' . ($params['fileid'] ?? $params['fileId']); + }); $this->mimeTypeDetector->expects($isDir ? $this->never() : $this->once()) ->method('detectPath') ->willReturn($mimeType); $this->assertSame( [ - 'link' => 'target-link', + 'link' => 'files.viewcontroller.showFile#' . $fileId, 'source' => 'mime-type-icon', 'mimeType' => $mimeType, 'isMimeTypeIcon' => true, @@ -851,31 +820,4 @@ public function testGetPreviewPathFromMimeType(string $mimeType, string $icon, s self::invokePrivate($this->controller, 'getPreviewPathFromMimeType', [$mimeType]) ); } - - public function dataGetPreviewLink(): array { - return [ - ['/folder', true, '', ['dir' => '/folder']], - ['/folder/sub1', true, 'trashbin', ['dir' => '/folder/sub1', 'view' => 'trashbin']], - ['/folder/sub1/sub2', true, '', ['dir' => '/folder/sub1/sub2']], - ['/file.txt', false, '', ['dir' => '/', 'scrollto' => 'file.txt']], - ['/folder/file.txt', false, 'trashbin', ['dir' => '/folder', 'scrollto' => 'file.txt', 'view' => 'trashbin']], - ['/folder/sub1/file.txt', false, '', ['dir' => '/folder/sub1', 'scrollto' => 'file.txt']], - ]; - } - - /** - * @dataProvider dataGetPreviewLink - * - * @param string $path - * @param bool $isDir - * @param string $view - * @param array $expected - */ - public function testGetPreviewLink(string $path, bool $isDir, string $view, array $expected): void { - $this->urlGenerator->expects($this->once()) - ->method('linkToRouteAbsolute') - ->with('files.view.index', $expected); - - self::invokePrivate($this->controller, 'getPreviewLink', [$path, $isDir, $view]); - } } diff --git a/tests/FilesHooksTest.php b/tests/FilesHooksTest.php index 1a55e9810..1c91738a1 100755 --- a/tests/FilesHooksTest.php +++ b/tests/FilesHooksTest.php @@ -453,23 +453,25 @@ public function testAddNotificationsForFileAction(array $filterUsers, bool $moun return $filterUsers[$method]; }); - $i = 2; + $addCalls = []; foreach ($addNotifications as $user => $arguments) { - $filesHooks->expects($this->at($i)) - ->method('addNotificationsForUser') - ->with( - $user, - $arguments['subject'], - $arguments['subject_params'], - 1337, - $arguments['path'], - true, - $arguments['email'], - $arguments['notification'], - Files::TYPE_SHARE_RESTORED - ); - $i++; + $addCalls[] = [ + $user, + $arguments['subject'], + $arguments['subject_params'], + 1337, + $arguments['path'], + true, + $arguments['email'], + $arguments['notification'], + Files::TYPE_SHARE_RESTORED, + ]; } + $filesHooks->expects($this->any()) + ->method('addNotificationsForUser') + ->withConsecutive( + ...$addCalls + ); self::invokePrivate($filesHooks, 'addNotificationsForFileAction', ['path', Files::TYPE_SHARE_RESTORED, 'restored_self', 'restored_by']); } @@ -676,12 +678,10 @@ public function testShareWithGroup(array $usersInGroup, int $settingCalls, int $ ]); $group = $this->createMock(IGroup::class); - for ($i = 0, $iMax = count($usersInGroup); $i < $iMax; $i++) { - $group->expects($this->at($i)) - ->method('searchUsers') - ->with('') - ->willReturn($usersInGroup[$i]); - } + $group->expects($this->any()) + ->method('searchUsers') + ->with('') + ->willReturnOnConsecutiveCalls(...$usersInGroup); $this->groupManager->expects($this->once()) ->method('get') @@ -705,23 +705,25 @@ public function testShareWithGroup(array $usersInGroup, int $settingCalls, int $ ->with('user', 'reshared_group_by', 'group1', 42, 'file') ->willReturnArgument(0); - $i = 3; + $addCalls = []; foreach ($addNotifications as $user => $arguments) { - $filesHooks->expects($this->at($i)) - ->method('addNotificationsForUser') - ->with( - $user, - $arguments['subject'], - $arguments['subject_params'], - 42, - $arguments['path'], - true, - $arguments['email'], - $arguments['notification'], - Files_Sharing::TYPE_SHARED - ); - $i++; + $addCalls[] = [ + $user, + $arguments['subject'], + $arguments['subject_params'], + 42, + $arguments['path'], + true, + $arguments['email'], + $arguments['notification'], + Files_Sharing::TYPE_SHARED, + ]; } + $filesHooks->expects($this->any()) + ->method('addNotificationsForUser') + ->withConsecutive( + ...$addCalls + ); self::invokePrivate($filesHooks, 'shareWithGroup', [ 'group1', 42, 'file', '/file', 1337, true diff --git a/tests/ViewInfoCacheTest.php b/tests/ViewInfoCacheTest.php index 2b002c497..5fae6d17f 100644 --- a/tests/ViewInfoCacheTest.php +++ b/tests/ViewInfoCacheTest.php @@ -22,14 +22,16 @@ namespace OCA\Activity\Tests; -use OC\Files\View; use OCA\Activity\ViewInfoCache; +use OCP\Files\File; +use OCP\Files\Folder; +use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; use PHPUnit\Framework\MockObject\MockObject; class ViewInfoCacheTest extends TestCase { - /** @var View|MockObject */ - protected $view; + /** @var IRootFolder|MockObject */ + protected $rootFolder; /** @var ViewInfoCache|MockObject */ protected $infoCache; @@ -37,7 +39,7 @@ class ViewInfoCacheTest extends TestCase { protected function setUp(): void { parent::setUp(); - $this->view = $this->createMock(View::class); + $this->rootFolder = $this->createMock(IRootFolder::class); } /** @@ -47,86 +49,18 @@ protected function setUp(): void { public function getCache(array $methods = []): ViewInfoCache { if (empty($methods)) { return new ViewInfoCache( - $this->view + $this->rootFolder ); } return $this->getMockBuilder(ViewInfoCache::class) ->setConstructorArgs([ - $this->view, + $this->rootFolder, ]) - ->setMethods($methods) + ->onlyMethods($methods) ->getMock(); } - public function dataGetInfoByPath(): array { - return [ - [ - 'user', 'path', [], true, 'findInfoByPath', - ], - [ - 'user', - 'path', - [ - 'user' => [ - 'different/path' => 'returnCache', - ] - ], - true, - 'findInfoByPath', - ], - [ - 'user', - 'path', - [ - 'different-user' => [ - 'path' => 'returnCache', - ] - ], - true, - 'findInfoByPath', - ], - [ - 'user', - 'path', - [ - 'user' => [ - 'path' => 'returnCache', - ] - ], - false, - 'returnCache', - ], - ]; - } - - /** - * @dataProvider dataGetInfoByPath - * - * @param string $user - * @param string $path - * @param array $cache - * @param bool $callsFind - * @param string $expected - */ - public function testGetInfoByPath(string $user, string $path, array $cache, bool $callsFind, string $expected): void { - $infoCache = $this->getCache([ - 'findInfoByPath', - ]); - - if ($callsFind) { - $infoCache->expects($this->once()) - ->method('findInfoByPath') - ->willReturn('findInfoByPath'); - } else { - $infoCache->expects($this->never()) - ->method('findInfoByPath'); - } - self::invokePrivate($infoCache, 'cachePath', [$cache]); - - $this->assertSame($expected, $infoCache->getInfoByPath($user, $path)); - } - public function dataGetInfoById(): array { return [ [ @@ -211,56 +145,6 @@ public function testGetInfoById(string $user, int $id, string $path, array $cach $this->assertSame($expected, $infoCache->getInfoById($user, $id, $path)); } - public function dataFindByPath(): array { - return [ - ['user1', 'path1', true, true, [ - 'path' => 'path1', - 'exists' => true, - 'is_dir' => true, - 'view' => '', - ]], - ['user2', 'path2', true, false, [ - 'path' => 'path2', - 'exists' => true, - 'is_dir' => false, - 'view' => '', - ]], - ['user3', 'path3', false, null, [ - 'path' => 'path3', - 'exists' => false, - 'is_dir' => false, - 'view' => '', - ]], - ]; - } - - /** - * @dataProvider dataFindByPath - * - * @param string $user - * @param string $path - * @param bool $exists - * @param bool $is_dir - * @param array $expected - */ - public function testFindByPath(string $user, string $path, bool $exists, ?bool $is_dir, array $expected): void { - $this->view->expects($this->once()) - ->method('chroot') - ->with('/' . $user . '/files'); - $this->view->expects($this->once()) - ->method('file_exists') - ->with($path) - ->willReturn($exists); - $this->view->expects($is_dir !== null ? $this->once() : $this->never()) - ->method('is_dir') - ->with($path) - ->willReturn($is_dir); - - $infoCache = $this->getCache(); - - $this->assertSame($expected, self::invokePrivate($infoCache, 'findInfoByPath', [$user, $path])); - } - public function dataFindInfoById(): array { return [ [ @@ -283,7 +167,7 @@ public function dataFindInfoById(): array { ], ], [ - 'user2', 23, '/test1', null, '/files/test3', '/files/test3', false, + 'user2', 23, '/test1', null, '/test3', '/test3', false, [ 'path' => '/test3', 'exists' => true, @@ -302,7 +186,7 @@ public function dataFindInfoById(): array { ], ], [ - 'user3', 23, '/test1', null, '/files/test3', '/files/test3', true, + 'user3', 23, '/test1', null, '/test3', '/test3', true, [ 'path' => '/test3', 'exists' => true, @@ -375,40 +259,62 @@ public function dataFindInfoById(): array { * @param array $expectedCache */ public function testFindInfoById(string $user, int $fileId, string $filename, ?string $path, ?string $pathTrash, string $isDirPath, bool $isDir, array $expected, array $expectedCache): void { - $this->view->expects($this->at(0)) - ->method('chroot') - ->with('/' . $user . '/files'); + $userFolder = $this->createMock(Folder::class); + + $this->rootFolder->expects($this->any()) + ->method('getUserFolder') + ->with($user) + ->willReturn($userFolder); if ($path === null) { - $this->view->expects($this->at(1)) - ->method('getPath') + $userFolder->expects($this->once()) + ->method('getById') ->with($fileId) ->willThrowException(new NotFoundException()); - $this->view->expects($this->at(2)) - ->method('chroot') - ->with('/' . $user . '/files_trashbin'); + $userTrashBin = $this->createMock(Folder::class); + $this->rootFolder->expects($this->once()) + ->method('get') + ->with('/' . $user . '/files_trashbin') + ->willReturn($userTrashBin); if ($pathTrash === null) { - $this->view->expects($this->at(3)) - ->method('getPath') + $userTrashBin->expects($this->once()) + ->method('getById') ->with($fileId) ->willThrowException(new NotFoundException()); } else { - $this->view->expects($this->at(3)) + $node = $this->createMock($isDir ? Folder::class : File::class); + $node->expects($this->any()) ->method('getPath') - ->with($fileId) + ->willReturn('/' . $user . '/files_trashbin' . $pathTrash); + $userTrashBin->expects($this->any()) + ->method('getRelativePath') + ->with('/' . $user . '/files_trashbin' . $pathTrash) ->willReturn($pathTrash); + + $userTrashBin->expects($this->once()) + ->method('getById') + ->with($fileId) + ->willReturn([2 => $node]); + $expected['node'] = $node; + $expectedCache[$user][$fileId]['node'] = $node; } } else { - $this->view->expects($this->at(1)) + $node = $this->createMock($isDir ? Folder::class : File::class); + $node->expects($this->any()) ->method('getPath') - ->with($fileId) + ->willReturn('/' . $user . '/files' . $path); + $userFolder->expects($this->any()) + ->method('getRelativePath') + ->with('/' . $user . '/files' . $path) ->willReturn($path); - } - $this->view->expects(($path === null && $pathTrash === null) ? $this->never() : $this->once()) - ->method('is_dir') - ->with($isDirPath) - ->willReturn($isDir); + $userFolder->expects($this->once()) + ->method('getById') + ->with($fileId) + ->willReturn([3 => $node]); + $expected['node'] = $node; + $expectedCache[$user][$fileId]['node'] = $node; + } $infoCache = $this->getCache(); $this->assertSame($expected, self::invokePrivate($infoCache, 'findInfoById', [$user, $fileId, $filename])); diff --git a/tests/stubs/oc_hooks_emitter.php b/tests/stubs/oc_hooks_emitter.php new file mode 100644 index 000000000..b4ad2730a --- /dev/null +++ b/tests/stubs/oc_hooks_emitter.php @@ -0,0 +1,23 @@ +