CleanupTest.php 5.2 KB

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