LocalStorageTest.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. <?php
  2. namespace App\Tests\Unit\Service\File\Storage;
  3. use App\Entity\Access\Access;
  4. use App\Entity\Core\File;
  5. use App\Entity\Organization\Organization;
  6. use App\Entity\Person\Person;
  7. use App\Enum\Core\FileHostEnum;
  8. use App\Enum\Core\FileSizeEnum;
  9. use App\Enum\Core\FileStatusEnum;
  10. use App\Enum\Core\FileTypeEnum;
  11. use App\Enum\Core\FileVisibilityEnum;
  12. use App\Repository\Access\AccessRepository;
  13. use App\Service\File\Factory\ImageFactory;
  14. use App\Service\File\Storage\LocalStorage;
  15. use App\Service\Utils\FileUtils;
  16. use App\Service\Utils\UrlBuilder;
  17. use Doctrine\ORM\EntityManagerInterface;
  18. use Gaufrette\Filesystem;
  19. use JetBrains\PhpStorm\Pure;
  20. use Knp\Bundle\GaufretteBundle\FilesystemMap;
  21. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  22. use Liip\ImagineBundle\Imagine\Data\DataManager;
  23. use PHPUnit\Framework\MockObject\MockObject;
  24. use PHPUnit\Framework\TestCase;
  25. class TestableLocalStorage extends LocalStorage
  26. {
  27. public const FS_KEY = parent::FS_KEY;
  28. public function getPrefix(mixed $owner, bool $isTemporary, ?FileTypeEnum $type = null): string
  29. {
  30. return parent::getPrefix($owner, $isTemporary, $type);
  31. }
  32. #[Pure]
  33. public function getOrganizationAndPersonFromOwner(mixed $owner): array
  34. {
  35. return parent::getOrganizationAndPersonFromOwner($owner);
  36. }
  37. public function getFilterFromSizeAndConfig(string $size, bool $configExist): string
  38. {
  39. return parent::getFilterFromSizeAndConfig($size, $configExist);
  40. }
  41. public function rrmDir(string $dirKey): void
  42. {
  43. parent::rrmDir($dirKey);
  44. }
  45. }
  46. class LocalStorageTest extends TestCase
  47. {
  48. private FilesystemMap $filesystemMap;
  49. private EntityManagerInterface $entityManager;
  50. private AccessRepository $accessRepository;
  51. private DataManager $dataManager;
  52. private CacheManager $cacheManager;
  53. private ImageFactory $imageFactory;
  54. private Filesystem $filesystem;
  55. private FileUtils $fileUtils;
  56. private UrlBuilder $urlBuilder;
  57. public function setUp(): void
  58. {
  59. $this->filesystemMap = $this->getMockBuilder(FilesystemMap::class)->disableOriginalConstructor()->getMock();
  60. $this->entityManager = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
  61. $this->accessRepository = $this->getMockBuilder(AccessRepository::class)->disableOriginalConstructor()->getMock();
  62. $this->dataManager = $this->getMockBuilder(DataManager::class)->disableOriginalConstructor()->getMock();
  63. $this->cacheManager = $this->getMockBuilder(CacheManager::class)->disableOriginalConstructor()->getMock();
  64. $this->imageFactory = $this->getMockBuilder(ImageFactory::class)->disableOriginalConstructor()->getMock();
  65. $this->fileUtils = $this->getMockBuilder(FileUtils::class)->disableOriginalConstructor()->getMock();
  66. $this->urlBuilder = $this->getMockBuilder(UrlBuilder::class)->disableOriginalConstructor()->getMock();
  67. $this->filesystem = $this->getMockBuilder(Filesystem::class)->disableOriginalConstructor()->getMock();
  68. $this->filesystemMap->method('get')->with(TestableLocalStorage::FS_KEY)->willReturn($this->filesystem);
  69. }
  70. public function getMockForMethod(string $methodName): TestableLocalStorage|MockObject
  71. {
  72. return $this->getMockBuilder(TestableLocalStorage::class)
  73. ->setConstructorArgs([
  74. $this->filesystemMap,
  75. $this->entityManager,
  76. $this->accessRepository,
  77. $this->dataManager,
  78. $this->cacheManager,
  79. $this->imageFactory,
  80. $this->fileUtils,
  81. $this->urlBuilder,
  82. '/file/storage/dir/',
  83. ])
  84. ->setMethodsExcept([$methodName])
  85. ->getMock();
  86. }
  87. /**
  88. * @see LocalStorage::exists()
  89. */
  90. public function testExists(): void
  91. {
  92. $fileStorage = $this->getMockForMethod('exists');
  93. $file = $this->getMockBuilder(File::class)->getMock();
  94. $file->method('getSlug')->willReturn('foo');
  95. $this->filesystem->expects(self::once())->method('has')->with('foo')->willReturn(true);
  96. $this->assertTrue($fileStorage->exists($file));
  97. }
  98. /**
  99. * @see LocalStorage::exists()
  100. */
  101. public function testExistsInexistant(): void
  102. {
  103. $fileStorage = $this->getMockForMethod('exists');
  104. $file = $this->getMockBuilder(File::class)->getMock();
  105. $file->method('getSlug')->willReturn('foo');
  106. $this->filesystem->expects(self::once())->method('has')->with('foo')->willReturn(false);
  107. $this->assertFalse($fileStorage->exists($file));
  108. }
  109. /**
  110. * @see LocalStorage::listByOwner()
  111. */
  112. public function testListByOwner(): void
  113. {
  114. $fileStorage = $this->getMockForMethod('listByOwner');
  115. $owner = $this->getMockBuilder(Organization::class)->getMock();
  116. $fileStorage->method('getPrefix')->with($owner, false, FileTypeEnum::LICENCE_CMF)->willReturn('foo');
  117. $this->filesystem->method('listKeys')->with('foo')->willReturn(['foo/a.txt', 'foo/b.pdf']);
  118. $this->assertEquals(
  119. ['foo/a.txt', 'foo/b.pdf'],
  120. $fileStorage->listByOwner($owner, FileTypeEnum::LICENCE_CMF)
  121. );
  122. }
  123. /**
  124. * @see LocalStorage::read()
  125. */
  126. public function testReadPdf(): void
  127. {
  128. $fileStorage = $this->getMockForMethod('read');
  129. $file = $this->getMockBuilder(File::class)->getMock();
  130. $file->method('getSlug')->willReturn('foo');
  131. $this->filesystem->method('read')->with('foo')->willReturn('12345679');
  132. $this->assertEquals(
  133. '12345679',
  134. $fileStorage->read($file)
  135. );
  136. }
  137. public function testGetImageUrl(): void
  138. {
  139. $localStorage = $this->getMockForMethod('getImageUrl');
  140. $localStorage
  141. ->method('getFilterFromSizeAndConfig')
  142. ->with('sm', true)
  143. ->willReturn('crop_sm');
  144. $file = $this->getMockBuilder(File::class)->getMock();
  145. $file->method('getSlug')->willReturn('abc');
  146. $file->method('getConfig')->willReturn('xyz');
  147. $this->cacheManager
  148. ->method('isStored')
  149. ->with('abc', 'crop_sm')
  150. ->willReturn(false);
  151. $this->cacheManager
  152. ->method('resolve')
  153. ->with('abc', 'crop_sm')
  154. ->willReturn('publicUrl/xyz');
  155. $this->assertEquals(
  156. 'publicUrl/xyz',
  157. $localStorage->getImageUrl($file, 'sm', false)
  158. );
  159. }
  160. public function testGetImageUrlRelativePath(): void
  161. {
  162. $localStorage = $this->getMockForMethod('getImageUrl');
  163. $localStorage
  164. ->method('getFilterFromSizeAndConfig')
  165. ->with('sm', true)
  166. ->willReturn('crop_sm');
  167. $file = $this->getMockBuilder(File::class)->getMock();
  168. $file->method('getSlug')->willReturn('abc');
  169. $file->method('getConfig')->willReturn('xyz');
  170. $this->cacheManager
  171. ->method('isStored')
  172. ->with('abc', 'crop_sm')
  173. ->willReturn(false);
  174. $this->cacheManager
  175. ->method('resolve')
  176. ->with('abc', 'crop_sm')
  177. ->willReturn('publicUrl/xyz');
  178. $this->urlBuilder
  179. ->method('getRelativeUrl')
  180. ->with('publicUrl/xyz')
  181. ->willReturn('xyz');
  182. $this->assertEquals(
  183. 'xyz',
  184. $localStorage->getImageUrl($file, 'sm', true)
  185. );
  186. }
  187. public function testGetImageUrlNotCached(): void
  188. {
  189. $localStorage = $this->getMockForMethod('getImageUrl');
  190. $localStorage
  191. ->method('getFilterFromSizeAndConfig')
  192. ->with('sm', true)
  193. ->willReturn('crop_sm');
  194. $file = $this->getMockBuilder(File::class)->getMock();
  195. $file->method('getSlug')->willReturn('abc');
  196. $file->method('getConfig')->willReturn('xyz');
  197. $this->cacheManager
  198. ->method('isStored')
  199. ->with('abc', 'crop_sm')
  200. ->willReturn(false);
  201. $this->imageFactory
  202. ->expects(self::once())
  203. ->method('createImageContent')
  204. ->with($file, 'crop_sm');
  205. $this->cacheManager
  206. ->method('resolve')
  207. ->with('abc', 'crop_sm')
  208. ->willReturn('publicUrl/xyz');
  209. $this->assertEquals(
  210. 'publicUrl/xyz',
  211. $localStorage->getImageUrl($file, 'sm', false)
  212. );
  213. }
  214. public function testGetImageUrlNotCachedMissingFile(): void
  215. {
  216. $localStorage = $this->getMockForMethod('getImageUrl');
  217. $localStorage
  218. ->method('getFilterFromSizeAndConfig')
  219. ->with('sm', true)
  220. ->willReturn('crop_sm');
  221. $file = $this->getMockBuilder(File::class)->getMock();
  222. $file->method('getSlug')->willReturn('abc');
  223. $file->method('getConfig')->willReturn('xyz');
  224. $this->cacheManager
  225. ->method('isStored')
  226. ->with('abc', 'crop_sm')
  227. ->willReturn(false);
  228. $this->imageFactory
  229. ->expects(self::once())
  230. ->method('createImageContent')
  231. ->with($file, 'crop_sm')
  232. ->willThrowException(new \Exception('File not found'));
  233. $this->cacheManager
  234. ->expects(self::never())
  235. ->method('resolve');
  236. $this->urlBuilder
  237. ->method('getAbsoluteUrl')
  238. ->with('images/missing-file.png')
  239. ->willReturn('publicUrl/images/missing-file.png');
  240. $this->assertEquals(
  241. 'publicUrl/images/missing-file.png',
  242. $localStorage->getImageUrl($file, 'sm', false)
  243. );
  244. }
  245. public function testGetImageUrlNotCachedMissingFileRelativePath(): void
  246. {
  247. $localStorage = $this->getMockForMethod('getImageUrl');
  248. $localStorage
  249. ->method('getFilterFromSizeAndConfig')
  250. ->with('sm', true)
  251. ->willReturn('crop_sm');
  252. $file = $this->getMockBuilder(File::class)->getMock();
  253. $file->method('getSlug')->willReturn('abc');
  254. $file->method('getConfig')->willReturn('xyz');
  255. $this->cacheManager
  256. ->method('isStored')
  257. ->with('abc', 'crop_sm')
  258. ->willReturn(false);
  259. $this->imageFactory
  260. ->expects(self::once())
  261. ->method('createImageContent')
  262. ->with($file, 'crop_sm')
  263. ->willThrowException(new \Exception('File not found'));
  264. $this->cacheManager
  265. ->expects(self::never())
  266. ->method('resolve');
  267. $this->assertEquals(
  268. 'images/missing-file.png',
  269. $localStorage->getImageUrl($file, 'sm', true)
  270. );
  271. }
  272. public function testGetFilterFromSizeAndConfig(): void
  273. {
  274. $fileStorage = $this->getMockForMethod('getFilterFromSizeAndConfig');
  275. $this->assertEquals(
  276. 'crop_sm',
  277. $fileStorage->getFilterFromSizeAndConfig(FileSizeEnum::SM->value, true)
  278. );
  279. $this->assertEquals(
  280. 'sm',
  281. $fileStorage->getFilterFromSizeAndConfig(FileSizeEnum::SM->value, false)
  282. );
  283. $this->assertEquals(
  284. 'crop_md',
  285. $fileStorage->getFilterFromSizeAndConfig(FileSizeEnum::MD->value, true)
  286. );
  287. $this->assertEquals(
  288. 'md',
  289. $fileStorage->getFilterFromSizeAndConfig(FileSizeEnum::MD->value, false)
  290. );
  291. $this->assertEquals(
  292. 'crop_lg',
  293. $fileStorage->getFilterFromSizeAndConfig(FileSizeEnum::LG->value, true)
  294. );
  295. $this->assertEquals(
  296. 'lg',
  297. $fileStorage->getFilterFromSizeAndConfig(FileSizeEnum::LG->value, false)
  298. );
  299. }
  300. /**
  301. * @see LocalStorage::prepareFile()
  302. */
  303. public function testPrepareFile(): void
  304. {
  305. $fileStorage = $this->getMockForMethod('prepareFile');
  306. $owner = $this->getMockBuilder(Organization::class)->getMock();
  307. $author = $this->getMockBuilder(Access::class)->getMock();
  308. $author->method('getId')->willReturn(123);
  309. $fileStorage->method('getOrganizationAndPersonFromOwner')->with($owner)->willReturn([$owner, null]);
  310. $this->entityManager->expects(self::once())->method('persist');
  311. $this->entityManager->expects(self::once())->method('flush');
  312. $file = $fileStorage->prepareFile(
  313. $owner,
  314. 'file.ext',
  315. FileTypeEnum::LICENCE_CMF,
  316. $author,
  317. true,
  318. FileVisibilityEnum::ONLY_ORGANIZATION,
  319. 'application/pdf'
  320. );
  321. $this->assertEquals($owner, $file->getOrganization());
  322. $this->assertEquals(null, $file->getPerson());
  323. $this->assertEquals('file.ext', $file->getName());
  324. $this->assertEquals(FileTypeEnum::LICENCE_CMF, $file->getType());
  325. $this->assertTrue($file->getIsTemporaryFile());
  326. $this->assertEquals(FileVisibilityEnum::ONLY_ORGANIZATION, $file->getVisibility());
  327. $this->assertEquals('application/pdf', $file->getMimeType());
  328. $this->assertEquals(123, $file->getCreatedBy());
  329. }
  330. /**
  331. * @see LocalStorage::prepareFile()
  332. */
  333. public function testPrepareFileDefaultValues(): void
  334. {
  335. $fileStorage = $this->getMockForMethod('prepareFile');
  336. $owner = $this->getMockBuilder(Person::class)->getMock();
  337. $author = $this->getMockBuilder(Access::class)->getMock();
  338. $fileStorage->method('getOrganizationAndPersonFromOwner')->with($owner)->willReturn([null, $owner]);
  339. $this->entityManager->expects(self::once())->method('persist');
  340. $this->entityManager->expects(self::once())->method('flush');
  341. $this->fileUtils->method('guessMimeTypeFromFilename')->with('file.txt')->willReturn('text/plain');
  342. $file = $fileStorage->prepareFile($owner, 'file.txt', FileTypeEnum::NONE, $author);
  343. $this->assertEquals(null, $file->getOrganization());
  344. $this->assertEquals($owner, $file->getPerson());
  345. $this->assertEquals('file.txt', $file->getName());
  346. $this->assertEquals(FileTypeEnum::NONE, $file->getType());
  347. $this->assertFalse($file->getIsTemporaryFile());
  348. $this->assertEquals(FileVisibilityEnum::NOBODY, $file->getVisibility());
  349. $this->assertEquals('text/plain', $file->getMimeType());
  350. }
  351. /**
  352. * @see LocalStorage::prepareFile()
  353. */
  354. public function testPrepareFileNoFlush(): void
  355. {
  356. $fileStorage = $this->getMockForMethod('prepareFile');
  357. $owner = $this->getMockBuilder(Organization::class)->getMock();
  358. $author = $this->getMockBuilder(Access::class)->getMock();
  359. $fileStorage->method('getOrganizationAndPersonFromOwner')->with($owner)->willReturn([$owner, null]);
  360. $this->entityManager->expects(self::once())->method('persist');
  361. $this->entityManager->expects(self::never())->method('flush');
  362. $this->fileUtils->method('guessMimeTypeFromFilename')->willReturn('text/plain');
  363. $fileStorage->prepareFile(
  364. $owner,
  365. 'file.txt',
  366. FileTypeEnum::NONE,
  367. $author,
  368. false,
  369. FileVisibilityEnum::NOBODY,
  370. 'text/plain',
  371. false
  372. );
  373. }
  374. /**
  375. * @see LocalStorage::write()
  376. */
  377. public function testWriteNewFile(): void
  378. {
  379. $fileStorage = $this->getMockForMethod('write');
  380. $organization = $this->getMockBuilder(Organization::class)->getMock();
  381. $author = $this->getMockBuilder(Access::class)->getMock();
  382. $author->method('getId')->willReturn(123);
  383. $file = $this->getMockBuilder(File::class)->getMock();
  384. $file->method('getName')->willReturn('foo.txt');
  385. $file->method('getOrganization')->willReturn($organization);
  386. $file->method('getPerson')->willReturn(null);
  387. $file->method('getIsTemporaryFile')->willReturn(false);
  388. $file->method('getSlug')->willReturn(null);
  389. $file->method('getType')->willReturn(FileTypeEnum::NONE);
  390. $fileStorage
  391. ->method('getPrefix')
  392. ->with($organization, false, FileTypeEnum::NONE)
  393. ->willReturn('prefix/');
  394. $content = '123456789';
  395. $size = strlen($content);
  396. $this->filesystem
  397. ->expects(self::once())
  398. ->method('write')
  399. ->with(self::matchesRegularExpression('/^prefix\/\w{16,24}\/foo.txt/'), $content, true)
  400. ->willReturn($size);
  401. $file->expects(self::once())->method('setSize')->with($size)->willReturnSelf();
  402. $file->expects(self::once())->method('setStatus')->with(FileStatusEnum::READY)->willReturnSelf();
  403. $file->expects(self::once())
  404. ->method('setSlug')
  405. ->with(self::matchesRegularExpression('/^prefix\/\w{16,24}\/foo.txt/'))
  406. ->willReturnSelf();
  407. $file->expects(self::once())->method('setCreateDate')->with(self::isInstanceOf(\DateTime::class))->willReturnSelf();
  408. $file->expects(self::once())->method('setCreatedBy')->with(123)->willReturnSelf();
  409. $file->expects(self::never())->method('setUpdateDate');
  410. $file->expects(self::never())->method('setUpdatedBy');
  411. $this->entityManager->expects(self::once())->method('flush');
  412. $returned = $fileStorage->write($file, $content, $author);
  413. $this->assertSame($file, $returned);
  414. }
  415. /**
  416. * @see LocalStorage::write()
  417. */
  418. public function testWriteExistingFile(): void
  419. {
  420. $fileStorage = $this->getMockForMethod('write');
  421. $person = $this->getMockBuilder(Person::class)->getMock();
  422. $author = $this->getMockBuilder(Access::class)->getMock();
  423. $author->method('getId')->willReturn(123);
  424. $key = 'prefix/uid/bar.txt';
  425. $file = $this->getMockBuilder(File::class)->getMock();
  426. $file->method('getName')->willReturn('bar.txt');
  427. $file->method('getOrganization')->willReturn(null);
  428. $file->method('getPerson')->willReturn($person);
  429. $file->method('getIsTemporaryFile')->willReturn(true);
  430. $file->method('getSlug')->willReturn($key);
  431. $file->method('getType')->willReturn(FileTypeEnum::NONE);
  432. $fileStorage->expects(self::never())->method('getPrefix');
  433. $content = '123 Soleil';
  434. $size = strlen($content);
  435. $this->filesystem
  436. ->expects(self::once())
  437. ->method('write')
  438. ->with($key, $content, true)
  439. ->willReturn($size);
  440. $this->filesystem->method('has')->with($key)->willReturn(true);
  441. $file->expects(self::once())->method('setSize')->with($size)->willReturnSelf();
  442. $file->expects(self::once())->method('setStatus')->with(FileStatusEnum::READY)->willReturnSelf();
  443. $file->expects(self::never())->method('setSlug');
  444. $file->expects(self::never())->method('setCreateDate');
  445. $file->expects(self::never())->method('setCreatedBy');
  446. $file->expects(self::once())->method('setUpdateDate')->with(self::isInstanceOf(\DateTime::class))->willReturnSelf();
  447. $file->expects(self::once())->method('setUpdatedBy')->with(123)->willReturnSelf();
  448. $this->entityManager->expects(self::once())->method('flush');
  449. $returned = $fileStorage->write($file, $content, $author);
  450. $this->assertEquals($file, $returned);
  451. }
  452. /**
  453. * @see LocalStorage::write()
  454. */
  455. public function testWriteExistingButMissingFile(): void
  456. {
  457. $fileStorage = $this->getMockForMethod('write');
  458. $person = $this->getMockBuilder(Person::class)->getMock();
  459. $author = $this->getMockBuilder(Access::class)->getMock();
  460. $key = 'prefix/uid/bar.txt';
  461. $file = $this->getMockBuilder(File::class)->getMock();
  462. $file->method('getName')->willReturn('bar.txt');
  463. $file->method('getOrganization')->willReturn(null);
  464. $file->method('getPerson')->willReturn($person);
  465. $file->method('getIsTemporaryFile')->willReturn(true);
  466. $file->method('getSlug')->willReturn($key);
  467. $file->method('getType')->willReturn(FileTypeEnum::NONE);
  468. $this->filesystem->expects(self::never())->method('write');
  469. $this->entityManager->expects(self::never())->method('flush');
  470. $this->filesystem->method('has')->with($key)->willReturn(false);
  471. $this->expectException(\RuntimeException::class);
  472. $this->expectExceptionMessage('The file `'.$key.'` does not exist in the file storage');
  473. $fileStorage->write($file, '12346', $author);
  474. }
  475. /**
  476. * @see LocalStorage::write()
  477. */
  478. public function testWriteWithAccessOwner(): void
  479. {
  480. $fileStorage = $this->getMockForMethod('write');
  481. $access = $this->getMockBuilder(Access::class)->getMock();
  482. $person = $this->getMockBuilder(Person::class)->getMock();
  483. $organization = $this->getMockBuilder(Organization::class)->getMock();
  484. $author = $this->getMockBuilder(Access::class)->getMock();
  485. $author->method('getId')->willReturn(123);
  486. $file = $this->getMockBuilder(File::class)->getMock();
  487. $file->method('getName')->willReturn('bar.txt');
  488. $file->method('getOrganization')->willReturn($organization);
  489. $file->method('getPerson')->willReturn($person);
  490. $file->method('getIsTemporaryFile')->willReturn(true);
  491. $file->method('getSlug')->willReturn(null);
  492. $file->method('getType')->willReturn(FileTypeEnum::NONE);
  493. $this->accessRepository
  494. ->expects(self::once())
  495. ->method('findOneBy')
  496. ->with(['organization' => $organization, 'person' => $person])
  497. ->willReturn($access);
  498. $fileStorage
  499. ->expects(self::once())
  500. ->method('getPrefix')
  501. ->with($access, true, FileTypeEnum::NONE)
  502. ->willReturn('prefix/');
  503. $content = '1';
  504. $this->filesystem->method('write')->willReturn(1);
  505. $fileStorage->write($file, $content, $author);
  506. }
  507. /**
  508. * @see LocalStorage::write()
  509. */
  510. public function testWriteWithNoName(): void
  511. {
  512. $fileStorage = $this->getMockForMethod('write');
  513. $author = $this->getMockBuilder(Access::class)->getMock();
  514. $file = $this->getMockBuilder(File::class)->getMock();
  515. $file->method('getName')->willReturn('');
  516. $this->expectException(\RuntimeException::class);
  517. $this->expectExceptionMessage('File has no filename');
  518. $fileStorage->write($file, '...', $author);
  519. }
  520. /**
  521. * @see LocalStorage::makeFile()
  522. */
  523. public function testMakeFile(): void
  524. {
  525. $fileStorage = $this->getMockForMethod('makeFile');
  526. $organization = $this->getMockBuilder(Organization::class)->getMock();
  527. $author = $this->getMockBuilder(Access::class)->getMock();
  528. $file = $this->getMockBuilder(File::class)->getMock();
  529. $fileStorage
  530. ->expects(self::once())
  531. ->method('prepareFile')
  532. ->with($organization, 'foo.txt', FileTypeEnum::NONE, $author, true, FileVisibilityEnum::ONLY_ORGANIZATION, 'mime/type')
  533. ->willReturn($file);
  534. $fileStorage
  535. ->expects(self::once())
  536. ->method('write')
  537. ->with($file, '...', $author)
  538. ->willReturn($file);
  539. $fileStorage->makeFile(
  540. $organization,
  541. 'foo.txt',
  542. FileTypeEnum::NONE,
  543. '...',
  544. $author,
  545. true,
  546. FileVisibilityEnum::ONLY_ORGANIZATION,
  547. 'mime/type');
  548. }
  549. /**
  550. * @see LocalStorage::softDelete()
  551. */
  552. public function testSoftdelete(): void
  553. {
  554. $fileStorage = $this->getMockForMethod('softDelete');
  555. $author = $this->getMockBuilder(Access::class)->getMock();
  556. $author->method('getId')->willReturn(123);
  557. $file = $this->getMockBuilder(File::class)->getMock();
  558. $file->method('getSlug')->willReturn('key');
  559. $file->expects(self::once())->method('setStatus')->with(FileStatusEnum::DELETED)->willReturnSelf();
  560. $file->expects(self::once())->method('setSize')->with(0)->willReturnSelf();
  561. $file->expects(self::once())->method('setUpdatedBy')->with(123)->willReturnSelf();
  562. $returned = $fileStorage->softDelete($file, $author);
  563. $this->assertEquals($file, $returned);
  564. }
  565. /**
  566. * @see LocalStorage::hardDelete()
  567. */
  568. public function testHardDelete(): void
  569. {
  570. $fileStorage = $this->getMockForMethod('hardDelete');
  571. $file = $this->getMockBuilder(File::class)->getMock();
  572. $file->method('getSlug')->willReturn('key');
  573. $this->filesystem->expects(self::once())->method('delete')->with('key')->willReturn(true);
  574. $fileStorage->hardDelete($file);
  575. }
  576. /**
  577. * @see LocalStorage::hardDelete()
  578. */
  579. public function testHardDeleteFailed(): void
  580. {
  581. $fileStorage = $this->getMockForMethod('hardDelete');
  582. $file = $this->getMockBuilder(File::class)->getMock();
  583. $file->method('getSlug')->willReturn('key');
  584. $this->filesystem->expects(self::once())->method('delete')->with('key')->willReturn(false);
  585. $this->expectException(\RuntimeException::class);
  586. $this->expectExceptionMessage('File `'.$file->getSlug().'` could\'nt be deleted');
  587. $fileStorage->hardDelete($file);
  588. }
  589. public function testDeleteOrganizationFiles(): void
  590. {
  591. $fileStorage = $this->getMockForMethod('deleteOrganizationFiles');
  592. $fileStorage
  593. ->expects(self::exactly(2))
  594. ->method('rrmDir')
  595. ->withConsecutive(
  596. ['organization/123'],
  597. ['temp/organization/123'],
  598. );
  599. $fileStorage->deleteOrganizationFiles(123);
  600. }
  601. public function testDeletePersonFiles(): void
  602. {
  603. $fileStorage = $this->getMockForMethod('deletePersonFiles');
  604. $fileStorage
  605. ->expects(self::exactly(2))
  606. ->method('rrmDir')
  607. ->withConsecutive(
  608. ['person/123'],
  609. ['temp/person/123'],
  610. );
  611. $fileStorage->deletePersonFiles(123);
  612. }
  613. public function testSupport(): void
  614. {
  615. $apiLegacyStorage = $this->getMockForMethod('support');
  616. $file1 = $this->getMockBuilder(File::class)->getMock();
  617. $file1->method('getHost')->willReturn(FileHostEnum::API1);
  618. $file2 = $this->getMockBuilder(File::class)->getMock();
  619. $file2->method('getHost')->willReturn(FileHostEnum::AP2I);
  620. $this->assertFalse($apiLegacyStorage->support($file1));
  621. $this->assertTrue($apiLegacyStorage->support($file2));
  622. }
  623. /**
  624. * @see LocalStorage::getPrefix()
  625. */
  626. public function testGetPrefixAccess(): void
  627. {
  628. $fileStorage = $this->getMockForMethod('getPrefix');
  629. $organization = $this->getMockBuilder(Organization::class)->getMock();
  630. $organization->method('getId')->willReturn(2);
  631. $access = $this->getMockBuilder(Access::class)->getMock();
  632. $access->method('getOrganization')->willReturn($organization);
  633. $access->method('getId')->willReturn(1);
  634. $prefix = $fileStorage->getPrefix($access, false);
  635. $this->assertEquals('organization/2/1', $prefix);
  636. }
  637. /**
  638. * @see LocalStorage::getPrefix()
  639. */
  640. public function testGetPrefixOrganization(): void
  641. {
  642. $fileStorage = $this->getMockForMethod('getPrefix');
  643. $organization = $this->getMockBuilder(Organization::class)->getMock();
  644. $organization->method('getId')->willReturn(1);
  645. $prefix = $fileStorage->getPrefix($organization, false);
  646. $this->assertEquals('organization/1', $prefix);
  647. }
  648. /**
  649. * @see LocalStorage::getPrefix()
  650. */
  651. public function testGetPrefixPerson(): void
  652. {
  653. $fileStorage = $this->getMockForMethod('getPrefix');
  654. $person = $this->getMockBuilder(Person::class)->getMock();
  655. $person->method('getId')->willReturn(1);
  656. $prefix = $fileStorage->getPrefix($person, false);
  657. $this->assertEquals('person/1', $prefix);
  658. }
  659. /**
  660. * @see LocalStorage::getPrefix()
  661. */
  662. public function testGetPrefixTemp(): void
  663. {
  664. $fileStorage = $this->getMockForMethod('getPrefix');
  665. $organization = $this->getMockBuilder(Organization::class)->getMock();
  666. $organization->method('getId')->willReturn(1);
  667. $prefix = $fileStorage->getPrefix($organization, true);
  668. $this->assertEquals('temp/organization/1', $prefix);
  669. }
  670. /**
  671. * @see LocalStorage::getPrefix()
  672. */
  673. public function testGetPrefixWithType(): void
  674. {
  675. $fileStorage = $this->getMockForMethod('getPrefix');
  676. $organization = $this->getMockBuilder(Organization::class)->getMock();
  677. $organization->method('getId')->willReturn(1);
  678. $prefix = $fileStorage->getPrefix($organization, false, FileTypeEnum::LICENCE_CMF);
  679. $this->assertEquals('organization/1/licence_cmf', $prefix);
  680. }
  681. /**
  682. * @see LocalStorage::getOrganizationAndPersonFromOwner()
  683. */
  684. public function testGetOrganizationAndPersonFromOwner(): void
  685. {
  686. $fileStorage = $this->getMockForMethod('getOrganizationAndPersonFromOwner');
  687. $organization = $this->getMockBuilder(Organization::class)->getMock();
  688. $organization->method('getId')->willReturn(2);
  689. $person = $this->getMockBuilder(Person::class)->getMock();
  690. $person->method('getId')->willReturn(1);
  691. $access = $this->getMockBuilder(Access::class)->getMock();
  692. $access->method('getOrganization')->willReturn($organization);
  693. $access->method('getPerson')->willReturn($person);
  694. $access->method('getId')->willReturn(1);
  695. $this->assertEquals([$organization, $person], $fileStorage->getOrganizationAndPersonFromOwner($access));
  696. $this->assertEquals([$organization, null], $fileStorage->getOrganizationAndPersonFromOwner($organization));
  697. $this->assertEquals([null, $person], $fileStorage->getOrganizationAndPersonFromOwner($person));
  698. }
  699. }