CleanUpTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\Files_Trashbin\Tests\Command;
  28. use OC\User\Manager;
  29. use OCA\Files_Trashbin\Command\CleanUp;
  30. use OCP\Files\IRootFolder;
  31. use OCP\IDBConnection;
  32. use Symfony\Component\Console\Exception\InvalidOptionException;
  33. use Symfony\Component\Console\Input\InputInterface;
  34. use Symfony\Component\Console\Output\NullOutput;
  35. use Symfony\Component\Console\Output\OutputInterface;
  36. use Test\TestCase;
  37. /**
  38. * Class CleanUpTest
  39. *
  40. * @group DB
  41. *
  42. * @package OCA\Files_Trashbin\Tests\Command
  43. */
  44. class CleanUpTest extends TestCase {
  45. /** @var CleanUp */
  46. protected $cleanup;
  47. /** @var \PHPUnit\Framework\MockObject\MockObject | Manager */
  48. protected $userManager;
  49. /** @var \PHPUnit\Framework\MockObject\MockObject | IRootFolder */
  50. protected $rootFolder;
  51. /** @var IDBConnection */
  52. protected $dbConnection;
  53. /** @var string */
  54. protected $trashTable = 'files_trash';
  55. /** @var string */
  56. protected $user0 = 'user0';
  57. protected function setUp(): void {
  58. parent::setUp();
  59. $this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')
  60. ->disableOriginalConstructor()->getMock();
  61. $this->userManager = $this->getMockBuilder('OC\User\Manager')
  62. ->disableOriginalConstructor()->getMock();
  63. $this->dbConnection = \OC::$server->getDatabaseConnection();
  64. $this->cleanup = new CleanUp($this->rootFolder, $this->userManager, $this->dbConnection);
  65. }
  66. /**
  67. * populate files_trash table with 10 dummy values
  68. */
  69. public function initTable() {
  70. $query = $this->dbConnection->getQueryBuilder();
  71. $query->delete($this->trashTable)->execute();
  72. for ($i = 0; $i < 10; $i++) {
  73. $query->insert($this->trashTable)
  74. ->values([
  75. 'id' => $query->expr()->literal('file'.$i),
  76. 'timestamp' => $query->expr()->literal($i),
  77. 'location' => $query->expr()->literal('.'),
  78. 'user' => $query->expr()->literal('user'.$i % 2)
  79. ])->execute();
  80. }
  81. $getAllQuery = $this->dbConnection->getQueryBuilder();
  82. $result = $getAllQuery->select('id')
  83. ->from($this->trashTable)
  84. ->execute()
  85. ->fetchAll();
  86. $this->assertSame(10, count($result));
  87. }
  88. /**
  89. * @dataProvider dataTestRemoveDeletedFiles
  90. * @param boolean $nodeExists
  91. */
  92. public function testRemoveDeletedFiles(bool $nodeExists) {
  93. $this->initTable();
  94. $this->rootFolder
  95. ->method('nodeExists')
  96. ->with('/' . $this->user0 . '/files_trashbin')
  97. ->willReturnOnConsecutiveCalls($nodeExists, false);
  98. if ($nodeExists) {
  99. $this->rootFolder
  100. ->method('get')
  101. ->with('/' . $this->user0 . '/files_trashbin')
  102. ->willReturn($this->rootFolder);
  103. $this->rootFolder
  104. ->method('delete');
  105. } else {
  106. $this->rootFolder->expects($this->never())->method('get');
  107. $this->rootFolder->expects($this->never())->method('delete');
  108. }
  109. $this->invokePrivate($this->cleanup, 'removeDeletedFiles', [$this->user0, new NullOutput(), false]);
  110. if ($nodeExists) {
  111. // if the delete operation was executed only files from user1
  112. // should be left.
  113. $query = $this->dbConnection->getQueryBuilder();
  114. $query->select('user')
  115. ->from($this->trashTable);
  116. $qResult = $query->execute();
  117. $result = $qResult->fetchAll();
  118. $qResult->closeCursor();
  119. $this->assertSame(5, count($result));
  120. foreach ($result as $r) {
  121. $this->assertSame('user1', $r['user']);
  122. }
  123. } else {
  124. // if no delete operation was executed we should still have all 10
  125. // database entries
  126. $getAllQuery = $this->dbConnection->getQueryBuilder();
  127. $result = $getAllQuery->select('id')
  128. ->from($this->trashTable)
  129. ->execute()
  130. ->fetchAll();
  131. $this->assertSame(10, count($result));
  132. }
  133. }
  134. public function dataTestRemoveDeletedFiles() {
  135. return [
  136. [true],
  137. [false]
  138. ];
  139. }
  140. /**
  141. * test remove deleted files from users given as parameter
  142. */
  143. public function testExecuteDeleteListOfUsers() {
  144. $userIds = ['user1', 'user2', 'user3'];
  145. $instance = $this->getMockBuilder('OCA\Files_Trashbin\Command\CleanUp')
  146. ->setMethods(['removeDeletedFiles'])
  147. ->setConstructorArgs([$this->rootFolder, $this->userManager, $this->dbConnection])
  148. ->getMock();
  149. $instance->expects($this->exactly(count($userIds)))
  150. ->method('removeDeletedFiles')
  151. ->willReturnCallback(function ($user) use ($userIds) {
  152. $this->assertTrue(in_array($user, $userIds));
  153. });
  154. $this->userManager->expects($this->exactly(count($userIds)))
  155. ->method('userExists')->willReturn(true);
  156. $inputInterface = $this->getMockBuilder('\Symfony\Component\Console\Input\InputInterface')
  157. ->disableOriginalConstructor()->getMock();
  158. $inputInterface->method('getArgument')
  159. ->with('user_id')
  160. ->willReturn($userIds);
  161. $inputInterface->method('getOption')
  162. ->willReturnMap([
  163. ['all-users', false],
  164. ['verbose', false],
  165. ]);
  166. $outputInterface = $this->getMockBuilder('\Symfony\Component\Console\Output\OutputInterface')
  167. ->disableOriginalConstructor()->getMock();
  168. $this->invokePrivate($instance, 'execute', [$inputInterface, $outputInterface]);
  169. }
  170. /**
  171. * test remove deleted files of all users
  172. */
  173. public function testExecuteAllUsers() {
  174. $userIds = [];
  175. $backendUsers = ['user1', 'user2'];
  176. $instance = $this->getMockBuilder('OCA\Files_Trashbin\Command\CleanUp')
  177. ->setMethods(['removeDeletedFiles'])
  178. ->setConstructorArgs([$this->rootFolder, $this->userManager, $this->dbConnection])
  179. ->getMock();
  180. $backend = $this->createMock(\OCP\UserInterface::class);
  181. $backend->method('getUsers')
  182. ->with('', 500, 0)
  183. ->willReturn($backendUsers);
  184. $instance->expects($this->exactly(count($backendUsers)))
  185. ->method('removeDeletedFiles')
  186. ->willReturnCallback(function ($user) use ($backendUsers) {
  187. $this->assertTrue(in_array($user, $backendUsers));
  188. });
  189. $inputInterface = $this->createMock(InputInterface::class);
  190. $inputInterface->method('getArgument')
  191. ->with('user_id')
  192. ->willReturn($userIds);
  193. $inputInterface->method('getOption')
  194. ->willReturnMap([
  195. ['all-users', true],
  196. ['verbose', false],
  197. ]);
  198. $outputInterface = $this->createMock(OutputInterface::class);
  199. $this->userManager
  200. ->method('getBackends')
  201. ->willReturn([$backend]);
  202. $this->invokePrivate($instance, 'execute', [$inputInterface, $outputInterface]);
  203. }
  204. public function testExecuteNoUsersAndNoAllUsers() {
  205. $inputInterface = $this->createMock(InputInterface::class);
  206. $inputInterface->method('getArgument')
  207. ->with('user_id')
  208. ->willReturn([]);
  209. $inputInterface->method('getOption')
  210. ->willReturnMap([
  211. ['all-users', false],
  212. ['verbose', false],
  213. ]);
  214. $outputInterface = $this->createMock(OutputInterface::class);
  215. $this->expectException(InvalidOptionException::class);
  216. $this->expectExceptionMessage('Either specify a user_id or --all-users');
  217. $this->invokePrivate($this->cleanup, 'execute', [$inputInterface, $outputInterface]);
  218. }
  219. public function testExecuteUsersAndAllUsers() {
  220. $inputInterface = $this->createMock(InputInterface::class);
  221. $inputInterface->method('getArgument')
  222. ->with('user_id')
  223. ->willReturn(['user1', 'user2']);
  224. $inputInterface->method('getOption')
  225. ->willReturnMap([
  226. ['all-users', true],
  227. ['verbose', false],
  228. ]);
  229. $outputInterface = $this->createMock(OutputInterface::class);
  230. $this->expectException(InvalidOptionException::class);
  231. $this->expectExceptionMessage('Either specify a user_id or --all-users');
  232. $this->invokePrivate($this->cleanup, 'execute', [$inputInterface, $outputInterface]);
  233. }
  234. }