123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- <?php
- /**
- * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
- * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
- * SPDX-License-Identifier: AGPL-3.0-only
- */
- namespace OCA\Files_External\Tests\Service;
- use OCA\Files_External\Service\DBConfigService;
- use OCP\IDBConnection;
- use Test\TestCase;
- /**
- * @group DB
- */
- class DBConfigServiceTest extends TestCase {
- /**
- * @var DBConfigService
- */
- private $dbConfig;
- /**
- * @var IDBConnection
- */
- private $connection;
- private $mounts = [];
- protected function setUp(): void {
- parent::setUp();
- $this->connection = \OC::$server->getDatabaseConnection();
- $this->dbConfig = new DBConfigService($this->connection, \OC::$server->getCrypto());
- }
- protected function tearDown(): void {
- foreach ($this->mounts as $mount) {
- $this->dbConfig->removeMount($mount);
- }
- $this->mounts = [];
- }
- private function addMount($mountPoint, $storageBackend, $authBackend, $priority, $type) {
- $id = $this->dbConfig->addMount($mountPoint, $storageBackend, $authBackend, $priority, $type);
- $this->mounts[] = $id;
- return $id;
- }
- public function testAddSimpleMount(): void {
- $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
- $mount = $this->dbConfig->getMountById($id);
- $this->assertEquals('/test', $mount['mount_point']);
- $this->assertEquals('foo', $mount['storage_backend']);
- $this->assertEquals('bar', $mount['auth_backend']);
- $this->assertEquals(100, $mount['priority']);
- $this->assertEquals(DBConfigService::MOUNT_TYPE_ADMIN, $mount['type']);
- $this->assertEquals([], $mount['applicable']);
- $this->assertEquals([], $mount['config']);
- $this->assertEquals([], $mount['options']);
- }
- public function testAddApplicable(): void {
- $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
- $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test');
- $mount = $this->dbConfig->getMountById($id);
- $this->assertEquals([
- ['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id]
- ], $mount['applicable']);
- $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, 'bar');
- $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
- $mount = $this->dbConfig->getMountById($id);
- $this->assertEquals([
- ['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id],
- ['type' => DBConfigService::APPLICABLE_TYPE_GROUP, 'value' => 'bar', 'mount_id' => $id],
- ['type' => DBConfigService::APPLICABLE_TYPE_GLOBAL, 'value' => null, 'mount_id' => $id]
- ], $mount['applicable']);
- }
- public function testAddApplicableDouble(): void {
- $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
- $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test');
- $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test');
- $mount = $this->dbConfig->getMountById($id);
- $this->assertEquals([
- ['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id]
- ], $mount['applicable']);
- }
- public function testDeleteMount(): void {
- $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
- $this->dbConfig->removeMount($id);
- $mount = $this->dbConfig->getMountById($id);
- $this->assertEquals(null, $mount);
- }
- public function testRemoveApplicable(): void {
- $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
- $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test');
- $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test');
- $mount = $this->dbConfig->getMountById($id);
- $this->assertEquals([], $mount['applicable']);
- }
- public function testRemoveApplicableGlobal(): void {
- $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
- $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
- $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
- $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test');
- $mount = $this->dbConfig->getMountById($id);
- $this->assertEquals([
- ['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id]
- ], $mount['applicable']);
- }
- public function testSetConfig(): void {
- $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
- $this->dbConfig->setConfig($id, 'foo', 'bar');
- $mount = $this->dbConfig->getMountById($id);
- $this->assertEquals(['foo' => 'bar'], $mount['config']);
- $this->dbConfig->setConfig($id, 'foo2', 'bar2');
- $mount = $this->dbConfig->getMountById($id);
- $this->assertEquals(['foo' => 'bar', 'foo2' => 'bar2'], $mount['config']);
- }
- public function testSetConfigOverwrite(): void {
- $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
- $this->dbConfig->setConfig($id, 'foo', 'bar');
- $this->dbConfig->setConfig($id, 'asd', '1');
- $this->dbConfig->setConfig($id, 'foo', 'qwerty');
- $mount = $this->dbConfig->getMountById($id);
- $this->assertEquals(['foo' => 'qwerty', 'asd' => '1'], $mount['config']);
- }
- public function testSetOption(): void {
- $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
- $this->dbConfig->setOption($id, 'foo', 'bar');
- $mount = $this->dbConfig->getMountById($id);
- $this->assertEquals(['foo' => 'bar'], $mount['options']);
- $this->dbConfig->setOption($id, 'foo2', 'bar2');
- $mount = $this->dbConfig->getMountById($id);
- $this->assertEquals(['foo' => 'bar', 'foo2' => 'bar2'], $mount['options']);
- }
- public function testSetOptionOverwrite(): void {
- $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
- $this->dbConfig->setOption($id, 'foo', 'bar');
- $this->dbConfig->setOption($id, 'asd', '1');
- $this->dbConfig->setOption($id, 'foo', 'qwerty');
- $mount = $this->dbConfig->getMountById($id);
- $this->assertEquals(['foo' => 'qwerty', 'asd' => '1'], $mount['options']);
- }
- public function testGetMountsFor(): void {
- $mounts = $this->dbConfig->getMountsFor(DBConfigService::APPLICABLE_TYPE_USER, 'test');
- $this->assertEquals([], $mounts);
- $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
- $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test');
- $mounts = $this->dbConfig->getMountsFor(DBConfigService::APPLICABLE_TYPE_USER, 'test');
- $this->assertCount(1, $mounts);
- $this->assertEquals($id, $mounts[0]['mount_id']);
- $this->assertEquals([['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id]], $mounts[0]['applicable']);
- }
- public function testGetAdminMounts(): void {
- $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
- $this->addMount('/test2', 'foo2', 'bar2', 100, DBConfigService::MOUNT_TYPE_PERSONAL);
- $mounts = $this->dbConfig->getAdminMounts();
- $this->assertCount(1, $mounts);
- $this->assertEquals($id1, $mounts[0]['mount_id']);
- }
- public function testGetAdminMountsFor(): void {
- $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
- $this->addMount('/test2', 'foo2', 'bar2', 100, DBConfigService::MOUNT_TYPE_ADMIN);
- $id3 = $this->addMount('/test3', 'foo3', 'bar3', 100, DBConfigService::MOUNT_TYPE_PERSONAL);
- $this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_USER, 'test');
- $this->dbConfig->addApplicable($id3, DBConfigService::APPLICABLE_TYPE_USER, 'test');
- $mounts = $this->dbConfig->getAdminMountsFor(DBConfigService::APPLICABLE_TYPE_USER, 'test');
- $this->assertCount(1, $mounts);
- $this->assertEquals($id1, $mounts[0]['mount_id']);
- $this->assertEquals([['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id1]], $mounts[0]['applicable']);
- }
- public function testGetUserMountsFor(): void {
- $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
- $this->addMount('/test2', 'foo2', 'bar2', 100, DBConfigService::MOUNT_TYPE_PERSONAL);
- $id3 = $this->addMount('/test3', 'foo3', 'bar3', 100, DBConfigService::MOUNT_TYPE_PERSONAL);
- $this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_USER, 'test');
- $this->dbConfig->addApplicable($id3, DBConfigService::APPLICABLE_TYPE_USER, 'test');
- $mounts = $this->dbConfig->getUserMountsFor(DBConfigService::APPLICABLE_TYPE_USER, 'test');
- $this->assertCount(1, $mounts);
- $this->assertEquals($id3, $mounts[0]['mount_id']);
- $this->assertEquals([['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id3]], $mounts[0]['applicable']);
- }
- public function testGetAdminMountsForGlobal(): void {
- $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
- $this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
- $mounts = $this->dbConfig->getAdminMountsFor(DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
- $this->assertCount(1, $mounts);
- $this->assertEquals($id1, $mounts[0]['mount_id']);
- $this->assertEquals([['type' => DBConfigService::APPLICABLE_TYPE_GLOBAL, 'value' => null, 'mount_id' => $id1]], $mounts[0]['applicable']);
- }
- public function testSetMountPoint(): void {
- $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
- $id2 = $this->addMount('/foo', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
- $this->dbConfig->setMountPoint($id1, '/asd');
- $mount = $this->dbConfig->getMountById($id1);
- $this->assertEquals('/asd', $mount['mount_point']);
- // remains unchanged
- $mount = $this->dbConfig->getMountById($id2);
- $this->assertEquals('/foo', $mount['mount_point']);
- }
- public function testSetAuthBackend(): void {
- $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
- $id2 = $this->addMount('/foo', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
- $this->dbConfig->setAuthBackend($id1, 'none');
- $mount = $this->dbConfig->getMountById($id1);
- $this->assertEquals('none', $mount['auth_backend']);
- // remains unchanged
- $mount = $this->dbConfig->getMountById($id2);
- $this->assertEquals('bar', $mount['auth_backend']);
- }
- public function testGetMountsForDuplicateByGroup(): void {
- $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
- $this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_GROUP, 'group1');
- $this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_GROUP, 'group2');
- $mounts = $this->dbConfig->getAdminMountsForMultiple(DBConfigService::APPLICABLE_TYPE_GROUP, ['group1', 'group2']);
- $this->assertCount(1, $mounts);
- $this->assertEquals($id1, $mounts[0]['mount_id']);
- }
- public function testGetAllMounts(): void {
- $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
- $id2 = $this->addMount('/test2', 'foo2', 'bar2', 100, DBConfigService::MOUNT_TYPE_PERSONAL);
- $mounts = $this->dbConfig->getAllMounts();
- $this->assertCount(2, $mounts);
- $this->assertEquals($id1, $mounts[0]['mount_id']);
- $this->assertEquals($id2, $mounts[1]['mount_id']);
- }
- }
|