CleanupTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 Morris Jobke <hey@morrisjobke.de>
  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_Versions\Tests\Command;
  27. use OC\User\Manager;
  28. use OCA\Files_Versions\Command\CleanUp;
  29. use OCP\Files\Cache\ICache;
  30. use OCP\Files\Folder;
  31. use OCP\Files\IRootFolder;
  32. use OCP\Files\Storage\IStorage;
  33. use Test\TestCase;
  34. /**
  35. * Class CleanupTest
  36. *
  37. * @group DB
  38. *
  39. * @package OCA\Files_Versions\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 \PHPUnit\Framework\MockObject\MockObject | VersionsMapper */
  49. protected $versionMapper;
  50. protected function setUp(): void {
  51. parent::setUp();
  52. $this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')
  53. ->disableOriginalConstructor()->getMock();
  54. $this->userManager = $this->getMockBuilder('OC\User\Manager')
  55. ->disableOriginalConstructor()->getMock();
  56. $this->versionMapper = $this->getMockBuilder('OCA\Files_Versions\Db\VersionsMapper')
  57. ->disableOriginalConstructor()->getMock();
  58. $this->cleanup = new CleanUp($this->rootFolder, $this->userManager, $this->versionMapper);
  59. }
  60. /**
  61. * @dataProvider dataTestDeleteVersions
  62. * @param boolean $nodeExists
  63. */
  64. public function testDeleteVersions($nodeExists) {
  65. $this->rootFolder->expects($this->once())
  66. ->method('nodeExists')
  67. ->with('/testUser/files_versions')
  68. ->willReturn($nodeExists);
  69. $userFolder = $this->createMock(Folder::class);
  70. $userHomeStorage = $this->createMock(IStorage::class);
  71. $userHomeStorageCache = $this->createMock(ICache::class);
  72. $this->rootFolder->expects($this->once())
  73. ->method('getUserFolder')
  74. ->willReturn($userFolder);
  75. $userFolder->expects($this->once())
  76. ->method('getStorage')
  77. ->willReturn($userHomeStorage);
  78. $userHomeStorage->expects($this->once())
  79. ->method('getCache')
  80. ->willReturn($userHomeStorageCache);
  81. $userHomeStorageCache->expects($this->once())
  82. ->method('getNumericStorageId')
  83. ->willReturn(1);
  84. if ($nodeExists) {
  85. $this->rootFolder->expects($this->once())
  86. ->method('get')
  87. ->with('/testUser/files_versions')
  88. ->willReturn($this->rootFolder);
  89. $this->rootFolder->expects($this->once())
  90. ->method('delete');
  91. } else {
  92. $this->rootFolder->expects($this->never())
  93. ->method('get');
  94. $this->rootFolder->expects($this->never())
  95. ->method('delete');
  96. }
  97. $this->invokePrivate($this->cleanup, 'deleteVersions', ['testUser']);
  98. }
  99. public function dataTestDeleteVersions() {
  100. return [
  101. [true],
  102. [false]
  103. ];
  104. }
  105. /**
  106. * test delete versions from users given as parameter
  107. */
  108. public function testExecuteDeleteListOfUsers() {
  109. $userIds = ['user1', 'user2', 'user3'];
  110. $instance = $this->getMockBuilder('OCA\Files_Versions\Command\CleanUp')
  111. ->setMethods(['deleteVersions'])
  112. ->setConstructorArgs([$this->rootFolder, $this->userManager, $this->versionMapper])
  113. ->getMock();
  114. $instance->expects($this->exactly(count($userIds)))
  115. ->method('deleteVersions')
  116. ->willReturnCallback(function ($user) use ($userIds) {
  117. $this->assertTrue(in_array($user, $userIds));
  118. });
  119. $this->userManager->expects($this->exactly(count($userIds)))
  120. ->method('userExists')->willReturn(true);
  121. $inputInterface = $this->getMockBuilder('\Symfony\Component\Console\Input\InputInterface')
  122. ->disableOriginalConstructor()->getMock();
  123. $inputInterface->expects($this->once())->method('getArgument')
  124. ->with('user_id')
  125. ->willReturn($userIds);
  126. $outputInterface = $this->getMockBuilder('\Symfony\Component\Console\Output\OutputInterface')
  127. ->disableOriginalConstructor()->getMock();
  128. $this->invokePrivate($instance, 'execute', [$inputInterface, $outputInterface]);
  129. }
  130. /**
  131. * test delete versions of all users
  132. */
  133. public function testExecuteAllUsers() {
  134. $userIds = [];
  135. $backendUsers = ['user1', 'user2'];
  136. $instance = $this->getMockBuilder('OCA\Files_Versions\Command\CleanUp')
  137. ->setMethods(['deleteVersions'])
  138. ->setConstructorArgs([$this->rootFolder, $this->userManager, $this->versionMapper])
  139. ->getMock();
  140. $backend = $this->getMockBuilder(\OCP\UserInterface::class)
  141. ->disableOriginalConstructor()->getMock();
  142. $backend->expects($this->once())->method('getUsers')
  143. ->with('', 500, 0)
  144. ->willReturn($backendUsers);
  145. $instance->expects($this->exactly(count($backendUsers)))
  146. ->method('deleteVersions')
  147. ->willReturnCallback(function ($user) use ($backendUsers) {
  148. $this->assertTrue(in_array($user, $backendUsers));
  149. });
  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->userManager->expects($this->once())
  158. ->method('getBackends')
  159. ->willReturn([$backend]);
  160. $this->invokePrivate($instance, 'execute', [$inputInterface, $outputInterface]);
  161. }
  162. }