You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CleanUpTest.php 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 Symfony\Component\Console\Exception\InvalidOptionException;
  32. use Symfony\Component\Console\Input\InputInterface;
  33. use Symfony\Component\Console\Output\OutputInterface;
  34. use Test\TestCase;
  35. /**
  36. * Class CleanUpTest
  37. *
  38. * @group DB
  39. *
  40. * @package OCA\Files_Trashbin\Tests\Command
  41. */
  42. class CleanUpTest extends TestCase {
  43. /** @var CleanUp */
  44. protected $cleanup;
  45. /** @var \PHPUnit\Framework\MockObject\MockObject | Manager */
  46. protected $userManager;
  47. /** @var \PHPUnit\Framework\MockObject\MockObject | IRootFolder */
  48. protected $rootFolder;
  49. /** @var \OC\DB\Connection */
  50. protected $dbConnection;
  51. /** @var string */
  52. protected $trashTable = 'files_trash';
  53. /** @var string */
  54. protected $user0 = 'user0';
  55. protected function setUp(): void {
  56. parent::setUp();
  57. $this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')
  58. ->disableOriginalConstructor()->getMock();
  59. $this->userManager = $this->getMockBuilder('OC\User\Manager')
  60. ->disableOriginalConstructor()->getMock();
  61. $this->dbConnection = \OC::$server->getDatabaseConnection();
  62. $this->cleanup = new CleanUp($this->rootFolder, $this->userManager, $this->dbConnection);
  63. }
  64. /**
  65. * populate files_trash table with 10 dummy values
  66. */
  67. public function initTable() {
  68. $query = $this->dbConnection->getQueryBuilder();
  69. $query->delete($this->trashTable)->execute();
  70. for ($i = 0; $i < 10; $i++) {
  71. $query->insert($this->trashTable)
  72. ->values([
  73. 'id' => $query->expr()->literal('file'.$i),
  74. 'timestamp' => $query->expr()->literal($i),
  75. 'location' => $query->expr()->literal('.'),
  76. 'user' => $query->expr()->literal('user'.$i % 2)
  77. ])->execute();
  78. }
  79. $getAllQuery = $this->dbConnection->getQueryBuilder();
  80. $result = $getAllQuery->select('id')
  81. ->from($this->trashTable)
  82. ->execute()
  83. ->fetchAll();
  84. $this->assertSame(10, count($result));
  85. }
  86. /**
  87. * @dataProvider dataTestRemoveDeletedFiles
  88. * @param boolean $nodeExists
  89. */
  90. public function testRemoveDeletedFiles($nodeExists) {
  91. $this->initTable();
  92. $this->rootFolder->expects($this->once())
  93. ->method('nodeExists')
  94. ->with('/' . $this->user0 . '/files_trashbin')
  95. ->willReturn($nodeExists);
  96. if ($nodeExists) {
  97. $this->rootFolder->expects($this->once())
  98. ->method('get')
  99. ->with('/' . $this->user0 . '/files_trashbin')
  100. ->willReturn($this->rootFolder);
  101. $this->rootFolder->expects($this->once())
  102. ->method('delete');
  103. } else {
  104. $this->rootFolder->expects($this->never())->method('get');
  105. $this->rootFolder->expects($this->never())->method('delete');
  106. }
  107. $this->invokePrivate($this->cleanup, 'removeDeletedFiles', [$this->user0]);
  108. if ($nodeExists) {
  109. // if the delete operation was execute only files from user1
  110. // should be left.
  111. $query = $this->dbConnection->getQueryBuilder();
  112. $result = $query->select('user')
  113. ->from($this->trashTable)
  114. ->execute()->fetchAll();
  115. $this->assertSame(5, count($result));
  116. foreach ($result as $r) {
  117. $this->assertSame('user1', $r['user']);
  118. }
  119. } else {
  120. // if no delete operation was execute we should still have all 10
  121. // database entries
  122. $getAllQuery = $this->dbConnection->getQueryBuilder();
  123. $result = $getAllQuery->select('id')
  124. ->from($this->trashTable)
  125. ->execute()
  126. ->fetchAll();
  127. $this->assertSame(10, count($result));
  128. }
  129. }
  130. public function dataTestRemoveDeletedFiles() {
  131. return [
  132. [true],
  133. [false]
  134. ];
  135. }
  136. /**
  137. * test remove deleted files from users given as parameter
  138. */
  139. public function testExecuteDeleteListOfUsers() {
  140. $userIds = ['user1', 'user2', 'user3'];
  141. $instance = $this->getMockBuilder('OCA\Files_Trashbin\Command\CleanUp')
  142. ->setMethods(['removeDeletedFiles'])
  143. ->setConstructorArgs([$this->rootFolder, $this->userManager, $this->dbConnection])
  144. ->getMock();
  145. $instance->expects($this->exactly(count($userIds)))
  146. ->method('removeDeletedFiles')
  147. ->willReturnCallback(function ($user) use ($userIds) {
  148. $this->assertTrue(in_array($user, $userIds));
  149. });
  150. $this->userManager->expects($this->exactly(count($userIds)))
  151. ->method('userExists')->willReturn(true);
  152. $inputInterface = $this->getMockBuilder('\Symfony\Component\Console\Input\InputInterface')
  153. ->disableOriginalConstructor()->getMock();
  154. $inputInterface->expects($this->once())->method('getArgument')
  155. ->with('user_id')
  156. ->willReturn($userIds);
  157. $outputInterface = $this->getMockBuilder('\Symfony\Component\Console\Output\OutputInterface')
  158. ->disableOriginalConstructor()->getMock();
  159. $this->invokePrivate($instance, 'execute', [$inputInterface, $outputInterface]);
  160. }
  161. /**
  162. * test remove deleted files of all users
  163. */
  164. public function testExecuteAllUsers() {
  165. $userIds = [];
  166. $backendUsers = ['user1', 'user2'];
  167. $instance = $this->getMockBuilder('OCA\Files_Trashbin\Command\CleanUp')
  168. ->setMethods(['removeDeletedFiles'])
  169. ->setConstructorArgs([$this->rootFolder, $this->userManager, $this->dbConnection])
  170. ->getMock();
  171. $backend = $this->createMock(\OCP\UserInterface::class);
  172. $backend->expects($this->once())->method('getUsers')
  173. ->with('', 500, 0)
  174. ->willReturn($backendUsers);
  175. $instance->expects($this->exactly(count($backendUsers)))
  176. ->method('removeDeletedFiles')
  177. ->willReturnCallback(function ($user) use ($backendUsers) {
  178. $this->assertTrue(in_array($user, $backendUsers));
  179. });
  180. $inputInterface = $this->createMock(InputInterface::class);
  181. $inputInterface->expects($this->once())->method('getArgument')
  182. ->with('user_id')
  183. ->willReturn($userIds);
  184. $inputInterface->method('getOption')
  185. ->with('all-users')
  186. ->willReturn(true);
  187. $outputInterface = $this->createMock(OutputInterface::class);
  188. $this->userManager->expects($this->once())
  189. ->method('getBackends')
  190. ->willReturn([$backend]);
  191. $this->invokePrivate($instance, 'execute', [$inputInterface, $outputInterface]);
  192. }
  193. public function testExecuteNoUsersAndNoAllUsers() {
  194. $inputInterface = $this->createMock(InputInterface::class);
  195. $inputInterface->expects($this->once())->method('getArgument')
  196. ->with('user_id')
  197. ->willReturn([]);
  198. $inputInterface->method('getOption')
  199. ->with('all-users')
  200. ->willReturn(false);
  201. $outputInterface = $this->createMock(OutputInterface::class);
  202. $this->expectException(InvalidOptionException::class);
  203. $this->expectExceptionMessage('Either specify a user_id or --all-users');
  204. $this->invokePrivate($this->cleanup, 'execute', [$inputInterface, $outputInterface]);
  205. }
  206. public function testExecuteUsersAndAllUsers() {
  207. $inputInterface = $this->createMock(InputInterface::class);
  208. $inputInterface->expects($this->once())->method('getArgument')
  209. ->with('user_id')
  210. ->willReturn(['user1', 'user2']);
  211. $inputInterface->method('getOption')
  212. ->with('all-users')
  213. ->willReturn(true);
  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. }