1
0

MountProviderTrait.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * Copyright (c) 2015 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\Traits;
  9. use OC\Files\Mount\MountPoint;
  10. use OC\Files\Storage\StorageFactory;
  11. use OCP\IUser;
  12. /**
  13. * Allow setting mounts for users
  14. */
  15. trait MountProviderTrait {
  16. /**
  17. * @var \OCP\Files\Config\IMountProvider
  18. */
  19. protected $mountProvider;
  20. /**
  21. * @var \OC\Files\Storage\StorageFactory
  22. */
  23. protected $storageFactory;
  24. protected $mounts = [];
  25. protected function registerMount($userId, $storage, $mountPoint, $arguments = null) {
  26. if (!isset($this->mounts[$userId])) {
  27. $this->mounts[$userId] = [];
  28. }
  29. $this->mounts[$userId][] = ['storage' => $storage, 'mountPoint' => $mountPoint, 'arguments' => $arguments];
  30. }
  31. protected function registerStorageWrapper($name, $wrapper) {
  32. $this->storageFactory->addStorageWrapper($name, $wrapper);
  33. }
  34. protected function setUpMountProviderTrait() {
  35. $this->storageFactory = new StorageFactory();
  36. $this->mountProvider = $this->getMockBuilder('\OCP\Files\Config\IMountProvider')->getMock();
  37. $this->mountProvider->expects($this->any())
  38. ->method('getMountsForUser')
  39. ->will($this->returnCallback(function (IUser $user) {
  40. if (isset($this->mounts[$user->getUID()])) {
  41. return array_map(function ($config) {
  42. return new MountPoint($config['storage'], $config['mountPoint'], $config['arguments'], $this->storageFactory);
  43. }, $this->mounts[$user->getUID()]);
  44. } else {
  45. return [];
  46. }
  47. }));
  48. \OC::$server->getMountProviderCollection()->registerProvider($this->mountProvider);
  49. }
  50. }