From e7a7b4a40184dc3da2c83e858c820625f660e48e Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 9 Feb 2024 09:54:52 +0100 Subject: [PATCH] perf: switch places that always use the first getById result to getFirstNodeById Signed-off-by: Robin Appelman --- apps/dav/lib/BulkUpload/BulkUploadPlugin.php | 2 +- .../lib/Connector/Sabre/FilesReportPlugin.php | 6 +- apps/dav/lib/Controller/DirectController.php | 5 +- apps/dav/lib/Direct/DirectFile.php | 9 +- apps/dav/lib/RootCollection.php | 3 +- .../SystemTagsRelationsCollection.php | 13 +- .../Connector/Sabre/FilesReportPluginTest.php | 12 +- .../unit/Controller/DirectControllerTest.php | 8 +- apps/dav/tests/unit/Direct/DirectFileTest.php | 4 +- .../SystemTagsObjectTypeCollectionTest.php | 32 ++--- .../lib/FederatedShareProvider.php | 8 +- apps/files/lib/Activity/Helper.php | 5 +- apps/files/lib/Activity/Provider.php | 6 +- .../lib/BackgroundJob/TransferOwnership.php | 6 +- .../Resources/ResourceProvider.php | 12 +- apps/files/lib/Controller/ViewController.php | 17 +-- .../lib/Listener/SyncLivePhotosListener.php | 6 +- .../tests/Controller/ViewControllerTest.php | 10 +- .../lib/Model/RichReminder.php | 5 +- .../lib/Notification/Notifier.php | 5 +- .../lib/Service/ReminderService.php | 4 +- .../Collaboration/ShareRecipientSorter.php | 6 +- .../Controller/DeletedShareAPIController.php | 6 +- .../lib/Controller/ShareAPIController.php | 30 ++--- .../lib/Controller/ShareController.php | 3 +- apps/files_sharing/lib/OrphanHelper.php | 4 +- apps/files_sharing/lib/SharedStorage.php | 4 +- .../ShareRecipientSorterTest.php | 10 +- .../Controller/ShareAPIControllerTest.php | 124 ++++++++++-------- .../tests/Controller/ShareControllerTest.php | 47 ++++--- .../files_sharing/tests/SharedStorageTest.php | 1 + .../lib/Trash/LegacyTrashBackend.php | 7 +- apps/files_versions/lib/Sabre/VersionRoot.php | 6 +- apps/files_versions/lib/Storage.php | 6 +- .../lib/Versions/LegacyVersionsBackend.php | 3 +- core/Command/FilesMetadata/Get.php | 6 +- core/Command/Info/FileUtils.php | 6 +- core/Command/Preview/Generate.php | 6 +- core/Controller/PreviewController.php | 6 +- .../Reference/File/FileReferenceProvider.php | 8 +- lib/private/DirectEditing/Manager.php | 6 +- lib/private/Files/Config/CachedMountInfo.php | 7 +- lib/private/Files/Node/Folder.php | 2 +- .../Job/UpdateSingleMetadata.php | 7 +- lib/private/Share20/Manager.php | 6 +- lib/private/Share20/Share.php | 14 +- lib/private/SpeechToText/TranscriptionJob.php | 2 +- lib/public/Share/IShare.php | 2 +- .../lib/Share20/DefaultShareProviderTest.php | 104 +++++++-------- tests/lib/Share20/ManagerTest.php | 12 +- 50 files changed, 316 insertions(+), 313 deletions(-) diff --git a/apps/dav/lib/BulkUpload/BulkUploadPlugin.php b/apps/dav/lib/BulkUpload/BulkUploadPlugin.php index 4d838d255eb..9890a615f93 100644 --- a/apps/dav/lib/BulkUpload/BulkUploadPlugin.php +++ b/apps/dav/lib/BulkUpload/BulkUploadPlugin.php @@ -91,7 +91,7 @@ class BulkUploadPlugin extends ServerPlugin { $node = $this->userFolder->newFile($headers['x-file-path'], $content); $node->touch($mtime); - $node = $this->userFolder->getById($node->getId())[0]; + $node = $this->userFolder->getFirstNodeById($node->getId()); $writtenFiles[$headers['x-file-path']] = [ "error" => false, diff --git a/apps/dav/lib/Connector/Sabre/FilesReportPlugin.php b/apps/dav/lib/Connector/Sabre/FilesReportPlugin.php index e43fb740cac..6a8cb3f0f59 100644 --- a/apps/dav/lib/Connector/Sabre/FilesReportPlugin.php +++ b/apps/dav/lib/Connector/Sabre/FilesReportPlugin.php @@ -424,14 +424,14 @@ class FilesReportPlugin extends ServerPlugin { } $folder = $this->userFolder; if (trim($rootNode->getPath(), '/') !== '') { + /** @var Folder $folder */ $folder = $folder->get($rootNode->getPath()); } $results = []; foreach ($fileIds as $fileId) { - $entry = $folder->getById($fileId); + $entry = $folder->getFirstNodeById($fileId); if ($entry) { - $entry = current($entry); $results[] = $this->wrapNode($entry); } } @@ -439,7 +439,7 @@ class FilesReportPlugin extends ServerPlugin { return $results; } - protected function wrapNode(\OCP\Files\File|\OCP\Files\Folder $node): File|Directory { + protected function wrapNode(\OCP\Files\Node $node): File|Directory { if ($node instanceof \OCP\Files\File) { return new File($this->fileView, $node); } else { diff --git a/apps/dav/lib/Controller/DirectController.php b/apps/dav/lib/Controller/DirectController.php index 0f618e46b22..2d4522c8d37 100644 --- a/apps/dav/lib/Controller/DirectController.php +++ b/apps/dav/lib/Controller/DirectController.php @@ -104,9 +104,9 @@ class DirectController extends OCSController { public function getUrl(int $fileId, int $expirationTime = 60 * 60 * 8): DataResponse { $userFolder = $this->rootFolder->getUserFolder($this->userId); - $files = $userFolder->getById($fileId); + $file = $userFolder->getFirstNodeById($fileId); - if ($files === []) { + if (!$file) { throw new OCSNotFoundException(); } @@ -114,7 +114,6 @@ class DirectController extends OCSController { throw new OCSBadRequestException('Expiration time should be greater than 0 and less than or equal to ' . (60 * 60 * 24)); } - $file = array_shift($files); if (!($file instanceof File)) { throw new OCSBadRequestException('Direct download only works for files'); } diff --git a/apps/dav/lib/Direct/DirectFile.php b/apps/dav/lib/Direct/DirectFile.php index 45c2114747e..b9d1757cfc2 100644 --- a/apps/dav/lib/Direct/DirectFile.php +++ b/apps/dav/lib/Direct/DirectFile.php @@ -108,13 +108,16 @@ class DirectFile implements IFile { private function getFile() { if ($this->file === null) { $userFolder = $this->rootFolder->getUserFolder($this->direct->getUserId()); - $files = $userFolder->getById($this->direct->getFileId()); + $file = $userFolder->getFirstNodeById($this->direct->getFileId()); - if ($files === []) { + if (!$file) { throw new NotFound(); } + if (!$file instanceof File) { + throw new Forbidden("direct download not allowed on directories"); + } - $this->file = array_shift($files); + $this->file = $file; } return $this->file; diff --git a/apps/dav/lib/RootCollection.php b/apps/dav/lib/RootCollection.php index 0e9af0a4276..d1f3dbc91bd 100644 --- a/apps/dav/lib/RootCollection.php +++ b/apps/dav/lib/RootCollection.php @@ -134,7 +134,8 @@ class RootCollection extends SimpleCollection { \OC::$server->getSystemTagObjectMapper(), \OC::$server->getUserSession(), $groupManager, - $dispatcher + $dispatcher, + $rootFolder, ); $systemTagInUseCollection = \OCP\Server::get(SystemTag\SystemTagsInUseCollection::class); $commentsCollection = new Comments\RootCollection( diff --git a/apps/dav/lib/SystemTag/SystemTagsRelationsCollection.php b/apps/dav/lib/SystemTag/SystemTagsRelationsCollection.php index 6c44713cb05..6911d40e208 100644 --- a/apps/dav/lib/SystemTag/SystemTagsRelationsCollection.php +++ b/apps/dav/lib/SystemTag/SystemTagsRelationsCollection.php @@ -27,6 +27,7 @@ namespace OCA\DAV\SystemTag; use OCP\EventDispatcher\IEventDispatcher; +use OCP\Files\IRootFolder; use OCP\IGroupManager; use OCP\IUserSession; use OCP\SystemTag\ISystemTagManager; @@ -42,6 +43,7 @@ class SystemTagsRelationsCollection extends SimpleCollection { IUserSession $userSession, IGroupManager $groupManager, IEventDispatcher $dispatcher, + IRootFolder $rootFolder, ) { $children = [ new SystemTagsObjectTypeCollection( @@ -50,9 +52,14 @@ class SystemTagsRelationsCollection extends SimpleCollection { $tagMapper, $userSession, $groupManager, - function ($name) { - $nodes = \OC::$server->getUserFolder()->getById((int)$name); - return !empty($nodes); + function (string $name) use ($rootFolder, $userSession): bool { + $user = $userSession->getUser(); + if ($user) { + $node = $rootFolder->getUserFolder($user->getUID())->getFirstNodeById((int)$name); + return $node !== null; + } else { + return false; + } } ), ]; diff --git a/apps/dav/tests/unit/Connector/Sabre/FilesReportPluginTest.php b/apps/dav/tests/unit/Connector/Sabre/FilesReportPluginTest.php index c636d16358d..d60d1123de9 100644 --- a/apps/dav/tests/unit/Connector/Sabre/FilesReportPluginTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/FilesReportPluginTest.php @@ -320,14 +320,14 @@ class FilesReportPluginTest extends \Test\TestCase { ->willReturn('/'); $this->userFolder->expects($this->exactly(2)) - ->method('getById') + ->method('getFirstNodeById') ->withConsecutive( ['111'], ['222'], ) ->willReturnOnConsecutiveCalls( - [$filesNode1], - [$filesNode2], + $filesNode1, + $filesNode2, ); /** @var \OCA\DAV\Connector\Sabre\Directory|MockObject $reportTargetNode */ @@ -373,14 +373,14 @@ class FilesReportPluginTest extends \Test\TestCase { ->willReturn($subNode); $subNode->expects($this->exactly(2)) - ->method('getById') + ->method('getFirstNodeById') ->withConsecutive( ['111'], ['222'], ) ->willReturnOnConsecutiveCalls( - [$filesNode1], - [$filesNode2], + $filesNode1, + $filesNode2, ); /** @var \OCA\DAV\Connector\Sabre\Directory|MockObject $reportTargetNode */ diff --git a/apps/dav/tests/unit/Controller/DirectControllerTest.php b/apps/dav/tests/unit/Controller/DirectControllerTest.php index fe6d4ea8f24..181b02bda2c 100644 --- a/apps/dav/tests/unit/Controller/DirectControllerTest.php +++ b/apps/dav/tests/unit/Controller/DirectControllerTest.php @@ -110,9 +110,9 @@ class DirectControllerTest extends TestCase { $folder = $this->createMock(Folder::class); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with(101) - ->willReturn([$folder]); + ->willReturn($folder); $this->expectException(OCSBadRequestException::class); $this->controller->getUrl(101); @@ -129,9 +129,9 @@ class DirectControllerTest extends TestCase { $this->timeFactory->method('getTime') ->willReturn(42); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with(101) - ->willReturn([$file]); + ->willReturn($file); $userFolder->method('getRelativePath') ->willReturn('/path'); diff --git a/apps/dav/tests/unit/Direct/DirectFileTest.php b/apps/dav/tests/unit/Direct/DirectFileTest.php index fdf7fa54923..bc8b3822532 100644 --- a/apps/dav/tests/unit/Direct/DirectFileTest.php +++ b/apps/dav/tests/unit/Direct/DirectFileTest.php @@ -73,9 +73,9 @@ class DirectFileTest extends TestCase { ->willReturn($this->userFolder); $this->file = $this->createMock(File::class); - $this->userFolder->method('getById') + $this->userFolder->method('getFirstNodeById') ->with(42) - ->willReturn([$this->file]); + ->willReturn($this->file); $this->eventDispatcher = $this->createMock(IEventDispatcher::class); diff --git a/apps/dav/tests/unit/SystemTag/SystemTagsObjectTypeCollectionTest.php b/apps/dav/tests/unit/SystemTag/SystemTagsObjectTypeCollectionTest.php index 4dd81615c01..143d598fd2d 100644 --- a/apps/dav/tests/unit/SystemTag/SystemTagsObjectTypeCollectionTest.php +++ b/apps/dav/tests/unit/SystemTag/SystemTagsObjectTypeCollectionTest.php @@ -84,8 +84,8 @@ class SystemTagsObjectTypeCollectionTest extends \Test\TestCase { $userFolder = $this->userFolder; $closure = function ($name) use ($userFolder) { - $nodes = $userFolder->getById(intval($name)); - return !empty($nodes); + $node = $userFolder->getFirstNodeById(intval($name)); + return $node !== null; }; $this->node = new \OCA\DAV\SystemTag\SystemTagsObjectTypeCollection( @@ -98,14 +98,14 @@ class SystemTagsObjectTypeCollectionTest extends \Test\TestCase { ); } - + public function testForbiddenCreateFile(): void { $this->expectException(\Sabre\DAV\Exception\Forbidden::class); $this->node->createFile('555'); } - + public function testForbiddenCreateDirectory(): void { $this->expectException(\Sabre\DAV\Exception\Forbidden::class); @@ -114,27 +114,27 @@ class SystemTagsObjectTypeCollectionTest extends \Test\TestCase { public function testGetChild(): void { $this->userFolder->expects($this->once()) - ->method('getById') + ->method('getFirstNodeById') ->with('555') - ->willReturn([true]); + ->willReturn($this->createMock(\OCP\Files\Node::class)); $childNode = $this->node->getChild('555'); $this->assertInstanceOf('\OCA\DAV\SystemTag\SystemTagsObjectMappingCollection', $childNode); $this->assertEquals('555', $childNode->getName()); } - + public function testGetChildWithoutAccess(): void { $this->expectException(\Sabre\DAV\Exception\NotFound::class); $this->userFolder->expects($this->once()) - ->method('getById') + ->method('getFirstNodeById') ->with('555') - ->willReturn([]); + ->willReturn(null); $this->node->getChild('555'); } - + public function testGetChildren(): void { $this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class); @@ -143,28 +143,28 @@ class SystemTagsObjectTypeCollectionTest extends \Test\TestCase { public function testChildExists(): void { $this->userFolder->expects($this->once()) - ->method('getById') + ->method('getFirstNodeById') ->with('123') - ->willReturn([true]); + ->willReturn($this->createMock(\OCP\Files\Node::class)); $this->assertTrue($this->node->childExists('123')); } public function testChildExistsWithoutAccess(): void { $this->userFolder->expects($this->once()) - ->method('getById') + ->method('getFirstNodeById') ->with('555') - ->willReturn([]); + ->willReturn(null); $this->assertFalse($this->node->childExists('555')); } - + public function testDelete(): void { $this->expectException(\Sabre\DAV\Exception\Forbidden::class); $this->node->delete(); } - + public function testSetName(): void { $this->expectException(\Sabre\DAV\Exception\Forbidden::class); diff --git a/apps/federatedfilesharing/lib/FederatedShareProvider.php b/apps/federatedfilesharing/lib/FederatedShareProvider.php index 69813289cae..45efd0c7c8a 100644 --- a/apps/federatedfilesharing/lib/FederatedShareProvider.php +++ b/apps/federatedfilesharing/lib/FederatedShareProvider.php @@ -880,7 +880,7 @@ class FederatedShareProvider implements IShareProvider { * * @param string $userId * @param int $id - * @return \OCP\Files\File|\OCP\Files\Folder + * @return \OCP\Files\Node * @throws InvalidShare */ private function getNode($userId, $id) { @@ -890,13 +890,13 @@ class FederatedShareProvider implements IShareProvider { throw new InvalidShare(); } - $nodes = $userFolder->getById($id); + $node = $userFolder->getFirstNodeById($id); - if (empty($nodes)) { + if (!$node) { throw new InvalidShare(); } - return $nodes[0]; + return $node; } /** diff --git a/apps/files/lib/Activity/Helper.php b/apps/files/lib/Activity/Helper.php index 7bbaf44ab4c..9ad790f5154 100644 --- a/apps/files/lib/Activity/Helper.php +++ b/apps/files/lib/Activity/Helper.php @@ -61,9 +61,8 @@ class Helper { $userFolder = $this->rootFolder->getUserFolder($user); $favoriteNodes = []; foreach ($favorites as $favorite) { - $nodes = $userFolder->getById($favorite); - if (!empty($nodes)) { - $node = array_shift($nodes); + $node = $userFolder->getFirstNodeById($favorite); + if ($node) { if (!$foldersOnly || $node instanceof Folder) { $favoriteNodes[] = $node; } diff --git a/apps/files/lib/Activity/Provider.php b/apps/files/lib/Activity/Provider.php index fb81e200688..50535cab5c6 100644 --- a/apps/files/lib/Activity/Provider.php +++ b/apps/files/lib/Activity/Provider.php @@ -433,8 +433,8 @@ class Provider implements IProvider { } $userFolder = $this->rootFolder->getUserFolder($this->activityManager->getCurrentUserId()); - $files = $userFolder->getById($fileId); - if (empty($files)) { + $file = $userFolder->getFirstNodeById($fileId); + if (!$file) { try { // Deleted, try with parent $file = $this->findExistingParent($userFolder, dirname($path)); @@ -450,8 +450,6 @@ class Provider implements IProvider { return $file; } - $file = array_shift($files); - if ($file instanceof Folder && $file->isEncrypted()) { // If the folder is encrypted, it is the Container, // but can be the name is just fine. diff --git a/apps/files/lib/BackgroundJob/TransferOwnership.php b/apps/files/lib/BackgroundJob/TransferOwnership.php index 1f182b5e999..5cf35396d43 100644 --- a/apps/files/lib/BackgroundJob/TransferOwnership.php +++ b/apps/files/lib/BackgroundJob/TransferOwnership.php @@ -61,14 +61,14 @@ class TransferOwnership extends QueuedJob { $fileId = $transfer->getFileId(); $userFolder = $this->rootFolder->getUserFolder($sourceUser); - $nodes = $userFolder->getById($fileId); + $node = $userFolder->getFirstNodeById($fileId); - if (empty($nodes)) { + if (!$node) { $this->logger->alert('Could not transfer ownership: Node not found'); $this->failedNotication($transfer); return; } - $path = $userFolder->getRelativePath($nodes[0]->getPath()); + $path = $userFolder->getRelativePath($node->getPath()); $sourceUserObject = $this->userManager->get($sourceUser); $destinationUserObject = $this->userManager->get($destinationUser); diff --git a/apps/files/lib/Collaboration/Resources/ResourceProvider.php b/apps/files/lib/Collaboration/Resources/ResourceProvider.php index 4c5afc76b2b..a15e1558057 100644 --- a/apps/files/lib/Collaboration/Resources/ResourceProvider.php +++ b/apps/files/lib/Collaboration/Resources/ResourceProvider.php @@ -60,9 +60,9 @@ class ResourceProvider implements IProvider { if (isset($this->nodes[(int) $resource->getId()])) { return $this->nodes[(int) $resource->getId()]; } - $nodes = $this->rootFolder->getById((int) $resource->getId()); - if (!empty($nodes)) { - $this->nodes[(int) $resource->getId()] = array_shift($nodes); + $node = $this->rootFolder->getFirstNodeById((int) $resource->getId()); + if ($node) { + $this->nodes[(int) $resource->getId()] = $node; return $this->nodes[(int) $resource->getId()]; } return null; @@ -113,10 +113,10 @@ class ResourceProvider implements IProvider { } $userFolder = $this->rootFolder->getUserFolder($user->getUID()); - $nodes = $userFolder->getById((int) $resource->getId()); + $node = $userFolder->getById((int) $resource->getId()); - if (!empty($nodes)) { - $this->nodes[(int) $resource->getId()] = array_shift($nodes); + if ($node) { + $this->nodes[(int) $resource->getId()] = $node; return true; } diff --git a/apps/files/lib/Controller/ViewController.php b/apps/files/lib/Controller/ViewController.php index e7398cc21c2..12de4e1f6a2 100644 --- a/apps/files/lib/Controller/ViewController.php +++ b/apps/files/lib/Controller/ViewController.php @@ -302,8 +302,7 @@ class ViewController extends Controller { $uid = $user->getUID(); $userFolder = $this->rootFolder->getUserFolder($uid); - $nodes = $userFolder->getById((int) $fileid); - $node = array_shift($nodes); + $node = $userFolder->getFirstNodeById((int) $fileid); if ($node === null) { return; @@ -343,17 +342,16 @@ class ViewController extends Controller { private function redirectToFileIfInTrashbin($fileId): RedirectResponse { $uid = $this->userSession->getUser()->getUID(); $baseFolder = $this->rootFolder->getUserFolder($uid); - $nodes = $baseFolder->getById($fileId); + $node = $baseFolder->getFirstNodeById($fileId); $params = []; - if (empty($nodes) && $this->appManager->isEnabledForUser('files_trashbin')) { + if (!$node && $this->appManager->isEnabledForUser('files_trashbin')) { /** @var Folder */ $baseFolder = $this->rootFolder->get($uid . '/files_trashbin/files/'); - $nodes = $baseFolder->getById($fileId); + $node = $baseFolder->getFirstNodeById($fileId); $params['view'] = 'trashbin'; - if (!empty($nodes)) { - $node = current($nodes); + if ($node) { $params['fileid'] = $fileId; if ($node instanceof Folder) { // set the full path to enter the folder @@ -378,7 +376,7 @@ class ViewController extends Controller { private function redirectToFile(int $fileId) { $uid = $this->userSession->getUser()->getUID(); $baseFolder = $this->rootFolder->getUserFolder($uid); - $nodes = $baseFolder->getById($fileId); + $node = $baseFolder->getFirstNodeById($fileId); $params = ['view' => 'files']; try { @@ -386,8 +384,7 @@ class ViewController extends Controller { } catch (NotFoundException $e) { } - if (!empty($nodes)) { - $node = current($nodes); + if ($node) { $params['fileid'] = $fileId; if ($node instanceof Folder) { // set the full path to enter the folder diff --git a/apps/files/lib/Listener/SyncLivePhotosListener.php b/apps/files/lib/Listener/SyncLivePhotosListener.php index b188ad24073..ea63c08f94d 100644 --- a/apps/files/lib/Listener/SyncLivePhotosListener.php +++ b/apps/files/lib/Listener/SyncLivePhotosListener.php @@ -233,9 +233,9 @@ class SyncLivePhotosListener implements IEventListener { $peerFileId = (int)$metadata->getString('files-live-photo'); // Check the user's folder. - $nodes = $this->userFolder->getById($peerFileId); - if (count($nodes) !== 0) { - return $nodes[0]; + $node = $this->userFolder->getFirstNodeById($peerFileId); + if ($node) { + return $node; } // Check the user's trashbin. diff --git a/apps/files/tests/Controller/ViewControllerTest.php b/apps/files/tests/Controller/ViewControllerTest.php index 0b71011f60c..2714a6b25c0 100644 --- a/apps/files/tests/Controller/ViewControllerTest.php +++ b/apps/files/tests/Controller/ViewControllerTest.php @@ -167,7 +167,7 @@ class ViewControllerTest extends TestCase { [$this->user->getUID(), 'files', 'crop_image_previews', true, true], [$this->user->getUID(), 'files', 'show_grid', true], ]); - + $baseFolderFiles = $this->getMockBuilder(Folder::class)->getMock(); $this->rootFolder->expects($this->any()) @@ -228,9 +228,9 @@ class ViewControllerTest extends TestCase { ->willReturn($baseFolderTrash); $baseFolderFiles->expects($this->any()) - ->method('getById') + ->method('getFirstNodeById') ->with(123) - ->willReturn([]); + ->willReturn(null); $node = $this->getMockBuilder(File::class)->getMock(); $node->expects($this->once()) @@ -238,9 +238,9 @@ class ViewControllerTest extends TestCase { ->willReturn($parentNode); $baseFolderTrash->expects($this->once()) - ->method('getById') + ->method('getFirstNodeById') ->with(123) - ->willReturn([$node]); + ->willReturn($node); $baseFolderTrash->expects($this->once()) ->method('getRelativePath') ->with('testuser1/files_trashbin/files/test.d1462861890/sub') diff --git a/apps/files_reminders/lib/Model/RichReminder.php b/apps/files_reminders/lib/Model/RichReminder.php index 10dc89799fe..f621a8f16d5 100644 --- a/apps/files_reminders/lib/Model/RichReminder.php +++ b/apps/files_reminders/lib/Model/RichReminder.php @@ -45,11 +45,10 @@ class RichReminder extends Reminder implements JsonSerializable { * @throws NodeNotFoundException */ public function getNode(): Node { - $nodes = $this->root->getUserFolder($this->getUserId())->getById($this->getFileId()); - if (empty($nodes)) { + $node = $this->root->getUserFolder($this->getUserId())->getFirstNodeById($this->getFileId()); + if (!$node) { throw new NodeNotFoundException(); } - $node = reset($nodes); return $node; } diff --git a/apps/files_reminders/lib/Notification/Notifier.php b/apps/files_reminders/lib/Notification/Notifier.php index f7ffa0b4437..a280c4d9336 100644 --- a/apps/files_reminders/lib/Notification/Notifier.php +++ b/apps/files_reminders/lib/Notification/Notifier.php @@ -69,11 +69,10 @@ class Notifier implements INotifier { $params = $notification->getSubjectParameters(); $fileId = $params['fileId']; - $nodes = $this->root->getUserFolder($notification->getUser())->getById($fileId); - if (empty($nodes)) { + $node = $this->root->getUserFolder($notification->getUser())->getFirstNodeById($fileId); + if (!$node) { throw new InvalidArgumentException(); } - $node = reset($nodes); $path = rtrim($node->getPath(), '/'); if (strpos($path, '/' . $notification->getUser() . '/files/') === 0) { diff --git a/apps/files_reminders/lib/Service/ReminderService.php b/apps/files_reminders/lib/Service/ReminderService.php index fabca65c45a..9d5daf0cc5e 100644 --- a/apps/files_reminders/lib/Service/ReminderService.php +++ b/apps/files_reminders/lib/Service/ReminderService.php @@ -98,8 +98,8 @@ class ReminderService { $this->reminderMapper->update($reminder); return false; } catch (DoesNotExistException $e) { - $nodes = $this->root->getUserFolder($user->getUID())->getById($fileId); - if (empty($nodes)) { + $node = $this->root->getUserFolder($user->getUID())->getFirstNodeById($fileId); + if (!$node) { throw new NodeNotFoundException(); } // Create new reminder if no reminder is found diff --git a/apps/files_sharing/lib/Collaboration/ShareRecipientSorter.php b/apps/files_sharing/lib/Collaboration/ShareRecipientSorter.php index 4ed30f44020..ee2d0e9885e 100644 --- a/apps/files_sharing/lib/Collaboration/ShareRecipientSorter.php +++ b/apps/files_sharing/lib/Collaboration/ShareRecipientSorter.php @@ -56,11 +56,11 @@ class ShareRecipientSorter implements ISorter { } $userFolder = $this->rootFolder->getUserFolder($user->getUID()); /** @var Node[] $nodes */ - $nodes = $userFolder->getById((int)$context['itemId']); - if (count($nodes) === 0) { + $node = $userFolder->getFirstNodeById((int)$context['itemId']); + if (!$node) { return; } - $al = $this->shareManager->getAccessList($nodes[0]); + $al = $this->shareManager->getAccessList($node); foreach ($sortArray as $type => &$byType) { if (!isset($al[$type]) || !is_array($al[$type])) { diff --git a/apps/files_sharing/lib/Controller/DeletedShareAPIController.php b/apps/files_sharing/lib/Controller/DeletedShareAPIController.php index 6b54a3489c6..c405c382329 100644 --- a/apps/files_sharing/lib/Controller/DeletedShareAPIController.php +++ b/apps/files_sharing/lib/Controller/DeletedShareAPIController.php @@ -117,15 +117,13 @@ class DeletedShareAPIController extends OCSController { 'path' => $share->getTarget(), ]; $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); - $nodes = $userFolder->getById($share->getNodeId()); - if (empty($nodes)) { + $node = $userFolder->getFirstNodeById($share->getNodeId()); + if (!$node) { // fallback to guessing the path $node = $userFolder->get($share->getTarget()); if ($node === null || $share->getTarget() === '') { throw new NotFoundException(); } - } else { - $node = $nodes[0]; } $result['path'] = $userFolder->getRelativePath($node->getPath()); diff --git a/apps/files_sharing/lib/Controller/ShareAPIController.php b/apps/files_sharing/lib/Controller/ShareAPIController.php index ea124382d9a..c9a1486db2f 100644 --- a/apps/files_sharing/lib/Controller/ShareAPIController.php +++ b/apps/files_sharing/lib/Controller/ShareAPIController.php @@ -203,15 +203,13 @@ class ShareAPIController extends OCSController { if ($recipientNode) { $node = $recipientNode; } else { - $nodes = $userFolder->getById($share->getNodeId()); - if (empty($nodes)) { + $node = $userFolder->getFirstNodeById($share->getNodeId()); + if (!$node) { // fallback to guessing the path $node = $userFolder->get($share->getTarget()); if ($node === null || $share->getTarget() === '') { throw new NotFoundException(); } - } else { - $node = reset($nodes); } } @@ -1142,8 +1140,7 @@ class ShareAPIController extends OCSController { $owner = $node->getOwner() ->getUID(); $userFolder = $this->rootFolder->getUserFolder($owner); - $nodes = $userFolder->getById($node->getId()); - $node = array_shift($nodes); + $node = $userFolder->getFirstNodeById($node->getId()); } $basePath = $userFolder->getPath(); @@ -1164,9 +1161,9 @@ class ShareAPIController extends OCSController { foreach ($nodes as $node) { $getShares = $this->getFormattedShares($owner, $node, false, true); - $currentUserNodes = $currentUserFolder->getById($node->getId()); - if (!empty($currentUserNodes)) { - $parent = array_pop($currentUserNodes); + $currentUserNode = $currentUserFolder->getFirstNodeById($node->getId()); + if ($currentUserNode) { + $parent = $currentUserNode; } $subPath = $currentUserFolder->getRelativePath($parent->getPath()); @@ -1423,15 +1420,13 @@ class ShareAPIController extends OCSController { $result = array_filter(array_map(function (IShare $share) { $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); - $nodes = $userFolder->getById($share->getNodeId()); - if (empty($nodes)) { + $node = $userFolder->getFirstNodeById($share->getNodeId()); + if (!$node) { // fallback to guessing the path $node = $userFolder->get($share->getTarget()); if ($node === null || $share->getTarget() === '') { return null; } - } else { - $node = $nodes[0]; } try { @@ -1516,8 +1511,8 @@ class ShareAPIController extends OCSController { // Have reshare rights on the shared file/folder ? // Does the currentUser have access to the shared file? $userFolder = $this->rootFolder->getUserFolder($this->currentUser); - $files = $userFolder->getById($share->getNodeId()); - if (!empty($files) && $this->shareProviderResharingRights($this->currentUser, $share, $files[0])) { + $file = $userFolder->getFirstNodeById($share->getNodeId()); + if ($file && $this->shareProviderResharingRights($this->currentUser, $share, $file)) { return true; } @@ -2091,11 +2086,10 @@ class ShareAPIController extends OCSController { return; // Probably in a test } $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); - $nodes = $userFolder->getById($share->getNodeId()); - if (empty($nodes)) { + $node = $userFolder->getFirstNodeById($share->getNodeId()); + if (!$node) { return; } - $node = $nodes[0]; if ($node->getStorage()->instanceOfStorage(SharedStorage::class)) { $storage = $node->getStorage(); if ($storage instanceof Wrapper) { diff --git a/apps/files_sharing/lib/Controller/ShareController.php b/apps/files_sharing/lib/Controller/ShareController.php index 0691137631b..d6780d37f2f 100644 --- a/apps/files_sharing/lib/Controller/ShareController.php +++ b/apps/files_sharing/lib/Controller/ShareController.php @@ -527,8 +527,7 @@ class ShareController extends AuthPublicShareController { $fileId = $node->getId(); $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); - $userNodeList = $userFolder->getById($fileId); - $userNode = $userNodeList[0]; + $userNode = $userFolder->getFirstNodeById($fileId); $ownerFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); $userPath = $userFolder->getRelativePath($userNode->getPath()); $ownerPath = $ownerFolder->getRelativePath($node->getPath()); diff --git a/apps/files_sharing/lib/OrphanHelper.php b/apps/files_sharing/lib/OrphanHelper.php index e4fd062db3b..7dfaadc773b 100644 --- a/apps/files_sharing/lib/OrphanHelper.php +++ b/apps/files_sharing/lib/OrphanHelper.php @@ -46,8 +46,8 @@ class OrphanHelper { } catch (NoUserException $e) { return false; } - $nodes = $userFolder->getById($fileId); - return count($nodes) > 0; + $node = $userFolder->getFirstNodeById($fileId); + return $node !== null; } /** diff --git a/apps/files_sharing/lib/SharedStorage.php b/apps/files_sharing/lib/SharedStorage.php index 6b605a6eddb..35e2c0a7e36 100644 --- a/apps/files_sharing/lib/SharedStorage.php +++ b/apps/files_sharing/lib/SharedStorage.php @@ -142,9 +142,7 @@ class SharedStorage extends \OC\Files\Storage\Wrapper\Jail implements ISharedSto $rootFolder = \OC::$server->get(IRootFolder::class); $this->ownerUserFolder = $rootFolder->getUserFolder($this->superShare->getShareOwner()); $sourceId = $this->superShare->getNodeId(); - $ownerNodes = $this->ownerUserFolder->getById($sourceId); - /** @var Node|false $ownerNode */ - $ownerNode = current($ownerNodes); + $ownerNode = $this->ownerUserFolder->getFirstNodeById($sourceId); if (!$ownerNode) { $this->storage = new FailedStorage(['exception' => new NotFoundException("File by id $sourceId not found")]); $this->cache = new FailedCache(); diff --git a/apps/files_sharing/tests/Collaboration/ShareRecipientSorterTest.php b/apps/files_sharing/tests/Collaboration/ShareRecipientSorterTest.php index ed35c81fb7d..6e1aa65cea8 100644 --- a/apps/files_sharing/tests/Collaboration/ShareRecipientSorterTest.php +++ b/apps/files_sharing/tests/Collaboration/ShareRecipientSorterTest.php @@ -77,9 +77,9 @@ class ShareRecipientSorterTest extends TestCase { if ($data['context']['itemType'] === 'files') { $folder->expects($this->once()) - ->method('getById') + ->method('getFirstNodeById') ->with($data['context']['itemId']) - ->willReturn([$node]); + ->willReturn($node); $this->shareManager->expects($this->once()) ->method('getAccessList') @@ -87,7 +87,7 @@ class ShareRecipientSorterTest extends TestCase { ->willReturn($data['accessList']); } else { $folder->expects($this->never()) - ->method('getById'); + ->method('getFirstNodeById'); $this->shareManager->expects($this->never()) ->method('getAccessList'); } @@ -106,8 +106,8 @@ class ShareRecipientSorterTest extends TestCase { ->willReturn($folder); $folder->expects($this->once()) - ->method('getById') - ->willReturn([]); + ->method('getFirstNodeById') + ->willReturn(null); $user = $this->createMock(IUser::class); $user->expects($this->any()) diff --git a/apps/files_sharing/tests/Controller/ShareAPIControllerTest.php b/apps/files_sharing/tests/Controller/ShareAPIControllerTest.php index 731650d8aee..b9e58d85688 100644 --- a/apps/files_sharing/tests/Controller/ShareAPIControllerTest.php +++ b/apps/files_sharing/tests/Controller/ShareAPIControllerTest.php @@ -367,6 +367,7 @@ class ShareAPIControllerTest extends TestCase { */ public function testDeleteShareFileOwner() { $node = $this->getMockBuilder(File::class)->getMock(); + $node->method('getId')->willReturn(1); $share = $this->newShare(); $share->setShareOwner($this->currentUser) @@ -399,6 +400,7 @@ class ShareAPIControllerTest extends TestCase { */ public function testDeleteSharedWithMyGroup() { $node = $this->getMockBuilder(File::class)->getMock(); + $node->method('getId')->willReturn(1); $share = $this->newShare(); $share->setShareType(IShare::TYPE_GROUP) @@ -435,9 +437,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with($share->getNodeId()) - ->willReturn([$share->getNode()]); + ->willReturn($share->getNode()); $this->shareManager->expects($this->once()) ->method('deleteFromSelf') @@ -461,6 +463,7 @@ class ShareAPIControllerTest extends TestCase { $this->expectExceptionMessage('Wrong share ID, share does not exist'); $node = $this->getMockBuilder(File::class)->getMock(); + $node->method('getId')->willReturn(42); $share = $this->newShare(); $share->setShareType(IShare::TYPE_GROUP) @@ -497,9 +500,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with($share->getNodeId()) - ->willReturn([$share->getNode()]); + ->willReturn($share->getNode()); $this->shareManager->expects($this->never()) ->method('deleteFromSelf'); @@ -813,6 +816,10 @@ class ShareAPIControllerTest extends TestCase { ->with($share->getNodeId()) ->willReturn([$share->getNode()]); + $userFolder->method('getFirstNodeById') + ->with($share->getNodeId()) + ->willReturn($share->getNode()); + $this->rootFolder->method('getUserFolder') ->with($this->currentUser) ->willReturn($userFolder); @@ -1497,9 +1504,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with($share->getNodeId()) - ->willReturn([$file]); + ->willReturn($file); $file->method('getPermissions') ->will($this->onConsecutiveCalls(\OCP\Constants::PERMISSION_SHARE, \OCP\Constants::PERMISSION_READ)); @@ -1593,9 +1600,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with($share->getNodeId()) - ->willReturn([$share->getNode()]); + ->willReturn($share->getNode()); if (!$helperAvailable) { $this->appManager->method('isEnabledForUser') @@ -1947,6 +1954,7 @@ class ShareAPIControllerTest extends TestCase { ]); $path = $this->getMockBuilder(Folder::class)->getMock(); + $path->method('getId')->willReturn(42); $storage = $this->createMock(Storage::class); $storage->method('instanceOfStorage') ->willReturnMap([ @@ -1970,6 +1978,7 @@ class ShareAPIControllerTest extends TestCase { $this->expectExceptionMessage('Public upload disabled by the administrator'); $path = $this->getMockBuilder(Folder::class)->getMock(); + $path->method('getId')->willReturn(42); $storage = $this->createMock(Storage::class); $storage->method('instanceOfStorage') ->willReturnMap([ @@ -1994,6 +2003,7 @@ class ShareAPIControllerTest extends TestCase { $this->expectExceptionMessage('Public upload is only possible for publicly shared folders'); $path = $this->getMockBuilder(File::class)->getMock(); + $path->method('getId')->willReturn(42); $storage = $this->createMock(Storage::class); $storage->method('instanceOfStorage') ->willReturnMap([ @@ -2017,6 +2027,7 @@ class ShareAPIControllerTest extends TestCase { $ocs = $this->mockFormatShare(); $path = $this->getMockBuilder(Folder::class)->getMock(); + $path->method('getId')->willReturn(1); $storage = $this->createMock(Storage::class); $storage->method('instanceOfStorage') ->willReturnMap([ @@ -2055,6 +2066,7 @@ class ShareAPIControllerTest extends TestCase { $ocs = $this->mockFormatShare(); $path = $this->getMockBuilder(Folder::class)->getMock(); + $path->method('getId')->willReturn(42); $storage = $this->createMock(Storage::class); $storage->method('instanceOfStorage') ->willReturnMap([ @@ -2093,6 +2105,7 @@ class ShareAPIControllerTest extends TestCase { $ocs = $this->mockFormatShare(); $path = $this->getMockBuilder(Folder::class)->getMock(); + $path->method('getId')->willReturn(42); $storage = $this->createMock(Storage::class); $storage->method('instanceOfStorage') ->willReturnMap([ @@ -2138,6 +2151,7 @@ class ShareAPIControllerTest extends TestCase { $ocs = $this->mockFormatShare(); $path = $this->getMockBuilder(Folder::class)->getMock(); + $path->method('getId')->willReturn(42); $storage = $this->createMock(Storage::class); $storage->method('instanceOfStorage') ->willReturnMap([ @@ -2176,6 +2190,7 @@ class ShareAPIControllerTest extends TestCase { ]); $path = $this->getMockBuilder(Folder::class)->getMock(); + $path->method('getId')->willReturn(42); $storage = $this->createMock(Storage::class); $storage->method('instanceOfStorage') ->willReturnMap([ @@ -2221,6 +2236,7 @@ class ShareAPIControllerTest extends TestCase { $ocs = $this->mockFormatShare(); $path = $this->getMockBuilder(Folder::class)->getMock(); + $path->method('getId')->willReturn(42); $storage = $this->createMock(Storage::class); $storage->method('instanceOfStorage') ->willReturnMap([ @@ -2579,6 +2595,8 @@ class ShareAPIControllerTest extends TestCase { ->willReturn($userFolder); $path = $this->getMockBuilder(Folder::class)->getMock(); + $path->method('getId')->willReturn(42); + $storage = $this->createMock(Storage::class); $storage->method('instanceOfStorage') ->willReturnMap([ @@ -2628,9 +2646,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with($share->getNodeId()) - ->willReturn([$share->getNode()]); + ->willReturn($share->getNode()); $this->ocs->updateShare(42); } @@ -2721,9 +2739,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with(42) - ->willReturn([$node]); + ->willReturn($node); $mountPoint = $this->createMock(IMountPoint::class); $node->method('getMountPoint') @@ -2775,9 +2793,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with(42) - ->willReturn([$folder]); + ->willReturn($folder); $mountPoint = $this->createMock(IMountPoint::class); $folder->method('getMountPoint') @@ -2825,9 +2843,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with(42) - ->willReturn([$folder]); + ->willReturn($folder); $mountPoint = $this->createMock(IMountPoint::class); $folder->method('getMountPoint') @@ -2883,9 +2901,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with(42) - ->willReturn([$folder]); + ->willReturn($folder); $mountPoint = $this->createMock(IMountPoint::class); $folder->method('getMountPoint') @@ -2941,9 +2959,9 @@ class ShareAPIControllerTest extends TestCase { $ocs = $this->mockFormatShare(); [$userFolder, $folder] = $this->getNonSharedUserFolder(); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with(42) - ->willReturn([$folder]); + ->willReturn($folder); $this->rootFolder->method('getUserFolder') ->with($this->currentUser) ->willReturn($userFolder); @@ -2988,9 +3006,9 @@ class ShareAPIControllerTest extends TestCase { $ocs = $this->mockFormatShare(); [$userFolder, $folder] = $this->getNonSharedUserFolder(); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with(42) - ->willReturn([$folder]); + ->willReturn($folder); $this->rootFolder->method('getUserFolder') ->with($this->currentUser) ->willReturn($userFolder); @@ -3020,9 +3038,9 @@ class ShareAPIControllerTest extends TestCase { $file->method('getId') ->willReturn(42); [$userFolder, $folder] = $this->getNonSharedUserFolder(); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with(42) - ->willReturn([$folder]); + ->willReturn($folder); $this->rootFolder->method('getUserFolder') ->with($this->currentUser) ->willReturn($userFolder); @@ -3047,9 +3065,9 @@ class ShareAPIControllerTest extends TestCase { [$userFolder, $node] = $this->getNonSharedUserFolder(); $node->method('getId')->willReturn(42); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with(42) - ->willReturn([$node]); + ->willReturn($node); $this->rootFolder->method('getUserFolder') ->with($this->currentUser) ->willReturn($userFolder); @@ -3098,9 +3116,9 @@ class ShareAPIControllerTest extends TestCase { $date->setTime(0, 0, 0); [$userFolder, $node] = $this->getNonSharedUserFolder(); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with(42) - ->willReturn([$node]); + ->willReturn($node); $this->rootFolder->method('getUserFolder') ->with($this->currentUser) ->willReturn($userFolder); @@ -3156,9 +3174,9 @@ class ShareAPIControllerTest extends TestCase { $date->setTime(0, 0, 0); [$userFolder, $node] = $this->getNonSharedUserFolder(); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with(42) - ->willReturn([$node]); + ->willReturn($node); $this->rootFolder->method('getUserFolder') ->with($this->currentUser) ->willReturn($userFolder); @@ -3196,9 +3214,9 @@ class ShareAPIControllerTest extends TestCase { $date->setTime(0, 0, 0); [$userFolder, $node] = $this->getNonSharedUserFolder(); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with(42) - ->willReturn([$node]); + ->willReturn($node); $this->rootFolder->method('getUserFolder') ->with($this->currentUser) ->willReturn($userFolder); @@ -3290,9 +3308,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with(42) - ->willReturn([$node]); + ->willReturn($node); $mountPoint = $this->createMock(IMountPoint::class); $node->method('getMountPoint') @@ -3358,9 +3376,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with(42) - ->willReturn([$node]); + ->willReturn($node); $mountPoint = $this->createMock(IMountPoint::class); $node->method('getMountPoint') @@ -3419,9 +3437,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with(42) - ->willReturn([$folder]); + ->willReturn($folder); $mountPoint = $this->createMock(IMountPoint::class); $folder->method('getMountPoint') @@ -3479,9 +3497,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with(42) - ->willReturn([$folder]); + ->willReturn($folder); $mountPoint = $this->createMock(IMountPoint::class); $folder->method('getMountPoint') @@ -3537,9 +3555,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with(42) - ->willReturn([$folder]); + ->willReturn($folder); $mountPoint = $this->createMock(IMountPoint::class); $folder->method('getMountPoint') @@ -3585,9 +3603,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with(42) - ->willReturn([$file]); + ->willReturn($file); $mountPoint = $this->createMock(IMountPoint::class); $file->method('getMountPoint') @@ -3651,9 +3669,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with(42) - ->willReturn([$folder]); + ->willReturn($folder); $mountPoint = $this->createMock(IMountPoint::class); $folder->method('getMountPoint') @@ -3721,9 +3739,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with(42) - ->willReturn([$folder]); + ->willReturn($folder); $mountPoint = $this->createMock(IMountPoint::class); $folder->method('getMountPoint') @@ -4651,9 +4669,9 @@ class ShareAPIControllerTest extends TestCase { $this->dateTimeZone->method('getTimezone')->willReturn(new \DateTimeZone('UTC')); if (!$exception) { - $this->rootFolder->method('getById') + $this->rootFolder->method('getFirstNodeById') ->with($share->getNodeId()) - ->willReturn([$share->getNode()]); + ->willReturn($share->getNode()); $this->rootFolder->method('getRelativePath') ->with($share->getNode()->getPath()) @@ -4846,9 +4864,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturnSelf(); - $this->rootFolder->method('getById') + $this->rootFolder->method('getFirstNodeById') ->with($share->getNodeId()) - ->willReturn([$share->getNode()]); + ->willReturn($share->getNode()); $this->rootFolder->method('getRelativePath') ->with($share->getNode()->getPath()) @@ -4890,6 +4908,7 @@ class ShareAPIControllerTest extends TestCase { ]); $userFolder->method('getStorage')->willReturn($storage); $node->method('getStorage')->willReturn($storage); + $node->method('getId')->willReturn(42); return [$userFolder, $node]; } @@ -4904,6 +4923,7 @@ class ShareAPIControllerTest extends TestCase { ]); $userFolder->method('getStorage')->willReturn($storage); $node->method('getStorage')->willReturn($storage); + $node->method('getId')->willReturn(42); return [$userFolder, $node]; } } diff --git a/apps/files_sharing/tests/Controller/ShareControllerTest.php b/apps/files_sharing/tests/Controller/ShareControllerTest.php index 3a73862bf77..c8dc6c5de66 100644 --- a/apps/files_sharing/tests/Controller/ShareControllerTest.php +++ b/apps/files_sharing/tests/Controller/ShareControllerTest.php @@ -41,6 +41,7 @@ use OC\Share20\Manager; use OCA\FederatedFileSharing\FederatedShareProvider; use OCA\Files_Sharing\Controller\ShareController; use OCA\Files_Sharing\DefaultPublicShareTemplateProvider; +use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent; use OCP\Accounts\IAccount; use OCP\Accounts\IAccountManager; use OCP\Accounts\IAccountProperty; @@ -258,6 +259,7 @@ class ShareControllerTest extends \Test\TestCase { $file->method('getSize')->willReturn(33); $file->method('isReadable')->willReturn(true); $file->method('isShareable')->willReturn(true); + $file->method('getId')->willReturn(111); $accountName = $this->createMock(IAccountProperty::class); $accountName->method('getScope') @@ -330,13 +332,15 @@ class ShareControllerTest extends \Test\TestCase { return null; }); - $this->eventDispatcher->expects($this->exactly(2)) - ->method('dispatchTyped') - ->with( - $this->callback(function ($event) use ($share) { + $this->eventDispatcher->method('dispatchTyped')->with( + $this->callback(function ($event) use ($share) { + if ($event instanceof BeforeTemplateRenderedEvent) { return $event->getShare() === $share; - }) - ); + } else { + return true; + } + }) + ); $this->l10n->expects($this->any()) ->method('t') @@ -416,6 +420,7 @@ class ShareControllerTest extends \Test\TestCase { $file->method('getSize')->willReturn(33); $file->method('isReadable')->willReturn(true); $file->method('isShareable')->willReturn(true); + $file->method('getId')->willReturn(111); $accountName = $this->createMock(IAccountProperty::class); $accountName->method('getScope') @@ -488,13 +493,15 @@ class ShareControllerTest extends \Test\TestCase { return null; }); - $this->eventDispatcher->expects($this->exactly(2)) - ->method('dispatchTyped') - ->with( - $this->callback(function ($event) use ($share) { + $this->eventDispatcher->method('dispatchTyped')->with( + $this->callback(function ($event) use ($share) { + if ($event instanceof BeforeTemplateRenderedEvent) { return $event->getShare() === $share; - }) - ); + } else { + return true; + } + }) + ); $this->l10n->expects($this->any()) ->method('t') @@ -574,6 +581,7 @@ class ShareControllerTest extends \Test\TestCase { $file->method('getSize')->willReturn(33); $file->method('isReadable')->willReturn(true); $file->method('isShareable')->willReturn(true); + $file->method('getId')->willReturn(111); $accountName = $this->createMock(IAccountProperty::class); $accountName->method('getScope') @@ -650,13 +658,15 @@ class ShareControllerTest extends \Test\TestCase { return null; }); - $this->eventDispatcher->expects($this->exactly(2)) - ->method('dispatchTyped') - ->with( - $this->callback(function ($event) use ($share) { + $this->eventDispatcher->method('dispatchTyped')->with( + $this->callback(function ($event) use ($share) { + if ($event instanceof BeforeTemplateRenderedEvent) { return $event->getShare() === $share; - }) - ); + } else { + return true; + } + }) + ); $this->l10n->expects($this->any()) ->method('t') @@ -741,6 +751,7 @@ class ShareControllerTest extends \Test\TestCase { $folder->method('getStorage')->willReturn($storage); $folder->method('get')->with('')->willReturn($folder); $folder->method('getSize')->willReturn(1337); + $folder->method('getId')->willReturn(111); $accountName = $this->createMock(IAccountProperty::class); $accountName->method('getScope') diff --git a/apps/files_sharing/tests/SharedStorageTest.php b/apps/files_sharing/tests/SharedStorageTest.php index 8e69c5a0f65..5209a30634a 100644 --- a/apps/files_sharing/tests/SharedStorageTest.php +++ b/apps/files_sharing/tests/SharedStorageTest.php @@ -586,6 +586,7 @@ class SharedStorageTest extends TestCase { public function testInitWithNotFoundSource() { $share = $this->createMock(IShare::class); $share->method('getShareOwner')->willReturn(self::TEST_FILES_SHARING_API_USER1); + $share->method('getNodeId')->willReturn(1); $ownerView = $this->createMock(View::class); $ownerView->method('getPath')->will($this->throwException(new NotFoundException())); $storage = new SharedStorage([ diff --git a/apps/files_trashbin/lib/Trash/LegacyTrashBackend.php b/apps/files_trashbin/lib/Trash/LegacyTrashBackend.php index 3e749169ad2..ada42dbe857 100644 --- a/apps/files_trashbin/lib/Trash/LegacyTrashBackend.php +++ b/apps/files_trashbin/lib/Trash/LegacyTrashBackend.php @@ -28,6 +28,7 @@ use OCA\Files_Trashbin\Helper; use OCA\Files_Trashbin\Storage; use OCA\Files_Trashbin\Trashbin; use OCP\Files\FileInfo; +use OCP\Files\Folder; use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; use OCP\Files\Storage\IStorage; @@ -121,11 +122,11 @@ class LegacyTrashBackend implements ITrashBackend { try { $userFolder = $this->rootFolder->getUserFolder($user->getUID()); $trash = $userFolder->getParent()->get('files_trashbin/files'); - $trashFiles = $trash->getById($fileId); - if (!$trashFiles) { + if ($trash instanceof Folder) { + return $trash->getFirstNodeById($fileId); + } else { return null; } - return $trashFiles ? array_pop($trashFiles) : null; } catch (NotFoundException $e) { return null; } diff --git a/apps/files_versions/lib/Sabre/VersionRoot.php b/apps/files_versions/lib/Sabre/VersionRoot.php index 69ac12ed8e9..2ae1bf04203 100644 --- a/apps/files_versions/lib/Sabre/VersionRoot.php +++ b/apps/files_versions/lib/Sabre/VersionRoot.php @@ -75,14 +75,12 @@ class VersionRoot implements ICollection { $userFolder = $this->rootFolder->getUserFolder($this->user->getUID()); $fileId = (int)$name; - $nodes = $userFolder->getById($fileId); + $node = $userFolder->getFirstNodeById($fileId); - if ($nodes === []) { + if (!$node) { throw new NotFound(); } - $node = array_pop($nodes); - if (!$node instanceof File) { throw new NotFound(); } diff --git a/apps/files_versions/lib/Storage.php b/apps/files_versions/lib/Storage.php index 07bd6b164d2..a35f151d956 100644 --- a/apps/files_versions/lib/Storage.php +++ b/apps/files_versions/lib/Storage.php @@ -209,9 +209,9 @@ class Storage { $mount = $file->getMountPoint(); if ($mount instanceof SharedMount) { $ownerFolder = $rootFolder->getUserFolder($mount->getShare()->getShareOwner()); - $ownerNodes = $ownerFolder->getById($file->getId()); - if (count($ownerNodes)) { - $file = current($ownerNodes); + $ownerNode = $ownerFolder->getFirstNodeById($file->getId()); + if ($ownerNode) { + $file = $ownerNode; $uid = $mount->getShare()->getShareOwner(); } } diff --git a/apps/files_versions/lib/Versions/LegacyVersionsBackend.php b/apps/files_versions/lib/Versions/LegacyVersionsBackend.php index a6bf6c2cb1a..4c365ed4c14 100644 --- a/apps/files_versions/lib/Versions/LegacyVersionsBackend.php +++ b/apps/files_versions/lib/Versions/LegacyVersionsBackend.php @@ -87,8 +87,7 @@ class LegacyVersionsBackend implements IVersionBackend, INameableVersionBackend, $userFolder = $this->rootFolder->getUserFolder($user->getUID()); - $nodes = $userFolder->getById($fileId); - $file = array_pop($nodes); + $file = $userFolder->getFirstNodeById($fileId); if (!$file) { throw new NotFoundException("version file not found for share owner"); diff --git a/core/Command/FilesMetadata/Get.php b/core/Command/FilesMetadata/Get.php index 99bc167f71d..d1def992c8a 100644 --- a/core/Command/FilesMetadata/Get.php +++ b/core/Command/FilesMetadata/Get.php @@ -96,12 +96,12 @@ class Get extends Command { } if ($input->getOption('refresh')) { - $node = $this->rootFolder->getUserFolder($input->getArgument('userId'))->getById($fileId); - if (count($node) === 0) { + $node = $this->rootFolder->getUserFolder($input->getArgument('userId'))->getFirstNodeById($fileId); + if (!$node) { throw new NotFoundException(); } $metadata = $this->filesMetadataManager->refreshMetadata( - $node[0], + $node, IFilesMetadataManager::PROCESS_LIVE | IFilesMetadataManager::PROCESS_BACKGROUND ); } else { diff --git a/core/Command/Info/FileUtils.php b/core/Command/Info/FileUtils.php index 694f30f78b4..595a0216a5c 100644 --- a/core/Command/Info/FileUtils.php +++ b/core/Command/Info/FileUtils.php @@ -87,11 +87,7 @@ class FileUtils { } $mount = $mounts[0]; $userFolder = $this->rootFolder->getUserFolder($mount->getUser()->getUID()); - $nodes = $userFolder->getById((int)$fileInput); - if (!$nodes) { - return null; - } - return $nodes[0]; + return $userFolder->getFirstNodeById((int)$fileInput); } else { try { return $this->rootFolder->get($fileInput); diff --git a/core/Command/Preview/Generate.php b/core/Command/Preview/Generate.php index a885d224fc7..86528319199 100644 --- a/core/Command/Preview/Generate.php +++ b/core/Command/Preview/Generate.php @@ -121,11 +121,7 @@ class Generate extends Command { } $mount = $mounts[0]; $userFolder = $this->rootFolder->getUserFolder($mount->getUser()->getUID()); - $nodes = $userFolder->getById((int)$fileInput); - if (!$nodes) { - return null; - } - return $nodes[0]; + return $userFolder->getFirstNodeById((int)$fileInput); } else { try { return $this->rootFolder->get($fileInput); diff --git a/core/Controller/PreviewController.php b/core/Controller/PreviewController.php index 34c21bd3ecb..7ab22dceaa2 100644 --- a/core/Controller/PreviewController.php +++ b/core/Controller/PreviewController.php @@ -133,14 +133,12 @@ class PreviewController extends Controller { } $userFolder = $this->root->getUserFolder($this->userId); - $nodes = $userFolder->getById($fileId); + $node = $userFolder->getFirstNodeById($fileId); - if (\count($nodes) === 0) { + if (!$node) { return new DataResponse([], Http::STATUS_NOT_FOUND); } - $node = array_pop($nodes); - return $this->fetchPreview($node, $x, $y, $a, $forceIcon, $mode, $mimeFallback); } diff --git a/lib/private/Collaboration/Reference/File/FileReferenceProvider.php b/lib/private/Collaboration/Reference/File/FileReferenceProvider.php index 5f384213976..125649246df 100644 --- a/lib/private/Collaboration/Reference/File/FileReferenceProvider.php +++ b/lib/private/Collaboration/Reference/File/FileReferenceProvider.php @@ -31,7 +31,6 @@ use OCP\Collaboration\Reference\Reference; use OCP\Files\IMimeTypeDetector; use OCP\Files\InvalidPathException; use OCP\Files\IRootFolder; -use OCP\Files\Node; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; use OCP\IL10N; @@ -121,15 +120,12 @@ class FileReferenceProvider extends ADiscoverableReferenceProvider { try { $userFolder = $this->rootFolder->getUserFolder($this->userId); - $files = $userFolder->getById($fileId); + $file = $userFolder->getFirstNodeById($fileId); - if (empty($files)) { + if (!$file) { throw new NotFoundException(); } - /** @var Node $file */ - $file = array_shift($files); - $reference->setTitle($file->getName()); $reference->setDescription($file->getMimetype()); $reference->setUrl($this->urlGenerator->getAbsoluteURL('/index.php/f/' . $fileId)); diff --git a/lib/private/DirectEditing/Manager.php b/lib/private/DirectEditing/Manager.php index d1be1f50330..da4811589da 100644 --- a/lib/private/DirectEditing/Manager.php +++ b/lib/private/DirectEditing/Manager.php @@ -310,11 +310,11 @@ class Manager implements IManager { if ($filePath !== null) { return $userFolder->get($filePath); } - $files = $userFolder->getById($fileId); - if (count($files) === 0) { + $file = $userFolder->getFirstNodeById($fileId); + if (!$file) { throw new NotFoundException('File nound found by id ' . $fileId); } - return $files[0]; + return $file; } public function isEnabled(): bool { diff --git a/lib/private/Files/Config/CachedMountInfo.php b/lib/private/Files/Config/CachedMountInfo.php index 7c97135a565..19fa87aa090 100644 --- a/lib/private/Files/Config/CachedMountInfo.php +++ b/lib/private/Files/Config/CachedMountInfo.php @@ -97,12 +97,7 @@ class CachedMountInfo implements ICachedMountInfo { // TODO injection etc Filesystem::initMountPoints($this->getUser()->getUID()); $userNode = \OC::$server->getUserFolder($this->getUser()->getUID()); - $nodes = $userNode->getParent()->getById($this->getRootId()); - if (count($nodes) > 0) { - return $nodes[0]; - } else { - return null; - } + return $userNode->getParent()->getFirstNodeById($this->getRootId()); } /** diff --git a/lib/private/Files/Node/Folder.php b/lib/private/Files/Node/Folder.php index cb8747e0055..014b66fdf63 100644 --- a/lib/private/Files/Node/Folder.php +++ b/lib/private/Files/Node/Folder.php @@ -314,7 +314,7 @@ class Folder extends Node implements \OCP\Files\Folder { } public function getFirstNodeById(int $id): ?\OCP\Files\Node { - return current($this->getById($id)); + return current($this->getById($id)) ?: null; } protected function getAppDataDirectoryName(): string { diff --git a/lib/private/FilesMetadata/Job/UpdateSingleMetadata.php b/lib/private/FilesMetadata/Job/UpdateSingleMetadata.php index d18c8aa3680..3a3b35ce205 100644 --- a/lib/private/FilesMetadata/Job/UpdateSingleMetadata.php +++ b/lib/private/FilesMetadata/Job/UpdateSingleMetadata.php @@ -55,10 +55,9 @@ class UpdateSingleMetadata extends QueuedJob { [$userId, $fileId] = $argument; try { - $node = $this->rootFolder->getUserFolder($userId)->getById($fileId); - if (count($node) > 0) { - $file = array_shift($node); - $this->filesMetadataManager->refreshMetadata($file, IFilesMetadataManager::PROCESS_BACKGROUND); + $node = $this->rootFolder->getUserFolder($userId)->getFirstNodeById($fileId); + if ($node) { + $this->filesMetadataManager->refreshMetadata($node, IFilesMetadataManager::PROCESS_BACKGROUND); } } catch (\Exception $e) { $this->logger->warning('issue while running UpdateSingleMetadata', ['exception' => $e, 'userId' => $userId, 'fileId' => $fileId]); diff --git a/lib/private/Share20/Manager.php b/lib/private/Share20/Manager.php index 53f88cf14ce..c1abaff2ec0 100644 --- a/lib/private/Share20/Manager.php +++ b/lib/private/Share20/Manager.php @@ -311,8 +311,7 @@ class Manager implements IManager { $mount = $userMount->getMountPoint(); // When it's a reshare use the parent share permissions as maximum $userMountPointId = $mount->getStorageRootId(); - $userMountPoints = $userFolder->getById($userMountPointId); - $userMountPoint = array_shift($userMountPoints); + $userMountPoint = $userFolder->getFirstNodeById($userMountPointId); if ($userMountPoint === null) { throw new GenericShareException('Could not get proper user mount for ' . $userMountPointId . '. Failing since else the next calls are called with null'); @@ -1723,8 +1722,7 @@ class Manager implements IManager { //Get node for the owner and correct the owner in case of external storage $userFolder = $this->rootFolder->getUserFolder($owner); if ($path->getId() !== $userFolder->getId() && !$userFolder->isSubNode($path)) { - $nodes = $userFolder->getById($path->getId()); - $path = array_shift($nodes); + $path = $userFolder->getFirstNodeById($path->getId()); if ($path === null || $path->getOwner() === null) { return []; } diff --git a/lib/private/Share20/Share.php b/lib/private/Share20/Share.php index c80d332e9db..19b36cb60e8 100644 --- a/lib/private/Share20/Share.php +++ b/lib/private/Share20/Share.php @@ -188,12 +188,12 @@ class Share implements IShare { $userFolder = $this->rootFolder->getUserFolder($this->sharedBy); } - $nodes = $userFolder->getById($this->fileId); - if (empty($nodes)) { + $node = $userFolder->getFirstNodeById($this->fileId); + if (!$node) { throw new NotFoundException('Node for share not found, fileid: ' . $this->fileId); } - $this->node = $nodes[0]; + $this->node = $node; } return $this->node; @@ -211,12 +211,16 @@ class Share implements IShare { /** * @inheritdoc */ - public function getNodeId() { + public function getNodeId(): int { if ($this->fileId === null) { $this->fileId = $this->getNode()->getId(); } - return $this->fileId; + if ($this->fileId === null) { + throw new NotFoundException("Share source not found"); + } else { + return $this->fileId; + } } /** diff --git a/lib/private/SpeechToText/TranscriptionJob.php b/lib/private/SpeechToText/TranscriptionJob.php index 8921d52ecd1..083cd129657 100644 --- a/lib/private/SpeechToText/TranscriptionJob.php +++ b/lib/private/SpeechToText/TranscriptionJob.php @@ -65,7 +65,7 @@ class TranscriptionJob extends QueuedJob { try { \OC_Util::setupFS($owner); $userFolder = $this->rootFolder->getUserFolder($owner); - $file = current($userFolder->getById($fileId)); + $file = $userFolder->getFirstNodeById($fileId); if (!($file instanceof File)) { $this->logger->warning('Transcription of file ' . $fileId . ' failed. The file could not be found'); $this->eventDispatcher->dispatchTyped( diff --git a/lib/public/Share/IShare.php b/lib/public/Share/IShare.php index 74d404101cd..a059696a75e 100644 --- a/lib/public/Share/IShare.php +++ b/lib/public/Share/IShare.php @@ -213,7 +213,7 @@ interface IShare { * @since 9.0.0 * @throws NotFoundException */ - public function getNodeId(); + public function getNodeId(): int; /** * Set the type of node (file/folder) diff --git a/tests/lib/Share20/DefaultShareProviderTest.php b/tests/lib/Share20/DefaultShareProviderTest.php index f977619b7b2..b658e20d837 100644 --- a/tests/lib/Share20/DefaultShareProviderTest.php +++ b/tests/lib/Share20/DefaultShareProviderTest.php @@ -210,7 +210,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $ownerPath = $this->createMock(File::class); $shareOwnerFolder = $this->createMock(Folder::class); - $shareOwnerFolder->method('getById')->with(42)->willReturn([$ownerPath]); + $shareOwnerFolder->method('getFirstNodeById')->with(42)->willReturn($ownerPath); $this->rootFolder ->method('getUserFolder') @@ -288,7 +288,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $ownerPath = $this->createMock(File::class); $shareOwnerFolder = $this->createMock(Folder::class); - $shareOwnerFolder->method('getById')->with(42)->willReturn([$ownerPath]); + $shareOwnerFolder->method('getFirstNodeById')->with(42)->willReturn($ownerPath); $this->rootFolder ->method('getUserFolder') @@ -332,7 +332,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $ownerPath = $this->createMock(Folder::class); $shareOwnerFolder = $this->createMock(Folder::class); - $shareOwnerFolder->method('getById')->with(42)->willReturn([$ownerPath]); + $shareOwnerFolder->method('getFirstNodeById')->with(42)->willReturn($ownerPath); $this->rootFolder ->method('getUserFolder') @@ -371,7 +371,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $node->method('getId')->willReturn(42); $this->rootFolder->method('getUserFolder')->with('user0')->willReturnSelf(); - $this->rootFolder->method('getById')->willReturn([$node]); + $this->rootFolder->method('getFirstNodeById')->willReturn($node); $this->userManager->method('get')->willReturnMap([ ['user0', $user0], @@ -416,7 +416,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $ownerPath = $this->createMock(Folder::class); $shareOwnerFolder = $this->createMock(Folder::class); - $shareOwnerFolder->method('getById')->with(42)->willReturn([$ownerPath]); + $shareOwnerFolder->method('getFirstNodeById')->with(42)->willReturn($ownerPath); $this->rootFolder ->method('getUserFolder') @@ -633,7 +633,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $ownerPath = $this->createMock(Folder::class); $ownerFolder = $this->createMock(Folder::class); - $ownerFolder->method('getById')->willReturn([$ownerPath]); + $ownerFolder->method('getFirstNodeById')->willReturn($ownerPath); $this->rootFolder ->method('getUserFolder') @@ -690,12 +690,12 @@ class DefaultShareProviderTest extends \Test\TestCase { ['shareOwner', $ownerFolder], ]); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with(100) - ->willReturn([$path]); - $ownerFolder->method('getById') + ->willReturn($path); + $ownerFolder->method('getFirstNodeById') ->with(100) - ->willReturn([$path]); + ->willReturn($path); $share->setShareType(IShare::TYPE_USER); $share->setSharedWith('sharedWith'); @@ -762,12 +762,12 @@ class DefaultShareProviderTest extends \Test\TestCase { ['shareOwner', $ownerFolder], ]); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with(100) - ->willReturn([$path]); - $ownerFolder->method('getById') + ->willReturn($path); + $ownerFolder->method('getFirstNodeById') ->with(100) - ->willReturn([$path]); + ->willReturn($path); $share->setShareType(IShare::TYPE_GROUP); $share->setSharedWith('sharedWith'); @@ -832,12 +832,12 @@ class DefaultShareProviderTest extends \Test\TestCase { ['shareOwner', $ownerFolder], ]); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with(100) - ->willReturn([$path]); - $ownerFolder->method('getById') + ->willReturn($path); + $ownerFolder->method('getFirstNodeById') ->with(100) - ->willReturn([$path]); + ->willReturn($path); $share->setShareType(IShare::TYPE_LINK); $share->setSharedBy('sharedBy'); @@ -890,7 +890,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $file = $this->createMock(File::class); $this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf(); - $this->rootFolder->method('getById')->with(42)->willReturn([$file]); + $this->rootFolder->method('getFirstNodeById')->with(42)->willReturn($file); $share = $this->provider->getShareByToken('secrettoken'); $this->assertEquals($id, $share->getId()); @@ -981,7 +981,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $file = $this->createMock(File::class); $this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf(); - $this->rootFolder->method('getById')->with($fileId)->willReturn([$file]); + $this->rootFolder->method('getFirstNodeById')->with($fileId)->willReturn($file); $share = $this->provider->getSharedWith('sharedWith', IShare::TYPE_USER, null, 1, 0); $this->assertCount(1, $share); @@ -1053,7 +1053,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $file = $this->createMock(File::class); $this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf(); - $this->rootFolder->method('getById')->with($fileId)->willReturn([$file]); + $this->rootFolder->method('getFirstNodeById')->with($fileId)->willReturn($file); $share = $this->provider->getSharedWith('sharedWith', IShare::TYPE_GROUP, null, 20, 1); $this->assertCount(1, $share); @@ -1141,7 +1141,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $file = $this->createMock(File::class); $this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf(); - $this->rootFolder->method('getById')->with($fileId)->willReturn([$file]); + $this->rootFolder->method('getFirstNodeById')->with($fileId)->willReturn($file); $share = $this->provider->getSharedWith('user', IShare::TYPE_GROUP, null, -1, 0); $this->assertCount(1, $share); @@ -1184,7 +1184,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $file->method('getId')->willReturn($fileId2); $this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf(); - $this->rootFolder->method('getById')->with($fileId2)->willReturn([$file]); + $this->rootFolder->method('getFirstNodeById')->with($fileId2)->willReturn($file); $share = $this->provider->getSharedWith('user0', IShare::TYPE_USER, $file, -1, 0); $this->assertCount(1, $share); @@ -1225,7 +1225,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $node = $this->createMock(Folder::class); $node->method('getId')->willReturn($fileId2); $this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf(); - $this->rootFolder->method('getById')->with($fileId2)->willReturn([$node]); + $this->rootFolder->method('getFirstNodeById')->with($fileId2)->willReturn($node); $share = $this->provider->getSharedWith('user0', IShare::TYPE_GROUP, $node, -1, 0); $this->assertCount(1, $share); @@ -1276,7 +1276,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $file = $this->createMock(File::class); $this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf(); - $this->rootFolder->method('getById')->with($deletedFileId)->willReturn([$file]); + $this->rootFolder->method('getFirstNodeById')->with($deletedFileId)->willReturn($file); $groups = []; foreach (range(0, 100) as $i) { @@ -1336,7 +1336,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $file = $this->createMock(File::class); $this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf(); - $this->rootFolder->method('getById')->with(42)->willReturn([$file]); + $this->rootFolder->method('getFirstNodeById')->with(42)->willReturn($file); $share = $this->provider->getSharesBy('sharedBy', IShare::TYPE_USER, null, false, 1, 0); $this->assertCount(1, $share); @@ -1386,7 +1386,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $file = $this->createMock(File::class); $file->method('getId')->willReturn(42); $this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf(); - $this->rootFolder->method('getById')->with(42)->willReturn([$file]); + $this->rootFolder->method('getFirstNodeById')->with(42)->willReturn($file); $share = $this->provider->getSharesBy('sharedBy', IShare::TYPE_USER, $file, false, 1, 0); $this->assertCount(1, $share); @@ -1436,7 +1436,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $file = $this->createMock(File::class); $file->method('getId')->willReturn(42); $this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf(); - $this->rootFolder->method('getById')->with(42)->willReturn([$file]); + $this->rootFolder->method('getFirstNodeById')->with(42)->willReturn($file); $shares = $this->provider->getSharesBy('shareOwner', IShare::TYPE_USER, null, true, -1, 0); $this->assertCount(2, $shares); @@ -1496,7 +1496,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $file->method('getId')->willReturn(1); $this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf(); - $this->rootFolder->method('getById')->with(1)->willReturn([$file]); + $this->rootFolder->method('getFirstNodeById')->with(1)->willReturn($file); $share = $this->provider->getShareById($id); @@ -1568,7 +1568,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $file->method('getId')->willReturn(1); $this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf(); - $this->rootFolder->method('getById')->with(1)->willReturn([$file]); + $this->rootFolder->method('getFirstNodeById')->with(1)->willReturn($file); $share = $this->provider->getShareById($id); @@ -1626,7 +1626,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $file->method('getId')->willReturn(1); $this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf(); - $this->rootFolder->method('getById')->with(1)->willReturn([$file]); + $this->rootFolder->method('getFirstNodeById')->with(1)->willReturn($file); $share = $this->provider->getShareById($id); @@ -1668,7 +1668,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $file->method('getId')->willReturn(1); $this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf(); - $this->rootFolder->method('getById')->with(1)->willReturn([$file]); + $this->rootFolder->method('getFirstNodeById')->with(1)->willReturn($file); $share = $this->provider->getShareById($id); @@ -1706,7 +1706,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $file->method('getId')->willReturn(1); $this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf(); - $this->rootFolder->method('getById')->with(1)->willReturn([$file]); + $this->rootFolder->method('getFirstNodeById')->with(1)->willReturn($file); $share = $this->provider->getShareById($id); @@ -1760,7 +1760,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $file->method('getId')->willReturn(1); $this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf(); - $this->rootFolder->method('getById')->with(1)->willReturn([$file]); + $this->rootFolder->method('getFirstNodeById')->with(1)->willReturn($file); $share = $this->provider->getShareById($id); @@ -1797,7 +1797,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $file->method('getId')->willReturn(1); $this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf(); - $this->rootFolder->method('getById')->with(1)->willReturn([$file]); + $this->rootFolder->method('getFirstNodeById')->with(1)->willReturn($file); $share = $this->provider->getShareById($id); @@ -1828,9 +1828,9 @@ class DefaultShareProviderTest extends \Test\TestCase { $file2->method('getId')->willReturn(43); $folder1 = $this->createMock(Folder::class); - $folder1->method('getById')->with(42)->willReturn([$file1]); + $folder1->method('getFirstNodeById')->with(42)->willReturn($file1); $folder2 = $this->createMock(Folder::class); - $folder2->method('getById')->with(43)->willReturn([$file2]); + $folder2->method('getFirstNodeById')->with(43)->willReturn($file2); $this->rootFolder->method('getUserFolder')->willReturnMap([ ['user2', $folder1], @@ -1885,9 +1885,9 @@ class DefaultShareProviderTest extends \Test\TestCase { $file2->method('getId')->willReturn(43); $folder1 = $this->createMock(Folder::class); - $folder1->method('getById')->with(42)->willReturn([$file1]); + $folder1->method('getFirstNodeById')->with(42)->willReturn($file1); $folder2 = $this->createMock(Folder::class); - $folder2->method('getById')->with(43)->willReturn([$file2]); + $folder2->method('getFirstNodeById')->with(43)->willReturn($file2); $this->rootFolder->method('getUserFolder')->willReturnMap([ ['user2', $folder1], @@ -1951,9 +1951,9 @@ class DefaultShareProviderTest extends \Test\TestCase { $file2->method('getId')->willReturn(43); $folder1 = $this->createMock(Folder::class); - $folder1->method('getById')->with(42)->willReturn([$file1]); + $folder1->method('getFirstNodeById')->with(42)->willReturn($file1); $folder2 = $this->createMock(Folder::class); - $folder2->method('getById')->with(43)->willReturn([$file2]); + $folder2->method('getFirstNodeById')->with(43)->willReturn($file2); $this->rootFolder->method('getUserFolder')->willReturnMap([ ['user2', $folder1], @@ -2022,9 +2022,9 @@ class DefaultShareProviderTest extends \Test\TestCase { $file2->method('getId')->willReturn(43); $folder1 = $this->createMock(Folder::class); - $folder1->method('getById')->with(42)->willReturn([$file1]); + $folder1->method('getFirstNodeById')->with(42)->willReturn($file1); $folder2 = $this->createMock(Folder::class); - $folder2->method('getById')->with(43)->willReturn([$file2]); + $folder2->method('getFirstNodeById')->with(43)->willReturn($file2); $this->rootFolder->method('getUserFolder')->willReturnMap([ ['user2', $folder1], @@ -2101,9 +2101,9 @@ class DefaultShareProviderTest extends \Test\TestCase { $file2->method('getId')->willReturn(43); $folder1 = $this->createMock(Folder::class); - $folder1->method('getById')->with(42)->willReturn([$file1]); + $folder1->method('getFirstNodeById')->with(42)->willReturn($file1); $folder2 = $this->createMock(Folder::class); - $folder2->method('getById')->with(43)->willReturn([$file2]); + $folder2->method('getFirstNodeById')->with(43)->willReturn($file2); $this->rootFolder->method('getUserFolder')->willReturnMap([ ['user2', $folder1], @@ -2179,7 +2179,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $file->method('getId')->willReturn(42); $this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf(); - $this->rootFolder->method('getById')->willReturn([$file]); + $this->rootFolder->method('getFirstNodeById')->willReturn($file); $share = $this->provider->getShareById($id, null); @@ -2215,7 +2215,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $folder->method('getId')->willReturn(42); $this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf(); - $this->rootFolder->method('getById')->willReturn([$folder]); + $this->rootFolder->method('getFirstNodeById')->willReturn($folder); $share = $this->provider->getShareById($id, 'user0'); @@ -2881,23 +2881,23 @@ class DefaultShareProviderTest extends \Test\TestCase { $ownerPath1 = $this->createMock(File::class); $shareOwner1Folder = $this->createMock(Folder::class); - $shareOwner1Folder->method('getById')->willReturn([$ownerPath1]); + $shareOwner1Folder->method('getFirstNodeById')->willReturn($ownerPath1); $ownerPath2 = $this->createMock(File::class); $shareOwner2Folder = $this->createMock(Folder::class); - $shareOwner2Folder->method('getById')->willReturn([$ownerPath2]); + $shareOwner2Folder->method('getFirstNodeById')->willReturn($ownerPath2); $ownerPath3 = $this->createMock(File::class); $shareOwner3Folder = $this->createMock(Folder::class); - $shareOwner3Folder->method('getById')->willReturn([$ownerPath3]); + $shareOwner3Folder->method('getFirstNodeById')->willReturn($ownerPath3); $ownerPath4 = $this->createMock(File::class); $shareOwner4Folder = $this->createMock(Folder::class); - $shareOwner4Folder->method('getById')->willReturn([$ownerPath4]); + $shareOwner4Folder->method('getFirstNodeById')->willReturn($ownerPath4); $ownerPath5 = $this->createMock(File::class); $shareOwner5Folder = $this->createMock(Folder::class); - $shareOwner5Folder->method('getById')->willReturn([$ownerPath5]); + $shareOwner5Folder->method('getFirstNodeById')->willReturn($ownerPath5); $this->rootFolder ->method('getUserFolder') diff --git a/tests/lib/Share20/ManagerTest.php b/tests/lib/Share20/ManagerTest.php index 25c011825da..049450133dd 100644 --- a/tests/lib/Share20/ManagerTest.php +++ b/tests/lib/Share20/ManagerTest.php @@ -795,9 +795,9 @@ class ManagerTest extends \Test\TestCase { ->willReturn(42); // Id 108 is used in the data to refer to the node of the share. $userFolder->expects($this->any()) - ->method('getById') + ->method('getFirstNodeById') ->with(108) - ->willReturn([$share->getNode()]); + ->willReturn($share->getNode()); $userFolder->expects($this->any()) ->method('getRelativePath') ->willReturnArgument(0); @@ -4464,9 +4464,9 @@ class ManagerTest extends \Test\TestCase { ->willReturn($userFolder); $folder->method('getPath') ->willReturn('/owner/files/folder'); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with($this->equalTo(42)) - ->willReturn([12 => $file]); + ->willReturn($file); $userFolder->method('getPath') ->willReturn('/user1/files'); @@ -4583,9 +4583,9 @@ class ManagerTest extends \Test\TestCase { ->willReturn($userFolder); $folder->method('getPath') ->willReturn('/owner/files/folder'); - $userFolder->method('getById') + $userFolder->method('getFirstNodeById') ->with($this->equalTo(42)) - ->willReturn([42 => $file]); + ->willReturn($file); $userFolder->method('getPath') ->willReturn('/user1/files'); -- 2.39.5