1
0

MountProviderTrait.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. if ($this->IsDatabaseAccessAllowed()) {
  31. $mount = new MountPoint($storage, $mountPoint, $arguments, $this->storageFactory);
  32. $storage = $mount->getStorage();
  33. $storage->getScanner()->scan('');
  34. }
  35. }
  36. protected function registerStorageWrapper($name, $wrapper) {
  37. $this->storageFactory->addStorageWrapper($name, $wrapper);
  38. }
  39. protected function setUpMountProviderTrait() {
  40. $this->storageFactory = new StorageFactory();
  41. $this->mountProvider = $this->getMockBuilder('\OCP\Files\Config\IMountProvider')->getMock();
  42. $this->mountProvider->expects($this->any())
  43. ->method('getMountsForUser')
  44. ->will($this->returnCallback(function (IUser $user) {
  45. if (isset($this->mounts[$user->getUID()])) {
  46. return array_map(function ($config) {
  47. return new MountPoint($config['storage'], $config['mountPoint'], $config['arguments'], $this->storageFactory);
  48. }, $this->mounts[$user->getUID()]);
  49. } else {
  50. return [];
  51. }
  52. }));
  53. \OC::$server->getMountProviderCollection()->registerProvider($this->mountProvider);
  54. }
  55. }