CleanupTest.php 5.0 KB

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