2025-12-18 07:34:00 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
/**
|
|
|
|
|
* SPDX-FileCopyrightText: 2025 Robin Appelman <robin@icewind.nl>
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace OCA\Files_Sharing\Tests;
|
|
|
|
|
|
2026-02-09 12:33:22 -05:00
|
|
|
use OC\EventDispatcher\EventDispatcher;
|
|
|
|
|
use OC\Files\SetupManager;
|
2025-12-18 07:34:00 -05:00
|
|
|
use OCA\Files_Sharing\ShareTargetValidator;
|
|
|
|
|
use OCP\Constants;
|
2026-02-09 12:33:22 -05:00
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2025-12-18 07:34:00 -05:00
|
|
|
use OCP\Files\Config\ICachedMountInfo;
|
2026-02-09 12:33:22 -05:00
|
|
|
use OCP\Files\Mount\IMountManager;
|
2025-12-18 07:34:00 -05:00
|
|
|
use OCP\IUser;
|
|
|
|
|
use OCP\Server;
|
2026-02-09 12:33:22 -05:00
|
|
|
use OCP\Share\Events\VerifyMountPointEvent;
|
|
|
|
|
use OCP\Share\IManager;
|
2025-12-18 07:34:00 -05:00
|
|
|
use OCP\Share\IShare;
|
2026-02-09 12:33:22 -05:00
|
|
|
use Psr\Container\ContainerInterface;
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher as SymfonyEventDispatcher;
|
2025-12-18 07:34:00 -05:00
|
|
|
|
|
|
|
|
#[\PHPUnit\Framework\Attributes\Group('DB')]
|
|
|
|
|
class ShareTargetValidatorTest extends TestCase {
|
2026-02-09 12:33:22 -05:00
|
|
|
private IEventDispatcher $eventDispatcher;
|
2025-12-18 07:34:00 -05:00
|
|
|
private ShareTargetValidator $targetValidator;
|
|
|
|
|
|
|
|
|
|
private IUser $user2;
|
|
|
|
|
protected string $folder2;
|
|
|
|
|
|
|
|
|
|
protected function setUp(): void {
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
$this->folder = '/folder_share_storage_test';
|
|
|
|
|
$this->folder2 = '/folder_share_storage_test2';
|
|
|
|
|
|
|
|
|
|
$this->filename = '/share-api-storage.txt';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$this->view->mkdir($this->folder);
|
|
|
|
|
$this->view->mkdir($this->folder2);
|
|
|
|
|
|
|
|
|
|
// save file with content
|
|
|
|
|
$this->view->file_put_contents($this->filename, 'root file');
|
|
|
|
|
$this->view->file_put_contents($this->folder . $this->filename, 'file in subfolder');
|
|
|
|
|
$this->view->file_put_contents($this->folder2 . $this->filename, 'file in subfolder2');
|
|
|
|
|
|
2026-02-09 12:33:22 -05:00
|
|
|
$this->eventDispatcher = new EventDispatcher(
|
|
|
|
|
new SymfonyEventDispatcher(),
|
|
|
|
|
Server::get(ContainerInterface::class),
|
|
|
|
|
$this->createMock(LoggerInterface::class),
|
|
|
|
|
);
|
|
|
|
|
$this->targetValidator = new ShareTargetValidator(
|
|
|
|
|
Server::get(IManager::class),
|
|
|
|
|
$this->eventDispatcher,
|
|
|
|
|
Server::get(SetupManager::class),
|
|
|
|
|
Server::get(IMountManager::class),
|
|
|
|
|
);
|
2025-12-18 07:34:00 -05:00
|
|
|
$this->user2 = $this->createMock(IUser::class);
|
|
|
|
|
$this->user2->method('getUID')
|
|
|
|
|
->willReturn(self::TEST_FILES_SHARING_API_USER2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* test if the mount point moves up if the parent folder no longer exists
|
|
|
|
|
*/
|
|
|
|
|
public function testShareMountLoseParentFolder(): void {
|
|
|
|
|
// share to user
|
|
|
|
|
$share = $this->share(
|
|
|
|
|
IShare::TYPE_USER,
|
|
|
|
|
$this->folder,
|
|
|
|
|
self::TEST_FILES_SHARING_API_USER1,
|
|
|
|
|
self::TEST_FILES_SHARING_API_USER2,
|
|
|
|
|
Constants::PERMISSION_ALL);
|
|
|
|
|
$this->shareManager->acceptShare($share, self::TEST_FILES_SHARING_API_USER2);
|
|
|
|
|
|
|
|
|
|
$share->setTarget('/foo/bar' . $this->folder);
|
|
|
|
|
$this->shareManager->moveShare($share, self::TEST_FILES_SHARING_API_USER2);
|
|
|
|
|
|
|
|
|
|
$share = $this->shareManager->getShareById($share->getFullId());
|
|
|
|
|
$this->assertSame('/foo/bar' . $this->folder, $share->getTarget());
|
|
|
|
|
|
|
|
|
|
$this->targetValidator->verifyMountPoint($this->user2, $share, [], [$share]);
|
|
|
|
|
|
|
|
|
|
$share = $this->shareManager->getShareById($share->getFullId());
|
|
|
|
|
$this->assertSame($this->folder, $share->getTarget());
|
|
|
|
|
|
|
|
|
|
//cleanup
|
|
|
|
|
self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
|
|
|
|
|
$this->shareManager->deleteShare($share);
|
|
|
|
|
$this->view->unlink($this->folder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* test if the mount point gets renamed if a folder exists at the target
|
|
|
|
|
*/
|
|
|
|
|
public function testShareMountOverFolder(): void {
|
|
|
|
|
self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
|
|
|
|
|
$this->view2->mkdir('bar');
|
|
|
|
|
|
|
|
|
|
self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
|
|
|
|
|
|
|
|
|
|
// share to user
|
|
|
|
|
$share = $this->share(
|
|
|
|
|
IShare::TYPE_USER,
|
|
|
|
|
$this->folder,
|
|
|
|
|
self::TEST_FILES_SHARING_API_USER1,
|
|
|
|
|
self::TEST_FILES_SHARING_API_USER2,
|
|
|
|
|
Constants::PERMISSION_ALL);
|
|
|
|
|
$this->shareManager->acceptShare($share, self::TEST_FILES_SHARING_API_USER2);
|
|
|
|
|
|
|
|
|
|
$share->setTarget('/bar');
|
|
|
|
|
$this->shareManager->moveShare($share, self::TEST_FILES_SHARING_API_USER2);
|
|
|
|
|
|
|
|
|
|
$share = $this->shareManager->getShareById($share->getFullId());
|
|
|
|
|
|
|
|
|
|
$this->targetValidator->verifyMountPoint($this->user2, $share, [], [$share]);
|
|
|
|
|
|
|
|
|
|
$share = $this->shareManager->getShareById($share->getFullId());
|
|
|
|
|
$this->assertSame('/bar (2)', $share->getTarget());
|
|
|
|
|
|
|
|
|
|
//cleanup
|
|
|
|
|
self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
|
|
|
|
|
$this->shareManager->deleteShare($share);
|
|
|
|
|
$this->view->unlink($this->folder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* test if the mount point gets renamed if another share exists at the target
|
|
|
|
|
*/
|
|
|
|
|
public function testShareMountOverShare(): void {
|
|
|
|
|
// share to user
|
|
|
|
|
$share2 = $this->share(
|
|
|
|
|
IShare::TYPE_USER,
|
|
|
|
|
$this->folder2,
|
|
|
|
|
self::TEST_FILES_SHARING_API_USER1,
|
|
|
|
|
self::TEST_FILES_SHARING_API_USER2,
|
|
|
|
|
Constants::PERMISSION_ALL);
|
|
|
|
|
$this->shareManager->acceptShare($share2, self::TEST_FILES_SHARING_API_USER2);
|
|
|
|
|
|
|
|
|
|
$conflictingMount = $this->createMock(ICachedMountInfo::class);
|
|
|
|
|
$this->targetValidator->verifyMountPoint($this->user2, $share2, [
|
|
|
|
|
'/' . $this->user2->getUID() . '/files' . $this->folder2 . '/' => $conflictingMount
|
|
|
|
|
], [$share2]);
|
|
|
|
|
|
|
|
|
|
$share2 = $this->shareManager->getShareById($share2->getFullId());
|
|
|
|
|
|
|
|
|
|
$this->assertSame("{$this->folder2} (2)", $share2->getTarget());
|
|
|
|
|
|
|
|
|
|
//cleanup
|
|
|
|
|
self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
|
|
|
|
|
$this->shareManager->deleteShare($share2);
|
|
|
|
|
$this->view->unlink($this->folder);
|
|
|
|
|
}
|
2026-02-09 12:33:22 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* test if the parent folder is created if asked for
|
|
|
|
|
*/
|
|
|
|
|
public function testShareMountCreateParentFolder(): void {
|
|
|
|
|
// share to user
|
|
|
|
|
$share = $this->share(
|
|
|
|
|
IShare::TYPE_USER,
|
|
|
|
|
$this->folder,
|
|
|
|
|
self::TEST_FILES_SHARING_API_USER1,
|
|
|
|
|
self::TEST_FILES_SHARING_API_USER2,
|
|
|
|
|
Constants::PERMISSION_ALL);
|
|
|
|
|
$this->shareManager->acceptShare($share, self::TEST_FILES_SHARING_API_USER2);
|
|
|
|
|
|
|
|
|
|
$share->setTarget('/foo/bar' . $this->folder);
|
|
|
|
|
$this->shareManager->moveShare($share, self::TEST_FILES_SHARING_API_USER2);
|
|
|
|
|
|
|
|
|
|
$share = $this->shareManager->getShareById($share->getFullId());
|
|
|
|
|
$this->assertSame('/foo/bar' . $this->folder, $share->getTarget());
|
|
|
|
|
|
|
|
|
|
$this->eventDispatcher->addListener(VerifyMountPointEvent::class, function (VerifyMountPointEvent $event) {
|
|
|
|
|
$event->setCreateParent(true);
|
|
|
|
|
});
|
|
|
|
|
$this->targetValidator->verifyMountPoint($this->user2, $share, [], [$share]);
|
|
|
|
|
|
|
|
|
|
$share = $this->shareManager->getShareById($share->getFullId());
|
|
|
|
|
$this->assertSame('/foo/bar' . $this->folder, $share->getTarget());
|
|
|
|
|
$userFolder = $this->rootFolder->getUserFolder(self::TEST_FILES_SHARING_API_USER2);
|
|
|
|
|
$this->assertTrue($userFolder->nodeExists('/foo/bar'));
|
|
|
|
|
|
|
|
|
|
//cleanup
|
|
|
|
|
self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
|
|
|
|
|
$this->shareManager->deleteShare($share);
|
|
|
|
|
$this->view->unlink($this->folder);
|
|
|
|
|
}
|
2025-12-18 07:34:00 -05:00
|
|
|
}
|