LocalStorageTest.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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('getPath')->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('getPath')->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('getPath')->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('getPath')->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('getPath')->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(null, $file->getSlug());
  325. $this->assertEquals(FileTypeEnum::LICENCE_CMF, $file->getType());
  326. $this->assertTrue($file->getIsTemporaryFile());
  327. $this->assertEquals(FileVisibilityEnum::ONLY_ORGANIZATION, $file->getVisibility());
  328. $this->assertEquals('application/pdf', $file->getMimeType());
  329. $this->assertEquals(123, $file->getCreatedBy());
  330. }
  331. /**
  332. * @see LocalStorage::prepareFile()
  333. */
  334. public function testPrepareFileDefaultValues(): void
  335. {
  336. $fileStorage = $this->getMockForMethod('prepareFile');
  337. $owner = $this->getMockBuilder(Person::class)->getMock();
  338. $author = $this->getMockBuilder(Access::class)->getMock();
  339. $fileStorage->method('getOrganizationAndPersonFromOwner')->with($owner)->willReturn([null, $owner]);
  340. $this->entityManager->expects(self::once())->method('persist');
  341. $this->entityManager->expects(self::once())->method('flush');
  342. $this->fileUtils->method('guessMimeTypeFromFilename')->with('file.txt')->willReturn('text/plain');
  343. $file = $fileStorage->prepareFile($owner, 'file.txt', FileTypeEnum::NONE, $author);
  344. $this->assertEquals(null, $file->getOrganization());
  345. $this->assertEquals($owner, $file->getPerson());
  346. $this->assertEquals('file.txt', $file->getName());
  347. $this->assertEquals(FileTypeEnum::NONE, $file->getType());
  348. $this->assertFalse($file->getIsTemporaryFile());
  349. $this->assertEquals(FileVisibilityEnum::NOBODY, $file->getVisibility());
  350. $this->assertEquals('text/plain', $file->getMimeType());
  351. }
  352. /**
  353. * @see LocalStorage::prepareFile()
  354. */
  355. public function testPrepareFileNoFlush(): void
  356. {
  357. $fileStorage = $this->getMockForMethod('prepareFile');
  358. $owner = $this->getMockBuilder(Organization::class)->getMock();
  359. $author = $this->getMockBuilder(Access::class)->getMock();
  360. $fileStorage->method('getOrganizationAndPersonFromOwner')->with($owner)->willReturn([$owner, null]);
  361. $this->entityManager->expects(self::once())->method('persist');
  362. $this->entityManager->expects(self::never())->method('flush');
  363. $this->fileUtils->method('guessMimeTypeFromFilename')->willReturn('text/plain');
  364. $fileStorage->prepareFile(
  365. $owner,
  366. 'file.txt',
  367. FileTypeEnum::NONE,
  368. $author,
  369. false,
  370. FileVisibilityEnum::NOBODY,
  371. 'text/plain',
  372. false
  373. );
  374. }
  375. /**
  376. * @see LocalStorage::write()
  377. */
  378. public function testWriteNewFile(): void
  379. {
  380. $fileStorage = $this->getMockForMethod('write');
  381. $organization = $this->getMockBuilder(Organization::class)->getMock();
  382. $author = $this->getMockBuilder(Access::class)->getMock();
  383. $author->method('getId')->willReturn(123);
  384. $file = $this->getMockBuilder(File::class)->getMock();
  385. $file->method('getName')->willReturn('foo.txt');
  386. $file->method('getOrganization')->willReturn($organization);
  387. $file->method('getPerson')->willReturn(null);
  388. $file->method('getIsTemporaryFile')->willReturn(false);
  389. $file->method('getSlug')->willReturn(null);
  390. $file->method('getType')->willReturn(FileTypeEnum::NONE);
  391. $fileStorage
  392. ->method('getPrefix')
  393. ->with($organization, false, FileTypeEnum::NONE)
  394. ->willReturn('prefix/');
  395. $content = '123456789';
  396. $size = strlen($content);
  397. $this->filesystem
  398. ->expects(self::once())
  399. ->method('write')
  400. ->with(self::matchesRegularExpression('/^prefix\/\w{16,24}\/foo.txt/'), $content, true)
  401. ->willReturn($size);
  402. $file->expects(self::once())->method('setSize')->with($size)->willReturnSelf();
  403. $file->expects(self::once())->method('setStatus')->with(FileStatusEnum::READY)->willReturnSelf();
  404. $file->expects(self::once())
  405. ->method('setSlug')
  406. ->with(self::matchesRegularExpression('/^prefix\/\w{16,24}\/foo.txt/'))
  407. ->willReturnSelf();
  408. $file->expects(self::once())->method('setCreateDate')->with(self::isInstanceOf(\DateTime::class))->willReturnSelf();
  409. $file->expects(self::once())->method('setCreatedBy')->with(123)->willReturnSelf();
  410. $file->expects(self::never())->method('setUpdateDate');
  411. $file->expects(self::never())->method('setUpdatedBy');
  412. $this->entityManager->expects(self::once())->method('flush');
  413. $returned = $fileStorage->write($file, $content, $author);
  414. $this->assertSame($file, $returned);
  415. }
  416. /**
  417. * @see LocalStorage::write()
  418. */
  419. public function testWriteExistingFile(): void
  420. {
  421. $fileStorage = $this->getMockForMethod('write');
  422. $person = $this->getMockBuilder(Person::class)->getMock();
  423. $author = $this->getMockBuilder(Access::class)->getMock();
  424. $author->method('getId')->willReturn(123);
  425. $key = 'prefix/uid/bar.txt';
  426. $file = $this->getMockBuilder(File::class)->getMock();
  427. $file->method('getName')->willReturn('bar.txt');
  428. $file->method('getOrganization')->willReturn(null);
  429. $file->method('getPerson')->willReturn($person);
  430. $file->method('getIsTemporaryFile')->willReturn(true);
  431. $file->method('getSlug')->willReturn($key);
  432. $file->method('getType')->willReturn(FileTypeEnum::NONE);
  433. $fileStorage->expects(self::never())->method('getPrefix');
  434. $content = '123 Soleil';
  435. $size = strlen($content);
  436. $this->filesystem
  437. ->expects(self::once())
  438. ->method('write')
  439. ->with($key, $content, true)
  440. ->willReturn($size);
  441. $this->filesystem->method('has')->with($key)->willReturn(true);
  442. $file->expects(self::once())->method('setSize')->with($size)->willReturnSelf();
  443. $file->expects(self::once())->method('setStatus')->with(FileStatusEnum::READY)->willReturnSelf();
  444. $file->expects(self::never())->method('setSlug');
  445. $file->expects(self::never())->method('setCreateDate');
  446. $file->expects(self::never())->method('setCreatedBy');
  447. $file->expects(self::once())->method('setUpdateDate')->with(self::isInstanceOf(\DateTime::class))->willReturnSelf();
  448. $file->expects(self::once())->method('setUpdatedBy')->with(123)->willReturnSelf();
  449. $this->entityManager->expects(self::once())->method('flush');
  450. $returned = $fileStorage->write($file, $content, $author);
  451. $this->assertEquals($file, $returned);
  452. }
  453. /**
  454. * @see LocalStorage::write()
  455. */
  456. public function testWriteExistingButMissingFile(): void
  457. {
  458. $fileStorage = $this->getMockForMethod('write');
  459. $person = $this->getMockBuilder(Person::class)->getMock();
  460. $author = $this->getMockBuilder(Access::class)->getMock();
  461. $key = 'prefix/uid/bar.txt';
  462. $file = $this->getMockBuilder(File::class)->getMock();
  463. $file->method('getName')->willReturn('bar.txt');
  464. $file->method('getOrganization')->willReturn(null);
  465. $file->method('getPerson')->willReturn($person);
  466. $file->method('getIsTemporaryFile')->willReturn(true);
  467. $file->method('getSlug')->willReturn($key);
  468. $file->method('getType')->willReturn(FileTypeEnum::NONE);
  469. $this->filesystem->expects(self::never())->method('write');
  470. $this->entityManager->expects(self::never())->method('flush');
  471. $this->filesystem->method('has')->with($key)->willReturn(false);
  472. $this->expectException(\RuntimeException::class);
  473. $this->expectExceptionMessage('The file `'.$key.'` does not exist in the file storage');
  474. $fileStorage->write($file, '12346', $author);
  475. }
  476. /**
  477. * @see LocalStorage::write()
  478. */
  479. public function testWriteWithAccessOwner(): void
  480. {
  481. $fileStorage = $this->getMockForMethod('write');
  482. $access = $this->getMockBuilder(Access::class)->getMock();
  483. $person = $this->getMockBuilder(Person::class)->getMock();
  484. $organization = $this->getMockBuilder(Organization::class)->getMock();
  485. $author = $this->getMockBuilder(Access::class)->getMock();
  486. $author->method('getId')->willReturn(123);
  487. $file = $this->getMockBuilder(File::class)->getMock();
  488. $file->method('getName')->willReturn('bar.txt');
  489. $file->method('getOrganization')->willReturn($organization);
  490. $file->method('getPerson')->willReturn($person);
  491. $file->method('getIsTemporaryFile')->willReturn(true);
  492. $file->method('getSlug')->willReturn(null);
  493. $file->method('getType')->willReturn(FileTypeEnum::NONE);
  494. $this->accessRepository
  495. ->expects(self::once())
  496. ->method('findOneBy')
  497. ->with(['organization' => $organization, 'person' => $person])
  498. ->willReturn($access);
  499. $fileStorage
  500. ->expects(self::once())
  501. ->method('getPrefix')
  502. ->with($access, true, FileTypeEnum::NONE)
  503. ->willReturn('prefix/');
  504. $content = '1';
  505. $this->filesystem->method('write')->willReturn(1);
  506. $fileStorage->write($file, $content, $author);
  507. }
  508. /**
  509. * @see LocalStorage::write()
  510. */
  511. public function testWriteWithNoName(): void
  512. {
  513. $fileStorage = $this->getMockForMethod('write');
  514. $author = $this->getMockBuilder(Access::class)->getMock();
  515. $file = $this->getMockBuilder(File::class)->getMock();
  516. $file->method('getName')->willReturn('');
  517. $this->expectException(\RuntimeException::class);
  518. $this->expectExceptionMessage('File has no filename');
  519. $fileStorage->write($file, '...', $author);
  520. }
  521. /**
  522. * @see LocalStorage::makeFile()
  523. */
  524. public function testMakeFile(): void
  525. {
  526. $fileStorage = $this->getMockForMethod('makeFile');
  527. $organization = $this->getMockBuilder(Organization::class)->getMock();
  528. $author = $this->getMockBuilder(Access::class)->getMock();
  529. $file = $this->getMockBuilder(File::class)->getMock();
  530. $fileStorage
  531. ->expects(self::once())
  532. ->method('prepareFile')
  533. ->with($organization, 'foo.txt', FileTypeEnum::NONE, $author, true, FileVisibilityEnum::ONLY_ORGANIZATION, 'mime/type')
  534. ->willReturn($file);
  535. $fileStorage
  536. ->expects(self::once())
  537. ->method('write')
  538. ->with($file, '...', $author)
  539. ->willReturn($file);
  540. $fileStorage->makeFile(
  541. $organization,
  542. 'foo.txt',
  543. FileTypeEnum::NONE,
  544. '...',
  545. $author,
  546. true,
  547. FileVisibilityEnum::ONLY_ORGANIZATION,
  548. 'mime/type');
  549. }
  550. /**
  551. * @see LocalStorage::softDelete()
  552. */
  553. public function testSoftdelete(): void
  554. {
  555. $fileStorage = $this->getMockForMethod('softDelete');
  556. $author = $this->getMockBuilder(Access::class)->getMock();
  557. $author->method('getId')->willReturn(123);
  558. $file = $this->getMockBuilder(File::class)->getMock();
  559. $file->method('getSlug')->willReturn('key');
  560. $file->expects(self::once())->method('setStatus')->with(FileStatusEnum::DELETED)->willReturnSelf();
  561. $file->expects(self::once())->method('setSize')->with(0)->willReturnSelf();
  562. $file->expects(self::once())->method('setUpdatedBy')->with(123)->willReturnSelf();
  563. $returned = $fileStorage->softDelete($file, $author);
  564. $this->assertEquals($file, $returned);
  565. }
  566. /**
  567. * @see LocalStorage::hardDelete()
  568. */
  569. public function testHardDelete(): void
  570. {
  571. $fileStorage = $this->getMockForMethod('hardDelete');
  572. $file = $this->getMockBuilder(File::class)->getMock();
  573. $file->method('getSlug')->willReturn('key');
  574. $this->filesystem->expects(self::once())->method('delete')->with('key')->willReturn(true);
  575. $fileStorage->hardDelete($file);
  576. }
  577. /**
  578. * @see LocalStorage::hardDelete()
  579. */
  580. public function testHardDeleteFailed(): void
  581. {
  582. $fileStorage = $this->getMockForMethod('hardDelete');
  583. $file = $this->getMockBuilder(File::class)->getMock();
  584. $file->method('getSlug')->willReturn('key');
  585. $this->filesystem->expects(self::once())->method('delete')->with('key')->willReturn(false);
  586. $this->expectException(\RuntimeException::class);
  587. $this->expectExceptionMessage('File `'.$file->getSlug().'` could\'nt be deleted');
  588. $fileStorage->hardDelete($file);
  589. }
  590. public function testDeleteOrganizationFiles(): void
  591. {
  592. $fileStorage = $this->getMockForMethod('deleteOrganizationFiles');
  593. $fileStorage
  594. ->expects(self::exactly(2))
  595. ->method('rrmDir')
  596. ->withConsecutive(
  597. ['organization/123'],
  598. ['temp/organization/123'],
  599. );
  600. $fileStorage->deleteOrganizationFiles(123);
  601. }
  602. public function testDeletePersonFiles(): void
  603. {
  604. $fileStorage = $this->getMockForMethod('deletePersonFiles');
  605. $fileStorage
  606. ->expects(self::exactly(2))
  607. ->method('rrmDir')
  608. ->withConsecutive(
  609. ['person/123'],
  610. ['temp/person/123'],
  611. );
  612. $fileStorage->deletePersonFiles(123);
  613. }
  614. public function testSupport(): void
  615. {
  616. $apiLegacyStorage = $this->getMockForMethod('support');
  617. $file1 = $this->getMockBuilder(File::class)->getMock();
  618. $file1->method('getHost')->willReturn(FileHostEnum::API1);
  619. $file2 = $this->getMockBuilder(File::class)->getMock();
  620. $file2->method('getHost')->willReturn(FileHostEnum::AP2I);
  621. $this->assertFalse($apiLegacyStorage->support($file1));
  622. $this->assertTrue($apiLegacyStorage->support($file2));
  623. }
  624. /**
  625. * @see LocalStorage::getPrefix()
  626. */
  627. public function testGetPrefixAccess(): void
  628. {
  629. $fileStorage = $this->getMockForMethod('getPrefix');
  630. $organization = $this->getMockBuilder(Organization::class)->getMock();
  631. $organization->method('getId')->willReturn(2);
  632. $access = $this->getMockBuilder(Access::class)->getMock();
  633. $access->method('getOrganization')->willReturn($organization);
  634. $access->method('getId')->willReturn(1);
  635. $prefix = $fileStorage->getPrefix($access, false);
  636. $this->assertEquals('organization/2/1', $prefix);
  637. }
  638. /**
  639. * @see LocalStorage::getPrefix()
  640. */
  641. public function testGetPrefixOrganization(): void
  642. {
  643. $fileStorage = $this->getMockForMethod('getPrefix');
  644. $organization = $this->getMockBuilder(Organization::class)->getMock();
  645. $organization->method('getId')->willReturn(1);
  646. $prefix = $fileStorage->getPrefix($organization, false);
  647. $this->assertEquals('organization/1', $prefix);
  648. }
  649. /**
  650. * @see LocalStorage::getPrefix()
  651. */
  652. public function testGetPrefixPerson(): void
  653. {
  654. $fileStorage = $this->getMockForMethod('getPrefix');
  655. $person = $this->getMockBuilder(Person::class)->getMock();
  656. $person->method('getId')->willReturn(1);
  657. $prefix = $fileStorage->getPrefix($person, false);
  658. $this->assertEquals('person/1', $prefix);
  659. }
  660. /**
  661. * @see LocalStorage::getPrefix()
  662. */
  663. public function testGetPrefixTemp(): void
  664. {
  665. $fileStorage = $this->getMockForMethod('getPrefix');
  666. $organization = $this->getMockBuilder(Organization::class)->getMock();
  667. $organization->method('getId')->willReturn(1);
  668. $prefix = $fileStorage->getPrefix($organization, true);
  669. $this->assertEquals('temp/organization/1', $prefix);
  670. }
  671. /**
  672. * @see LocalStorage::getPrefix()
  673. */
  674. public function testGetPrefixWithType(): void
  675. {
  676. $fileStorage = $this->getMockForMethod('getPrefix');
  677. $organization = $this->getMockBuilder(Organization::class)->getMock();
  678. $organization->method('getId')->willReturn(1);
  679. $prefix = $fileStorage->getPrefix($organization, false, FileTypeEnum::LICENCE_CMF);
  680. $this->assertEquals('organization/1/licence_cmf', $prefix);
  681. }
  682. /**
  683. * @see LocalStorage::getOrganizationAndPersonFromOwner()
  684. */
  685. public function testGetOrganizationAndPersonFromOwner(): void
  686. {
  687. $fileStorage = $this->getMockForMethod('getOrganizationAndPersonFromOwner');
  688. $organization = $this->getMockBuilder(Organization::class)->getMock();
  689. $organization->method('getId')->willReturn(2);
  690. $person = $this->getMockBuilder(Person::class)->getMock();
  691. $person->method('getId')->willReturn(1);
  692. $access = $this->getMockBuilder(Access::class)->getMock();
  693. $access->method('getOrganization')->willReturn($organization);
  694. $access->method('getPerson')->willReturn($person);
  695. $access->method('getId')->willReturn(1);
  696. $this->assertEquals([$organization, $person], $fileStorage->getOrganizationAndPersonFromOwner($access));
  697. $this->assertEquals([$organization, null], $fileStorage->getOrganizationAndPersonFromOwner($organization));
  698. $this->assertEquals([null, $person], $fileStorage->getOrganizationAndPersonFromOwner($person));
  699. }
  700. }