Browse Source

perf: switch places that always use the first getById result to getFirstNodeById

Signed-off-by: Robin Appelman <robin@icewind.nl>
tags/v29.0.0beta1
Robin Appelman 3 months ago
parent
commit
e7a7b4a401
50 changed files with 316 additions and 313 deletions
  1. 1
    1
      apps/dav/lib/BulkUpload/BulkUploadPlugin.php
  2. 3
    3
      apps/dav/lib/Connector/Sabre/FilesReportPlugin.php
  3. 2
    3
      apps/dav/lib/Controller/DirectController.php
  4. 6
    3
      apps/dav/lib/Direct/DirectFile.php
  5. 2
    1
      apps/dav/lib/RootCollection.php
  6. 10
    3
      apps/dav/lib/SystemTag/SystemTagsRelationsCollection.php
  7. 6
    6
      apps/dav/tests/unit/Connector/Sabre/FilesReportPluginTest.php
  8. 4
    4
      apps/dav/tests/unit/Controller/DirectControllerTest.php
  9. 2
    2
      apps/dav/tests/unit/Direct/DirectFileTest.php
  10. 16
    16
      apps/dav/tests/unit/SystemTag/SystemTagsObjectTypeCollectionTest.php
  11. 4
    4
      apps/federatedfilesharing/lib/FederatedShareProvider.php
  12. 2
    3
      apps/files/lib/Activity/Helper.php
  13. 2
    4
      apps/files/lib/Activity/Provider.php
  14. 3
    3
      apps/files/lib/BackgroundJob/TransferOwnership.php
  15. 6
    6
      apps/files/lib/Collaboration/Resources/ResourceProvider.php
  16. 7
    10
      apps/files/lib/Controller/ViewController.php
  17. 3
    3
      apps/files/lib/Listener/SyncLivePhotosListener.php
  18. 5
    5
      apps/files/tests/Controller/ViewControllerTest.php
  19. 2
    3
      apps/files_reminders/lib/Model/RichReminder.php
  20. 2
    3
      apps/files_reminders/lib/Notification/Notifier.php
  21. 2
    2
      apps/files_reminders/lib/Service/ReminderService.php
  22. 3
    3
      apps/files_sharing/lib/Collaboration/ShareRecipientSorter.php
  23. 2
    4
      apps/files_sharing/lib/Controller/DeletedShareAPIController.php
  24. 12
    18
      apps/files_sharing/lib/Controller/ShareAPIController.php
  25. 1
    2
      apps/files_sharing/lib/Controller/ShareController.php
  26. 2
    2
      apps/files_sharing/lib/OrphanHelper.php
  27. 1
    3
      apps/files_sharing/lib/SharedStorage.php
  28. 5
    5
      apps/files_sharing/tests/Collaboration/ShareRecipientSorterTest.php
  29. 72
    52
      apps/files_sharing/tests/Controller/ShareAPIControllerTest.php
  30. 29
    18
      apps/files_sharing/tests/Controller/ShareControllerTest.php
  31. 1
    0
      apps/files_sharing/tests/SharedStorageTest.php
  32. 4
    3
      apps/files_trashbin/lib/Trash/LegacyTrashBackend.php
  33. 2
    4
      apps/files_versions/lib/Sabre/VersionRoot.php
  34. 3
    3
      apps/files_versions/lib/Storage.php
  35. 1
    2
      apps/files_versions/lib/Versions/LegacyVersionsBackend.php
  36. 3
    3
      core/Command/FilesMetadata/Get.php
  37. 1
    5
      core/Command/Info/FileUtils.php
  38. 1
    5
      core/Command/Preview/Generate.php
  39. 2
    4
      core/Controller/PreviewController.php
  40. 2
    6
      lib/private/Collaboration/Reference/File/FileReferenceProvider.php
  41. 3
    3
      lib/private/DirectEditing/Manager.php
  42. 1
    6
      lib/private/Files/Config/CachedMountInfo.php
  43. 1
    1
      lib/private/Files/Node/Folder.php
  44. 3
    4
      lib/private/FilesMetadata/Job/UpdateSingleMetadata.php
  45. 2
    4
      lib/private/Share20/Manager.php
  46. 9
    5
      lib/private/Share20/Share.php
  47. 1
    1
      lib/private/SpeechToText/TranscriptionJob.php
  48. 1
    1
      lib/public/Share/IShare.php
  49. 52
    52
      tests/lib/Share20/DefaultShareProviderTest.php
  50. 6
    6
      tests/lib/Share20/ManagerTest.php

+ 1
- 1
apps/dav/lib/BulkUpload/BulkUploadPlugin.php View File



$node = $this->userFolder->newFile($headers['x-file-path'], $content); $node = $this->userFolder->newFile($headers['x-file-path'], $content);
$node->touch($mtime); $node->touch($mtime);
$node = $this->userFolder->getById($node->getId())[0];
$node = $this->userFolder->getFirstNodeById($node->getId());


$writtenFiles[$headers['x-file-path']] = [ $writtenFiles[$headers['x-file-path']] = [
"error" => false, "error" => false,

+ 3
- 3
apps/dav/lib/Connector/Sabre/FilesReportPlugin.php View File

} }
$folder = $this->userFolder; $folder = $this->userFolder;
if (trim($rootNode->getPath(), '/') !== '') { if (trim($rootNode->getPath(), '/') !== '') {
/** @var Folder $folder */
$folder = $folder->get($rootNode->getPath()); $folder = $folder->get($rootNode->getPath());
} }


