CleanUpTest.php 7.9 KB

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