MountProviderTrait.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Traits;
  8. use OC\Files\Mount\MountPoint;
  9. use OC\Files\Storage\StorageFactory;
  10. use OCP\IUser;
  11. /**
  12. * Allow setting mounts for users
  13. */
  14. trait MountProviderTrait {
  15. /**
  16. * @var \OCP\Files\Config\IMountProvider
  17. */
  18. protected $mountProvider;
  19. /**
  20. * @var \OC\Files\Storage\StorageFactory
  21. */
  22. protected $storageFactory;
  23. protected $mounts = [];
  24. protected function registerMount($userId, $storage, $mountPoint, $arguments = null) {
  25. if (!isset($this->mounts[$userId])) {
  26. $this->mounts[$userId] = [];
  27. }
  28. $this->mounts[$userId][] = ['storage' => $storage, 'mountPoint' => $mountPoint, 'arguments' => $arguments];
  29. if ($this->IsDatabaseAccessAllowed()) {
  30. $mount = new MountPoint($storage, $mountPoint, $arguments, $this->storageFactory);
  31. $storage = $mount->getStorage();
  32. $storage->getScanner()->scan('');
  33. }
  34. }
  35. protected function registerStorageWrapper($name, $wrapper) {
  36. $this->storageFactory->addStorageWrapper($name, $wrapper);
  37. }
  38. protected function setUpMountProviderTrait() {
  39. $this->storageFactory = new StorageFactory();
  40. $this->mountProvider = $this->getMockBuilder('\OCP\Files\Config\IMountProvider')->getMock();
  41. $this->mountProvider->expects($this->any())
  42. ->method('getMountsForUser')
  43. ->will($this->returnCallback(function (IUser $user) {
  44. if (isset($this->mounts[$user->getUID()])) {
  45. return array_map(function ($config) {
  46. return new MountPoint($config['storage'], $config['mountPoint'], $config['arguments'], $this->storageFactory);
  47. }, $this->mounts[$user->getUID()]);
  48. } else {
  49. return [];
  50. }
  51. }));
  52. \OC::$server->getMountProviderCollection()->registerProvider($this->mountProvider);
  53. }
  54. }