LocalStorageTest.php 30 KB

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