CleanupRemoteStoragesTest.php 6.7 KB

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