-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
test(files_sharing): Check mount events are dispatched for share recipients #64715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
marcelklehr
wants to merge
2
commits into
master
Choose a base branch
from
test/share-mount-events
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Files_Sharing\Tests; | ||
|
|
||
| use OCA\Files_Sharing\Listener\SharesUpdatedListener; | ||
| use OCP\Constants; | ||
| use OCP\EventDispatcher\Event; | ||
| use OCP\EventDispatcher\IEventDispatcher; | ||
| use OCP\Files\Config\Event\UserMountAddedEvent; | ||
| use OCP\Files\Config\Event\UserMountRemovedEvent; | ||
| use OCP\Files\Folder; | ||
| use OCP\Server; | ||
| use OCP\Share\IShare; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
|
|
||
| /** | ||
| * Apps like recognize keep per-user data in sync with file access by listening to | ||
| * UserMountAddedEvent and UserMountRemovedEvent. These events have to be dispatched | ||
| * for the recipients of a share when it is created or deleted, at the latest when a | ||
| * recipient sets up their file system afterwards, regardless of whether the share | ||
| * mounts of the recipient are updated immediately or deferred until then. | ||
| */ | ||
| #[\PHPUnit\Framework\Attributes\Group(name: 'DB')] | ||
| class ShareMountEventsTest extends TestCase { | ||
| private const FOLDER = 'shared-folder'; | ||
|
|
||
| private IEventDispatcher $eventDispatcher; | ||
| /** @var list<Event> */ | ||
| private array $events = []; | ||
| /** @var \Closure(Event): void */ | ||
| private \Closure $listener; | ||
|
|
||
| protected function setUp(): void { | ||
| parent::setUp(); | ||
|
|
||
| $this->eventDispatcher = Server::get(IEventDispatcher::class); | ||
| $this->listener = function (Event $event): void { | ||
| $this->events[] = $event; | ||
| }; | ||
| $this->eventDispatcher->addListener(UserMountAddedEvent::class, $this->listener); | ||
| $this->eventDispatcher->addListener(UserMountRemovedEvent::class, $this->listener); | ||
|
|
||
| $this->rootFolder->getUserFolder(self::TEST_FILES_SHARING_API_USER1)->newFolder(self::FOLDER); | ||
|
|
||
| // The recipient has used their file system before | ||
| $this->assertFalse($this->getUserFolder(self::TEST_FILES_SHARING_API_USER2)->nodeExists(self::FOLDER)); | ||
| $this->loginHelper(self::TEST_FILES_SHARING_API_USER1); | ||
| $this->events = []; | ||
| } | ||
|
|
||
| protected function tearDown(): void { | ||
| $this->eventDispatcher->removeListener(UserMountAddedEvent::class, $this->listener); | ||
| $this->eventDispatcher->removeListener(UserMountRemovedEvent::class, $this->listener); | ||
| Server::get(SharesUpdatedListener::class)->setCutOffMarkTime(-1); | ||
|
|
||
| parent::tearDown(); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, array{int, string, float}> | ||
| */ | ||
| public static function shareProvider(): array { | ||
| // A cutoff time of -1 updates the share mounts of recipients immediately, | ||
| // 0 marks recipients so their share mounts are updated on their next setup | ||
| return [ | ||
| 'user share, immediate update' => [IShare::TYPE_USER, self::TEST_FILES_SHARING_API_USER2, -1], | ||
| 'user share, deferred update' => [IShare::TYPE_USER, self::TEST_FILES_SHARING_API_USER2, 0], | ||
| 'group share, immediate update' => [IShare::TYPE_GROUP, self::TEST_FILES_SHARING_API_GROUP1, -1], | ||
| 'group share, deferred update' => [IShare::TYPE_GROUP, self::TEST_FILES_SHARING_API_GROUP1, 0], | ||
| ]; | ||
| } | ||
|
|
||
| #[DataProvider(methodName: 'shareProvider')] | ||
| public function testMountAddedEventForCreatedShare(int $shareType, string $recipient, float $cutOffTime): void { | ||
| Server::get(SharesUpdatedListener::class)->setCutOffMarkTime($cutOffTime); | ||
|
|
||
| $this->share($shareType, self::FOLDER, self::TEST_FILES_SHARING_API_USER1, $recipient, Constants::PERMISSION_ALL); | ||
| $this->getUserFolder(self::TEST_FILES_SHARING_API_USER2)->get(self::FOLDER); | ||
|
|
||
| $this->assertEquals( | ||
| ['/' . self::TEST_FILES_SHARING_API_USER2 . '/files/' . self::FOLDER . '/'], | ||
| $this->getMountPointsOfEvents(UserMountAddedEvent::class, self::TEST_FILES_SHARING_API_USER2), | ||
| 'A UserMountAddedEvent should be dispatched for the share mount of the recipient', | ||
| ); | ||
| } | ||
|
|
||
| #[DataProvider(methodName: 'shareProvider')] | ||
| public function testMountRemovedEventForDeletedShare(int $shareType, string $recipient, float $cutOffTime): void { | ||
| $share = $this->share($shareType, self::FOLDER, self::TEST_FILES_SHARING_API_USER1, $recipient, Constants::PERMISSION_ALL); | ||
| $this->getUserFolder(self::TEST_FILES_SHARING_API_USER2)->get(self::FOLDER); | ||
| $this->loginHelper(self::TEST_FILES_SHARING_API_USER1); | ||
| $this->events = []; | ||
|
|
||
| Server::get(SharesUpdatedListener::class)->setCutOffMarkTime($cutOffTime); | ||
| $this->shareManager->deleteShare($share); | ||
| $this->assertFalse( | ||
| $this->getUserFolder(self::TEST_FILES_SHARING_API_USER2)->nodeExists(self::FOLDER), | ||
| 'The share should no longer be mounted for the recipient', | ||
| ); | ||
|
|
||
| $this->assertEquals( | ||
| ['/' . self::TEST_FILES_SHARING_API_USER2 . '/files/' . self::FOLDER . '/'], | ||
| $this->getMountPointsOfEvents(UserMountRemovedEvent::class, self::TEST_FILES_SHARING_API_USER2), | ||
| 'A UserMountRemovedEvent should be dispatched for the share mount of the recipient', | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Log in as the user and get their user folder, as a new request by them would | ||
| */ | ||
| private function getUserFolder(string $userId): Folder { | ||
| $this->loginHelper($userId); | ||
| return $this->rootFolder->getUserFolder($userId); | ||
| } | ||
|
marcelklehr marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * @param class-string<UserMountAddedEvent|UserMountRemovedEvent> $eventClass | ||
| * @return list<string> | ||
| */ | ||
| private function getMountPointsOfEvents(string $eventClass, string $userId): array { | ||
| $mountPoints = []; | ||
| foreach ($this->events as $event) { | ||
| if ($event instanceof $eventClass && $event->mountPoint->getUser()->getUID() === $userId) { | ||
| $mountPoints[] = $event->mountPoint->getMountPoint(); | ||
| } | ||
| } | ||
| return $mountPoints; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.