CacheMountProviderTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Daniel Kesselberg <mail@danielkesselberg.de>
  5. *
  6. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  7. *
  8. * @license AGPL-3.0-or-later
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace Test\Files\Mount;
  24. use OC\Files\Mount\CacheMountProvider;
  25. use OC\Files\Storage\StorageFactory;
  26. use OCP\Files\Storage\IStorageFactory;
  27. use OCP\IConfig;
  28. use OCP\IUser;
  29. use Test\TestCase;
  30. class CacheMountProviderTestStream {
  31. public static $statCounter = 0;
  32. public static $mkdirCounter = 0;
  33. public $context;
  34. public function mkdir(string $path, int $mode, int $options): bool {
  35. self::$mkdirCounter++;
  36. return true;
  37. }
  38. public function url_stat(string $path, int $flags): array|false {
  39. self::$statCounter++;
  40. return false;
  41. }
  42. }
  43. class CacheMountProviderTest extends TestCase {
  44. private IConfig $config;
  45. private IUser $user;
  46. private IStorageFactory $storageFactory;
  47. protected function setUp(): void {
  48. $this->config = $this->createMock(IConfig::class);
  49. $this->user = $this->createMock(IUser::class);
  50. $this->storageFactory = new StorageFactory();
  51. stream_wrapper_register('cachemountprovidertest', CacheMountProviderTestStream::class);
  52. }
  53. protected function tearDown(): void {
  54. stream_wrapper_unregister('cachemountprovidertest');
  55. }
  56. public function testGetMountsForUser(): void {
  57. $provider = new CacheMountProvider($this->config);
  58. $this->assertCount(0, $provider->getMountsForUser($this->user, $this->storageFactory));
  59. }
  60. public function testGetMountsForUserCacheDir(): void {
  61. $this->config->expects($this->exactly(1))
  62. ->method('getSystemValueString')
  63. ->willReturnMap([
  64. ['cache_path', '', 'cachemountprovidertest:////cache_path'],
  65. ]);
  66. $this->user->method('getUID')
  67. ->willReturn('bob');
  68. $provider = new CacheMountProvider($this->config);
  69. $mounts = $provider->getMountsForUser($this->user, $this->storageFactory);
  70. $this->assertCount(2, $mounts);
  71. $this->assertEquals(1, CacheMountProviderTestStream::$statCounter);
  72. $this->assertEquals(2, CacheMountProviderTestStream::$mkdirCounter);
  73. $cacheMountProvider = $mounts[0];
  74. $this->assertEquals('/bob/cache/', $cacheMountProvider->getMountPoint());
  75. $cacheStorage = $cacheMountProvider->getStorage();
  76. $this->assertEquals('local::cachemountprovidertest://cache_path/bob/', $cacheStorage->getId());
  77. $uploadsMountProvider = $mounts[1];
  78. $this->assertEquals('/bob/uploads/', $uploadsMountProvider->getMountPoint());
  79. $uploadsStorage = $uploadsMountProvider->getStorage();
  80. $this->assertEquals('local::cachemountprovidertest://cache_path/bob/uploads/', $uploadsStorage->getId());
  81. $cacheStorage->mkdir('foobar');
  82. $this->assertEquals(3, CacheMountProviderTestStream::$mkdirCounter);
  83. $uploadsStorage->mkdir('foobar');
  84. $this->assertEquals(4, CacheMountProviderTestStream::$mkdirCounter);
  85. }
  86. }