CleanPreviewsBackgroundJobTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\Repair\Owncloud;
  7. use OC\Repair\Owncloud\CleanPreviewsBackgroundJob;
  8. use OCP\AppFramework\Utility\ITimeFactory;
  9. use OCP\BackgroundJob\IJobList;
  10. use OCP\Files\Folder;
  11. use OCP\Files\IRootFolder;
  12. use OCP\Files\NotFoundException;
  13. use OCP\Files\NotPermittedException;
  14. use OCP\IUserManager;
  15. use Psr\Log\LoggerInterface;
  16. use Test\TestCase;
  17. class CleanPreviewsBackgroundJobTest extends TestCase {
  18. /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */
  19. private $rootFolder;
  20. /** @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */
  21. private $logger;
  22. /** @var IJobList|\PHPUnit_Framework_MockObject_MockObject */
  23. private $jobList;
  24. /** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
  25. private $timeFactory;
  26. /** @var CleanPreviewsBackgroundJob */
  27. private $job;
  28. /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
  29. private $userManager;
  30. public function setUp(): void {
  31. parent::setUp();
  32. $this->rootFolder = $this->createMock(IRootFolder::class);
  33. $this->logger = $this->createMock(LoggerInterface::class);
  34. $this->jobList = $this->createMock(IJobList::class);
  35. $this->timeFactory = $this->createMock(ITimeFactory::class);
  36. $this->userManager = $this->createMock(IUserManager::class);
  37. $this->userManager->expects($this->any())->method('userExists')->willReturn(true);
  38. $this->job = new CleanPreviewsBackgroundJob(
  39. $this->rootFolder,
  40. $this->logger,
  41. $this->jobList,
  42. $this->timeFactory,
  43. $this->userManager
  44. );
  45. }
  46. public function testCleanupPreviewsUnfinished() {
  47. $userFolder = $this->createMock(Folder::class);
  48. $userRoot = $this->createMock(Folder::class);
  49. $thumbnailFolder = $this->createMock(Folder::class);
  50. $this->rootFolder->method('getUserFolder')
  51. ->with($this->equalTo('myuid'))
  52. ->willReturn($userFolder);
  53. $userFolder->method('getParent')->willReturn($userRoot);
  54. $userRoot->method('get')
  55. ->with($this->equalTo('thumbnails'))
  56. ->willReturn($thumbnailFolder);
  57. $previewFolder1 = $this->createMock(Folder::class);
  58. $previewFolder1->expects($this->once())
  59. ->method('delete');
  60. $thumbnailFolder->method('getDirectoryListing')
  61. ->willReturn([$previewFolder1]);
  62. $thumbnailFolder->expects($this->never())
  63. ->method('delete');
  64. $this->timeFactory->method('getTime')
  65. ->will($this->onConsecutiveCalls(100, 200));
  66. $this->jobList->expects($this->once())
  67. ->method('add')
  68. ->with(
  69. $this->equalTo(CleanPreviewsBackgroundJob::class),
  70. $this->equalTo(['uid' => 'myuid'])
  71. );
  72. $this->logger->expects($this->exactly(2))
  73. ->method('info')
  74. ->withConsecutive(
  75. [$this->equalTo('Started preview cleanup for myuid')],
  76. [$this->equalTo('New preview cleanup scheduled for myuid')],
  77. );
  78. $this->job->run(['uid' => 'myuid']);
  79. }
  80. public function testCleanupPreviewsFinished() {
  81. $userFolder = $this->createMock(Folder::class);
  82. $userRoot = $this->createMock(Folder::class);
  83. $thumbnailFolder = $this->createMock(Folder::class);
  84. $this->rootFolder->method('getUserFolder')
  85. ->with($this->equalTo('myuid'))
  86. ->willReturn($userFolder);
  87. $userFolder->method('getParent')->willReturn($userRoot);
  88. $userRoot->method('get')
  89. ->with($this->equalTo('thumbnails'))
  90. ->willReturn($thumbnailFolder);
  91. $previewFolder1 = $this->createMock(Folder::class);
  92. $previewFolder1->expects($this->once())
  93. ->method('delete');
  94. $thumbnailFolder->method('getDirectoryListing')
  95. ->willReturn([$previewFolder1]);
  96. $this->timeFactory->method('getTime')
  97. ->will($this->onConsecutiveCalls(100, 101));
  98. $this->jobList->expects($this->never())
  99. ->method('add');
  100. $this->logger->expects($this->exactly(2))
  101. ->method('info')
  102. ->withConsecutive(
  103. [$this->equalTo('Started preview cleanup for myuid')],
  104. [$this->equalTo('Preview cleanup done for myuid')],
  105. );
  106. $thumbnailFolder->expects($this->once())
  107. ->method('delete');
  108. $this->job->run(['uid' => 'myuid']);
  109. }
  110. public function testNoUserFolder() {
  111. $this->rootFolder->method('getUserFolder')
  112. ->with($this->equalTo('myuid'))
  113. ->willThrowException(new NotFoundException());
  114. $this->logger->expects($this->exactly(2))
  115. ->method('info')
  116. ->withConsecutive(
  117. [$this->equalTo('Started preview cleanup for myuid')],
  118. [$this->equalTo('Preview cleanup done for myuid')],
  119. );
  120. $this->job->run(['uid' => 'myuid']);
  121. }
  122. public function testNoThumbnailFolder() {
  123. $userFolder = $this->createMock(Folder::class);
  124. $userRoot = $this->createMock(Folder::class);
  125. $this->rootFolder->method('getUserFolder')
  126. ->with($this->equalTo('myuid'))
  127. ->willReturn($userFolder);
  128. $userFolder->method('getParent')->willReturn($userRoot);
  129. $userRoot->method('get')
  130. ->with($this->equalTo('thumbnails'))
  131. ->willThrowException(new NotFoundException());
  132. $this->logger->expects($this->exactly(2))
  133. ->method('info')
  134. ->withConsecutive(
  135. [$this->equalTo('Started preview cleanup for myuid')],
  136. [$this->equalTo('Preview cleanup done for myuid')],
  137. );
  138. $this->job->run(['uid' => 'myuid']);
  139. }
  140. public function testNotPermittedToDelete() {
  141. $userFolder = $this->createMock(Folder::class);
  142. $userRoot = $this->createMock(Folder::class);
  143. $thumbnailFolder = $this->createMock(Folder::class);
  144. $this->rootFolder->method('getUserFolder')
  145. ->with($this->equalTo('myuid'))
  146. ->willReturn($userFolder);
  147. $userFolder->method('getParent')->willReturn($userRoot);
  148. $userRoot->method('get')
  149. ->with($this->equalTo('thumbnails'))
  150. ->willReturn($thumbnailFolder);
  151. $previewFolder1 = $this->createMock(Folder::class);
  152. $previewFolder1->expects($this->once())
  153. ->method('delete')
  154. ->willThrowException(new NotPermittedException());
  155. $thumbnailFolder->method('getDirectoryListing')
  156. ->willReturn([$previewFolder1]);
  157. $this->timeFactory->method('getTime')
  158. ->will($this->onConsecutiveCalls(100, 101));
  159. $this->jobList->expects($this->never())
  160. ->method('add');
  161. $this->logger->expects($this->exactly(2))
  162. ->method('info')
  163. ->withConsecutive(
  164. [$this->equalTo('Started preview cleanup for myuid')],
  165. [$this->equalTo('Preview cleanup done for myuid')],
  166. );
  167. $thumbnailFolder->expects($this->once())
  168. ->method('delete')
  169. ->willThrowException(new NotPermittedException());
  170. $this->job->run(['uid' => 'myuid']);
  171. }
  172. }