LocalStorageTest.php 30 KB

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