|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-only |
| 6 | + */ |
| 7 | + |
| 8 | +namespace OCA\Files_Sharing\Tests; |
| 9 | + |
| 10 | +use OCA\Files_Sharing\MountProvider; |
| 11 | +use OCA\Files_Sharing\ShareRecipientUpdater; |
| 12 | +use OCA\Files_Sharing\ShareTargetValidator; |
| 13 | +use OCP\Files\Cache\ICacheEntry; |
| 14 | +use OCP\Files\Config\ICachedMountInfo; |
| 15 | +use OCP\Files\Config\IUserMountCache; |
| 16 | +use OCP\Files\Mount\IMountPoint; |
| 17 | +use OCP\Files\Node; |
| 18 | +use OCP\Files\Storage\IStorageFactory; |
| 19 | +use OCP\IUser; |
| 20 | +use OCP\Share\IShare; |
| 21 | +use PHPUnit\Framework\MockObject\MockObject; |
| 22 | +use Test\Traits\UserTrait; |
| 23 | + |
| 24 | +class ShareRecipientUpdaterTest extends \Test\TestCase { |
| 25 | + use UserTrait; |
| 26 | + |
| 27 | + private IUserMountCache&MockObject $userMountCache; |
| 28 | + private MountProvider&MockObject $shareMountProvider; |
| 29 | + private ShareTargetValidator&MockObject $shareTargetValidator; |
| 30 | + private IStorageFactory&MockObject $storageFactory; |
| 31 | + private ShareRecipientUpdater $updater; |
| 32 | + |
| 33 | + protected function setUp(): void { |
| 34 | + parent::setUp(); |
| 35 | + |
| 36 | + $this->userMountCache = $this->createMock(IUserMountCache::class); |
| 37 | + $this->shareMountProvider = $this->createMock(MountProvider::class); |
| 38 | + $this->shareTargetValidator = $this->createMock(ShareTargetValidator::class); |
| 39 | + $this->storageFactory = $this->createMock(IStorageFactory::class); |
| 40 | + |
| 41 | + $this->updater = new ShareRecipientUpdater( |
| 42 | + $this->userMountCache, |
| 43 | + $this->shareMountProvider, |
| 44 | + $this->shareTargetValidator, |
| 45 | + $this->storageFactory, |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + public function testUpdateForShare() { |
| 50 | + $share = $this->createMock(IShare::class); |
| 51 | + $node = $this->createMock(Node::class); |
| 52 | + $cacheEntry = $this->createMock(ICacheEntry::class); |
| 53 | + $share->method('getNode') |
| 54 | + ->willReturn($node); |
| 55 | + $node->method('getData') |
| 56 | + ->willReturn($cacheEntry); |
| 57 | + $user1 = $this->createUser('user1', ''); |
| 58 | + |
| 59 | + $this->userMountCache->method('getMountsForUser') |
| 60 | + ->with($user1) |
| 61 | + ->willReturn([]); |
| 62 | + |
| 63 | + $this->shareTargetValidator->method('verifyMountPoint') |
| 64 | + ->with($user1, $share, fn ($path) => null, [$share]) |
| 65 | + ->willReturn('/new-target'); |
| 66 | + |
| 67 | + $this->userMountCache->expects($this->exactly(1)) |
| 68 | + ->method('addMount') |
| 69 | + ->with($user1, '/user1/files/new-target/', $cacheEntry, MountProvider::class); |
| 70 | + |
| 71 | + $this->updater->updateForAddedShare($user1, $share); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @param IUser $user |
| 76 | + * @param list<array{fileid: int, mount_point: string, provider: string}> $mounts |
| 77 | + * @return void |
| 78 | + */ |
| 79 | + private function setCachedMounts(IUser $user, array $mounts) { |
| 80 | + $cachedMounts = array_map(function (array $mount): ICachedMountInfo { |
| 81 | + $cachedMount = $this->createMock(ICachedMountInfo::class); |
| 82 | + $cachedMount->method('getRootId') |
| 83 | + ->willReturn($mount['fileid']); |
| 84 | + $cachedMount->method('getMountPoint') |
| 85 | + ->willReturn($mount['mount_point']); |
| 86 | + $cachedMount->method('getMountProvider') |
| 87 | + ->willReturn($mount['provider']); |
| 88 | + return $cachedMount; |
| 89 | + }, $mounts); |
| 90 | + $mountKeys = array_map(function (array $mount): string { |
| 91 | + return $mount['fileid'] . '::' . $mount['mount_point']; |
| 92 | + }, $mounts); |
| 93 | + |
| 94 | + $this->userMountCache->method('getMountsForUser') |
| 95 | + ->with($user) |
| 96 | + ->willReturn(array_combine($mountKeys, $cachedMounts)); |
| 97 | + } |
| 98 | + |
| 99 | + public function testUpdateForUserAddedNoExisting() { |
| 100 | + $share = $this->createMock(IShare::class); |
| 101 | + $share->method('getTarget') |
| 102 | + ->willReturn('/target'); |
| 103 | + $share->method('getNodeId') |
| 104 | + ->willReturn(111); |
| 105 | + $user1 = $this->createUser('user1', ''); |
| 106 | + $newMount = $this->createMock(IMountPoint::class); |
| 107 | + |
| 108 | + $this->shareMountProvider->method('getSuperSharesForUser') |
| 109 | + ->with($user1, []) |
| 110 | + ->willReturn([[ |
| 111 | + $share, |
| 112 | + [$share], |
| 113 | + ]]); |
| 114 | + |
| 115 | + $this->shareMountProvider->method('getMountsFromSuperShares') |
| 116 | + ->with($user1, [[ |
| 117 | + $share, |
| 118 | + [$share], |
| 119 | + ]], $this->storageFactory) |
| 120 | + ->willReturn([$newMount]); |
| 121 | + |
| 122 | + $this->setCachedMounts($user1, []); |
| 123 | + |
| 124 | + $this->shareTargetValidator->method('verifyMountPoint') |
| 125 | + ->with($user1, $share, fn ($path) => null, [$share]) |
| 126 | + ->willReturn('/new-target'); |
| 127 | + |
| 128 | + $this->userMountCache->expects($this->exactly(1)) |
| 129 | + ->method('registerMounts') |
| 130 | + ->with($user1, [$newMount], [MountProvider::class]); |
| 131 | + |
| 132 | + $this->updater->updateForUser($user1); |
| 133 | + } |
| 134 | + |
| 135 | + public function testUpdateForUserNoChanges() { |
| 136 | + $share = $this->createMock(IShare::class); |
| 137 | + $share->method('getTarget') |
| 138 | + ->willReturn('/target'); |
| 139 | + $share->method('getNodeId') |
| 140 | + ->willReturn(111); |
| 141 | + $user1 = $this->createUser('user1', ''); |
| 142 | + |
| 143 | + $this->shareMountProvider->method('getSuperSharesForUser') |
| 144 | + ->with($user1, []) |
| 145 | + ->willReturn([[ |
| 146 | + $share, |
| 147 | + [$share], |
| 148 | + ]]); |
| 149 | + |
| 150 | + $this->setCachedMounts($user1, [ |
| 151 | + ['fileid' => 111, 'mount_point' => '/user1/files/target/', 'provider' => MountProvider::class], |
| 152 | + ]); |
| 153 | + |
| 154 | + $this->shareTargetValidator->expects($this->never()) |
| 155 | + ->method('verifyMountPoint'); |
| 156 | + |
| 157 | + $this->userMountCache->expects($this->never()) |
| 158 | + ->method('registerMounts'); |
| 159 | + |
| 160 | + $this->updater->updateForUser($user1); |
| 161 | + } |
| 162 | + |
| 163 | + public function testUpdateForUserRemoved() { |
| 164 | + $share = $this->createMock(IShare::class); |
| 165 | + $share->method('getTarget') |
| 166 | + ->willReturn('/target'); |
| 167 | + $share->method('getNodeId') |
| 168 | + ->willReturn(111); |
| 169 | + $user1 = $this->createUser('user1', ''); |
| 170 | + |
| 171 | + $this->shareMountProvider->method('getSuperSharesForUser') |
| 172 | + ->with($user1, []) |
| 173 | + ->willReturn([]); |
| 174 | + |
| 175 | + $this->setCachedMounts($user1, [ |
| 176 | + ['fileid' => 111, 'mount_point' => '/user1/files/target/', 'provider' => MountProvider::class], |
| 177 | + ]); |
| 178 | + |
| 179 | + $this->shareTargetValidator->expects($this->never()) |
| 180 | + ->method('verifyMountPoint'); |
| 181 | + |
| 182 | + $this->userMountCache->expects($this->exactly(1)) |
| 183 | + ->method('registerMounts') |
| 184 | + ->with($user1, [], [MountProvider::class]); |
| 185 | + |
| 186 | + $this->updater->updateForUser($user1); |
| 187 | + } |
| 188 | + |
| 189 | + public function testDeletedShare() { |
| 190 | + $share = $this->createMock(IShare::class); |
| 191 | + $share->method('getTarget') |
| 192 | + ->willReturn('/target'); |
| 193 | + $share->method('getNodeId') |
| 194 | + ->willReturn(111); |
| 195 | + $user1 = $this->createUser('user1', ''); |
| 196 | + |
| 197 | + $this->shareTargetValidator->expects($this->never()) |
| 198 | + ->method('verifyMountPoint'); |
| 199 | + |
| 200 | + $this->userMountCache->expects($this->exactly(1)) |
| 201 | + ->method('removeMount') |
| 202 | + ->with('/user1/files/target/'); |
| 203 | + |
| 204 | + $this->updater->updateForDeletedShare($user1, $share); |
| 205 | + } |
| 206 | +} |
0 commit comments