CleanTempFilesTest.php 13 KB

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