1
0

CacheMountProviderTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Files\Mount;
  8. use OC\Files\Mount\CacheMountProvider;
  9. use OC\Files\Storage\StorageFactory;
  10. use OCP\Files\Storage\IStorageFactory;
  11. use OCP\IConfig;
  12. use OCP\IUser;
  13. use Test\TestCase;
  14. class CacheMountProviderTestStream {
  15. public static $statCounter = 0;
  16. public static $mkdirCounter = 0;
  17. public $context;
  18. public function mkdir(string $path, int $mode, int $options): bool {
  19. self::$mkdirCounter++;
  20. return true;
  21. }
  22. public function url_stat(string $path, int $flags): array|false {
  23. self::$statCounter++;
  24. return false;
  25. }
  26. }
  27. class CacheMountProviderTest extends TestCase {
  28. private IConfig $config;
  29. private IUser $user;
  30. private IStorageFactory $storageFactory;
  31. protected function setUp(): void {
  32. $this->config = $this->createMock(IConfig::class);
  33. $this->user = $this->createMock(IUser::class);
  34. $this->storageFactory = new StorageFactory();
  35. stream_wrapper_register('cachemountprovidertest', CacheMountProviderTestStream::class);
  36. }
  37. protected function tearDown(): void {
  38. stream_wrapper_unregister('cachemountprovidertest');
  39. }
  40. public function testGetMountsForUser(): void {
  41. $provider = new CacheMountProvider($this->config);
  42. $this->assertCount(0, $provider->getMountsForUser($this->user, $this->storageFactory));
  43. }
  44. public function testGetMountsForUserCacheDir(): void {
  45. $this->config->expects($this->exactly(1))
  46. ->method('getSystemValueString')
  47. ->willReturnMap([
  48. ['cache_path', '', 'cachemountprovidertest:////cache_path'],
  49. ]);
  50. $this->user->method('getUID')
  51. ->willReturn('bob');
  52. $provider = new CacheMountProvider($this->config);
  53. $mounts = $provider->getMountsForUser($this->user, $this->storageFactory);
  54. $this->assertCount(2, $mounts);
  55. $this->assertEquals(1, CacheMountProviderTestStream::$statCounter);
  56. $this->assertEquals(2, CacheMountProviderTestStream::$mkdirCounter);
  57. $cacheMountProvider = $mounts[0];
  58. $this->assertEquals('/bob/cache/', $cacheMountProvider->getMountPoint());
  59. $cacheStorage = $cacheMountProvider->getStorage();
  60. $this->assertEquals('local::cachemountprovidertest://cache_path/bob/', $cacheStorage->getId());
  61. $uploadsMountProvider = $mounts[1];
  62. $this->assertEquals('/bob/uploads/', $uploadsMountProvider->getMountPoint());
  63. $uploadsStorage = $uploadsMountProvider->getStorage();
  64. $this->assertEquals('local::cachemountprovidertest://cache_path/bob/uploads/', $uploadsStorage->getId());
  65. $cacheStorage->mkdir('foobar');
  66. $this->assertEquals(3, CacheMountProviderTestStream::$mkdirCounter);
  67. $uploadsStorage->mkdir('foobar');
  68. $this->assertEquals(4, CacheMountProviderTestStream::$mkdirCounter);
  69. }
  70. }