1
0

CleanPreviewsBackgroundJobTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * @copyright 2016, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  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
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\Repair\Owncloud;
  24. use OC\Repair\Owncloud\CleanPreviewsBackgroundJob;
  25. use OCP\AppFramework\Utility\ITimeFactory;
  26. use OCP\BackgroundJob\IJobList;
  27. use OCP\Files\Folder;
  28. use OCP\Files\IRootFolder;
  29. use OCP\Files\NotFoundException;
  30. use OCP\Files\NotPermittedException;
  31. use OCP\IUserManager;
  32. use Psr\Log\LoggerInterface;
  33. use Test\TestCase;
  34. class CleanPreviewsBackgroundJobTest extends TestCase {
  35. /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */
  36. private $rootFolder;
  37. /** @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */
  38. private $logger;
  39. /** @var IJobList|\PHPUnit_Framework_MockObject_MockObject */
  40. private $jobList;
  41. /** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
  42. private $timeFactory;
  43. /** @var CleanPreviewsBackgroundJob */
  44. private $job;
  45. /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
  46. private $userManager;
  47. public function setUp(): void {
  48. parent::setUp();
  49. $this->rootFolder = $this->createMock(IRootFolder::class);
  50. $this->logger = $this->createMock(LoggerInterface::class);
  51. $this->jobList = $this->createMock(IJobList::class);
  52. $this->timeFactory = $this->createMock(ITimeFactory::class);
  53. $this->userManager = $this->createMock(IUserManager::class);
  54. $this->userManager->expects($this->any())->method('userExists')->willReturn(true);
  55. $this->job = new CleanPreviewsBackgroundJob(
  56. $this->rootFolder,
  57. $this->logger,
  58. $this->jobList,
  59. $this->timeFactory,
  60. $this->userManager
  61. );
  62. }
  63. public function testCleanupPreviewsUnfinished() {
  64. $userFolder = $this->createMock(Folder::class);
  65. $userRoot = $this->createMock(Folder::class);
  66. $thumbnailFolder = $this->createMock(Folder::class);
  67. $this->rootFolder->method('getUserFolder')
  68. ->with($this->equalTo('myuid'))
  69. ->willReturn($userFolder);
  70. $userFolder->method('getParent')->willReturn($userRoot);
  71. $userRoot->method('get')
  72. ->with($this->equalTo('thumbnails'))
  73. ->willReturn($thumbnailFolder);
  74. $previewFolder1 = $this->createMock(Folder::class);
  75. $previewFolder1->expects($this->once())
  76. ->method('delete');
  77. $thumbnailFolder->method('getDirectoryListing')
  78. ->willReturn([$previewFolder1]);
  79. $thumbnailFolder->expects($this->never())
  80. ->method('delete');
  81. $this->timeFactory->method('getTime')
  82. ->will($this->onConsecutiveCalls(100, 200));
  83. $this->jobList->expects($this->once())
  84. ->method('add')
  85. ->with(
  86. $this->equalTo(CleanPreviewsBackgroundJob::class),
  87. $this->equalTo(['uid' => 'myuid'])
  88. );
  89. $this->logger->expects($this->exactly(2))
  90. ->method('info')
  91. ->withConsecutive(
  92. [$this->equalTo('Started preview cleanup for myuid')],
  93. [$this->equalTo('New preview cleanup scheduled for myuid')],
  94. );
  95. $this->job->run(['uid' => 'myuid']);
  96. }
  97. public function testCleanupPreviewsFinished() {
  98. $userFolder = $this->createMock(Folder::class);
  99. $userRoot = $this->createMock(Folder::class);
  100. $thumbnailFolder = $this->createMock(Folder::class);
  101. $this->rootFolder->method('getUserFolder')
  102. ->with($this->equalTo('myuid'))
  103. ->willReturn($userFolder);
  104. $userFolder->method('getParent')->willReturn($userRoot);
  105. $userRoot->method('get')
  106. ->with($this->equalTo('thumbnails'))
  107. ->willReturn($thumbnailFolder);
  108. $previewFolder1 = $this->createMock(Folder::class);
  109. $previewFolder1->expects($this->once())
  110. ->method('delete');
  111. $thumbnailFolder->method('getDirectoryListing')
  112. ->willReturn([$previewFolder1]);
  113. $this->timeFactory->method('getTime')
  114. ->will($this->onConsecutiveCalls(100, 101));
  115. $this->jobList->expects($this->never())
  116. ->method('add');
  117. $this->logger->expects($this->exactly(2))
  118. ->method('info')
  119. ->withConsecutive(
  120. [$this->equalTo('Started preview cleanup for myuid')],
  121. [$this->equalTo('Preview cleanup done for myuid')],
  122. );
  123. $thumbnailFolder->expects($this->once())
  124. ->method('delete');
  125. $this->job->run(['uid' => 'myuid']);
  126. }
  127. public function testNoUserFolder() {
  128. $this->rootFolder->method('getUserFolder')
  129. ->with($this->equalTo('myuid'))
  130. ->willThrowException(new NotFoundException());
  131. $this->logger->expects($this->exactly(2))
  132. ->method('info')
  133. ->withConsecutive(
  134. [$this->equalTo('Started preview cleanup for myuid')],
  135. [$this->equalTo('Preview cleanup done for myuid')],
  136. );
  137. $this->job->run(['uid' => 'myuid']);
  138. }
  139. public function testNoThumbnailFolder() {
  140. $userFolder = $this->createMock(Folder::class);
  141. $userRoot = $this->createMock(Folder::class);
  142. $this->rootFolder->method('getUserFolder')
  143. ->with($this->equalTo('myuid'))
  144. ->willReturn($userFolder);
  145. $userFolder->method('getParent')->willReturn($userRoot);
  146. $userRoot->method('get')
  147. ->with($this->equalTo('thumbnails'))
  148. ->willThrowException(new NotFoundException());
  149. $this->logger->expects($this->exactly(2))
  150. ->method('info')
  151. ->withConsecutive(
  152. [$this->equalTo('Started preview cleanup for myuid')],
  153. [$this->equalTo('Preview cleanup done for myuid')],
  154. );
  155. $this->job->run(['uid' => 'myuid']);
  156. }
  157. public function testNotPermittedToDelete() {
  158. $userFolder = $this->createMock(Folder::class);
  159. $userRoot = $this->createMock(Folder::class);
  160. $thumbnailFolder = $this->createMock(Folder::class);
  161. $this->rootFolder->method('getUserFolder')
  162. ->with($this->equalTo('myuid'))
  163. ->willReturn($userFolder);
  164. $userFolder->method('getParent')->willReturn($userRoot);
  165. $userRoot->method('get')
  166. ->with($this->equalTo('thumbnails'))
  167. ->willReturn($thumbnailFolder);
  168. $previewFolder1 = $this->createMock(Folder::class);
  169. $previewFolder1->expects($this->once())
  170. ->method('delete')
  171. ->willThrowException(new NotPermittedException());
  172. $thumbnailFolder->method('getDirectoryListing')
  173. ->willReturn([$previewFolder1]);
  174. $this->timeFactory->method('getTime')
  175. ->will($this->onConsecutiveCalls(100, 101));
  176. $this->jobList->expects($this->never())
  177. ->method('add');
  178. $this->logger->expects($this->exactly(2))
  179. ->method('info')
  180. ->withConsecutive(
  181. [$this->equalTo('Started preview cleanup for myuid')],
  182. [$this->equalTo('Preview cleanup done for myuid')],
  183. );
  184. $thumbnailFolder->expects($this->once())
  185. ->method('delete')
  186. ->willThrowException(new NotPermittedException());
  187. $this->job->run(['uid' => 'myuid']);
  188. }
  189. }