CleanUpTest.php 8.0 KB

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