Skip to content

Commit 2836698

Browse files
authored
Merge pull request #2449 from nextcloud/backport/2427/stable33
[stable33] fix: return type formatting for user paths
2 parents ce78e3e + 7f689d7 commit 2836698

2 files changed

Lines changed: 71 additions & 5 deletions

File tree

lib/FilesHooks.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -572,24 +572,29 @@ protected function generateMoveActivities($users, $beforePathMap, $afterPathMap,
572572
*
573573
* @param string $path
574574
* @param string $uidOwner
575-
* @return array
575+
* @return array{ownerPath?: string, remotes: array<string, array{node_path: string, token: string}>, users: array<string, string>}
576576
*/
577577
protected function getUserPathsFromPath($path, $uidOwner) {
578+
$emptyResult = ['users' => [], 'remotes' => []];
579+
578580
try {
579581
$node = $this->rootFolder->getUserFolder($uidOwner)->get($path);
580582
} catch (NotFoundException $e) {
581-
return [];
583+
return $emptyResult;
582584
}
583585

584586
if (!$node instanceof Node) {
585-
return [];
587+
return $emptyResult;
586588
}
587589

588590
$accessList = $this->shareHelper->getPathsForAccessList($node);
589591

590592
$path = $node->getPath();
591-
$accessList['ownerPath'] = $this->getVisiblePath($path);
592-
return $accessList;
593+
return [
594+
'ownerPath' => $this->getVisiblePath($path),
595+
'users' => $accessList['users'],
596+
'remotes' => $accessList['remotes'],
597+
];
593598
}
594599

595600
protected function getVisiblePath(string $absolutePath): string {

tests/FilesHooksTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
use OCP\Files\Folder;
4040
use OCP\Files\IRootFolder;
4141
use OCP\Files\Node;
42+
use OCP\Files\NotFoundException;
4243
use OCP\IConfig;
4344
use OCP\IDBConnection;
4445
use OCP\IGroup;
@@ -991,4 +992,64 @@ public function testLeaveShare(): void {
991992

992993
self::invokePrivate($filesHooks, 'unShareSelf', [$share]);
993994
}
995+
996+
public function testGetUserPathsFromPathFileNotFound(): void {
997+
$userFolder = $this->createMock(Folder::class);
998+
$userFolder->method('get')
999+
->with('/test/path')
1000+
->willThrowException(new NotFoundException());
1001+
1002+
$this->rootFolder->method('getUserFolder')
1003+
->with('owner')
1004+
->willReturn($userFolder);
1005+
1006+
$result = self::invokePrivate($this->filesHooks, 'getUserPathsFromPath', ['/test/path', 'owner']);
1007+
1008+
$this->assertSame([], $result['users']);
1009+
$this->assertSame([], $result['remotes']);
1010+
}
1011+
1012+
public function testGetUserPathsFromPathNotANode(): void {
1013+
$userFolder = $this->createMock(Folder::class);
1014+
$userFolder->method('get')
1015+
->with('/test/path')
1016+
->willReturn(null);
1017+
1018+
$this->rootFolder->method('getUserFolder')
1019+
->with('owner')
1020+
->willReturn($userFolder);
1021+
1022+
$result = self::invokePrivate($this->filesHooks, 'getUserPathsFromPath', ['/test/path', 'owner']);
1023+
1024+
$this->assertSame([], $result['users']);
1025+
$this->assertSame([], $result['remotes']);
1026+
}
1027+
1028+
public function testGetUserPathsFromPathSuccess(): void {
1029+
$node = $this->createMock(File::class);
1030+
$node->method('getPath')
1031+
->willReturn('/owner/files/test/path');
1032+
1033+
$userFolder = $this->createMock(Folder::class);
1034+
$userFolder->method('get')
1035+
->with('/test/path')
1036+
->willReturn($node);
1037+
1038+
$this->rootFolder->method('getUserFolder')
1039+
->with('owner')
1040+
->willReturn($userFolder);
1041+
1042+
$this->shareHelper->method('getPathsForAccessList')
1043+
->with($node)
1044+
->willReturn([
1045+
'users' => ['user1' => '/path1'],
1046+
'remotes' => ['remote1' => ['token' => 'abc']],
1047+
]);
1048+
1049+
$result = self::invokePrivate($this->filesHooks, 'getUserPathsFromPath', ['/test/path', 'owner']);
1050+
1051+
$this->assertSame(['user1' => '/path1'], $result['users']);
1052+
$this->assertSame(['remote1' => ['token' => 'abc']], $result['remotes']);
1053+
$this->assertSame('/test/path', $result['ownerPath']);
1054+
}
9941055
}

0 commit comments

Comments
 (0)