CleanupRemoteStoragesTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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', '?')
  73. ->setValue('owner', '?')
  74. ->setValue('user', '?')
  75. ->setValue('mountpoint', '?')
  76. ->setValue('mountpoint_hash', '?');
  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'] = $storageQuery->getLastInsertId();
  87. }
  88. if (isset($storage['share_token'])) {
  89. $shareExternalQuery
  90. ->setParameter(0, $storage['share_token'])
  91. ->setParameter(1, $storage['remote'])
  92. ->setParameter(2, 'irrelevant')
  93. ->setParameter(3, 'irrelevant')
  94. ->setParameter(4, $storage['user'])
  95. ->setParameter(5, 'irrelevant')
  96. ->setParameter(6, 'irrelevant');
  97. $shareExternalQuery->executeStatement();
  98. }
  99. if (isset($storage['files_count'])) {
  100. for ($i = 0; $i < $storage['files_count']; $i++) {
  101. $filesQuery->setParameter(0, $storage['numeric_id']);
  102. $filesQuery->setParameter(1, 'file' . $i);
  103. $filesQuery->setParameter(2, md5('file' . $i));
  104. $filesQuery->executeStatement();
  105. }
  106. }
  107. }
  108. $this->cloudIdManager = $this->createMock(ICloudIdManager::class);
  109. $this->command = new CleanupRemoteStorages($this->connection, $this->cloudIdManager);
  110. }
  111. protected function tearDown(): void {
  112. $storageQuery = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  113. $storageQuery->delete('storages')
  114. ->where($storageQuery->expr()->eq('id', $storageQuery->createParameter('id')));
  115. $shareExternalQuery = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  116. $shareExternalQuery->delete('share_external')
  117. ->where($shareExternalQuery->expr()->eq('share_token', $shareExternalQuery->createParameter('share_token')))
  118. ->andWhere($shareExternalQuery->expr()->eq('remote', $shareExternalQuery->createParameter('remote')));
  119. foreach ($this->storages as $storage) {
  120. if (isset($storage['id'])) {
  121. $storageQuery->setParameter('id', $storage['id']);
  122. $storageQuery->execute();
  123. }
  124. if (isset($storage['share_token'])) {
  125. $shareExternalQuery->setParameter('share_token', $storage['share_token']);
  126. $shareExternalQuery->setParameter('remote', $storage['remote']);
  127. $shareExternalQuery->execute();
  128. }
  129. }
  130. parent::tearDown();
  131. }
  132. private function doesStorageExist($numericId) {
  133. $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  134. $qb->select('*')
  135. ->from('storages')
  136. ->where($qb->expr()->eq('numeric_id', $qb->createNamedParameter($numericId)));
  137. $qResult = $qb->executeQuery();
  138. $result = $qResult->fetch();
  139. $qResult->closeCursor();
  140. if (!empty($result)) {
  141. return true;
  142. }
  143. $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  144. $qb->select('*')
  145. ->from('filecache')
  146. ->where($qb->expr()->eq('storage', $qb->createNamedParameter($numericId)));
  147. $qResult = $qb->executeQuery();
  148. $result = $qResult->fetch();
  149. $qResult->closeCursor();
  150. if (!empty($result)) {
  151. return true;
  152. }
  153. return false;
  154. }
  155. /**
  156. * Test cleanup of orphaned storages
  157. */
  158. public function testCleanup() {
  159. $input = $this->getMockBuilder(InputInterface::class)
  160. ->disableOriginalConstructor()
  161. ->getMock();
  162. $output = $this->getMockBuilder(OutputInterface::class)
  163. ->disableOriginalConstructor()
  164. ->getMock();
  165. // parent folder, `files`, ´test` and `welcome.txt` => 4 elements
  166. $output
  167. ->expects($this->any())
  168. ->method('writeln')
  169. ->withConsecutive(
  170. ['5 remote storage(s) need(s) to be checked'],
  171. ['5 remote share(s) exist'],
  172. );
  173. $this->cloudIdManager
  174. ->expects($this->any())
  175. ->method('getCloudId')
  176. ->will($this->returnCallback(function (string $user, string $remote) {
  177. $cloudIdMock = $this->createMock(ICloudId::class);
  178. // The remotes are already sanitized in the original data, so
  179. // they can be directly returned.
  180. $cloudIdMock
  181. ->expects($this->any())
  182. ->method('getRemote')
  183. ->willReturn($remote);
  184. return $cloudIdMock;
  185. }));
  186. $this->command->execute($input, $output);
  187. $this->assertTrue($this->doesStorageExist($this->storages[0]['numeric_id']));
  188. $this->assertTrue($this->doesStorageExist($this->storages[1]['numeric_id']));
  189. $this->assertFalse($this->doesStorageExist($this->storages[3]['numeric_id']));
  190. $this->assertTrue($this->doesStorageExist($this->storages[4]['numeric_id']));
  191. $this->assertFalse($this->doesStorageExist($this->storages[5]['numeric_id']));
  192. }
  193. }