CleanupRemoteStoragesTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud GmbH.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Sharing\Tests\Command;
  8. use OCA\Files_Sharing\Command\CleanupRemoteStorages;
  9. use OCP\Federation\ICloudId;
  10. use OCP\Federation\ICloudIdManager;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. use Test\TestCase;
  14. /**
  15. * Class CleanupRemoteStoragesTest
  16. *
  17. * @group DB
  18. *
  19. * @package OCA\Files_Sharing\Tests\Command
  20. */
  21. class CleanupRemoteStoragesTest extends TestCase {
  22. /**
  23. * @var CleanupRemoteStorages
  24. */
  25. private $command;
  26. /**
  27. * @var \OCP\IDBConnection
  28. */
  29. private $connection;
  30. /**
  31. * @var ICloudIdManager|\PHPUnit\Framework\MockObject\MockObject
  32. */
  33. private $cloudIdManager;
  34. private $storages = [
  35. ['id' => 'shared::7b4a322b22f9d0047c38d77d471ce3cf', 'share_token' => 'f2c69dad1dc0649f26976fd210fc62e1', 'remote' => 'https://hostname.tld/owncloud1', 'user' => 'user1'],
  36. ['id' => 'shared::efe3b456112c3780da6155d3a9b9141c', 'share_token' => 'f2c69dad1dc0649f26976fd210fc62e2', 'remote' => 'https://hostname.tld/owncloud2', 'user' => 'user2'],
  37. ['notExistingId' => 'shared::33323d9f4ca416a9e3525b435354bc6f', 'share_token' => 'f2c69dad1dc0649f26976fd210fc62e3', 'remote' => 'https://hostname.tld/owncloud3', 'user' => 'user3'],
  38. ['id' => 'shared::7fe41a07d3f517a923f4b2b599e72cbb', 'files_count' => 2],
  39. ['id' => 'shared::de4aeb2f378d222b6d2c5fd8f4e42f8e', 'share_token' => 'f2c69dad1dc0649f26976fd210fc62e5', 'remote' => 'https://hostname.tld/owncloud5', 'user' => 'user5'],
  40. ['id' => 'shared::af712293ab5eb9e6a1745a13818b99fe', 'files_count' => 3],
  41. ['notExistingId' => 'shared::c34568c143cdac7d2f06e0800b5280f9', 'share_token' => 'f2c69dad1dc0649f26976fd210fc62e7', 'remote' => 'https://hostname.tld/owncloud7', 'user' => 'user7'],
  42. ];
  43. protected function setUp(): void {
  44. parent::setUp();
  45. $this->connection = \OC::$server->getDatabaseConnection();
  46. $storageQuery = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  47. $storageQuery->insert('storages')
  48. ->setValue('id', $storageQuery->createParameter('id'));
  49. $shareExternalQuery = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  50. $shareExternalQuery->insert('share_external')
  51. ->setValue('share_token', $shareExternalQuery->createParameter('share_token'))
  52. ->setValue('remote', $shareExternalQuery->createParameter('remote'))
  53. ->setValue('name', $shareExternalQuery->createParameter('name'))
  54. ->setValue('owner', $shareExternalQuery->createParameter('owner'))
  55. ->setValue('user', $shareExternalQuery->createParameter('user'))
  56. ->setValue('mountpoint', $shareExternalQuery->createParameter('mountpoint'))
  57. ->setValue('mountpoint_hash', $shareExternalQuery->createParameter('mountpoint_hash'));
  58. $filesQuery = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  59. $filesQuery->insert('filecache')
  60. ->setValue('storage', $filesQuery->createParameter('storage'))
  61. ->setValue('path', $filesQuery->createParameter('path'))
  62. ->setValue('path_hash', $filesQuery->createParameter('path_hash'));
  63. foreach ($this->storages as &$storage) {
  64. if (isset($storage['id'])) {
  65. $storageQuery->setParameter('id', $storage['id']);
  66. $storageQuery->execute();
  67. $storage['numeric_id'] = $storageQuery->getLastInsertId();
  68. }
  69. if (isset($storage['share_token'])) {
  70. $shareExternalQuery
  71. ->setParameter('share_token', $storage['share_token'])
  72. ->setParameter('remote', $storage['remote'])
  73. ->setParameter('name', 'irrelevant')
  74. ->setParameter('owner', 'irrelevant')
  75. ->setParameter('user', $storage['user'])
  76. ->setParameter('mountpoint', 'irrelevant')
  77. ->setParameter('mountpoint_hash', 'irrelevant');
  78. $shareExternalQuery->executeStatement();
  79. }
  80. if (isset($storage['files_count'])) {
  81. for ($i = 0; $i < $storage['files_count']; $i++) {
  82. $filesQuery->setParameter('storage', $storage['numeric_id']);
  83. $filesQuery->setParameter('path', 'file' . $i);
  84. $filesQuery->setParameter('path_hash', md5('file' . $i));
  85. $filesQuery->executeStatement();
  86. }
  87. }
  88. }
  89. $this->cloudIdManager = $this->createMock(ICloudIdManager::class);
  90. $this->command = new CleanupRemoteStorages($this->connection, $this->cloudIdManager);
  91. }
  92. protected function tearDown(): void {
  93. $storageQuery = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  94. $storageQuery->delete('storages')
  95. ->where($storageQuery->expr()->eq('id', $storageQuery->createParameter('id')));
  96. $shareExternalQuery = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  97. $shareExternalQuery->delete('share_external')
  98. ->where($shareExternalQuery->expr()->eq('share_token', $shareExternalQuery->createParameter('share_token')))
  99. ->andWhere($shareExternalQuery->expr()->eq('remote', $shareExternalQuery->createParameter('remote')));
  100. foreach ($this->storages as $storage) {
  101. if (isset($storage['id'])) {
  102. $storageQuery->setParameter('id', $storage['id']);
  103. $storageQuery->execute();
  104. }
  105. if (isset($storage['share_token'])) {
  106. $shareExternalQuery->setParameter('share_token', $storage['share_token']);
  107. $shareExternalQuery->setParameter('remote', $storage['remote']);
  108. $shareExternalQuery->execute();
  109. }
  110. }
  111. parent::tearDown();
  112. }
  113. private function doesStorageExist($numericId) {
  114. $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  115. $qb->select('*')
  116. ->from('storages')
  117. ->where($qb->expr()->eq('numeric_id', $qb->createNamedParameter($numericId)));
  118. $qResult = $qb->executeQuery();
  119. $result = $qResult->fetch();
  120. $qResult->closeCursor();
  121. if (!empty($result)) {
  122. return true;
  123. }
  124. $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  125. $qb->select('*')
  126. ->from('filecache')
  127. ->where($qb->expr()->eq('storage', $qb->createNamedParameter($numericId)));
  128. $qResult = $qb->executeQuery();
  129. $result = $qResult->fetch();
  130. $qResult->closeCursor();
  131. if (!empty($result)) {
  132. return true;
  133. }
  134. return false;
  135. }
  136. /**
  137. * Test cleanup of orphaned storages
  138. */
  139. public function testCleanup(): void {
  140. $input = $this->getMockBuilder(InputInterface::class)
  141. ->disableOriginalConstructor()
  142. ->getMock();
  143. $output = $this->getMockBuilder(OutputInterface::class)
  144. ->disableOriginalConstructor()
  145. ->getMock();
  146. // parent folder, `files`, ´test` and `welcome.txt` => 4 elements
  147. $output
  148. ->expects($this->any())
  149. ->method('writeln')
  150. ->withConsecutive(
  151. ['5 remote storage(s) need(s) to be checked'],
  152. ['5 remote share(s) exist'],
  153. );
  154. $this->cloudIdManager
  155. ->expects($this->any())
  156. ->method('getCloudId')
  157. ->will($this->returnCallback(function (string $user, string $remote) {
  158. $cloudIdMock = $this->createMock(ICloudId::class);
  159. // The remotes are already sanitized in the original data, so
  160. // they can be directly returned.
  161. $cloudIdMock
  162. ->expects($this->any())
  163. ->method('getRemote')
  164. ->willReturn($remote);
  165. return $cloudIdMock;
  166. }));
  167. $this->command->execute($input, $output);
  168. $this->assertTrue($this->doesStorageExist($this->storages[0]['numeric_id']));
  169. $this->assertTrue($this->doesStorageExist($this->storages[1]['numeric_id']));
  170. $this->assertFalse($this->doesStorageExist($this->storages[3]['numeric_id']));
  171. $this->assertTrue($this->doesStorageExist($this->storages[4]['numeric_id']));
  172. $this->assertFalse($this->doesStorageExist($this->storages[5]['numeric_id']));
  173. }
  174. }