diff --git a/apps/files_sharing/tests/ShareRecipientUpdaterTest.php b/apps/files_sharing/tests/ShareRecipientUpdaterTest.php new file mode 100644 index 00000000000..e32f7d88fb4 --- /dev/null +++ b/apps/files_sharing/tests/ShareRecipientUpdaterTest.php @@ -0,0 +1,188 @@ +userMountCache = $this->createMock(IUserMountCache::class); + $this->shareMountProvider = $this->createMock(MountProvider::class); + $this->shareTargetValidator = $this->createMock(ShareTargetValidator::class); + $this->storageFactory = $this->createMock(IStorageFactory::class); + + $this->updater = new ShareRecipientUpdater( + $this->userMountCache, + $this->shareMountProvider, + $this->shareTargetValidator, + $this->storageFactory, + ); + } + + public function testUpdateForShare() { + $share = $this->createMock(IShare::class); + $node = $this->createMock(Node::class); + $cacheEntry = $this->createMock(ICacheEntry::class); + $share->method('getNode') + ->willReturn($node); + $node->method('getData') + ->willReturn($cacheEntry); + $user1 = $this->createUser('user1', ''); + + $this->userMountCache->method('getMountsForUser') + ->with($user1) + ->willReturn([]); + + $this->shareTargetValidator->method('verifyMountPoint') + ->with($user1, $share, [], [$share]) + ->willReturn('/new-target'); + + $this->userMountCache->expects($this->exactly(1)) + ->method('addMount') + ->with($user1, '/user1/files/new-target/', $cacheEntry, MountProvider::class); + + $this->updater->updateForShare($user1, $share); + } + + /** + * @param IUser $user + * @param list $mounts + * @return void + */ + private function setCachedMounts(IUser $user, array $mounts) { + $cachedMounts = array_map(function (array $mount): ICachedMountInfo { + $cachedMount = $this->createMock(ICachedMountInfo::class); + $cachedMount->method('getRootId') + ->willReturn($mount['fileid']); + $cachedMount->method('getMountPoint') + ->willReturn($mount['mount_point']); + $cachedMount->method('getMountProvider') + ->willReturn($mount['provider']); + return $cachedMount; + }, $mounts); + $mountKeys = array_map(function (array $mount): string { + return $mount['fileid'] . '::' . $mount['mount_point']; + }, $mounts); + + $this->userMountCache->method('getMountsForUser') + ->with($user) + ->willReturn(array_combine($mountKeys, $cachedMounts)); + } + + public function testUpdateForUserAddedNoExisting() { + $share = $this->createMock(IShare::class); + $share->method('getTarget') + ->willReturn('/target'); + $share->method('getNodeId') + ->willReturn(111); + $user1 = $this->createUser('user1', ''); + $newMount = $this->createMock(IMountPoint::class); + + $this->shareMountProvider->method('getSuperSharesForUser') + ->with($user1, []) + ->willReturn([[ + $share, + [$share], + ]]); + + $this->shareMountProvider->method('getMountsFromSuperShares') + ->with($user1, [[ + $share, + [$share], + ]], $this->storageFactory) + ->willReturn([$newMount]); + + $this->setCachedMounts($user1, []); + + $this->shareTargetValidator->method('verifyMountPoint') + ->with($user1, $share, [], [$share]) + ->willReturn('/new-target'); + + $this->userMountCache->expects($this->exactly(1)) + ->method('registerMounts') + ->with($user1, [$newMount], [MountProvider::class]); + + $this->updater->updateForUser($user1); + } + + public function testUpdateForUserNoChanges() { + $share = $this->createMock(IShare::class); + $share->method('getTarget') + ->willReturn('/target'); + $share->method('getNodeId') + ->willReturn(111); + $user1 = $this->createUser('user1', ''); + + $this->shareMountProvider->method('getSuperSharesForUser') + ->with($user1, []) + ->willReturn([[ + $share, + [$share], + ]]); + + $this->setCachedMounts($user1, [ + ['fileid' => 111, 'mount_point' => '/user1/files/target/', 'provider' => MountProvider::class], + ]); + + $this->shareTargetValidator->expects($this->never()) + ->method('verifyMountPoint'); + + $this->userMountCache->expects($this->never()) + ->method('registerMounts'); + + $this->updater->updateForUser($user1); + } + + public function testUpdateForUserRemoved() { + $share = $this->createMock(IShare::class); + $share->method('getTarget') + ->willReturn('/target'); + $share->method('getNodeId') + ->willReturn(111); + $user1 = $this->createUser('user1', ''); + + $this->shareMountProvider->method('getSuperSharesForUser') + ->with($user1, []) + ->willReturn([]); + + $this->setCachedMounts($user1, [ + ['fileid' => 111, 'mount_point' => '/user1/files/target/', 'provider' => MountProvider::class], + ]); + + $this->shareTargetValidator->expects($this->never()) + ->method('verifyMountPoint'); + + $this->userMountCache->expects($this->exactly(1)) + ->method('registerMounts') + ->with($user1, [], [MountProvider::class]); + + $this->updater->updateForUser($user1); + } +}