|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2025 Robin Appelman <robin@icewind.nl> |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OCA\Files_Sharing\Tests; |
| 10 | + |
| 11 | +use OC\EventDispatcher\EventDispatcher; |
| 12 | +use OC\Files\SetupManager; |
| 13 | +use OCA\Files_Sharing\ShareTargetValidator; |
| 14 | +use OCP\Constants; |
| 15 | +use OCP\EventDispatcher\IEventDispatcher; |
| 16 | +use OCP\Files\Config\ICachedMountInfo; |
| 17 | +use OCP\Files\Mount\IMountManager; |
| 18 | +use OCP\IUser; |
| 19 | +use OCP\Server; |
| 20 | +use OCP\Share\Events\VerifyMountPointEvent; |
| 21 | +use OCP\Share\IManager; |
| 22 | +use OCP\Share\IShare; |
| 23 | +use Psr\Container\ContainerInterface; |
| 24 | +use Psr\Log\LoggerInterface; |
| 25 | +use Symfony\Component\EventDispatcher\EventDispatcher as SymfonyEventDispatcher; |
| 26 | + |
| 27 | +#[\PHPUnit\Framework\Attributes\Group('DB')] |
| 28 | +class ShareTargetValidatorTest extends TestCase { |
| 29 | + private IEventDispatcher $eventDispatcher; |
| 30 | + private ShareTargetValidator $targetValidator; |
| 31 | + |
| 32 | + private IUser $user2; |
| 33 | + protected string $folder2; |
| 34 | + |
| 35 | + protected function setUp(): void { |
| 36 | + parent::setUp(); |
| 37 | + |
| 38 | + $this->folder = '/folder_share_storage_test'; |
| 39 | + $this->folder2 = '/folder_share_storage_test2'; |
| 40 | + |
| 41 | + $this->filename = '/share-api-storage.txt'; |
| 42 | + |
| 43 | + |
| 44 | + $this->view->mkdir($this->folder); |
| 45 | + $this->view->mkdir($this->folder2); |
| 46 | + |
| 47 | + // save file with content |
| 48 | + $this->view->file_put_contents($this->filename, 'root file'); |
| 49 | + $this->view->file_put_contents($this->folder . $this->filename, 'file in subfolder'); |
| 50 | + $this->view->file_put_contents($this->folder2 . $this->filename, 'file in subfolder2'); |
| 51 | + |
| 52 | + $this->eventDispatcher = new EventDispatcher( |
| 53 | + new SymfonyEventDispatcher(), |
| 54 | + Server::get(ContainerInterface::class), |
| 55 | + $this->createMock(LoggerInterface::class), |
| 56 | + ); |
| 57 | + $this->targetValidator = new ShareTargetValidator( |
| 58 | + Server::get(IManager::class), |
| 59 | + $this->eventDispatcher, |
| 60 | + Server::get(SetupManager::class), |
| 61 | + Server::get(IMountManager::class), |
| 62 | + ); |
| 63 | + $this->user2 = $this->createMock(IUser::class); |
| 64 | + $this->user2->method('getUID') |
| 65 | + ->willReturn(self::TEST_FILES_SHARING_API_USER2); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * test if the parent folder is created if asked for |
| 70 | + */ |
| 71 | + public function testShareMountCreateParentFolder(): void { |
| 72 | + // share to user |
| 73 | + $share = $this->share( |
| 74 | + IShare::TYPE_USER, |
| 75 | + $this->folder, |
| 76 | + self::TEST_FILES_SHARING_API_USER1, |
| 77 | + self::TEST_FILES_SHARING_API_USER2, |
| 78 | + Constants::PERMISSION_ALL); |
| 79 | + $this->shareManager->acceptShare($share, self::TEST_FILES_SHARING_API_USER2); |
| 80 | + |
| 81 | + $share->setTarget('/foo/bar' . $this->folder); |
| 82 | + $this->shareManager->moveShare($share, self::TEST_FILES_SHARING_API_USER2); |
| 83 | + |
| 84 | + $share = $this->shareManager->getShareById($share->getFullId()); |
| 85 | + $this->assertSame('/foo/bar' . $this->folder, $share->getTarget()); |
| 86 | + |
| 87 | + $this->eventDispatcher->addListener(VerifyMountPointEvent::class, function (VerifyMountPointEvent $event) { |
| 88 | + $event->setCreateParent(true); |
| 89 | + }); |
| 90 | + $this->targetValidator->verifyMountPoint($this->user2, $share, [], [$share]); |
| 91 | + |
| 92 | + $share = $this->shareManager->getShareById($share->getFullId()); |
| 93 | + $this->assertSame('/foo/bar' . $this->folder, $share->getTarget()); |
| 94 | + $userFolder = $this->rootFolder->getUserFolder(self::TEST_FILES_SHARING_API_USER2); |
| 95 | + $this->assertTrue($userFolder->nodeExists('/foo/bar')); |
| 96 | + |
| 97 | + //cleanup |
| 98 | + self::loginHelper(self::TEST_FILES_SHARING_API_USER1); |
| 99 | + $this->shareManager->deleteShare($share); |
| 100 | + $this->view->unlink($this->folder); |
| 101 | + } |
| 102 | +} |
0 commit comments