CleanupTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Thomas Müller <thomas.mueller@tmit.eu>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\Files_Versions\Tests\Command;
  24. use OCA\Files_Versions\Command\CleanUp;
  25. use Test\TestCase;
  26. use OC\User\Manager;
  27. use OCP\Files\IRootFolder;
  28. /**
  29. * Class CleanupTest
  30. *
  31. * @group DB
  32. *
  33. * @package OCA\Files_Versions\Tests\Command
  34. */
  35. class CleanupTest extends TestCase {
  36. /** @var CleanUp */
  37. protected $cleanup;
  38. /** @var \PHPUnit_Framework_MockObject_MockObject | Manager */
  39. protected $userManager;
  40. /** @var \PHPUnit_Framework_MockObject_MockObject | IRootFolder */
  41. protected $rootFolder;
  42. public function setUp() {
  43. parent::setUp();
  44. $this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')
  45. ->disableOriginalConstructor()->getMock();
  46. $this->userManager = $this->getMockBuilder('OC\User\Manager')
  47. ->disableOriginalConstructor()->getMock();
  48. $this->cleanup = new CleanUp($this->rootFolder, $this->userManager);
  49. }
  50. /**
  51. * @dataProvider dataTestDeleteVersions
  52. * @param boolean $nodeExists
  53. */
  54. public function testDeleteVersions($nodeExists) {
  55. $this->rootFolder->expects($this->once())
  56. ->method('nodeExists')
  57. ->with('/testUser/files_versions')
  58. ->willReturn($nodeExists);
  59. if($nodeExists) {
  60. $this->rootFolder->expects($this->once())
  61. ->method('get')
  62. ->with('/testUser/files_versions')
  63. ->willReturn($this->rootFolder);
  64. $this->rootFolder->expects($this->once())
  65. ->method('delete');
  66. } else {
  67. $this->rootFolder->expects($this->never())
  68. ->method('get');
  69. $this->rootFolder->expects($this->never())
  70. ->method('delete');
  71. }
  72. $this->invokePrivate($this->cleanup, 'deleteVersions', ['testUser']);
  73. }
  74. public function dataTestDeleteVersions() {
  75. return array(
  76. array(true),
  77. array(false)
  78. );
  79. }
  80. /**
  81. * test delete versions from users given as parameter
  82. */
  83. public function testExecuteDeleteListOfUsers() {
  84. $userIds = ['user1', 'user2', 'user3'];
  85. $instance = $this->getMockBuilder('OCA\Files_Versions\Command\CleanUp')
  86. ->setMethods(['deleteVersions'])
  87. ->setConstructorArgs([$this->rootFolder, $this->userManager])
  88. ->getMock();
  89. $instance->expects($this->exactly(count($userIds)))
  90. ->method('deleteVersions')
  91. ->willReturnCallback(function ($user) use ($userIds) {
  92. $this->assertTrue(in_array($user, $userIds));
  93. });
  94. $this->userManager->expects($this->exactly(count($userIds)))
  95. ->method('userExists')->willReturn(true);
  96. $inputInterface = $this->getMockBuilder('\Symfony\Component\Console\Input\InputInterface')
  97. ->disableOriginalConstructor()->getMock();
  98. $inputInterface->expects($this->once())->method('getArgument')
  99. ->with('user_id')
  100. ->willReturn($userIds);
  101. $outputInterface = $this->getMockBuilder('\Symfony\Component\Console\Output\OutputInterface')
  102. ->disableOriginalConstructor()->getMock();
  103. $this->invokePrivate($instance, 'execute', [$inputInterface, $outputInterface]);
  104. }
  105. /**
  106. * test delete versions of all users
  107. */
  108. public function testExecuteAllUsers() {
  109. $userIds = [];
  110. $backendUsers = ['user1', 'user2'];
  111. $instance = $this->getMockBuilder('OCA\Files_Versions\Command\CleanUp')
  112. ->setMethods(['deleteVersions'])
  113. ->setConstructorArgs([$this->rootFolder, $this->userManager])
  114. ->getMock();
  115. $backend = $this->getMockBuilder('OC_User_Interface')
  116. ->disableOriginalConstructor()->getMock();
  117. $backend->expects($this->once())->method('getUsers')
  118. ->with('', 500, 0)
  119. ->willReturn($backendUsers);
  120. $instance->expects($this->exactly(count($backendUsers)))
  121. ->method('deleteVersions')
  122. ->willReturnCallback(function ($user) use ($backendUsers) {
  123. $this->assertTrue(in_array($user, $backendUsers));
  124. });
  125. $inputInterface = $this->getMockBuilder('\Symfony\Component\Console\Input\InputInterface')
  126. ->disableOriginalConstructor()->getMock();
  127. $inputInterface->expects($this->once())->method('getArgument')
  128. ->with('user_id')
  129. ->willReturn($userIds);
  130. $outputInterface = $this->getMockBuilder('\Symfony\Component\Console\Output\OutputInterface')
  131. ->disableOriginalConstructor()->getMock();
  132. $this->userManager->expects($this->once())
  133. ->method('getBackends')
  134. ->willReturn([$backend]);
  135. $this->invokePrivate($instance, 'execute', [$inputInterface, $outputInterface]);
  136. }
  137. }