diff --git a/lib/FilesHooks.php b/lib/FilesHooks.php index cbf2a8457..d43683d9b 100644 --- a/lib/FilesHooks.php +++ b/lib/FilesHooks.php @@ -572,17 +572,19 @@ protected function generateMoveActivities($users, $beforePathMap, $afterPathMap, * * @param string $path * @param string $uidOwner - * @return array + * @return array{ownerPath?: string, remotes: array, users: array} */ protected function getUserPathsFromPath($path, $uidOwner) { + $emptyResult = ['users' => [], 'remotes' => []]; + try { $node = $this->rootFolder->getUserFolder($uidOwner)->get($path); } catch (NotFoundException $e) { - return []; + return $emptyResult; } if (!$node instanceof Node) { - return []; + return $emptyResult; } $accessList = $this->shareHelper->getPathsForAccessList($node); diff --git a/tests/FilesHooksTest.php b/tests/FilesHooksTest.php index 90b4dece7..ee7e62f69 100644 --- a/tests/FilesHooksTest.php +++ b/tests/FilesHooksTest.php @@ -39,6 +39,7 @@ use OCP\Files\Folder; use OCP\Files\IRootFolder; use OCP\Files\Node; +use OCP\Files\NotFoundException; use OCP\IConfig; use OCP\IDBConnection; use OCP\IGroup; @@ -998,4 +999,64 @@ public function testLeaveShare(): void { self::invokePrivate($filesHooks, 'unShareSelf', [$share]); } + + public function testGetUserPathsFromPathFileNotFound(): void { + $userFolder = $this->createMock(Folder::class); + $userFolder->method('get') + ->with('/test/path') + ->willThrowException(new NotFoundException()); + + $this->rootFolder->method('getUserFolder') + ->with('owner') + ->willReturn($userFolder); + + $result = self::invokePrivate($this->filesHooks, 'getUserPathsFromPath', ['/test/path', 'owner']); + + $this->assertSame([], $result['users']); + $this->assertSame([], $result['remotes']); + } + + public function testGetUserPathsFromPathNotANode(): void { + $userFolder = $this->createMock(Folder::class); + $userFolder->method('get') + ->with('/test/path') + ->willReturn(null); + + $this->rootFolder->method('getUserFolder') + ->with('owner') + ->willReturn($userFolder); + + $result = self::invokePrivate($this->filesHooks, 'getUserPathsFromPath', ['/test/path', 'owner']); + + $this->assertSame([], $result['users']); + $this->assertSame([], $result['remotes']); + } + + public function testGetUserPathsFromPathSuccess(): void { + $node = $this->createMock(File::class); + $node->method('getPath') + ->willReturn('/owner/files/test/path'); + + $userFolder = $this->createMock(Folder::class); + $userFolder->method('get') + ->with('/test/path') + ->willReturn($node); + + $this->rootFolder->method('getUserFolder') + ->with('owner') + ->willReturn($userFolder); + + $this->shareHelper->method('getPathsForAccessList') + ->with($node) + ->willReturn([ + 'users' => ['user1' => '/path1'], + 'remotes' => ['remote1' => ['token' => 'abc']], + ]); + + $result = self::invokePrivate($this->filesHooks, 'getUserPathsFromPath', ['/test/path', 'owner']); + + $this->assertSame(['user1' => '/path1'], $result['users']); + $this->assertSame(['remote1' => ['token' => 'abc']], $result['remotes']); + $this->assertSame('/test/path', $result['ownerPath']); + } }