CleanPreviewsBackgroundJobTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 PHPUnit\Framework\MockObject\MockObject;
  16. use Psr\Log\LoggerInterface;
  17. use Test\TestCase;
  18. class CleanPreviewsBackgroundJobTest extends TestCase {
  19. private IRootFolder&MockObject $rootFolder;
  20. private LoggerInterface&MockObject $logger;
  21. private IJobList&MockObject $jobList;
  22. private ITimeFactory&MockObject $timeFactory;
  23. private IUserManager&MockObject $userManager;
  24. private CleanPreviewsBackgroundJob $job;
  25. public function setUp(): void {
  26. parent::setUp();
  27. $this->rootFolder = $this->createMock(IRootFolder::class);
  28. $this->logger = $this->createMock(LoggerInterface::class);
  29. $this->jobList = $this->createMock(IJobList::class);
  30. $this->timeFactory = $this->createMock(ITimeFactory::class);
  31. $this->userManager = $this->createMock(IUserManager::class);
  32. $this->userManager->expects($this->any())->method('userExists')->willReturn(true);
  33. $this->job = new CleanPreviewsBackgroundJob(
  34. $this->rootFolder,
  35. $this->logger,
  36. $this->jobList,
  37. $this->timeFactory,
  38. $this->userManager
  39. );
  40. }
  41. public function testCleanupPreviewsUnfinished(): void {
  42. $userFolder = $this->createMock(Folder::class);
  43. $userRoot = $this->createMock(Folder::class);
  44. $thumbnailFolder = $this->createMock(Folder::class);
  45. $this->rootFolder->method('getUserFolder')
  46. ->with($this->equalTo('myuid'))
  47. ->willReturn($userFolder);
  48. $userFolder->method('getParent')->willReturn($userRoot);
  49. $userRoot->method('get')
  50. ->with($this->equalTo('thumbnails'))
  51. ->willReturn($thumbnailFolder);
  52. $previewFolder1 = $this->createMock(Folder::class);
  53. $previewFolder1->expects($this->once())
  54. ->method('delete');
  55. $thumbnailFolder->method('getDirectoryListing')
  56. ->willReturn([$previewFolder1]);
  57. $thumbnailFolder->expects($this->never())
  58. ->method('delete');
  59. $this->timeFactory->method('getTime')
  60. ->will($this->onConsecutiveCalls(100, 200));
  61. $this->jobList->expects($this->once())
  62. ->method('add')
  63. ->with(
  64. $this->equalTo(CleanPreviewsBackgroundJob::class),
  65. $this->equalTo(['uid' => 'myuid'])
  66. );
  67. $loggerCalls = [];
  68. $this->logger->expects($this->exactly(2))
  69. ->method('info')
  70. ->willReturnCallback(function () use (&$loggerCalls) {
  71. $loggerCalls[] = func_get_args();
  72. });
  73. $this->job->run(['uid' => 'myuid']);
  74. self::assertEquals([
  75. ['Started preview cleanup for myuid', []],
  76. ['New preview cleanup scheduled for myuid', []],
  77. ], $loggerCalls);
  78. }
  79. public function testCleanupPreviewsFinished(): void {
  80. $userFolder = $this->createMock(Folder::class);
  81. $userRoot = $this->createMock(Folder::class);
  82. $thumbnailFolder = $this->createMock(Folder::class);
  83. $this->rootFolder->method('getUserFolder')
  84. ->with($this->equalTo('myuid'))
  85. ->willReturn($userFolder);
  86. $userFolder->method('getParent')->willReturn($userRoot);
  87. $userRoot->method('get')
  88. ->with($this->equalTo('thumbnails'))
  89. ->willReturn($thumbnailFolder);
  90. $previewFolder1 = $this->createMock(Folder::class);
  91. $previewFolder1->expects($this->once())
  92. ->method('delete');
  93. $thumbnailFolder->method('getDirectoryListing')
  94. ->willReturn([$previewFolder1]);
  95. $this->timeFactory->method('getTime')
  96. ->will($this->onConsecutiveCalls(100, 101));
  97. $this->jobList->expects($this->never())
  98. ->method('add');
  99. $loggerCalls = [];
  100. $this->logger->expects($this->exactly(2))
  101. ->method('info')
  102. ->willReturnCallback(function () use (&$loggerCalls) {
  103. $loggerCalls[] = func_get_args();
  104. });
  105. $thumbnailFolder->expects($this->once())
  106. ->method('delete');
  107. $this->job->run(['uid' => 'myuid']);
  108. self::assertEquals([
  109. ['Started preview cleanup for myuid', []],
  110. ['Preview cleanup done for myuid', []],
  111. ], $loggerCalls);
  112. }
  113. public function testNoUserFolder(): void {
  114. $this->rootFolder->method('getUserFolder')
  115. ->with($this->equalTo('myuid'))
  116. ->willThrowException(new NotFoundException());
  117. $loggerCalls = [];
  118. $this->logger->expects($this->exactly(2))
  119. ->method('info')
  120. ->willReturnCallback(function () use (&$loggerCalls) {
  121. $loggerCalls[] = func_get_args();
  122. });
  123. $this->job->run(['uid' => 'myuid']);
  124. self::assertEquals([
  125. ['Started preview cleanup for myuid', []],
  126. ['Preview cleanup done for myuid', []],
  127. ], $loggerCalls);
  128. }
  129. public function testNoThumbnailFolder(): void {
  130. $userFolder = $this->createMock(Folder::class);
  131. $userRoot = $this->createMock(Folder::class);
  132. $this->rootFolder->method('getUserFolder')
  133. ->with($this->equalTo('myuid'))
  134. ->willReturn($userFolder);
  135. $userFolder->method('getParent')->willReturn($userRoot);
  136. $userRoot->method('get')
  137. ->with($this->equalTo('thumbnails'))
  138. ->willThrowException(new NotFoundException());
  139. $loggerCalls = [];
  140. $this->logger->expects($this->exactly(2))
  141. ->method('info')
  142. ->willReturnCallback(function () use (&$loggerCalls) {
  143. $loggerCalls[] = func_get_args();
  144. });
  145. $this->job->run(['uid' => 'myuid']);
  146. self::assertEquals([
  147. ['Started preview cleanup for myuid', []],
  148. ['Preview cleanup done for myuid', []],
  149. ], $loggerCalls);
  150. }
  151. public function testNotPermittedToDelete(): void {
  152. $userFolder = $this->createMock(Folder::class);
  153. $userRoot = $this->createMock(Folder::class);
  154. $thumbnailFolder = $this->createMock(Folder::class);
  155. $this->rootFolder->method('getUserFolder')
  156. ->with($this->equalTo('myuid'))
  157. ->willReturn($userFolder);
  158. $userFolder->method('getParent')->willReturn($userRoot);
  159. $userRoot->method('get')
  160. ->with($this->equalTo('thumbnails'))
  161. ->willReturn($thumbnailFolder);
  162. $previewFolder1 = $this->createMock(Folder::class);
  163. $previewFolder1->expects($this->once())
  164. ->method('delete')
  165. ->willThrowException(new NotPermittedException());
  166. $thumbnailFolder->method('getDirectoryListing')
  167. ->willReturn([$previewFolder1]);
  168. $this->timeFactory->method('getTime')
  169. ->will($this->onConsecutiveCalls(100, 101));
  170. $this->jobList->expects($this->never())
  171. ->method('add');
  172. $thumbnailFolder->expects($this->once())
  173. ->method('delete')
  174. ->willThrowException(new NotPermittedException());
  175. $loggerCalls = [];
  176. $this->logger->expects($this->exactly(2))
  177. ->method('info')
  178. ->willReturnCallback(function () use (&$loggerCalls) {
  179. $loggerCalls[] = func_get_args();
  180. });
  181. $this->job->run(['uid' => 'myuid']);
  182. self::assertEquals([
  183. ['Started preview cleanup for myuid', []],
  184. ['Preview cleanup done for myuid', []],
  185. ], $loggerCalls);
  186. }
  187. }