1
0

DBConfigServiceTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Tests\Service;
  8. use OCA\Files_External\Service\DBConfigService;
  9. use OCP\IDBConnection;
  10. use Test\TestCase;
  11. /**
  12. * @group DB
  13. */
  14. class DBConfigServiceTest extends TestCase {
  15. /**
  16. * @var DBConfigService
  17. */
  18. private $dbConfig;
  19. /**
  20. * @var IDBConnection
  21. */
  22. private $connection;
  23. private $mounts = [];
  24. protected function setUp(): void {
  25. parent::setUp();
  26. $this->connection = \OC::$server->getDatabaseConnection();
  27. $this->dbConfig = new DBConfigService($this->connection, \OC::$server->getCrypto());
  28. }
  29. protected function tearDown(): void {
  30. foreach ($this->mounts as $mount) {
  31. $this->dbConfig->removeMount($mount);
  32. }
  33. $this->mounts = [];
  34. }
  35. private function addMount($mountPoint, $storageBackend, $authBackend, $priority, $type) {
  36. $id = $this->dbConfig->addMount($mountPoint, $storageBackend, $authBackend, $priority, $type);
  37. $this->mounts[] = $id;
  38. return $id;
  39. }
  40. public function testAddSimpleMount(): void {
  41. $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  42. $mount = $this->dbConfig->getMountById($id);
  43. $this->assertEquals('/test', $mount['mount_point']);
  44. $this->assertEquals('foo', $mount['storage_backend']);
  45. $this->assertEquals('bar', $mount['auth_backend']);
  46. $this->assertEquals(100, $mount['priority']);
  47. $this->assertEquals(DBConfigService::MOUNT_TYPE_ADMIN, $mount['type']);
  48. $this->assertEquals([], $mount['applicable']);
  49. $this->assertEquals([], $mount['config']);
  50. $this->assertEquals([], $mount['options']);
  51. }
  52. public function testAddApplicable(): void {
  53. $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  54. $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test');
  55. $mount = $this->dbConfig->getMountById($id);
  56. $this->assertEquals([
  57. ['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id]
  58. ], $mount['applicable']);
  59. $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, 'bar');
  60. $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
  61. $mount = $this->dbConfig->getMountById($id);
  62. $this->assertEquals([
  63. ['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id],
  64. ['type' => DBConfigService::APPLICABLE_TYPE_GROUP, 'value' => 'bar', 'mount_id' => $id],
  65. ['type' => DBConfigService::APPLICABLE_TYPE_GLOBAL, 'value' => null, 'mount_id' => $id]
  66. ], $mount['applicable']);
  67. }
  68. public function testAddApplicableDouble(): void {
  69. $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  70. $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test');
  71. $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test');
  72. $mount = $this->dbConfig->getMountById($id);
  73. $this->assertEquals([
  74. ['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id]
  75. ], $mount['applicable']);
  76. }
  77. public function testDeleteMount(): void {
  78. $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  79. $this->dbConfig->removeMount($id);
  80. $mount = $this->dbConfig->getMountById($id);
  81. $this->assertEquals(null, $mount);
  82. }
  83. public function testRemoveApplicable(): void {
  84. $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  85. $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test');
  86. $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test');
  87. $mount = $this->dbConfig->getMountById($id);
  88. $this->assertEquals([], $mount['applicable']);
  89. }
  90. public function testRemoveApplicableGlobal(): void {
  91. $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  92. $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
  93. $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
  94. $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test');
  95. $mount = $this->dbConfig->getMountById($id);
  96. $this->assertEquals([
  97. ['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id]
  98. ], $mount['applicable']);
  99. }
  100. public function testSetConfig(): void {
  101. $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  102. $this->dbConfig->setConfig($id, 'foo', 'bar');
  103. $mount = $this->dbConfig->getMountById($id);
  104. $this->assertEquals(['foo' => 'bar'], $mount['config']);
  105. $this->dbConfig->setConfig($id, 'foo2', 'bar2');
  106. $mount = $this->dbConfig->getMountById($id);
  107. $this->assertEquals(['foo' => 'bar', 'foo2' => 'bar2'], $mount['config']);
  108. }
  109. public function testSetConfigOverwrite(): void {
  110. $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  111. $this->dbConfig->setConfig($id, 'foo', 'bar');
  112. $this->dbConfig->setConfig($id, 'asd', '1');
  113. $this->dbConfig->setConfig($id, 'foo', 'qwerty');
  114. $mount = $this->dbConfig->getMountById($id);
  115. $this->assertEquals(['foo' => 'qwerty', 'asd' => '1'], $mount['config']);
  116. }
  117. public function testSetOption(): void {
  118. $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  119. $this->dbConfig->setOption($id, 'foo', 'bar');
  120. $mount = $this->dbConfig->getMountById($id);
  121. $this->assertEquals(['foo' => 'bar'], $mount['options']);
  122. $this->dbConfig->setOption($id, 'foo2', 'bar2');
  123. $mount = $this->dbConfig->getMountById($id);
  124. $this->assertEquals(['foo' => 'bar', 'foo2' => 'bar2'], $mount['options']);
  125. }
  126. public function testSetOptionOverwrite(): void {
  127. $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  128. $this->dbConfig->setOption($id, 'foo', 'bar');
  129. $this->dbConfig->setOption($id, 'asd', '1');
  130. $this->dbConfig->setOption($id, 'foo', 'qwerty');
  131. $mount = $this->dbConfig->getMountById($id);
  132. $this->assertEquals(['foo' => 'qwerty', 'asd' => '1'], $mount['options']);
  133. }
  134. public function testGetMountsFor(): void {
  135. $mounts = $this->dbConfig->getMountsFor(DBConfigService::APPLICABLE_TYPE_USER, 'test');
  136. $this->assertEquals([], $mounts);
  137. $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  138. $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test');
  139. $mounts = $this->dbConfig->getMountsFor(DBConfigService::APPLICABLE_TYPE_USER, 'test');
  140. $this->assertCount(1, $mounts);
  141. $this->assertEquals($id, $mounts[0]['mount_id']);
  142. $this->assertEquals([['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id]], $mounts[0]['applicable']);
  143. }
  144. public function testGetAdminMounts(): void {
  145. $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  146. $this->addMount('/test2', 'foo2', 'bar2', 100, DBConfigService::MOUNT_TYPE_PERSONAL);
  147. $mounts = $this->dbConfig->getAdminMounts();
  148. $this->assertCount(1, $mounts);
  149. $this->assertEquals($id1, $mounts[0]['mount_id']);
  150. }
  151. public function testGetAdminMountsFor(): void {
  152. $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  153. $this->addMount('/test2', 'foo2', 'bar2', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  154. $id3 = $this->addMount('/test3', 'foo3', 'bar3', 100, DBConfigService::MOUNT_TYPE_PERSONAL);
  155. $this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_USER, 'test');
  156. $this->dbConfig->addApplicable($id3, DBConfigService::APPLICABLE_TYPE_USER, 'test');
  157. $mounts = $this->dbConfig->getAdminMountsFor(DBConfigService::APPLICABLE_TYPE_USER, 'test');
  158. $this->assertCount(1, $mounts);
  159. $this->assertEquals($id1, $mounts[0]['mount_id']);
  160. $this->assertEquals([['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id1]], $mounts[0]['applicable']);
  161. }
  162. public function testGetUserMountsFor(): void {
  163. $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  164. $this->addMount('/test2', 'foo2', 'bar2', 100, DBConfigService::MOUNT_TYPE_PERSONAL);
  165. $id3 = $this->addMount('/test3', 'foo3', 'bar3', 100, DBConfigService::MOUNT_TYPE_PERSONAL);
  166. $this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_USER, 'test');
  167. $this->dbConfig->addApplicable($id3, DBConfigService::APPLICABLE_TYPE_USER, 'test');
  168. $mounts = $this->dbConfig->getUserMountsFor(DBConfigService::APPLICABLE_TYPE_USER, 'test');
  169. $this->assertCount(1, $mounts);
  170. $this->assertEquals($id3, $mounts[0]['mount_id']);
  171. $this->assertEquals([['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id3]], $mounts[0]['applicable']);
  172. }
  173. public function testGetAdminMountsForGlobal(): void {
  174. $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  175. $this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
  176. $mounts = $this->dbConfig->getAdminMountsFor(DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
  177. $this->assertCount(1, $mounts);
  178. $this->assertEquals($id1, $mounts[0]['mount_id']);
  179. $this->assertEquals([['type' => DBConfigService::APPLICABLE_TYPE_GLOBAL, 'value' => null, 'mount_id' => $id1]], $mounts[0]['applicable']);
  180. }
  181. public function testSetMountPoint(): void {
  182. $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  183. $id2 = $this->addMount('/foo', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  184. $this->dbConfig->setMountPoint($id1, '/asd');
  185. $mount = $this->dbConfig->getMountById($id1);
  186. $this->assertEquals('/asd', $mount['mount_point']);
  187. // remains unchanged
  188. $mount = $this->dbConfig->getMountById($id2);
  189. $this->assertEquals('/foo', $mount['mount_point']);
  190. }
  191. public function testSetAuthBackend(): void {
  192. $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  193. $id2 = $this->addMount('/foo', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  194. $this->dbConfig->setAuthBackend($id1, 'none');
  195. $mount = $this->dbConfig->getMountById($id1);
  196. $this->assertEquals('none', $mount['auth_backend']);
  197. // remains unchanged
  198. $mount = $this->dbConfig->getMountById($id2);
  199. $this->assertEquals('bar', $mount['auth_backend']);
  200. }
  201. public function testGetMountsForDuplicateByGroup(): void {
  202. $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  203. $this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_GROUP, 'group1');
  204. $this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_GROUP, 'group2');
  205. $mounts = $this->dbConfig->getAdminMountsForMultiple(DBConfigService::APPLICABLE_TYPE_GROUP, ['group1', 'group2']);
  206. $this->assertCount(1, $mounts);
  207. $this->assertEquals($id1, $mounts[0]['mount_id']);
  208. }
  209. public function testGetAllMounts(): void {
  210. $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  211. $id2 = $this->addMount('/test2', 'foo2', 'bar2', 100, DBConfigService::MOUNT_TYPE_PERSONAL);
  212. $mounts = $this->dbConfig->getAllMounts();
  213. $this->assertCount(2, $mounts);
  214. $this->assertEquals($id1, $mounts[0]['mount_id']);
  215. $this->assertEquals($id2, $mounts[1]['mount_id']);
  216. }
  217. }