MountPointTest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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\Files\Mount;
  8. use OC\Files\Storage\StorageFactory;
  9. use OC\Lockdown\Filesystem\NullStorage;
  10. use OCP\Files\Storage\IStorage;
  11. class MountPointTest extends \Test\TestCase {
  12. public function testGetStorage(): void {
  13. $storage = $this->createMock(IStorage::class);
  14. $storage->expects($this->once())
  15. ->method('getId')
  16. ->willReturn(123);
  17. $loader = $this->createMock(StorageFactory::class);
  18. $loader->expects($this->once())
  19. ->method('wrap')
  20. ->willReturn($storage);
  21. $mountPoint = new \OC\Files\Mount\MountPoint(
  22. // just use this because a real class is needed
  23. NullStorage::class,
  24. '/mountpoint',
  25. null,
  26. $loader
  27. );
  28. $this->assertEquals($storage, $mountPoint->getStorage());
  29. $this->assertEquals(123, $mountPoint->getStorageId());
  30. $this->assertEquals('/mountpoint/', $mountPoint->getMountPoint());
  31. $mountPoint->setMountPoint('another');
  32. $this->assertEquals('/another/', $mountPoint->getMountPoint());
  33. }
  34. public function testInvalidStorage(): void {
  35. $loader = $this->createMock(StorageFactory::class);
  36. $loader->expects($this->once())
  37. ->method('wrap')
  38. ->will($this->throwException(new \Exception('Test storage init exception')));
  39. $called = false;
  40. $wrapper = function ($mountPoint, $storage) use ($called) {
  41. $called = true;
  42. };
  43. $mountPoint = new \OC\Files\Mount\MountPoint(
  44. // just use this because a real class is needed
  45. NullStorage::class,
  46. '/mountpoint',
  47. null,
  48. $loader
  49. );
  50. $this->assertNull($mountPoint->getStorage());
  51. // call it again to make sure the init code only ran once
  52. $this->assertNull($mountPoint->getStorage());
  53. $this->assertNull($mountPoint->getStorageId());
  54. // wrapping doesn't fail
  55. $mountPoint->wrapStorage($wrapper);
  56. $this->assertNull($mountPoint->getStorage());
  57. // storage wrapper never called
  58. $this->assertFalse($called);
  59. }
  60. }