Skip to content

Commit 649a40d

Browse files
committed
fix: return type formatting for user paths
Signed-off-by: Anna Larch <anna@nextcloud.com>
1 parent 5b8946d commit 649a40d

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;
@@ -1053,4 +1054,64 @@ public function testLeaveShare(): void {
10531054

10541055
self::invokePrivate($filesHooks, 'unShareSelf', [$share]);
10551056
}
1057+
1058+
public function testGetUserPathsFromPathFileNotFound(): void {
1059+
$userFolder = $this->createMock(Folder::class);
1060+
$userFolder->method('get')
1061+
->with('/test/path')
1062+
->willThrowException(new NotFoundException());
1063+
1064+
$this->rootFolder->method('getUserFolder')
1065+
->with('owner')
1066+
->willReturn($userFolder);
1067+
1068+
$result = self::invokePrivate($this->filesHooks, 'getUserPathsFromPath', ['/test/path', 'owner']);
1069+
1070+
$this->assertSame([], $result['users']);
1071+
$this->assertSame([], $result['remotes']);
1072+
}
1073+
1074+
public function testGetUserPathsFromPathNotANode(): void {
1075+
$userFolder = $this->createMock(Folder::class);
1076+
$userFolder->method('get')
1077+
->with('/test/path')
1078+
->willReturn(null);
1079+
1080+
$this->rootFolder->method('getUserFolder')
1081+
->with('owner')
1082+
->willReturn($userFolder);
1083+
1084+
$result = self::invokePrivate($this->filesHooks, 'getUserPathsFromPath', ['/test/path', 'owner']);
1085+
1086+
$this->assertSame([], $result['users']);
1087+
$this->assertSame([], $result['remotes']);
1088+
}
1089+
1090+
public function testGetUserPathsFromPathSuccess(): void {
1091+
$node = $this->createMock(File::class);
1092+
$node->method('getPath')
1093+
->willReturn('/owner/files/test/path');
1094+
1095+
$userFolder = $this->createMock(Folder::class);
1096+
$userFolder->method('get')
1097+
->with('/test/path')
1098+
->willReturn($node);
1099+
1100+
$this->rootFolder->method('getUserFolder')
1101+
->with('owner')
1102+
->willReturn($userFolder);
1103+
1104+
$this->shareHelper->method('getPathsForAccessList')
1105+
->with($node)
1106+
->willReturn([
1107+
'users' => ['user1' => '/path1'],
1108+
'remotes' => ['remote1' => ['token' => 'abc']],
1109+
]);
1110+
1111+
$result = self::invokePrivate($this->filesHooks, 'getUserPathsFromPath', ['/test/path', 'owner']);
1112+
1113+
$this->assertSame(['user1' => '/path1'], $result['users']);
1114+
$this->assertSame(['remote1' => ['token' => 'abc']], $result['remotes']);
1115+
$this->assertSame('/test/path', $result['ownerPath']);
1116+
}
10561117
}

0 commit comments

Comments
 (0)