$results = []; $results = [];
foreach ($fileIds as $fileId) { foreach ($fileIds as $fileId) {
$entry = $folder->getById($fileId);
$entry = $folder->getFirstNodeById($fileId);
if ($entry) { if ($entry) {
$entry = current($entry);
$results[] = $this->wrapNode($entry); $results[] = $this->wrapNode($entry);
} }
} }
return $results; 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) { if ($node instanceof \OCP\Files\File) {
return new File($this->fileView, $node); return new File($this->fileView, $node);
} else { } else {

+ 2
- 3
apps/dav/lib/Controller/DirectController.php View File

public function getUrl(int $fileId, int $expirationTime = 60 * 60 * 8): DataResponse { public function getUrl(int $fileId, int $expirationTime = 60 * 60 * 8): DataResponse {
$userFolder = $this->rootFolder->getUserFolder($this->userId); $userFolder = $this->rootFolder->getUserFolder($this->userId);


$files = $userFolder->getById($fileId);
$file = $userFolder->getFirstNodeById($fileId);


if ($files === []) {
if (!$file) {
throw new OCSNotFoundException(); throw new OCSNotFoundException();
} }


throw new OCSBadRequestException('Expiration time should be greater than 0 and less than or equal to ' . (60 * 60 * 24)); 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)) { if (!($file instanceof File)) {
throw new OCSBadRequestException('Direct download only works for files'); throw new OCSBadRequestException('Direct download only works for files');
} }

+ 6
- 3
apps/dav/lib/Direct/DirectFile.php View File

private function getFile() { private function getFile() {
if ($this->file === null) { if ($this->file === null) {
$userFolder = $this->rootFolder->getUserFolder($this->direct->getUserId()); $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(); 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; return $this->file;

+ 2
- 1
apps/dav/lib/RootCollection.php View File

\OC::$server->getSystemTagObjectMapper(), \OC::$server->getSystemTagObjectMapper(),
\OC::$server->getUserSession(), \OC::$server->getUserSession(),
$groupManager, $groupManager,
$dispatcher
$dispatcher,
$rootFolder,
); );
$systemTagInUseCollection = \OCP\Server::get(SystemTag\SystemTagsInUseCollection::class); $systemTagInUseCollection = \OCP\Server::get(SystemTag\SystemTagsInUseCollection::class);
$commentsCollection = new Comments\RootCollection( $commentsCollection = new Comments\RootCollection(

+ 10
- 3
apps/dav/lib/SystemTag/SystemTagsRelationsCollection.php View File

namespace OCA\DAV\SystemTag; namespace OCA\DAV\SystemTag;


use OCP\EventDispatcher\IEventDispatcher; use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\IRootFolder;
use OCP\IGroupManager; use OCP\IGroupManager;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\SystemTag\ISystemTagManager; use OCP\SystemTag\ISystemTagManager;
IUserSession $userSession, IUserSession $userSession,
IGroupManager $groupManager, IGroupManager $groupManager,
IEventDispatcher $dispatcher, IEventDispatcher $dispatcher,
IRootFolder $rootFolder,
) { ) {
$children = [ $children = [
new SystemTagsObjectTypeCollection( new SystemTagsObjectTypeCollection(
$tagMapper, $tagMapper,
$userSession, $userSession,
$groupManager, $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;
}
} }
), ),
]; ];

+ 6
- 6
apps/dav/tests/unit/Connector/Sabre/FilesReportPluginTest.php View File

->willReturn('/'); ->willReturn('/');


$this->userFolder->expects($this->exactly(2)) $this->userFolder->expects($this->exactly(2))
->method('getById')
->method('getFirstNodeById')
->withConsecutive( ->withConsecutive(
['111'], ['111'],
['222'], ['222'],
) )
->willReturnOnConsecutiveCalls( ->willReturnOnConsecutiveCalls(
[$filesNode1],
[$filesNode2],
$filesNode1,
$filesNode2,
); );


/** @var \OCA\DAV\Connector\Sabre\Directory|MockObject $reportTargetNode */ /** @var \OCA\DAV\Connector\Sabre\Directory|MockObject $reportTargetNode */
->willReturn($subNode); ->willReturn($subNode);


$subNode->expects($this->exactly(2)) $subNode->expects($this->exactly(2))
->method('getById')
->method('getFirstNodeById')
->withConsecutive( ->withConsecutive(
['111'], ['111'],
['222'], ['222'],
) )
->willReturnOnConsecutiveCalls( ->willReturnOnConsecutiveCalls(
[$filesNode1],
[$filesNode2],
$filesNode1,
$filesNode2,
); );


/** @var \OCA\DAV\Connector\Sabre\Directory|MockObject $reportTargetNode */ /** @var \OCA\DAV\Connector\Sabre\Directory|MockObject $reportTargetNode */

+ 4
- 4
apps/dav/tests/unit/Controller/DirectControllerTest.php View File



$folder = $this->createMock(Folder::class); $folder = $this->createMock(Folder::class);


$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with(101) ->with(101)
->willReturn([$folder]);
->willReturn($folder);


$this->expectException(OCSBadRequestException::class); $this->expectException(OCSBadRequestException::class);
$this->controller->getUrl(101); $this->controller->getUrl(101);
$this->timeFactory->method('getTime') $this->timeFactory->method('getTime')
->willReturn(42); ->willReturn(42);


$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with(101) ->with(101)
->willReturn([$file]);
->willReturn($file);


$userFolder->method('getRelativePath') $userFolder->method('getRelativePath')
->willReturn('/path'); ->willReturn('/path');

+ 2
- 2
apps/dav/tests/unit/Direct/DirectFileTest.php View File

->willReturn($this->userFolder); ->willReturn($this->userFolder);


$this->file = $this->createMock(File::class); $this->file = $this->createMock(File::class);
$this->userFolder->method('getById')
$this->userFolder->method('getFirstNodeById')
->with(42) ->with(42)
->willReturn([$this->file]);
->willReturn($this->file);


$this->eventDispatcher = $this->createMock(IEventDispatcher::class); $this->eventDispatcher = $this->createMock(IEventDispatcher::class);



+ 16
- 16
apps/dav/tests/unit/SystemTag/SystemTagsObjectTypeCollectionTest.php View File

$userFolder = $this->userFolder; $userFolder = $this->userFolder;


$closure = function ($name) use ($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( $this->node = new \OCA\DAV\SystemTag\SystemTagsObjectTypeCollection(
); );
} }


public function testForbiddenCreateFile(): void { public function testForbiddenCreateFile(): void {
$this->expectException(\Sabre\DAV\Exception\Forbidden::class); $this->expectException(\Sabre\DAV\Exception\Forbidden::class);


$this->node->createFile('555'); $this->node->createFile('555');
} }


public function testForbiddenCreateDirectory(): void { public function testForbiddenCreateDirectory(): void {
$this->expectException(\Sabre\DAV\Exception\Forbidden::class); $this->expectException(\Sabre\DAV\Exception\Forbidden::class);




public function testGetChild(): void { public function testGetChild(): void {
$this->userFolder->expects($this->once()) $this->userFolder->expects($this->once())
->method('getById')
->method('getFirstNodeById')
->with('555') ->with('555')
->willReturn([true]);
->willReturn($this->createMock(\OCP\Files\Node::class));
$childNode = $this->node->getChild('555'); $childNode = $this->node->getChild('555');


$this->assertInstanceOf('\OCA\DAV\SystemTag\SystemTagsObjectMappingCollection', $childNode); $this->assertInstanceOf('\OCA\DAV\SystemTag\SystemTagsObjectMappingCollection', $childNode);
$this->assertEquals('555', $childNode->getName()); $this->assertEquals('555', $childNode->getName());
} }


public function testGetChildWithoutAccess(): void { public function testGetChildWithoutAccess(): void {
$this->expectException(\Sabre\DAV\Exception\NotFound::class); $this->expectException(\Sabre\DAV\Exception\NotFound::class);


$this->userFolder->expects($this->once()) $this->userFolder->expects($this->once())
->method('getById')
->method('getFirstNodeById')
->with('555') ->with('555')
->willReturn([]);
->willReturn(null);
$this->node->getChild('555'); $this->node->getChild('555');
} }


public function testGetChildren(): void { public function testGetChildren(): void {
$this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class); $this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class);




public function testChildExists(): void { public function testChildExists(): void {
$this->userFolder->expects($this->once()) $this->userFolder->expects($this->once())
->method('getById')
->method('getFirstNodeById')
->with('123') ->with('123')
->willReturn([true]);
->willReturn($this->createMock(\OCP\Files\Node::class));
$this->assertTrue($this->node->childExists('123')); $this->assertTrue($this->node->childExists('123'));
} }


public function testChildExistsWithoutAccess(): void { public function testChildExistsWithoutAccess(): void {
$this->userFolder->expects($this->once()) $this->userFolder->expects($this->once())
->method('getById')
->method('getFirstNodeById')
->with('555') ->with('555')
->willReturn([]);
->willReturn(null);
$this->assertFalse($this->node->childExists('555')); $this->assertFalse($this->node->childExists('555'));
} }


public function testDelete(): void { public function testDelete(): void {
$this->expectException(\Sabre\DAV\Exception\Forbidden::class); $this->expectException(\Sabre\DAV\Exception\Forbidden::class);


$this->node->delete(); $this->node->delete();
} }


public function testSetName(): void { public function testSetName(): void {
$this->expectException(\Sabre\DAV\Exception\Forbidden::class); $this->expectException(\Sabre\DAV\Exception\Forbidden::class);



+ 4
- 4
apps/federatedfilesharing/lib/FederatedShareProvider.php View File

* *
* @param string $userId * @param string $userId
* @param int $id * @param int $id
* @return \OCP\Files\File|\OCP\Files\Folder
* @return \OCP\Files\Node
* @throws InvalidShare * @throws InvalidShare
*/ */
private function getNode($userId, $id) { private function getNode($userId, $id) {
throw new InvalidShare(); throw new InvalidShare();
} }


$nodes = $userFolder->getById($id);
$node = $userFolder->getFirstNodeById($id);


if (empty($nodes)) {
if (!$node) {
throw new InvalidShare(); throw new InvalidShare();
} }


return $nodes[0];
return $node;
} }


/** /**

+ 2
- 3
apps/files/lib/Activity/Helper.php View File

$userFolder = $this->rootFolder->getUserFolder($user); $userFolder = $this->rootFolder->getUserFolder($user);
$favoriteNodes = []; $favoriteNodes = [];
foreach ($favorites as $favorite) { 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) { if (!$foldersOnly || $node instanceof Folder) {
$favoriteNodes[] = $node; $favoriteNodes[] = $node;
} }

+ 2
- 4
apps/files/lib/Activity/Provider.php View File

} }


$userFolder = $this->rootFolder->getUserFolder($this->activityManager->getCurrentUserId()); $userFolder = $this->rootFolder->getUserFolder($this->activityManager->getCurrentUserId());
$files = $userFolder->getById($fileId);
if (empty($files)) {
$file = $userFolder->getFirstNodeById($fileId);
if (!$file) {
try { try {
// Deleted, try with parent // Deleted, try with parent
$file = $this->findExistingParent($userFolder, dirname($path)); $file = $this->findExistingParent($userFolder, dirname($path));
return $file; return $file;
} }


$file = array_shift($files);

if ($file instanceof Folder && $file->isEncrypted()) { if ($file instanceof Folder && $file->isEncrypted()) {
// If the folder is encrypted, it is the Container, // If the folder is encrypted, it is the Container,
// but can be the name is just fine. // but can be the name is just fine.

+ 3
- 3
apps/files/lib/BackgroundJob/TransferOwnership.php View File

$fileId = $transfer->getFileId(); $fileId = $transfer->getFileId();


$userFolder = $this->rootFolder->getUserFolder($sourceUser); $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->logger->alert('Could not transfer ownership: Node not found');
$this->failedNotication($transfer); $this->failedNotication($transfer);
return; return;
} }
$path = $userFolder->getRelativePath($nodes[0]->getPath());
$path = $userFolder->getRelativePath($node->getPath());


$sourceUserObject = $this->userManager->get($sourceUser); $sourceUserObject = $this->userManager->get($sourceUser);
$destinationUserObject = $this->userManager->get($destinationUser); $destinationUserObject = $this->userManager->get($destinationUser);

+ 6
- 6
apps/files/lib/Collaboration/Resources/ResourceProvider.php View File

if (isset($this->nodes[(int) $resource->getId()])) { if (isset($this->nodes[(int) $resource->getId()])) {
return $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 $this->nodes[(int) $resource->getId()];
} }
return null; return null;
} }


$userFolder = $this->rootFolder->getUserFolder($user->getUID()); $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; return true;
} }



+ 7
- 10
apps/files/lib/Controller/ViewController.php View File



$uid = $user->getUID(); $uid = $user->getUID();
$userFolder = $this->rootFolder->getUserFolder($uid); $userFolder = $this->rootFolder->getUserFolder($uid);
$nodes = $userFolder->getById((int) $fileid);
$node = array_shift($nodes);
$node = $userFolder->getFirstNodeById((int) $fileid);


if ($node === null) { if ($node === null) {
return; return;
private function redirectToFileIfInTrashbin($fileId): RedirectResponse { private function redirectToFileIfInTrashbin($fileId): RedirectResponse {
$uid = $this->userSession->getUser()->getUID(); $uid = $this->userSession->getUser()->getUID();
$baseFolder = $this->rootFolder->getUserFolder($uid); $baseFolder = $this->rootFolder->getUserFolder($uid);
$nodes = $baseFolder->getById($fileId);
$node = $baseFolder->getFirstNodeById($fileId);
$params = []; $params = [];


if (empty($nodes) && $this->appManager->isEnabledForUser('files_trashbin')) {
if (!$node && $this->appManager->isEnabledForUser('files_trashbin')) {
/** @var Folder */ /** @var Folder */
$baseFolder = $this->rootFolder->get($uid . '/files_trashbin/files/'); $baseFolder = $this->rootFolder->get($uid . '/files_trashbin/files/');
$nodes = $baseFolder->getById($fileId);
$node = $baseFolder->getFirstNodeById($fileId);
$params['view'] = 'trashbin'; $params['view'] = 'trashbin';


if (!empty($nodes)) {
$node = current($nodes);
if ($node) {
$params['fileid'] = $fileId; $params['fileid'] = $fileId;
if ($node instanceof Folder) { if ($node instanceof Folder) {
// set the full path to enter the folder // set the full path to enter the folder
private function redirectToFile(int $fileId) { private function redirectToFile(int $fileId) {
$uid = $this->userSession->getUser()->getUID(); $uid = $this->userSession->getUser()->getUID();
$baseFolder = $this->rootFolder->getUserFolder($uid); $baseFolder = $this->rootFolder->getUserFolder($uid);
$nodes = $baseFolder->getById($fileId);
$node = $baseFolder->getFirstNodeById($fileId);
$params = ['view' => 'files']; $params = ['view' => 'files'];


try { try {
} catch (NotFoundException $e) { } catch (NotFoundException $e) {
} }


if (!empty($nodes)) {
$node = current($nodes);
if ($node) {
$params['fileid'] = $fileId; $params['fileid'] = $fileId;
if ($node instanceof Folder) { if ($node instanceof Folder) {
// set the full path to enter the folder // set the full path to enter the folder

+ 3
- 3
apps/files/lib/Listener/SyncLivePhotosListener.php View File

$peerFileId = (int)$metadata->getString('files-live-photo'); $peerFileId = (int)$metadata->getString('files-live-photo');


// Check the user's folder. // 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. // Check the user's trashbin.

+ 5
- 5
apps/files/tests/Controller/ViewControllerTest.php View File

[$this->user->getUID(), 'files', 'crop_image_previews', true, true], [$this->user->getUID(), 'files', 'crop_image_previews', true, true],
[$this->user->getUID(), 'files', 'show_grid', true], [$this->user->getUID(), 'files', 'show_grid', true],
]); ]);
$baseFolderFiles = $this->getMockBuilder(Folder::class)->getMock(); $baseFolderFiles = $this->getMockBuilder(Folder::class)->getMock();


$this->rootFolder->expects($this->any()) $this->rootFolder->expects($this->any())
->willReturn($baseFolderTrash); ->willReturn($baseFolderTrash);


$baseFolderFiles->expects($this->any()) $baseFolderFiles->expects($this->any())
->method('getById')
->method('getFirstNodeById')
->with(123) ->with(123)
->willReturn([]);
->willReturn(null);


$node = $this->getMockBuilder(File::class)->getMock(); $node = $this->getMockBuilder(File::class)->getMock();
$node->expects($this->once()) $node->expects($this->once())
->willReturn($parentNode); ->willReturn($parentNode);


$baseFolderTrash->expects($this->once()) $baseFolderTrash->expects($this->once())
->method('getById')
->method('getFirstNodeById')
->with(123) ->with(123)
->willReturn([$node]);
->willReturn($node);
$baseFolderTrash->expects($this->once()) $baseFolderTrash->expects($this->once())
->method('getRelativePath') ->method('getRelativePath')
->with('testuser1/files_trashbin/files/test.d1462861890/sub') ->with('testuser1/files_trashbin/files/test.d1462861890/sub')

+ 2
- 3
apps/files_reminders/lib/Model/RichReminder.php View File

* @throws NodeNotFoundException * @throws NodeNotFoundException
*/ */
public function getNode(): Node { 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(); throw new NodeNotFoundException();
} }
$node = reset($nodes);
return $node; return $node;
} }



+ 2
- 3
apps/files_reminders/lib/Notification/Notifier.php View File

$params = $notification->getSubjectParameters(); $params = $notification->getSubjectParameters();
$fileId = $params['fileId']; $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(); throw new InvalidArgumentException();
} }
$node = reset($nodes);


$path = rtrim($node->getPath(), '/'); $path = rtrim($node->getPath(), '/');
if (strpos($path, '/' . $notification->getUser() . '/files/') === 0) { if (strpos($path, '/' . $notification->getUser() . '/files/') === 0) {

+ 2
- 2
apps/files_reminders/lib/Service/ReminderService.php View File

$this->reminderMapper->update($reminder); $this->reminderMapper->update($reminder);
return false; return false;
} catch (DoesNotExistException $e) { } 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(); throw new NodeNotFoundException();
} }
// Create new reminder if no reminder is found // Create new reminder if no reminder is found

+ 3
- 3
apps/files_sharing/lib/Collaboration/ShareRecipientSorter.php View File

} }
$userFolder = $this->rootFolder->getUserFolder($user->getUID()); $userFolder = $this->rootFolder->getUserFolder($user->getUID());
/** @var Node[] $nodes */ /** @var Node[] $nodes */
$nodes = $userFolder->getById((int)$context['itemId']);
if (count($nodes) === 0) {
$node = $userFolder->getFirstNodeById((int)$context['itemId']);
if (!$node) {
return; return;
} }
$al = $this->shareManager->getAccessList($nodes[0]);
$al = $this->shareManager->getAccessList($node);


foreach ($sortArray as $type => &$byType) { foreach ($sortArray as $type => &$byType) {
if (!isset($al[$type]) || !is_array($al[$type])) { if (!isset($al[$type]) || !is_array($al[$type])) {

+ 2
- 4
apps/files_sharing/lib/Controller/DeletedShareAPIController.php View File

'path' => $share->getTarget(), 'path' => $share->getTarget(),
]; ];
$userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); $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 // fallback to guessing the path
$node = $userFolder->get($share->getTarget()); $node = $userFolder->get($share->getTarget());
if ($node === null || $share->getTarget() === '') { if ($node === null || $share->getTarget() === '') {
throw new NotFoundException(); throw new NotFoundException();
} }
} else {
$node = $nodes[0];
} }


$result['path'] = $userFolder->getRelativePath($node->getPath()); $result['path'] = $userFolder->getRelativePath($node->getPath());

+ 12
- 18
apps/files_sharing/lib/Controller/ShareAPIController.php View File

if ($recipientNode) { if ($recipientNode) {
$node = $recipientNode; $node = $recipientNode;
} else { } else {
$nodes = $userFolder->getById($share->getNodeId());
if (empty($nodes)) {
$node = $userFolder->getFirstNodeById($share->getNodeId());
if (!$node) {
// fallback to guessing the path // fallback to guessing the path
$node = $userFolder->get($share->getTarget()); $node = $userFolder->get($share->getTarget());
if ($node === null || $share->getTarget() === '') { if ($node === null || $share->getTarget() === '') {
throw new NotFoundException(); throw new NotFoundException();
} }
} else {
$node = reset($nodes);
} }
} }


$owner = $node->getOwner() $owner = $node->getOwner()
->getUID(); ->getUID();
$userFolder = $this->rootFolder->getUserFolder($owner); $userFolder = $this->rootFolder->getUserFolder($owner);
$nodes = $userFolder->getById($node->getId());
$node = array_shift($nodes);
$node = $userFolder->getFirstNodeById($node->getId());
} }
$basePath = $userFolder->getPath(); $basePath = $userFolder->getPath();


