ObjectHomeMountProviderTest.php 7.8 KB

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