CleanupRemoteStoragesTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud GmbH.
  4. *
  5. * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\Files_Sharing\Tests\Command;
  27. use OCA\Files_Sharing\Command\CleanupRemoteStorages;
  28. use OCP\Federation\ICloudId;
  29. use OCP\Federation\ICloudIdManager;
  30. use Symfony\Component\Console\Input\InputInterface;
  31. use Symfony\Component\Console\Output\OutputInterface;
  32. use Test\TestCase;
  33. /**
  34. * Class CleanupRemoteStoragesTest
  35. *
  36. * @group DB
  37. *
  38. * @package OCA\Files_Sharing\Tests\Command
  39. */
  40. class CleanupRemoteStoragesTest extends TestCase {
  41. /**
  42. * @var CleanupRemoteStorages
  43. */
  44. private $command;
  45. /**
  46. * @var \OCP\IDBConnection
  47. */
  48. private $connection;
  49. /**
  50. * @var ICloudIdManager|\PHPUnit\Framework\MockObject\MockObject
  51. */
  52. private $cloudIdManager;
  53. private $storages = [
  54. ['id' => 'shared::7b4a322b22f9d0047c38d77d471ce3cf', 'share_token' => 'f2c69dad1dc0649f26976fd210fc62e1', 'remote' => 'https://hostname.tld/owncloud1', 'user' => 'user1'],
  55. ['id' => 'shared::efe3b456112c3780da6155d3a9b9141c', 'share_token' => 'f2c69dad1dc0649f26976fd210fc62e2', 'remote' => 'https://hostname.tld/owncloud2', 'user' => 'user2'],
  56. ['notExistingId' => 'shared::33323d9f4ca416a9e3525b435354bc6f', 'share_token' => 'f2c69dad1dc0649f26976fd210fc62e3', 'remote' => 'https://hostname.tld/owncloud3', 'user' => 'user3'],
  57. ['id' => 'shared::7fe41a07d3f517a923f4b2b599e72cbb', 'files_count' => 2],
  58. ['id' => 'shared::de4aeb2f378d222b6d2c5fd8f4e42f8e', 'share_token' => 'f2c69dad1dc0649f26976fd210fc62e5', 'remote' => 'https://hostname.tld/owncloud5', 'user' => 'user5'],
  59. ['id' => 'shared::af712293ab5eb9e6a1745a13818b99fe', 'files_count' => 3],
  60. ['notExistingId' => 'shared::c34568c143cdac7d2f06e0800b5280f9', 'share_token' => 'f2c69dad1dc0649f26976fd210fc62e7', 'remote' => 'https://hostname.tld/owncloud7', 'user' => 'user7'],
  61. ];
  62. protected function setUp(): void {
  63. parent::setUp();
  64. $this->connection = \OC::$server->getDatabaseConnection();
  65. $storageQuery = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  66. $storageQuery->insert('storages')
  67. ->setValue('id', '?');
  68. $shareExternalQuery = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  69. $shareExternalQuery->insert('share_external')
  70. ->setValue('share_token', '?')
  71. ->setValue('remote', '?')
  72. ->setValue('name', '?')->setParameter(2, 'irrelevant')
  73. ->setValue('owner', '?')->setParameter(3, 'irrelevant')
  74. ->setValue('user', '?')
  75. ->setValue('mountpoint', '?')->setParameter(5, 'irrelevant')
  76. ->setValue('mountpoint_hash', '?')->setParameter(6, 'irrelevant');
  77. $filesQuery = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  78. $filesQuery->insert('filecache')
  79. ->setValue('storage', '?')
  80. ->setValue('path', '?')
  81. ->setValue('path_hash', '?');
  82. foreach ($this->storages as &$storage) {
  83. if (isset($storage['id'])) {
  84. $storageQuery->setParameter(0, $storage['id']);
  85. $storageQuery->execute();
  86. $storage['numeric_id'] = $this->connection->lastInsertId('*PREFIX*storages');
  87. }
  88. if (isset($storage['share_token'])) {
  89. $shareExternalQuery
  90. ->setParameter(0, $storage['share_token'])
  91. ->setParameter(1, $storage['remote'])
  92. ->setParameter(4, $storage['user']);
  93. $shareExternalQuery->execute();
  94. }
  95. if (isset($storage['files_count'])) {
  96. for ($i = 0; $i < $storage['files_count']; $i++) {
  97. $filesQuery->setParameter(0, $storage['numeric_id']);
  98. $filesQuery->setParameter(1, 'file' . $i);
  99. $filesQuery->setParameter(2, md5('file' . $i));
  100. $filesQuery->execute();
  101. }
  102. }
  103. }
  104. $this->cloudIdManager = $this->createMock(ICloudIdManager::class);
  105. $this->command = new CleanupRemoteStorages($this->connection, $this->cloudIdManager);
  106. }
  107. protected function tearDown(): void {
  108. $storageQuery = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  109. $storageQuery->delete('storages')
  110. ->where($storageQuery->expr()->eq('id', $storageQuery->createParameter('id')));
  111. $shareExternalQuery = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  112. $shareExternalQuery->delete('share_external')
  113. ->where($shareExternalQuery->expr()->eq('share_token', $shareExternalQuery->createParameter('share_token')))
  114. ->andWhere($shareExternalQuery->expr()->eq('remote', $shareExternalQuery->createParameter('remote')));
  115. foreach ($this->storages as $storage) {
  116. if (isset($storage['id'])) {
  117. $storageQuery->setParameter('id', $storage['id']);
  118. $storageQuery->execute();
  119. }
  120. if (isset($storage['share_token'])) {
  121. $shareExternalQuery->setParameter('share_token', $storage['share_token']);
  122. $shareExternalQuery->setParameter('remote', $storage['remote']);
  123. $shareExternalQuery->execute();
  124. }
  125. }
  126. parent::tearDown();
  127. }
  128. private function doesStorageExist($numericId) {
  129. $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  130. $qb->select('*')
  131. ->from('storages')
  132. ->where($qb->expr()->eq('numeric_id', $qb->createNamedParameter($numericId)));
  133. $qResult = $qb->execute();
  134. $result = $qResult->fetchAll();
  135. $qResult->closeCursor();
  136. if (!empty($result)) {
  137. return true;
  138. }
  139. $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  140. $qb->select('*')
  141. ->from('filecache')
  142. ->where($qb->expr()->eq('storage', $qb->createNamedParameter($numericId)));
  143. $qResult = $qb->execute();
  144. $result = $qResult->fetchAll();
  145. $qResult->closeCursor();
  146. if (!empty($result)) {
  147. return true;
  148. }
  149. return false;
  150. }
  151. /**
  152. * Test cleanup of orphaned storages
  153. */
  154. public function testCleanup() {
  155. $input = $this->getMockBuilder(InputInterface::class)
  156. ->disableOriginalConstructor()
  157. ->getMock();
  158. $output = $this->getMockBuilder(OutputInterface::class)
  159. ->disableOriginalConstructor()
  160. ->getMock();
  161. // parent folder, `files`, ´test` and `welcome.txt` => 4 elements
  162. $output
  163. ->expects($this->any())
  164. ->method('writeln')
  165. ->withConsecutive(
  166. ['5 remote storage(s) need(s) to be checked'],
  167. ['5 remote share(s) exist'],
  168. );
  169. $this->cloudIdManager
  170. ->expects($this->any())
  171. ->method('getCloudId')
  172. ->will($this->returnCallback(function (string $user, string $remote) {
  173. $cloudIdMock = $this->createMock(ICloudId::class);
  174. // The remotes are already sanitized in the original data, so
  175. // they can be directly returned.
  176. $cloudIdMock
  177. ->expects($this->any())
  178. ->method('getRemote')
  179. ->willReturn($remote);
  180. return $cloudIdMock;
  181. }));
  182. $this->command->execute($input, $output);
  183. $this->assertTrue($this->doesStorageExist($this->storages[0]['numeric_id']));
  184. $this->assertTrue($this->doesStorageExist($this->storages[1]['numeric_id']));
  185. $this->assertFalse($this->doesStorageExist($this->storages[3]['numeric_id']));
  186. $this->assertTrue($this->doesStorageExist($this->storages[4]['numeric_id']));
  187. $this->assertFalse($this->doesStorageExist($this->storages[5]['numeric_id']));
  188. }
  189. }