foreach ($nodes as $node) { foreach ($nodes as $node) {
$getShares = $this->getFormattedShares($owner, $node, false, true); $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()); $subPath = $currentUserFolder->getRelativePath($parent->getPath());


$result = array_filter(array_map(function (IShare $share) { $result = array_filter(array_map(function (IShare $share) {
$userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); $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 // fallback to guessing the path
$node = $userFolder->get($share->getTarget()); $node = $userFolder->get($share->getTarget());
if ($node === null || $share->getTarget() === '') { if ($node === null || $share->getTarget() === '') {
return null; return null;
} }
} else {
$node = $nodes[0];
} }


try { try {
// Have reshare rights on the shared file/folder ? // Have reshare rights on the shared file/folder ?
// Does the currentUser have access to the shared file? // Does the currentUser have access to the shared file?
$userFolder = $this->rootFolder->getUserFolder($this->currentUser); $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; return true;
} }


return; // Probably in a test return; // Probably in a test
} }
$userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
$nodes = $userFolder->getById($share->getNodeId());
if (empty($nodes)) {
$node = $userFolder->getFirstNodeById($share->getNodeId());
if (!$node) {
return; return;
} }
$node = $nodes[0];
if ($node->getStorage()->instanceOfStorage(SharedStorage::class)) { if ($node->getStorage()->instanceOfStorage(SharedStorage::class)) {
$storage = $node->getStorage(); $storage = $node->getStorage();
if ($storage instanceof Wrapper) { if ($storage instanceof Wrapper) {

+ 1
- 2
apps/files_sharing/lib/Controller/ShareController.php View File

$fileId = $node->getId(); $fileId = $node->getId();


$userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
$userNodeList = $userFolder->getById($fileId);
$userNode = $userNodeList[0];
$userNode = $userFolder->getFirstNodeById($fileId);
$ownerFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); $ownerFolder = $this->rootFolder->getUserFolder($share->getShareOwner());
$userPath = $userFolder->getRelativePath($userNode->getPath()); $userPath = $userFolder->getRelativePath($userNode->getPath());
$ownerPath = $ownerFolder->getRelativePath($node->getPath()); $ownerPath = $ownerFolder->getRelativePath($node->getPath());

+ 2
- 2
apps/files_sharing/lib/OrphanHelper.php View File

} catch (NoUserException $e) { } catch (NoUserException $e) {
return false; return false;
} }
$nodes = $userFolder->getById($fileId);
return count($nodes) > 0;
$node = $userFolder->getFirstNodeById($fileId);
return $node !== null;
} }


/** /**

+ 1
- 3
apps/files_sharing/lib/SharedStorage.php View File

$rootFolder = \OC::$server->get(IRootFolder::class); $rootFolder = \OC::$server->get(IRootFolder::class);
$this->ownerUserFolder = $rootFolder->getUserFolder($this->superShare->getShareOwner()); $this->ownerUserFolder = $rootFolder->getUserFolder($this->superShare->getShareOwner());
$sourceId = $this->superShare->getNodeId(); $sourceId = $this->superShare->getNodeId();
$ownerNodes = $this->ownerUserFolder->getById($sourceId);
/** @var Node|false $ownerNode */
$ownerNode = current($ownerNodes);
$ownerNode = $this->ownerUserFolder->getFirstNodeById($sourceId);
if (!$ownerNode) { if (!$ownerNode) {
$this->storage = new FailedStorage(['exception' => new NotFoundException("File by id $sourceId not found")]); $this->storage = new FailedStorage(['exception' => new NotFoundException("File by id $sourceId not found")]);
$this->cache = new FailedCache(); $this->cache = new FailedCache();

+ 5
- 5
apps/files_sharing/tests/Collaboration/ShareRecipientSorterTest.php View File



if ($data['context']['itemType'] === 'files') { if ($data['context']['itemType'] === 'files') {
$folder->expects($this->once()) $folder->expects($this->once())
->method('getById')
->method('getFirstNodeById')
->with($data['context']['itemId']) ->with($data['context']['itemId'])
->willReturn([$node]);
->willReturn($node);


$this->shareManager->expects($this->once()) $this->shareManager->expects($this->once())
->method('getAccessList') ->method('getAccessList')
->willReturn($data['accessList']); ->willReturn($data['accessList']);
} else { } else {
$folder->expects($this->never()) $folder->expects($this->never())
->method('getById');
->method('getFirstNodeById');
$this->shareManager->expects($this->never()) $this->shareManager->expects($this->never())
->method('getAccessList'); ->method('getAccessList');
} }
->willReturn($folder); ->willReturn($folder);


$folder->expects($this->once()) $folder->expects($this->once())
->method('getById')
->willReturn([]);
->method('getFirstNodeById')
->willReturn(null);


$user = $this->createMock(IUser::class); $user = $this->createMock(IUser::class);
$user->expects($this->any()) $user->expects($this->any())

+ 72
- 52
apps/files_sharing/tests/Controller/ShareAPIControllerTest.php View File

*/ */
public function testDeleteShareFileOwner() { public function testDeleteShareFileOwner() {
$node = $this->getMockBuilder(File::class)->getMock(); $node = $this->getMockBuilder(File::class)->getMock();
$node->method('getId')->willReturn(1);


$share = $this->newShare(); $share = $this->newShare();
$share->setShareOwner($this->currentUser) $share->setShareOwner($this->currentUser)
*/ */
public function testDeleteSharedWithMyGroup() { public function testDeleteSharedWithMyGroup() {
$node = $this->getMockBuilder(File::class)->getMock(); $node = $this->getMockBuilder(File::class)->getMock();
$node->method('getId')->willReturn(1);


$share = $this->newShare(); $share = $this->newShare();
$share->setShareType(IShare::TYPE_GROUP) $share->setShareType(IShare::TYPE_GROUP)
->with($this->currentUser) ->with($this->currentUser)
->willReturn($userFolder); ->willReturn($userFolder);


$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with($share->getNodeId()) ->with($share->getNodeId())
->willReturn([$share->getNode()]);
->willReturn($share->getNode());


$this->shareManager->expects($this->once()) $this->shareManager->expects($this->once())
->method('deleteFromSelf') ->method('deleteFromSelf')
$this->expectExceptionMessage('Wrong share ID, share does not exist'); $this->expectExceptionMessage('Wrong share ID, share does not exist');


$node = $this->getMockBuilder(File::class)->getMock(); $node = $this->getMockBuilder(File::class)->getMock();
$node->method('getId')->willReturn(42);


$share = $this->newShare(); $share = $this->newShare();
$share->setShareType(IShare::TYPE_GROUP) $share->setShareType(IShare::TYPE_GROUP)
->with($this->currentUser) ->with($this->currentUser)
->willReturn($userFolder); ->willReturn($userFolder);


$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with($share->getNodeId()) ->with($share->getNodeId())
->willReturn([$share->getNode()]);
->willReturn($share->getNode());


$this->shareManager->expects($this->never()) $this->shareManager->expects($this->never())
->method('deleteFromSelf'); ->method('deleteFromSelf');
->with($share->getNodeId()) ->with($share->getNodeId())
->willReturn([$share->getNode()]); ->willReturn([$share->getNode()]);


$userFolder->method('getFirstNodeById')
->with($share->getNodeId())
->willReturn($share->getNode());

$this->rootFolder->method('getUserFolder') $this->rootFolder->method('getUserFolder')
->with($this->currentUser) ->with($this->currentUser)
->willReturn($userFolder); ->willReturn($userFolder);
->with($this->currentUser) ->with($this->currentUser)
->willReturn($userFolder); ->willReturn($userFolder);


$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with($share->getNodeId()) ->with($share->getNodeId())
->willReturn([$file]);
->willReturn($file);


$file->method('getPermissions') $file->method('getPermissions')
->will($this->onConsecutiveCalls(\OCP\Constants::PERMISSION_SHARE, \OCP\Constants::PERMISSION_READ)); ->will($this->onConsecutiveCalls(\OCP\Constants::PERMISSION_SHARE, \OCP\Constants::PERMISSION_READ));
->with($this->currentUser) ->with($this->currentUser)
->willReturn($userFolder); ->willReturn($userFolder);


$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with($share->getNodeId()) ->with($share->getNodeId())
->willReturn([$share->getNode()]);
->willReturn($share->getNode());


if (!$helperAvailable) { if (!$helperAvailable) {
$this->appManager->method('isEnabledForUser') $this->appManager->method('isEnabledForUser')
]); ]);


$path = $this->getMockBuilder(Folder::class)->getMock(); $path = $this->getMockBuilder(Folder::class)->getMock();
$path->method('getId')->willReturn(42);
$storage = $this->createMock(Storage::class); $storage = $this->createMock(Storage::class);
$storage->method('instanceOfStorage') $storage->method('instanceOfStorage')
->willReturnMap([ ->willReturnMap([
$this->expectExceptionMessage('Public upload disabled by the administrator'); $this->expectExceptionMessage('Public upload disabled by the administrator');


$path = $this->getMockBuilder(Folder::class)->getMock(); $path = $this->getMockBuilder(Folder::class)->getMock();
$path->method('getId')->willReturn(42);
$storage = $this->createMock(Storage::class); $storage = $this->createMock(Storage::class);
$storage->method('instanceOfStorage') $storage->method('instanceOfStorage')
->willReturnMap([ ->willReturnMap([
$this->expectExceptionMessage('Public upload is only possible for publicly shared folders'); $this->expectExceptionMessage('Public upload is only possible for publicly shared folders');


$path = $this->getMockBuilder(File::class)->getMock(); $path = $this->getMockBuilder(File::class)->getMock();
$path->method('getId')->willReturn(42);
$storage = $this->createMock(Storage::class); $storage = $this->createMock(Storage::class);
$storage->method('instanceOfStorage') $storage->method('instanceOfStorage')
->willReturnMap([ ->willReturnMap([
$ocs = $this->mockFormatShare(); $ocs = $this->mockFormatShare();


$path = $this->getMockBuilder(Folder::class)->getMock(); $path = $this->getMockBuilder(Folder::class)->getMock();
$path->method('getId')->willReturn(1);
$storage = $this->createMock(Storage::class); $storage = $this->createMock(Storage::class);
$storage->method('instanceOfStorage') $storage->method('instanceOfStorage')
->willReturnMap([ ->willReturnMap([
$ocs = $this->mockFormatShare(); $ocs = $this->mockFormatShare();


$path = $this->getMockBuilder(Folder::class)->getMock(); $path = $this->getMockBuilder(Folder::class)->getMock();
$path->method('getId')->willReturn(42);
$storage = $this->createMock(Storage::class); $storage = $this->createMock(Storage::class);
$storage->method('instanceOfStorage') $storage->method('instanceOfStorage')
->willReturnMap([ ->willReturnMap([
$ocs = $this->mockFormatShare(); $ocs = $this->mockFormatShare();


$path = $this->getMockBuilder(Folder::class)->getMock(); $path = $this->getMockBuilder(Folder::class)->getMock();
$path->method('getId')->willReturn(42);
$storage = $this->createMock(Storage::class); $storage = $this->createMock(Storage::class);
$storage->method('instanceOfStorage') $storage->method('instanceOfStorage')
->willReturnMap([ ->willReturnMap([
$ocs = $this->mockFormatShare(); $ocs = $this->mockFormatShare();


$path = $this->getMockBuilder(Folder::class)->getMock(); $path = $this->getMockBuilder(Folder::class)->getMock();
$path->method('getId')->willReturn(42);
$storage = $this->createMock(Storage::class); $storage = $this->createMock(Storage::class);
$storage->method('instanceOfStorage') $storage->method('instanceOfStorage')
->willReturnMap([ ->willReturnMap([
]); ]);


$path = $this->getMockBuilder(Folder::class)->getMock(); $path = $this->getMockBuilder(Folder::class)->getMock();
$path->method('getId')->willReturn(42);
$storage = $this->createMock(Storage::class); $storage = $this->createMock(Storage::class);
$storage->method('instanceOfStorage') $storage->method('instanceOfStorage')
->willReturnMap([ ->willReturnMap([
$ocs = $this->mockFormatShare(); $ocs = $this->mockFormatShare();


$path = $this->getMockBuilder(Folder::class)->getMock(); $path = $this->getMockBuilder(Folder::class)->getMock();
$path->method('getId')->willReturn(42);
$storage = $this->createMock(Storage::class); $storage = $this->createMock(Storage::class);
$storage->method('instanceOfStorage') $storage->method('instanceOfStorage')
->willReturnMap([ ->willReturnMap([
->willReturn($userFolder); ->willReturn($userFolder);


$path = $this->getMockBuilder(Folder::class)->getMock(); $path = $this->getMockBuilder(Folder::class)->getMock();
$path->method('getId')->willReturn(42);

$storage = $this->createMock(Storage::class); $storage = $this->createMock(Storage::class);
$storage->method('instanceOfStorage') $storage->method('instanceOfStorage')
->willReturnMap([ ->willReturnMap([
->with($this->currentUser) ->with($this->currentUser)
->willReturn($userFolder); ->willReturn($userFolder);


$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with($share->getNodeId()) ->with($share->getNodeId())
->willReturn([$share->getNode()]);
->willReturn($share->getNode());


$this->ocs->updateShare(42); $this->ocs->updateShare(42);
} }
->with($this->currentUser) ->with($this->currentUser)
->willReturn($userFolder); ->willReturn($userFolder);


$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with(42) ->with(42)
->willReturn([$node]);
->willReturn($node);


$mountPoint = $this->createMock(IMountPoint::class); $mountPoint = $this->createMock(IMountPoint::class);
$node->method('getMountPoint') $node->method('getMountPoint')
->with($this->currentUser) ->with($this->currentUser)
->willReturn($userFolder); ->willReturn($userFolder);


$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with(42) ->with(42)
->willReturn([$folder]);
->willReturn($folder);


$mountPoint = $this->createMock(IMountPoint::class); $mountPoint = $this->createMock(IMountPoint::class);
$folder->method('getMountPoint') $folder->method('getMountPoint')
->with($this->currentUser) ->with($this->currentUser)
->willReturn($userFolder); ->willReturn($userFolder);


$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with(42) ->with(42)
->willReturn([$folder]);
->willReturn($folder);


$mountPoint = $this->createMock(IMountPoint::class); $mountPoint = $this->createMock(IMountPoint::class);
$folder->method('getMountPoint') $folder->method('getMountPoint')
->with($this->currentUser) ->with($this->currentUser)
->willReturn($userFolder); ->willReturn($userFolder);


$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with(42) ->with(42)
->willReturn([$folder]);
->willReturn($folder);


$mountPoint = $this->createMock(IMountPoint::class); $mountPoint = $this->createMock(IMountPoint::class);
$folder->method('getMountPoint') $folder->method('getMountPoint')


$ocs = $this->mockFormatShare(); $ocs = $this->mockFormatShare();
[$userFolder, $folder] = $this->getNonSharedUserFolder(); [$userFolder, $folder] = $this->getNonSharedUserFolder();
$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with(42) ->with(42)
->willReturn([$folder]);
->willReturn($folder);
$this->rootFolder->method('getUserFolder') $this->rootFolder->method('getUserFolder')
->with($this->currentUser) ->with($this->currentUser)
->willReturn($userFolder); ->willReturn($userFolder);


$ocs = $this->mockFormatShare(); $ocs = $this->mockFormatShare();
[$userFolder, $folder] = $this->getNonSharedUserFolder(); [$userFolder, $folder] = $this->getNonSharedUserFolder();
$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with(42) ->with(42)
->willReturn([$folder]);
->willReturn($folder);
$this->rootFolder->method('getUserFolder') $this->rootFolder->method('getUserFolder')
->with($this->currentUser) ->with($this->currentUser)
->willReturn($userFolder); ->willReturn($userFolder);
$file->method('getId') $file->method('getId')
->willReturn(42); ->willReturn(42);
[$userFolder, $folder] = $this->getNonSharedUserFolder(); [$userFolder, $folder] = $this->getNonSharedUserFolder();
$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with(42) ->with(42)
->willReturn([$folder]);
->willReturn($folder);
$this->rootFolder->method('getUserFolder') $this->rootFolder->method('getUserFolder')
->with($this->currentUser) ->with($this->currentUser)
->willReturn($userFolder); ->willReturn($userFolder);


[$userFolder, $node] = $this->getNonSharedUserFolder(); [$userFolder, $node] = $this->getNonSharedUserFolder();
$node->method('getId')->willReturn(42); $node->method('getId')->willReturn(42);
$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with(42) ->with(42)
->willReturn([$node]);
->willReturn($node);
$this->rootFolder->method('getUserFolder') $this->rootFolder->method('getUserFolder')
->with($this->currentUser) ->with($this->currentUser)
->willReturn($userFolder); ->willReturn($userFolder);
$date->setTime(0, 0, 0); $date->setTime(0, 0, 0);


[$userFolder, $node] = $this->getNonSharedUserFolder(); [$userFolder, $node] = $this->getNonSharedUserFolder();
$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with(42) ->with(42)
->willReturn([$node]);
->willReturn($node);
$this->rootFolder->method('getUserFolder') $this->rootFolder->method('getUserFolder')
->with($this->currentUser) ->with($this->currentUser)
->willReturn($userFolder); ->willReturn($userFolder);
$date->setTime(0, 0, 0); $date->setTime(0, 0, 0);


[$userFolder, $node] = $this->getNonSharedUserFolder(); [$userFolder, $node] = $this->getNonSharedUserFolder();
$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with(42) ->with(42)
->willReturn([$node]);
->willReturn($node);
$this->rootFolder->method('getUserFolder') $this->rootFolder->method('getUserFolder')
->with($this->currentUser) ->with($this->currentUser)
->willReturn($userFolder); ->willReturn($userFolder);
$date->setTime(0, 0, 0); $date->setTime(0, 0, 0);


[$userFolder, $node] = $this->getNonSharedUserFolder(); [$userFolder, $node] = $this->getNonSharedUserFolder();
$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with(42) ->with(42)
->willReturn([$node]);
->willReturn($node);
$this->rootFolder->method('getUserFolder') $this->rootFolder->method('getUserFolder')
->with($this->currentUser) ->with($this->currentUser)
->willReturn($userFolder); ->willReturn($userFolder);
->with($this->currentUser) ->with($this->currentUser)
->willReturn($userFolder); ->willReturn($userFolder);


$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with(42) ->with(42)
->willReturn([$node]);
->willReturn($node);


$mountPoint = $this->createMock(IMountPoint::class); $mountPoint = $this->createMock(IMountPoint::class);
$node->method('getMountPoint') $node->method('getMountPoint')
->with($this->currentUser) ->with($this->currentUser)
->willReturn($userFolder); ->willReturn($userFolder);


$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with(42) ->with(42)
->willReturn([$node]);
->willReturn($node);


$mountPoint = $this->createMock(IMountPoint::class); $mountPoint = $this->createMock(IMountPoint::class);
$node->method('getMountPoint') $node->method('getMountPoint')
->with($this->currentUser) ->with($this->currentUser)
->willReturn($userFolder); ->willReturn($userFolder);


$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with(42) ->with(42)
->willReturn([$folder]);
->willReturn($folder);


$mountPoint = $this->createMock(IMountPoint::class); $mountPoint = $this->createMock(IMountPoint::class);
$folder->method('getMountPoint') $folder->method('getMountPoint')
->with($this->currentUser) ->with($this->currentUser)
->willReturn($userFolder); ->willReturn($userFolder);


$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with(42) ->with(42)
->willReturn([$folder]);
->willReturn($folder);


$mountPoint = $this->createMock(IMountPoint::class); $mountPoint = $this->createMock(IMountPoint::class);
$folder->method('getMountPoint') $folder->method('getMountPoint')
->with($this->currentUser) ->with($this->currentUser)
->willReturn($userFolder); ->willReturn($userFolder);


$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with(42) ->with(42)
->willReturn([$folder]);
->willReturn($folder);


$mountPoint = $this->createMock(IMountPoint::class); $mountPoint = $this->createMock(IMountPoint::class);
$folder->method('getMountPoint') $folder->method('getMountPoint')
->with($this->currentUser) ->with($this->currentUser)
->willReturn($userFolder); ->willReturn($userFolder);


$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with(42) ->with(42)
->willReturn([$file]);
->willReturn($file);


$mountPoint = $this->createMock(IMountPoint::class); $mountPoint = $this->createMock(IMountPoint::class);
$file->method('getMountPoint') $file->method('getMountPoint')
->with($this->currentUser) ->with($this->currentUser)
->willReturn($userFolder); ->willReturn($userFolder);


$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with(42) ->with(42)
->willReturn([$folder]);
->willReturn($folder);


$mountPoint = $this->createMock(IMountPoint::class); $mountPoint = $this->createMock(IMountPoint::class);
$folder->method('getMountPoint') $folder->method('getMountPoint')
->with($this->currentUser) ->with($this->currentUser)
->willReturn($userFolder); ->willReturn($userFolder);


$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with(42) ->with(42)
->willReturn([$folder]);
->willReturn($folder);


$mountPoint = $this->createMock(IMountPoint::class); $mountPoint = $this->createMock(IMountPoint::class);
$folder->method('getMountPoint') $folder->method('getMountPoint')
$this->dateTimeZone->method('getTimezone')->willReturn(new \DateTimeZone('UTC')); $this->dateTimeZone->method('getTimezone')->willReturn(new \DateTimeZone('UTC'));


if (!$exception) { if (!$exception) {
$this->rootFolder->method('getById')
$this->rootFolder->method('getFirstNodeById')
->with($share->getNodeId()) ->with($share->getNodeId())
->willReturn([$share->getNode()]);
->willReturn($share->getNode());


$this->rootFolder->method('getRelativePath') $this->rootFolder->method('getRelativePath')
->with($share->getNode()->getPath()) ->with($share->getNode()->getPath())
->with($this->currentUser) ->with($this->currentUser)
->willReturnSelf(); ->willReturnSelf();


$this->rootFolder->method('getById')
$this->rootFolder->method('getFirstNodeById')
->with($share->getNodeId()) ->with($share->getNodeId())
->willReturn([$share->getNode()]);
->willReturn($share->getNode());


$this->rootFolder->method('getRelativePath') $this->rootFolder->method('getRelativePath')
->with($share->getNode()->getPath()) ->with($share->getNode()->getPath())
]); ]);
$userFolder->method('getStorage')->willReturn($storage); $userFolder->method('getStorage')->willReturn($storage);
$node->method('getStorage')->willReturn($storage); $node->method('getStorage')->willReturn($storage);
$node->method('getId')->willReturn(42);
return [$userFolder, $node]; return [$userFolder, $node];
} }


]); ]);
$userFolder->method('getStorage')->willReturn($storage); $userFolder->method('getStorage')->willReturn($storage);
$node->method('getStorage')->willReturn($storage); $node->method('getStorage')->willReturn($storage);
$node->method('getId')->willReturn(42);
return [$userFolder, $node]; return [$userFolder, $node];
} }
} }

+ 29
- 18
apps/files_sharing/tests/Controller/ShareControllerTest.php View File

use OCA\FederatedFileSharing\FederatedShareProvider; use OCA\FederatedFileSharing\FederatedShareProvider;
use OCA\Files_Sharing\Controller\ShareController; use OCA\Files_Sharing\Controller\ShareController;
use OCA\Files_Sharing\DefaultPublicShareTemplateProvider; use OCA\Files_Sharing\DefaultPublicShareTemplateProvider;
use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent;
use OCP\Accounts\IAccount; use OCP\Accounts\IAccount;
use OCP\Accounts\IAccountManager; use OCP\Accounts\IAccountManager;
use OCP\Accounts\IAccountProperty; use OCP\Accounts\IAccountProperty;
$file->method('getSize')->willReturn(33); $file->method('getSize')->willReturn(33);
$file->method('isReadable')->willReturn(true); $file->method('isReadable')->willReturn(true);
$file->method('isShareable')->willReturn(true); $file->method('isShareable')->willReturn(true);
$file->method('getId')->willReturn(111);


$accountName = $this->createMock(IAccountProperty::class); $accountName = $this->createMock(IAccountProperty::class);
$accountName->method('getScope') $accountName->method('getScope')
return null; 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; return $event->getShare() === $share;
})
);
} else {
return true;
}
})
);


$this->l10n->expects($this->any()) $this->l10n->expects($this->any())
->method('t') ->method('t')
$file->method('getSize')->willReturn(33); $file->method('getSize')->willReturn(33);
$file->method('isReadable')->willReturn(true); $file->method('isReadable')->willReturn(true);
$file->method('isShareable')->willReturn(true); $file->method('isShareable')->willReturn(true);
$file->method('getId')->willReturn(111);


$accountName = $this->createMock(IAccountProperty::class); $accountName = $this->createMock(IAccountProperty::class);
$accountName->method('getScope') $accountName->method('getScope')
return null; 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; return $event->getShare() === $share;
})
);
} else {
return true;
}
})
);


$this->l10n->expects($this->any()) $this->l10n->expects($this->any())
->method('t') ->method('t')
$file->method('getSize')->willReturn(33); $file->method('getSize')->willReturn(33);
$file->method('isReadable')->willReturn(true); $file->method('isReadable')->willReturn(true);
$file->method('isShareable')->willReturn(true); $file->method('isShareable')->willReturn(true);
$file->method('getId')->willReturn(111);


$accountName = $this->createMock(IAccountProperty::class); $accountName = $this->createMock(IAccountProperty::class);
$accountName->method('getScope') $accountName->method('getScope')
return null; 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; return $event->getShare() === $share;
})
);
} else {
return true;
}
})
);


$this->l10n->expects($this->any()) $this->l10n->expects($this->any())
->method('t') ->method('t')
$folder->method('getStorage')->willReturn($storage); $folder->method('getStorage')->willReturn($storage);
$folder->method('get')->with('')->willReturn($folder); $folder->method('get')->with('')->willReturn($folder);
$folder->method('getSize')->willReturn(1337); $folder->method('getSize')->willReturn(1337);
$folder->method('getId')->willReturn(111);


$accountName = $this->createMock(IAccountProperty::class); $accountName = $this->createMock(IAccountProperty::class);
$accountName->method('getScope') $accountName->method('getScope')

+ 1
- 0
apps/files_sharing/tests/SharedStorageTest.php View File

public function testInitWithNotFoundSource() { public function testInitWithNotFoundSource() {
$share = $this->createMock(IShare::class); $share = $this->createMock(IShare::class);
$share->method('getShareOwner')->willReturn(self::TEST_FILES_SHARING_API_USER1); $share->method('getShareOwner')->willReturn(self::TEST_FILES_SHARING_API_USER1);
$share->method('getNodeId')->willReturn(1);
$ownerView = $this->createMock(View::class); $ownerView = $this->createMock(View::class);
$ownerView->method('getPath')->will($this->throwException(new NotFoundException())); $ownerView->method('getPath')->will($this->throwException(new NotFoundException()));
$storage = new SharedStorage([ $storage = new SharedStorage([

+ 4
- 3
apps/files_trashbin/lib/Trash/LegacyTrashBackend.php View File

use OCA\Files_Trashbin\Storage; use OCA\Files_Trashbin\Storage;
use OCA\Files_Trashbin\Trashbin; use OCA\Files_Trashbin\Trashbin;
use OCP\Files\FileInfo; use OCP\Files\FileInfo;
use OCP\Files\Folder;
use OCP\Files\IRootFolder; use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\Files\Storage\IStorage; use OCP\Files\Storage\IStorage;
try { try {
$userFolder = $this->rootFolder->getUserFolder($user->getUID()); $userFolder = $this->rootFolder->getUserFolder($user->getUID());
$trash = $userFolder->getParent()->get('files_trashbin/files'); $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 null;
} }
return $trashFiles ? array_pop($trashFiles) : null;
} catch (NotFoundException $e) { } catch (NotFoundException $e) {
return null; return null;
} }

+ 2
- 4
apps/files_versions/lib/Sabre/VersionRoot.php View File

$userFolder = $this->rootFolder->getUserFolder($this->user->getUID()); $userFolder = $this->rootFolder->getUserFolder($this->user->getUID());


$fileId = (int)$name; $fileId = (int)$name;
$nodes = $userFolder->getById($fileId);
$node = $userFolder->getFirstNodeById($fileId);


if ($nodes === []) {
if (!$node) {
throw new NotFound(); throw new NotFound();
} }


$node = array_pop($nodes);

if (!$node instanceof File) { if (!$node instanceof File) {
throw new NotFound(); throw new NotFound();
} }

+ 3
- 3
apps/files_versions/lib/Storage.php View File

$mount = $file->getMountPoint(); $mount = $file->getMountPoint();
if ($mount instanceof SharedMount) { if ($mount instanceof SharedMount) {
$ownerFolder = $rootFolder->getUserFolder($mount->getShare()->getShareOwner()); $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(); $uid = $mount->getShare()->getShareOwner();
} }
} }

+ 1
- 2
apps/files_versions/lib/Versions/LegacyVersionsBackend.php View File



$userFolder = $this->rootFolder->getUserFolder($user->getUID()); $userFolder = $this->rootFolder->getUserFolder($user->getUID());


$nodes = $userFolder->getById($fileId);
$file = array_pop($nodes);
$file = $userFolder->getFirstNodeById($fileId);


if (!$file) { if (!$file) {
throw new NotFoundException("version file not found for share owner"); throw new NotFoundException("version file not found for share owner");

+ 3
- 3
core/Command/FilesMetadata/Get.php View File

} }


if ($input->getOption('refresh')) { 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(); throw new NotFoundException();
} }
$metadata = $this->filesMetadataManager->refreshMetadata( $metadata = $this->filesMetadataManager->refreshMetadata(
$node[0],
$node,
IFilesMetadataManager::PROCESS_LIVE | IFilesMetadataManager::PROCESS_BACKGROUND IFilesMetadataManager::PROCESS_LIVE | IFilesMetadataManager::PROCESS_BACKGROUND
); );
} else { } else {

+ 1
- 5
core/Command/Info/FileUtils.php View File

} }
$mount = $mounts[0]; $mount = $mounts[0];
$userFolder = $this->rootFolder->getUserFolder($mount->getUser()->getUID()); $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 { } else {
try { try {
return $this->rootFolder->get($fileInput); return $this->rootFolder->get($fileInput);

+ 1
- 5
core/Command/Preview/Generate.php View File

} }
$mount = $mounts[0]; $mount = $mounts[0];
$userFolder = $this->rootFolder->getUserFolder($mount->getUser()->getUID()); $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 { } else {
try { try {
return $this->rootFolder->get($fileInput); return $this->rootFolder->get($fileInput);

+ 2
- 4
core/Controller/PreviewController.php View File

} }


$userFolder = $this->root->getUserFolder($this->userId); $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); return new DataResponse([], Http::STATUS_NOT_FOUND);
} }


$node = array_pop($nodes);

return $this->fetchPreview($node, $x, $y, $a, $forceIcon, $mode, $mimeFallback); return $this->fetchPreview($node, $x, $y, $a, $forceIcon, $mode, $mimeFallback);
} }



+ 2
- 6
lib/private/Collaboration/Reference/File/FileReferenceProvider.php View File

use OCP\Files\IMimeTypeDetector; use OCP\Files\IMimeTypeDetector;
use OCP\Files\InvalidPathException; use OCP\Files\InvalidPathException;
use OCP\Files\IRootFolder; use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException; use OCP\Files\NotPermittedException;
use OCP\IL10N; use OCP\IL10N;


try { try {
$userFolder = $this->rootFolder->getUserFolder($this->userId); $userFolder = $this->rootFolder->getUserFolder($this->userId);
$files = $userFolder->getById($fileId);
$file = $userFolder->getFirstNodeById($fileId);


if (empty($files)) {
if (!$file) {
throw new NotFoundException(); throw new NotFoundException();
} }


/** @var Node $file */
$file = array_shift($files);

$reference->setTitle($file->getName()); $reference->setTitle($file->getName());
$reference->setDescription($file->getMimetype()); $reference->setDescription($file->getMimetype());
$reference->setUrl($this->urlGenerator->getAbsoluteURL('/index.php/f/' . $fileId)); $reference->setUrl($this->urlGenerator->getAbsoluteURL('/index.php/f/' . $fileId));

+ 3
- 3
lib/private/DirectEditing/Manager.php View File

if ($filePath !== null) { if ($filePath !== null) {
return $userFolder->get($filePath); 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); throw new NotFoundException('File nound found by id ' . $fileId);
} }
return $files[0];
return $file;
} }


public function isEnabled(): bool { public function isEnabled(): bool {

+ 1
- 6
lib/private/Files/Config/CachedMountInfo.php View File

// TODO injection etc // TODO injection etc
Filesystem::initMountPoints($this->getUser()->getUID()); Filesystem::initMountPoints($this->getUser()->getUID());
$userNode = \OC::$server->getUserFolder($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());
} }


/** /**

+ 1
- 1
lib/private/Files/Node/Folder.php View File

} }


public function getFirstNodeById(int $id): ?\OCP\Files\Node { public function getFirstNodeById(int $id): ?\OCP\Files\Node {
return current($this->getById($id));
return current($this->getById($id)) ?: null;
} }


protected function getAppDataDirectoryName(): string { protected function getAppDataDirectoryName(): string {

+ 3
- 4
lib/private/FilesMetadata/Job/UpdateSingleMetadata.php View File

[$userId, $fileId] = $argument; [$userId, $fileId] = $argument;


try { 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) { } catch (\Exception $e) {
$this->logger->warning('issue while running UpdateSingleMetadata', ['exception' => $e, 'userId' => $userId, 'fileId' => $fileId]); $this->logger->warning('issue while running UpdateSingleMetadata', ['exception' => $e, 'userId' => $userId, 'fileId' => $fileId]);

+ 2
- 4
lib/private/Share20/Manager.php View File

$mount = $userMount->getMountPoint(); $mount = $userMount->getMountPoint();
// When it's a reshare use the parent share permissions as maximum // When it's a reshare use the parent share permissions as maximum
$userMountPointId = $mount->getStorageRootId(); $userMountPointId = $mount->getStorageRootId();
$userMountPoints = $userFolder->getById($userMountPointId);
$userMountPoint = array_shift($userMountPoints);
$userMountPoint = $userFolder->getFirstNodeById($userMountPointId);


if ($userMountPoint === null) { if ($userMountPoint === null) {
throw new GenericShareException('Could not get proper user mount for ' . $userMountPointId . '. Failing since else the next calls are called with null'); throw new GenericShareException('Could not get proper user mount for ' . $userMountPointId . '. Failing since else the next calls are called with null');
//Get node for the owner and correct the owner in case of external storage //Get node for the owner and correct the owner in case of external storage
$userFolder = $this->rootFolder->getUserFolder($owner); $userFolder = $this->rootFolder->getUserFolder($owner);
if ($path->getId() !== $userFolder->getId() && !$userFolder->isSubNode($path)) { 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) { if ($path === null || $path->getOwner() === null) {
return []; return [];
} }

+ 9
- 5
lib/private/Share20/Share.php View File

$userFolder = $this->rootFolder->getUserFolder($this->sharedBy); $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); throw new NotFoundException('Node for share not found, fileid: ' . $this->fileId);
} }


$this->node = $nodes[0];
$this->node = $node;
} }


return $this->node; return $this->node;
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function getNodeId() {
public function getNodeId(): int {
if ($this->fileId === null) { if ($this->fileId === null) {
$this->fileId = $this->getNode()->getId(); $this->fileId = $this->getNode()->getId();
} }


return $this->fileId;
if ($this->fileId === null) {
throw new NotFoundException("Share source not found");
} else {
return $this->fileId;
}
} }


/** /**

+ 1
- 1
lib/private/SpeechToText/TranscriptionJob.php View File

try { try {
\OC_Util::setupFS($owner); \OC_Util::setupFS($owner);
$userFolder = $this->rootFolder->getUserFolder($owner); $userFolder = $this->rootFolder->getUserFolder($owner);
$file = current($userFolder->getById($fileId));
$file = $userFolder->getFirstNodeById($fileId);
if (!($file instanceof File)) { if (!($file instanceof File)) {
$this->logger->warning('Transcription of file ' . $fileId . ' failed. The file could not be found'); $this->logger->warning('Transcription of file ' . $fileId . ' failed. The file could not be found');
$this->eventDispatcher->dispatchTyped( $this->eventDispatcher->dispatchTyped(

+ 1
- 1
lib/public/Share/IShare.php View File

* @since 9.0.0 * @since 9.0.0
* @throws NotFoundException * @throws NotFoundException
*/ */
public function getNodeId();
public function getNodeId(): int;


/** /**
* Set the type of node (file/folder) * Set the type of node (file/folder)

+ 52
- 52
tests/lib/Share20/DefaultShareProviderTest.php View File



$ownerPath = $this->createMock(File::class); $ownerPath = $this->createMock(File::class);
$shareOwnerFolder = $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 $this->rootFolder
->method('getUserFolder') ->method('getUserFolder')
$ownerPath = $this->createMock(File::class); $ownerPath = $this->createMock(File::class);


$shareOwnerFolder = $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 $this->rootFolder
->method('getUserFolder') ->method('getUserFolder')


$ownerPath = $this->createMock(Folder::class); $ownerPath = $this->createMock(Folder::class);
$shareOwnerFolder = $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 $this->rootFolder
->method('getUserFolder') ->method('getUserFolder')
$node->method('getId')->willReturn(42); $node->method('getId')->willReturn(42);


$this->rootFolder->method('getUserFolder')->with('user0')->willReturnSelf(); $this->rootFolder->method('getUserFolder')->with('user0')->willReturnSelf();
$this->rootFolder->method('getById')->willReturn([$node]);
$this->rootFolder->method('getFirstNodeById')->willReturn($node);


$this->userManager->method('get')->willReturnMap([ $this->userManager->method('get')->willReturnMap([
['user0', $user0], ['user0', $user0],


$ownerPath = $this->createMock(Folder::class); $ownerPath = $this->createMock(Folder::class);
$shareOwnerFolder = $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 $this->rootFolder
->method('getUserFolder') ->method('getUserFolder')


$ownerPath = $this->createMock(Folder::class); $ownerPath = $this->createMock(Folder::class);
$ownerFolder = $this->createMock(Folder::class); $ownerFolder = $this->createMock(Folder::class);
$ownerFolder->method('getById')->willReturn([$ownerPath]);
$ownerFolder->method('getFirstNodeById')->willReturn($ownerPath);


$this->rootFolder $this->rootFolder
->method('getUserFolder') ->method('getUserFolder')
['shareOwner', $ownerFolder], ['shareOwner', $ownerFolder],
]); ]);


$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with(100) ->with(100)
->willReturn([$path]);
$ownerFolder->method('getById')
->willReturn($path);
$ownerFolder->method('getFirstNodeById')
->with(100) ->with(100)
->willReturn([$path]);
->willReturn($path);


$share->setShareType(IShare::TYPE_USER); $share->setShareType(IShare::TYPE_USER);
$share->setSharedWith('sharedWith'); $share->setSharedWith('sharedWith');
['shareOwner', $ownerFolder], ['shareOwner', $ownerFolder],
]); ]);


$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with(100) ->with(100)
->willReturn([$path]);
$ownerFolder->method('getById')
->willReturn($path);
$ownerFolder->method('getFirstNodeById')
->with(100) ->with(100)
->willReturn([$path]);
->willReturn($path);


$share->setShareType(IShare::TYPE_GROUP); $share->setShareType(IShare::TYPE_GROUP);
$share->setSharedWith('sharedWith'); $share->setSharedWith('sharedWith');
['shareOwner', $ownerFolder], ['shareOwner', $ownerFolder],
]); ]);


$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with(100) ->with(100)
->willReturn([$path]);
$ownerFolder->method('getById')
->willReturn($path);
$ownerFolder->method('getFirstNodeById')
->with(100) ->with(100)
->willReturn([$path]);
->willReturn($path);


$share->setShareType(IShare::TYPE_LINK); $share->setShareType(IShare::TYPE_LINK);
$share->setSharedBy('sharedBy'); $share->setSharedBy('sharedBy');
$file = $this->createMock(File::class); $file = $this->createMock(File::class);


$this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf(); $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'); $share = $this->provider->getShareByToken('secrettoken');
$this->assertEquals($id, $share->getId()); $this->assertEquals($id, $share->getId());


$file = $this->createMock(File::class); $file = $this->createMock(File::class);
$this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf(); $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); $share = $this->provider->getSharedWith('sharedWith', IShare::TYPE_USER, null, 1, 0);
$this->assertCount(1, $share); $this->assertCount(1, $share);


$file = $this->createMock(File::class); $file = $this->createMock(File::class);
$this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf(); $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); $share = $this->provider->getSharedWith('sharedWith', IShare::TYPE_GROUP, null, 20, 1);
$this->assertCount(1, $share); $this->assertCount(1, $share);


$file = $this->createMock(File::class); $file = $this->createMock(File::class);
$this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf(); $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); $share = $this->provider->getSharedWith('user', IShare::TYPE_GROUP, null, -1, 0);
$this->assertCount(1, $share); $this->assertCount(1, $share);
$file->method('getId')->willReturn($fileId2); $file->method('getId')->willReturn($fileId2);


$this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf(); $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); $share = $this->provider->getSharedWith('user0', IShare::TYPE_USER, $file, -1, 0);
$this->assertCount(1, $share); $this->assertCount(1, $share);
$node = $this->createMock(Folder::class); $node = $this->createMock(Folder::class);
$node->method('getId')->willReturn($fileId2); $node->method('getId')->willReturn($fileId2);
$this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf(); $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); $share = $this->provider->getSharedWith('user0', IShare::TYPE_GROUP, $node, -1, 0);
$this->assertCount(1, $share); $this->assertCount(1, $share);


$file = $this->createMock(File::class); $file = $this->createMock(File::class);
$this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf(); $this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf();
$this->rootFolder->method('getById')->with($deletedFileId)->willReturn([$file]);
$this->rootFolder->method('getFirstNodeById')->with($deletedFileId)->willReturn($file);


$groups = []; $groups = [];
foreach (range(0, 100) as $i) { foreach (range(0, 100) as $i) {


$file = $this->createMock(File::class); $file = $this->createMock(File::class);
$this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf(); $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); $share = $this->provider->getSharesBy('sharedBy', IShare::TYPE_USER, null, false, 1, 0);
$this->assertCount(1, $share); $this->assertCount(1, $share);
$file = $this->createMock(File::class); $file = $this->createMock(File::class);
$file->method('getId')->willReturn(42); $file->method('getId')->willReturn(42);
$this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf(); $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); $share = $this->provider->getSharesBy('sharedBy', IShare::TYPE_USER, $file, false, 1, 0);
$this->assertCount(1, $share); $this->assertCount(1, $share);
$file = $this->createMock(File::class); $file = $this->createMock(File::class);
$file->method('getId')->willReturn(42); $file->method('getId')->willReturn(42);
$this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf(); $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); $shares = $this->provider->getSharesBy('shareOwner', IShare::TYPE_USER, null, true, -1, 0);
$this->assertCount(2, $shares); $this->assertCount(2, $shares);
$file->method('getId')->willReturn(1); $file->method('getId')->willReturn(1);


$this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf(); $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); $share = $this->provider->getShareById($id);


$file->method('getId')->willReturn(1); $file->method('getId')->willReturn(1);


$this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf(); $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); $share = $this->provider->getShareById($id);


$file->method('getId')->willReturn(1); $file->method('getId')->willReturn(1);


$this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf(); $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); $share = $this->provider->getShareById($id);


$file->method('getId')->willReturn(1); $file->method('getId')->willReturn(1);


$this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf(); $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); $share = $this->provider->getShareById($id);


$file->method('getId')->willReturn(1); $file->method('getId')->willReturn(1);


$this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf(); $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); $share = $this->provider->getShareById($id);


$file->method('getId')->willReturn(1); $file->method('getId')->willReturn(1);


$this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf(); $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); $share = $this->provider->getShareById($id);


$file->method('getId')->willReturn(1); $file->method('getId')->willReturn(1);


$this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf(); $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); $share = $this->provider->getShareById($id);


$file2->method('getId')->willReturn(43); $file2->method('getId')->willReturn(43);


$folder1 = $this->createMock(Folder::class); $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 = $this->createMock(Folder::class);
$folder2->method('getById')->with(43)->willReturn([$file2]);
$folder2->method('getFirstNodeById')->with(43)->willReturn($file2);


$this->rootFolder->method('getUserFolder')->willReturnMap([ $this->rootFolder->method('getUserFolder')->willReturnMap([
['user2', $folder1], ['user2', $folder1],
$file2->method('getId')->willReturn(43); $file2->method('getId')->willReturn(43);


$folder1 = $this->createMock(Folder::class); $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 = $this->createMock(Folder::class);
$folder2->method('getById')->with(43)->willReturn([$file2]);
$folder2->method('getFirstNodeById')->with(43)->willReturn($file2);


$this->rootFolder->method('getUserFolder')->willReturnMap([ $this->rootFolder->method('getUserFolder')->willReturnMap([
['user2', $folder1], ['user2', $folder1],
$file2->method('getId')->willReturn(43); $file2->method('getId')->willReturn(43);


$folder1 = $this->createMock(Folder::class); $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 = $this->createMock(Folder::class);
$folder2->method('getById')->with(43)->willReturn([$file2]);
$folder2->method('getFirstNodeById')->with(43)->willReturn($file2);


$this->rootFolder->method('getUserFolder')->willReturnMap([ $this->rootFolder->method('getUserFolder')->willReturnMap([
['user2', $folder1], ['user2', $folder1],
$file2->method('getId')->willReturn(43); $file2->method('getId')->willReturn(43);


$folder1 = $this->createMock(Folder::class); $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 = $this->createMock(Folder::class);
$folder2->method('getById')->with(43)->willReturn([$file2]);
$folder2->method('getFirstNodeById')->with(43)->willReturn($file2);


$this->rootFolder->method('getUserFolder')->willReturnMap([ $this->rootFolder->method('getUserFolder')->willReturnMap([
['user2', $folder1], ['user2', $folder1],
$file2->method('getId')->willReturn(43); $file2->method('getId')->willReturn(43);


$folder1 = $this->createMock(Folder::class); $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 = $this->createMock(Folder::class);
$folder2->method('getById')->with(43)->willReturn([$file2]);
$folder2->method('getFirstNodeById')->with(43)->willReturn($file2);


$this->rootFolder->method('getUserFolder')->willReturnMap([ $this->rootFolder->method('getUserFolder')->willReturnMap([
['user2', $folder1], ['user2', $folder1],
$file->method('getId')->willReturn(42); $file->method('getId')->willReturn(42);


$this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf(); $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); $share = $this->provider->getShareById($id, null);


$folder->method('getId')->willReturn(42); $folder->method('getId')->willReturn(42);


$this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf(); $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'); $share = $this->provider->getShareById($id, 'user0');




$ownerPath1 = $this->createMock(File::class); $ownerPath1 = $this->createMock(File::class);
$shareOwner1Folder = $this->createMock(Folder::class); $shareOwner1Folder = $this->createMock(Folder::class);
$shareOwner1Folder->method('getById')->willReturn([$ownerPath1]);
$shareOwner1Folder->method('getFirstNodeById')->willReturn($ownerPath1);


$ownerPath2 = $this->createMock(File::class); $ownerPath2 = $this->createMock(File::class);
$shareOwner2Folder = $this->createMock(Folder::class); $shareOwner2Folder = $this->createMock(Folder::class);
$shareOwner2Folder->method('getById')->willReturn([$ownerPath2]);
$shareOwner2Folder->method('getFirstNodeById')->willReturn($ownerPath2);


$ownerPath3 = $this->createMock(File::class); $ownerPath3 = $this->createMock(File::class);
$shareOwner3Folder = $this->createMock(Folder::class); $shareOwner3Folder = $this->createMock(Folder::class);
$shareOwner3Folder->method('getById')->willReturn([$ownerPath3]);
$shareOwner3Folder->method('getFirstNodeById')->willReturn($ownerPath3);


$ownerPath4 = $this->createMock(File::class); $ownerPath4 = $this->createMock(File::class);
$shareOwner4Folder = $this->createMock(Folder::class); $shareOwner4Folder = $this->createMock(Folder::class);
$shareOwner4Folder->method('getById')->willReturn([$ownerPath4]);
$shareOwner4Folder->method('getFirstNodeById')->willReturn($ownerPath4);


$ownerPath5 = $this->createMock(File::class); $ownerPath5 = $this->createMock(File::class);
$shareOwner5Folder = $this->createMock(Folder::class); $shareOwner5Folder = $this->createMock(Folder::class);
$shareOwner5Folder->method('getById')->willReturn([$ownerPath5]);
$shareOwner5Folder->method('getFirstNodeById')->willReturn($ownerPath5);


$this->rootFolder $this->rootFolder
->method('getUserFolder') ->method('getUserFolder')

+ 6
- 6
tests/lib/Share20/ManagerTest.php View File

->willReturn(42); ->willReturn(42);
// Id 108 is used in the data to refer to the node of the share. // Id 108 is used in the data to refer to the node of the share.
$userFolder->expects($this->any()) $userFolder->expects($this->any())
->method('getById')
->method('getFirstNodeById')
->with(108) ->with(108)
->willReturn([$share->getNode()]);
->willReturn($share->getNode());
$userFolder->expects($this->any()) $userFolder->expects($this->any())
->method('getRelativePath') ->method('getRelativePath')
->willReturnArgument(0); ->willReturnArgument(0);
->willReturn($userFolder); ->willReturn($userFolder);
$folder->method('getPath') $folder->method('getPath')
->willReturn('/owner/files/folder'); ->willReturn('/owner/files/folder');
$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with($this->equalTo(42)) ->with($this->equalTo(42))
->willReturn([12 => $file]);
->willReturn($file);
$userFolder->method('getPath') $userFolder->method('getPath')
->willReturn('/user1/files'); ->willReturn('/user1/files');


->willReturn($userFolder); ->willReturn($userFolder);
$folder->method('getPath') $folder->method('getPath')
->willReturn('/owner/files/folder'); ->willReturn('/owner/files/folder');
$userFolder->method('getById')
$userFolder->method('getFirstNodeById')
->with($this->equalTo(42)) ->with($this->equalTo(42))
->willReturn([42 => $file]);
->willReturn($file);
$userFolder->method('getPath') $userFolder->method('getPath')
->willReturn('/user1/files'); ->willReturn('/user1/files');



Loading…
Cancel
Save