Skip to content

Commit

Permalink
fix: Use getRelativePath method to check if node is inside folder
Browse files Browse the repository at this point in the history
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
  • Loading branch information
come-nc committed Oct 14, 2024
1 parent e584e9b commit d6ba524
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
3 changes: 2 additions & 1 deletion lib/private/Share20/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,8 @@ protected function promoteReshares(IShare $share): void {
/* Ignore share of non-existing node */
continue;
}
if (str_starts_with($path, $node->getPath() . '/') || ($path === $node->getPath())) {
if ($node->getRelativePath($path) !== null) {
/* If relative path is not null it means the shared node is the same or in a subfolder */
$reshareRecords[] = $share;
}
}
Expand Down
24 changes: 14 additions & 10 deletions tests/lib/Share20/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use DateTimeZone;
use OC\Files\Mount\MoveableMount;
use OC\Files\Utils\PathHelper;
use OC\KnownUser\KnownUserService;
use OC\Share20\DefaultShareProvider;
use OC\Share20\Exception;
Expand Down Expand Up @@ -199,6 +200,14 @@ private function createManagerMock() {
]);
}

private function createFolderMock(string $folderPath): MockObject&Folder {
$folder = $this->createMock(Folder::class);
$folder->method('getPath')->willReturn($folderPath);
$folder->method('getRelativePath')->willReturnCallback(
fn (string $path): ?string => PathHelper::getRelativePath($folderPath, $path)
);
return $folder;
}

public function testDeleteNoShareId(): void {
$this->expectException(\InvalidArgumentException::class);
Expand Down Expand Up @@ -514,14 +523,11 @@ public function testPromoteReshare(): void {
->setMethods(['updateShare', 'getSharesInFolder', 'generalCreateChecks'])
->getMock();

$folder = $this->createMock(Folder::class);
$folder->method('getPath')->willReturn('/path/to/folder');
$folder = $this->createFolderMock('/path/to/folder');

$subFolder = $this->createMock(Folder::class);
$subFolder->method('getPath')->willReturn('/path/to/folder/sub');
$subFolder = $this->createFolderMock('/path/to/folder/sub');

$otherFolder = $this->createMock(Folder::class);
$otherFolder->method('getPath')->willReturn('/path/to/otherfolder/');
$otherFolder = $this->createFolderMock('/path/to/otherfolder/');

$share = $this->createMock(IShare::class);
$share->method('getShareType')->willReturn(IShare::TYPE_USER);
Expand Down Expand Up @@ -567,8 +573,7 @@ public function testPromoteReshareWhenUserHasAnotherShare(): void {
->setMethods(['updateShare', 'getSharesInFolder', 'getSharedWith', 'generalCreateChecks'])
->getMock();

$folder = $this->createMock(Folder::class);
$folder->method('getPath')->willReturn('/path/to/folder');
$folder = $this->createFolderMock('/path/to/folder');

$share = $this->createMock(IShare::class);
$share->method('getShareType')->willReturn(IShare::TYPE_USER);
Expand Down Expand Up @@ -596,8 +601,7 @@ public function testPromoteReshareOfUsersInGroupShare(): void {
->setMethods(['updateShare', 'getSharesInFolder', 'getSharedWith', 'generalCreateChecks'])
->getMock();

$folder = $this->createMock(Folder::class);
$folder->method('getPath')->willReturn('/path/to/folder');
$folder = $this->createFolderMock('/path/to/folder');

$userA = $this->createMock(IUser::class);
$userA->method('getUID')->willReturn('userA');
Expand Down

0 comments on commit d6ba524

Please sign in to comment.