CleanTempFilesTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. namespace App\Tests\Unit\Service\Cron\Job;
  3. use App\Entity\Access\Access;
  4. use App\Entity\Core\File;
  5. use App\Repository\Access\AccessRepository;
  6. use App\Repository\Core\FileRepository;
  7. use App\Service\Cron\Job\CleanTempFiles;
  8. use App\Service\Cron\UI\CronUIInterface;
  9. use App\Service\File\Storage\LocalStorage;
  10. use App\Service\Utils\DatesUtils;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Collections\Collection;
  13. use Doctrine\Common\Collections\Criteria;
  14. use Doctrine\ORM\AbstractQuery;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use Doctrine\ORM\Query\Expr;
  17. use Doctrine\ORM\QueryBuilder;
  18. use PHPUnit\Framework\MockObject\MockObject;
  19. use PHPUnit\Framework\TestCase;
  20. use Psr\Log\LoggerInterface;
  21. class TestableCleanTempFile extends CleanTempFiles {
  22. public function listFilesToDelete(): Collection { return parent::listFilesToDelete(); }
  23. public function deleteFiles(Collection $files): void { parent::deleteFiles($files); }
  24. public function purgeDb(bool $commit = true): void { parent::purgeDb($commit); }
  25. }
  26. class CleanTempFilesTest extends TestCase
  27. {
  28. private CronUIInterface|MockObject $ui;
  29. private MockObject|LoggerInterface $logger;
  30. private FileRepository|MockObject $fileRepository;
  31. private EntityManagerInterface|MockObject $em;
  32. private AccessRepository|MockObject $accessRepository;
  33. private LocalStorage|MockObject $storage;
  34. public function setUp(): void
  35. {
  36. $this->ui = $this->getMockBuilder(CronUIInterface::class)->disableOriginalConstructor()->getMock();
  37. $this->logger = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock();
  38. $this->fileRepository = $this->getMockBuilder(FileRepository::class)->disableOriginalConstructor()->getMock();
  39. $this->em = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
  40. $this->accessRepository = $this->getMockBuilder(AccessRepository::class)->disableOriginalConstructor()->getMock();
  41. $this->storage = $this->getMockBuilder(LocalStorage::class)->disableOriginalConstructor()->getMock();
  42. }
  43. private function getMockFor(string $method) {
  44. $cleanTempFiles = $this->getMockBuilder(TestableCleanTempFile::class)
  45. ->setConstructorArgs([$this->fileRepository, $this->em, $this->accessRepository, $this->storage])
  46. ->setMethodsExcept([$method, 'setUI', 'setLoggerInterface'])
  47. ->getMock();
  48. $cleanTempFiles->setUI($this->ui);
  49. $cleanTempFiles->setLoggerInterface($this->logger);
  50. return $cleanTempFiles;
  51. }
  52. public function testPreview(): void
  53. {
  54. $cleanTempFiles = $this->getMockFor('preview');
  55. $file1 = $this->getMockBuilder(File::class)->getMock();
  56. $file1->method('getPath')->willReturn('/foo');
  57. $file2 = $this->getMockBuilder(File::class)->getMock();
  58. $file2->method('getPath')->willReturn('/bar');
  59. $file3 = $this->getMockBuilder(File::class)->getMock();
  60. $file3->method('getPath')->willReturn('/foo/bar');
  61. $cleanTempFiles->method('listFilesToDelete')->willReturn(new ArrayCollection([$file1, $file2, $file3]));
  62. $this->ui->expects(self::atLeastOnce())->method('print')->withConsecutive(
  63. ["3 temporary files to be removed"],
  64. ["> Printing the first and last 50 :"],
  65. [" * /foo"],
  66. [" * /bar"],
  67. [" * /foo/bar"]
  68. );
  69. $cleanTempFiles->expects(self::once())->method('purgeDb')->with(false);
  70. $cleanTempFiles->preview();
  71. }
  72. public function testExecute(): void {
  73. $cleanTempFiles = $this->getMockFor('execute');
  74. $files = new ArrayCollection([
  75. $this->getMockBuilder(File::class)->getMock(),
  76. $this->getMockBuilder(File::class)->getMock(),
  77. $this->getMockBuilder(File::class)->getMock()
  78. ]);
  79. $cleanTempFiles->method('listFilesToDelete')->willReturn($files);
  80. $cleanTempFiles->expects(self::once())->method('deleteFiles')->with($files);
  81. $cleanTempFiles->expects(self::once())->method('purgeDb')->with();
  82. $cleanTempFiles->execute();
  83. }
  84. public function testListFilesToDelete(): void {
  85. $cleanTempFiles = $this->getMockFor('listFilesToDelete');
  86. DatesUtils::setFakeDatetime('2022-01-08 12:00:00');
  87. $this->ui->expects(self::once())->method('print')->with(
  88. 'List temporary files created before 2022-01-01T12:00:00+00:00'
  89. );
  90. $file1 = $this->getMockBuilder(File::class)->getMock(); // Temporary, but recent : do not remove
  91. $file1->method('getId')->willReturn(1);
  92. $file1->method('getCreateDate')->willReturn(new \DateTime('2022-01-02'));
  93. $file1->method('getIsTemporaryFile')->willReturn(true);
  94. $file2 = $this->getMockBuilder(File::class)->getMock(); // Temporary and 1-year-old : remove
  95. $file1->method('getId')->willReturn(2);
  96. $file2->method('getCreateDate')->willReturn(new \DateTime('2021-01-01'));
  97. $file2->method('getIsTemporaryFile')->willReturn(true);
  98. $file3 = $this->getMockBuilder(File::class)->getMock(); // 2 years old but not temporary : do not remove
  99. $file1->method('getId')->willReturn(3);
  100. $file3->method('getCreateDate')->willReturn(new \DateTime('2020-01-01'));
  101. $file3->method('getIsTemporaryFile')->willReturn(false);
  102. $files = new ArrayCollection([$file1, $file2, $file3]);
  103. $this->fileRepository
  104. ->expects(self::once())
  105. ->method('matching')
  106. ->with(
  107. self::callback(static function (Criteria $c) use ($files) {
  108. $matching = $files->matching($c)->toArray();
  109. return count($matching) === 1 && array_values($matching)[0] === $files[1];
  110. })
  111. )->willReturn($files);
  112. $this->assertEquals(
  113. $files,
  114. $cleanTempFiles->listFilesToDelete()
  115. );
  116. }
  117. public function testDeleteFiles(): void
  118. {
  119. $cleanTempFiles = $this->getMockFor('deleteFiles');
  120. $file1 = $this->getMockBuilder(File::class)->getMock();
  121. $file2 = $this->getMockBuilder(File::class)->getMock();
  122. $file3 = $this->getMockBuilder(File::class)->getMock();
  123. $files = new ArrayCollection([$file1, $file2, $file3]);
  124. $this->ui->expects(self::atLeastOnce())->method('print')->withConsecutive(
  125. ['3 temporary files to be removed'],
  126. ['Deleting files...'],
  127. ['3 files deleted'],
  128. );
  129. $author = $this->getMockBuilder(Access::class)->getMock();
  130. $this->accessRepository->method('find')->with(10984)->willReturn($author);
  131. $this->ui->expects(self::exactly(4))->method('progress')->withConsecutive([0, 3], [1, 3], [2, 3], [3, 3]);
  132. $this->storage
  133. ->expects(self::exactly(3))
  134. ->method('delete')
  135. ->withConsecutive(
  136. [$file1, $author],
  137. [$file2, $author],
  138. [$file3, $author]
  139. );
  140. $cleanTempFiles->deleteFiles($files);
  141. }
  142. public function testDeleteFilesUnknownAuthor(): void
  143. {
  144. $cleanTempFiles = $this->getMockFor('deleteFiles');
  145. $file1 = $this->getMockBuilder(File::class)->getMock();
  146. $file2 = $this->getMockBuilder(File::class)->getMock();
  147. $file3 = $this->getMockBuilder(File::class)->getMock();
  148. $files = new ArrayCollection([$file1, $file2, $file3]);
  149. $this->accessRepository->method('find')->with(10984)->willReturn(null);
  150. $this->ui->expects(self::never())->method('progress');
  151. $this->storage->expects(self::never())->method('delete');
  152. $this->expectException(\RuntimeException::class);
  153. $this->expectExceptionMessage('Access 10984 could not be found');
  154. $cleanTempFiles->deleteFiles($files);
  155. }
  156. public function testDeleteFilesDeletionError(): void
  157. {
  158. $cleanTempFiles = $this->getMockFor('deleteFiles');
  159. $file1 = $this->getMockBuilder(File::class)->getMock();
  160. $file1->method('getId')->willReturn(1);
  161. $file2 = $this->getMockBuilder(File::class)->getMock();
  162. $file2->method('getId')->willReturn(2);
  163. $files = new ArrayCollection([$file1, $file2]);
  164. $this->ui->expects(self::atLeastOnce())->method('print')->withConsecutive(
  165. ['2 temporary files to be removed'],
  166. ['Deleting files...'],
  167. ['ERROR : foo'],
  168. ['1 files deleted'],
  169. );
  170. $author = $this->getMockBuilder(Access::class)->getMock();
  171. $this->accessRepository->method('find')->with(10984)->willReturn($author);
  172. $this->ui->expects(self::exactly(3))->method('progress')->withConsecutive([0, 2], [1, 2], [2, 2]);
  173. $this->storage
  174. ->expects(self::exactly(2))
  175. ->method('delete')
  176. ->willReturnCallback(static function ($file, $author): File {
  177. if ($file->getId() === 1) {
  178. throw new \RuntimeException('foo');
  179. }
  180. return $file;
  181. });
  182. $cleanTempFiles->deleteFiles($files);
  183. }
  184. public function testPurgeDb(): void {
  185. $cleanTempFiles = $this->getMockFor('purgeDb');
  186. DatesUtils::setFakeDatetime('2022-01-01 12:00:00');
  187. $this->ui->expects(self::exactly(2))->method('print')->withConsecutive(
  188. ['Purge DB from records of files deleted before 2021-01-01T12:00:00+00:00'],
  189. ['DB purged - 3 records permanently deleted'],
  190. );
  191. $qb = $this->getMockBuilder(QueryBuilder::class)->disableOriginalConstructor()->getMock();
  192. $this->em->expects(self::once())->method('createQueryBuilder')->willReturn($qb);
  193. $exprBuilder = $this->getMockBuilder(Expr::class)->disableOriginalConstructor()->getMock();
  194. $qb->method('expr')->willReturn($exprBuilder);
  195. $cmp1 = $this->getMockBuilder(Expr::class)->disableOriginalConstructor()->getMock();
  196. $exprBuilder->expects(self::once())->method('eq')->with('f.isTemporaryFile', 1)->willReturn($cmp1);
  197. $cmp2 = $this->getMockBuilder(Expr::class)->disableOriginalConstructor()->getMock();
  198. $exprBuilder->expects(self::once())->method('lt')->with('f.updateDate', ':maxDate')->willReturn($cmp2);
  199. $qb->expects(self::once())->method('delete')->with('File', 'f')->willReturnSelf();
  200. $qb->method('where')->with($cmp1)->willReturnSelf();
  201. $qb->method('andWhere')->with($cmp2)->willReturnSelf();
  202. $qb->method('setParameter')->with('maxDate', DatesUtils::new('2021-01-01T12:00:00+00:00'))->willReturnSelf();
  203. $q = $this->getMockBuilder(AbstractQuery::class)->disableOriginalConstructor()->getMock();
  204. $qb->method('getQuery')->willReturn($q);
  205. $q->method('getResult')->willReturn(3);
  206. $this->em->expects(self::once())->method('commit');
  207. $cleanTempFiles->purgeDb();
  208. }
  209. public function testPurgeDbNoCommit(): void {
  210. $cleanTempFiles = $this->getMockFor('purgeDb');
  211. DatesUtils::setFakeDatetime('2022-01-01 12:00:00');
  212. $this->ui->expects(self::exactly(2))->method('print')->withConsecutive(
  213. ['Purge DB from records of files deleted before 2021-01-01T12:00:00+00:00'],
  214. ['DB purged - 3 records would be permanently deleted'],
  215. );
  216. $qb = $this->getMockBuilder(QueryBuilder::class)->disableOriginalConstructor()->getMock();
  217. $this->em->expects(self::once())->method('createQueryBuilder')->willReturn($qb);
  218. $exprBuilder = $this->getMockBuilder(Expr::class)->disableOriginalConstructor()->getMock();
  219. $qb->method('expr')->willReturn($exprBuilder);
  220. $cmp1 = $this->getMockBuilder(Expr::class)->disableOriginalConstructor()->getMock();
  221. $exprBuilder->expects(self::once())->method('eq')->with('f.isTemporaryFile', 1)->willReturn($cmp1);
  222. $cmp2 = $this->getMockBuilder(Expr::class)->disableOriginalConstructor()->getMock();
  223. $exprBuilder->expects(self::once())->method('lt')->with('f.updateDate', ':maxDate')->willReturn($cmp2);
  224. $qb->expects(self::once())->method('delete')->with('File', 'f')->willReturnSelf();
  225. $qb->method('where')->willReturnSelf();
  226. $qb->method('andWhere')->willReturnSelf();
  227. $qb->method('setParameter')->willReturnSelf();
  228. $q = $this->getMockBuilder(AbstractQuery::class)->disableOriginalConstructor()->getMock();
  229. $qb->method('getQuery')->willReturn($q);
  230. $q->method('getResult')->willReturn(3);
  231. $this->em->expects(self::never())->method('commit');
  232. $this->em->expects(self::once())->method('rollback');
  233. $cleanTempFiles->purgeDb(false);
  234. }
  235. }