1
0

ObjectHomeMountProviderTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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-only
  6. */
  7. namespace Test\Files\Mount;
  8. use OC\Files\Mount\ObjectHomeMountProvider;
  9. use OCP\Files\Storage\IStorageFactory;
  10. use OCP\IConfig;
  11. use OCP\IUser;
  12. class ObjectHomeMountProviderTest extends \Test\TestCase {
  13. /** @var ObjectHomeMountProvider */
  14. protected $provider;
  15. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  16. protected $config;
  17. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
  18. protected $user;
  19. /** @var IStorageFactory|\PHPUnit\Framework\MockObject\MockObject */
  20. protected $loader;
  21. protected function setUp(): void {
  22. parent::setUp();
  23. $this->config = $this->createMock(IConfig::class);
  24. $this->user = $this->createMock(IUser::class);
  25. $this->loader = $this->createMock(IStorageFactory::class);
  26. $this->provider = new ObjectHomeMountProvider($this->config);
  27. }
  28. public function testSingleBucket() {
  29. $this->config->expects($this->once())
  30. ->method('getSystemValue')
  31. ->with($this->equalTo('objectstore'), '')
  32. ->willReturn([
  33. 'class' => 'Test\Files\Mount\FakeObjectStore',
  34. ]);
  35. $this->user->expects($this->never())->method($this->anything());
  36. $this->loader->expects($this->never())->method($this->anything());
  37. $config = $this->invokePrivate($this->provider, 'getSingleBucketObjectStoreConfig', [$this->user, $this->loader]);
  38. $this->assertArrayHasKey('class', $config);
  39. $this->assertEquals($config['class'], 'Test\Files\Mount\FakeObjectStore');
  40. $this->assertArrayHasKey('arguments', $config);
  41. $this->assertArrayHasKey('user', $config['arguments']);
  42. $this->assertSame($this->user, $config['arguments']['user']);
  43. $this->assertArrayHasKey('objectstore', $config['arguments']);
  44. $this->assertInstanceOf('Test\Files\Mount\FakeObjectStore', $config['arguments']['objectstore']);
  45. }
  46. public function testMultiBucket() {
  47. $this->config->expects($this->exactly(2))
  48. ->method('getSystemValue')
  49. ->with($this->equalTo('objectstore_multibucket'), '')
  50. ->willReturn([
  51. 'class' => 'Test\Files\Mount\FakeObjectStore',
  52. ]);
  53. $this->user->method('getUID')
  54. ->willReturn('uid');
  55. $this->loader->expects($this->never())->method($this->anything());
  56. $this->config->expects($this->once())
  57. ->method('getUserValue')
  58. ->with(
  59. $this->equalTo('uid'),
  60. $this->equalTo('homeobjectstore'),
  61. $this->equalTo('bucket'),
  62. $this->equalTo(null)
  63. )->willReturn(null);
  64. $this->config->expects($this->once())
  65. ->method('setUserValue')
  66. ->with(
  67. $this->equalTo('uid'),
  68. $this->equalTo('homeobjectstore'),
  69. $this->equalTo('bucket'),
  70. $this->equalTo('49'),
  71. $this->equalTo(null)
  72. );
  73. $config = $this->invokePrivate($this->provider, 'getMultiBucketObjectStoreConfig', [$this->user, $this->loader]);
  74. $this->assertArrayHasKey('class', $config);
  75. $this->assertEquals($config['class'], 'Test\Files\Mount\FakeObjectStore');
  76. $this->assertArrayHasKey('arguments', $config);
  77. $this->assertArrayHasKey('user', $config['arguments']);
  78. $this->assertSame($this->user, $config['arguments']['user']);
  79. $this->assertArrayHasKey('objectstore', $config['arguments']);
  80. $this->assertInstanceOf('Test\Files\Mount\FakeObjectStore', $config['arguments']['objectstore']);
  81. $this->assertArrayHasKey('bucket', $config['arguments']);
  82. $this->assertEquals('49', $config['arguments']['bucket']);
  83. }
  84. public function testMultiBucketWithPrefix() {
  85. $this->config->expects($this->exactly(2))
  86. ->method('getSystemValue')
  87. ->with('objectstore_multibucket')
  88. ->willReturn([
  89. 'class' => 'Test\Files\Mount\FakeObjectStore',
  90. 'arguments' => [
  91. 'bucket' => 'myBucketPrefix',
  92. ],
  93. ]);
  94. $this->user->method('getUID')
  95. ->willReturn('uid');
  96. $this->loader->expects($this->never())->method($this->anything());
  97. $this->config->expects($this->once())
  98. ->method('getUserValue')
  99. ->with(
  100. $this->equalTo('uid'),
  101. $this->equalTo('homeobjectstore'),
  102. $this->equalTo('bucket'),
  103. $this->equalTo(null)
  104. )->willReturn(null);
  105. $this->config->expects($this->once())
  106. ->method('setUserValue')
  107. ->with(
  108. $this->equalTo('uid'),
  109. $this->equalTo('homeobjectstore'),
  110. $this->equalTo('bucket'),
  111. $this->equalTo('myBucketPrefix49'),
  112. $this->equalTo(null)
  113. );
  114. $config = $this->invokePrivate($this->provider, 'getMultiBucketObjectStoreConfig', [$this->user, $this->loader]);
  115. $this->assertArrayHasKey('class', $config);
  116. $this->assertEquals($config['class'], 'Test\Files\Mount\FakeObjectStore');
  117. $this->assertArrayHasKey('arguments', $config);
  118. $this->assertArrayHasKey('user', $config['arguments']);
  119. $this->assertSame($this->user, $config['arguments']['user']);
  120. $this->assertArrayHasKey('objectstore', $config['arguments']);
  121. $this->assertInstanceOf('Test\Files\Mount\FakeObjectStore', $config['arguments']['objectstore']);
  122. $this->assertArrayHasKey('bucket', $config['arguments']);
  123. $this->assertEquals('myBucketPrefix49', $config['arguments']['bucket']);
  124. }
  125. public function testMultiBucketBucketAlreadySet() {
  126. $this->config->expects($this->once())
  127. ->method('getSystemValue')
  128. ->with('objectstore_multibucket')
  129. ->willReturn([
  130. 'class' => 'Test\Files\Mount\FakeObjectStore',
  131. 'arguments' => [
  132. 'bucket' => 'myBucketPrefix',
  133. ],
  134. ]);
  135. $this->user->method('getUID')
  136. ->willReturn('uid');
  137. $this->loader->expects($this->never())->method($this->anything());
  138. $this->config->expects($this->once())
  139. ->method('getUserValue')
  140. ->with(
  141. $this->equalTo('uid'),
  142. $this->equalTo('homeobjectstore'),
  143. $this->equalTo('bucket'),
  144. $this->equalTo(null)
  145. )->willReturn('awesomeBucket1');
  146. $this->config->expects($this->never())
  147. ->method('setUserValue');
  148. $config = $this->invokePrivate($this->provider, 'getMultiBucketObjectStoreConfig', [$this->user, $this->loader]);
  149. $this->assertArrayHasKey('class', $config);
  150. $this->assertEquals($config['class'], 'Test\Files\Mount\FakeObjectStore');
  151. $this->assertArrayHasKey('arguments', $config);
  152. $this->assertArrayHasKey('user', $config['arguments']);
  153. $this->assertSame($this->user, $config['arguments']['user']);
  154. $this->assertArrayHasKey('objectstore', $config['arguments']);
  155. $this->assertInstanceOf('Test\Files\Mount\FakeObjectStore', $config['arguments']['objectstore']);
  156. $this->assertArrayHasKey('bucket', $config['arguments']);
  157. $this->assertEquals('awesomeBucket1', $config['arguments']['bucket']);
  158. }
  159. public function testMultiBucketConfigFirst() {
  160. $this->config->expects($this->exactly(2))
  161. ->method('getSystemValue')
  162. ->with('objectstore_multibucket')
  163. ->willReturn([
  164. 'class' => 'Test\Files\Mount\FakeObjectStore',
  165. ]);
  166. $this->user->method('getUID')
  167. ->willReturn('uid');
  168. $this->loader->expects($this->never())->method($this->anything());
  169. $mount = $this->provider->getHomeMountForUser($this->user, $this->loader);
  170. $this->assertInstanceOf('OC\Files\Mount\MountPoint', $mount);
  171. }
  172. public function testMultiBucketConfigFirstFallBackSingle() {
  173. $this->config->expects($this->exactly(2))
  174. ->method('getSystemValue')
  175. ->withConsecutive(
  176. [$this->equalTo('objectstore_multibucket')],
  177. [$this->equalTo('objectstore')],
  178. )->willReturnOnConsecutiveCalls(
  179. '',
  180. [
  181. 'class' => 'Test\Files\Mount\FakeObjectStore',
  182. ],
  183. );
  184. $this->user->method('getUID')
  185. ->willReturn('uid');
  186. $this->loader->expects($this->never())->method($this->anything());
  187. $mount = $this->provider->getHomeMountForUser($this->user, $this->loader);
  188. $this->assertInstanceOf('OC\Files\Mount\MountPoint', $mount);
  189. }
  190. public function testNoObjectStore() {
  191. $this->config->expects($this->exactly(2))
  192. ->method('getSystemValue')
  193. ->willReturn('');
  194. $mount = $this->provider->getHomeMountForUser($this->user, $this->loader);
  195. $this->assertNull($mount);
  196. }
  197. }
  198. class FakeObjectStore {
  199. private $arguments;
  200. public function __construct(array $arguments) {
  201. $this->arguments = $arguments;
  202. }
  203. public function getArguments() {
  204. return $this->arguments;
  205. }
  206. }