CleanupTest.php 5.0 KB

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