CleanupRemoteStoragesTest.php 7.4 KB

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