CleanupTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Versions\Tests\Command;
  8. use OC\User\Manager;
  9. use OCA\Files_Versions\Command\CleanUp;
  10. use OCP\Files\Cache\ICache;
  11. use OCP\Files\Folder;
  12. use OCP\Files\IRootFolder;
  13. use OCP\Files\Storage\IStorage;
  14. use OCP\UserInterface;
  15. use Test\TestCase;
  16. /**
  17. * Class CleanupTest
  18. *
  19. * @group DB
  20. *
  21. * @package OCA\Files_Versions\Tests\Command
  22. */
  23. class CleanupTest extends TestCase {
  24. /** @var CleanUp */
  25. protected $cleanup;
  26. /** @var \PHPUnit\Framework\MockObject\MockObject | Manager */
  27. protected $userManager;
  28. /** @var \PHPUnit\Framework\MockObject\MockObject | IRootFolder */
  29. protected $rootFolder;
  30. /** @var \PHPUnit\Framework\MockObject\MockObject | VersionsMapper */
  31. protected $versionMapper;
  32. protected function setUp(): void {
  33. parent::setUp();
  34. $this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')
  35. ->disableOriginalConstructor()->getMock();
  36. $this->userManager = $this->getMockBuilder('OC\User\Manager')
  37. ->disableOriginalConstructor()->getMock();
  38. $this->versionMapper = $this->getMockBuilder('OCA\Files_Versions\Db\VersionsMapper')
  39. ->disableOriginalConstructor()->getMock();
  40. $this->cleanup = new CleanUp($this->rootFolder, $this->userManager, $this->versionMapper);
  41. }
  42. /**
  43. * @dataProvider dataTestDeleteVersions
  44. * @param boolean $nodeExists
  45. */
  46. public function testDeleteVersions($nodeExists): void {
  47. $this->rootFolder->expects($this->once())
  48. ->method('nodeExists')
  49. ->with('/testUser/files_versions')
  50. ->willReturn($nodeExists);
  51. $userFolder = $this->createMock(Folder::class);
  52. $userHomeStorage = $this->createMock(IStorage::class);
  53. $userHomeStorageCache = $this->createMock(ICache::class);
  54. $this->rootFolder->expects($this->once())
  55. ->method('getUserFolder')
  56. ->willReturn($userFolder);
  57. $userFolder->expects($this->once())
  58. ->method('getStorage')
  59. ->willReturn($userHomeStorage);
  60. $userHomeStorage->expects($this->once())
  61. ->method('getCache')
  62. ->willReturn($userHomeStorageCache);
  63. $userHomeStorageCache->expects($this->once())
  64. ->method('getNumericStorageId')
  65. ->willReturn(1);
  66. if ($nodeExists) {
  67. $this->rootFolder->expects($this->once())
  68. ->method('get')
  69. ->with('/testUser/files_versions')
  70. ->willReturn($this->rootFolder);
  71. $this->rootFolder->expects($this->once())
  72. ->method('delete');
  73. } else {
  74. $this->rootFolder->expects($this->never())
  75. ->method('get');
  76. $this->rootFolder->expects($this->never())
  77. ->method('delete');
  78. }
  79. $this->invokePrivate($this->cleanup, 'deleteVersions', ['testUser']);
  80. }
  81. public function dataTestDeleteVersions() {
  82. return [
  83. [true],
  84. [false]
  85. ];
  86. }
  87. /**
  88. * test delete versions from users given as parameter
  89. */
  90. public function testExecuteDeleteListOfUsers(): void {
  91. $userIds = ['user1', 'user2', 'user3'];
  92. $instance = $this->getMockBuilder('OCA\Files_Versions\Command\CleanUp')
  93. ->setMethods(['deleteVersions'])
  94. ->setConstructorArgs([$this->rootFolder, $this->userManager, $this->versionMapper])
  95. ->getMock();
  96. $instance->expects($this->exactly(count($userIds)))
  97. ->method('deleteVersions')
  98. ->willReturnCallback(function ($user) use ($userIds): void {
  99. $this->assertTrue(in_array($user, $userIds));
  100. });
  101. $this->userManager->expects($this->exactly(count($userIds)))
  102. ->method('userExists')->willReturn(true);
  103. $inputInterface = $this->getMockBuilder('\Symfony\Component\Console\Input\InputInterface')
  104. ->disableOriginalConstructor()->getMock();
  105. $inputInterface->expects($this->once())->method('getArgument')
  106. ->with('user_id')
  107. ->willReturn($userIds);
  108. $outputInterface = $this->getMockBuilder('\Symfony\Component\Console\Output\OutputInterface')
  109. ->disableOriginalConstructor()->getMock();
  110. $this->invokePrivate($instance, 'execute', [$inputInterface, $outputInterface]);
  111. }
  112. /**
  113. * test delete versions of all users
  114. */
  115. public function testExecuteAllUsers(): void {
  116. $userIds = [];
  117. $backendUsers = ['user1', 'user2'];
  118. $instance = $this->getMockBuilder('OCA\Files_Versions\Command\CleanUp')
  119. ->setMethods(['deleteVersions'])
  120. ->setConstructorArgs([$this->rootFolder, $this->userManager, $this->versionMapper])
  121. ->getMock();
  122. $backend = $this->getMockBuilder(UserInterface::class)
  123. ->disableOriginalConstructor()->getMock();
  124. $backend->expects($this->once())->method('getUsers')
  125. ->with('', 500, 0)
  126. ->willReturn($backendUsers);
  127. $instance->expects($this->exactly(count($backendUsers)))
  128. ->method('deleteVersions')
  129. ->willReturnCallback(function ($user) use ($backendUsers): void {
  130. $this->assertTrue(in_array($user, $backendUsers));
  131. });
  132. $inputInterface = $this->getMockBuilder('\Symfony\Component\Console\Input\InputInterface')
  133. ->disableOriginalConstructor()->getMock();
  134. $inputInterface->expects($this->once())->method('getArgument')
  135. ->with('user_id')
  136. ->willReturn($userIds);
  137. $outputInterface = $this->getMockBuilder('\Symfony\Component\Console\Output\OutputInterface')
  138. ->disableOriginalConstructor()->getMock();
  139. $this->userManager->expects($this->once())
  140. ->method('getBackends')
  141. ->willReturn([$backend]);
  142. $this->invokePrivate($instance, 'execute', [$inputInterface, $outputInterface]);
  143. }
  144. }