LocalStorageTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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\FileStatusEnum;
  8. use App\Enum\Core\FileTypeEnum;
  9. use App\Repository\Access\AccessRepository;
  10. use App\Service\File\Storage\LocalStorage;
  11. use App\Service\File\Utils\FileUtils;
  12. use DateTime;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Gaufrette\Filesystem;
  15. use JetBrains\PhpStorm\Pure;
  16. use Knp\Bundle\GaufretteBundle\FilesystemMap;
  17. use PHPUnit\Framework\TestCase;
  18. use RuntimeException;
  19. class TestableLocalStorage extends LocalStorage {
  20. public const FS_KEY = parent::FS_KEY;
  21. public function getPrefix(mixed $owner, bool $isTemporary, string $type = null): string {
  22. return parent::getPrefix($owner, $isTemporary, $type);
  23. }
  24. #[Pure] public function getOrganizationAndPersonFromOwner(mixed $owner): array {
  25. return parent::getOrganizationAndPersonFromOwner($owner);
  26. }
  27. }
  28. class LocalStorageTest extends TestCase
  29. {
  30. private FilesystemMap $filesystemMap;
  31. private EntityManagerInterface $entityManager;
  32. private AccessRepository $accessRepository;
  33. private Filesystem $filesystem;
  34. private FileUtils $imageUtils;
  35. public function setUp(): void
  36. {
  37. $this->filesystemMap = $this->getMockBuilder(FilesystemMap::class)->disableOriginalConstructor()->getMock();
  38. $this->entityManager = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
  39. $this->accessRepository = $this->getMockBuilder(AccessRepository::class)->disableOriginalConstructor()->getMock();
  40. $this->imageUtils = $this->getMockBuilder(FileUtils::class)->disableOriginalConstructor()->getMock();
  41. $this->filesystem = $this->getMockBuilder(Filesystem::class)->disableOriginalConstructor()->getMock();
  42. $this->filesystemMap->method('get')->with(TestableLocalStorage::FS_KEY)->willReturn($this->filesystem);
  43. }
  44. /**
  45. * @see LocalStorage::exists()
  46. */
  47. public function testExists(): void {
  48. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  49. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
  50. ->setMethodsExcept(['exists'])
  51. ->getMock();
  52. $file = $this->getMockBuilder(File::class)->getMock();
  53. $file->method('getSlug')->willReturn('foo');
  54. $this->filesystem->expects(self::once())->method('has')->with('foo')->willReturn(true);
  55. $this->assertTrue($fileStorage->exists($file));
  56. }
  57. /**
  58. * @see LocalStorage::exists()
  59. */
  60. public function testExistsInexistant(): void {
  61. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  62. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
  63. ->setMethodsExcept(['exists'])
  64. ->getMock();
  65. $file = $this->getMockBuilder(File::class)->getMock();
  66. $file->method('getSlug')->willReturn('foo');
  67. $this->filesystem->expects(self::once())->method('has')->with('foo')->willReturn(false);
  68. $this->assertFalse($fileStorage->exists($file));
  69. }
  70. /**
  71. * @see LocalStorage::listByOwner()
  72. */
  73. public function testListByOwner(): void
  74. {
  75. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  76. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
  77. ->setMethodsExcept(['listByOwner'])
  78. ->getMock();
  79. $owner = $this->getMockBuilder(Organization::class)->getMock();
  80. $fileStorage->method('getPrefix')->with($owner, false, FileTypeEnum::LICENCE_CMF()->getValue())->willReturn('foo');
  81. $this->filesystem->method('listKeys')->with('foo')->willReturn(['foo/a.txt', 'foo/b.pdf']);
  82. $this->assertEquals(
  83. ['foo/a.txt', 'foo/b.pdf'],
  84. $fileStorage->listByOwner($owner, FileTypeEnum::LICENCE_CMF())
  85. );
  86. }
  87. /**
  88. * @see LocalStorage::read()
  89. */
  90. public function testReadPdf(): void
  91. {
  92. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  93. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
  94. ->setMethodsExcept(['read'])
  95. ->getMock();
  96. $file = $this->getMockBuilder(File::class)->getMock();
  97. $file->method('getSlug')->willReturn('foo');
  98. $file->method('getMimeType')->willReturn('application/pdf');
  99. $this->filesystem->method('read')->with('foo')->willReturn('12345679');
  100. $this->assertEquals(
  101. '12345679',
  102. $fileStorage->read($file)
  103. );
  104. }
  105. /**
  106. * @see LocalStorage::read()
  107. */
  108. public function testReadImage(): void
  109. {
  110. $file = $this->getMockBuilder(File::class)->getMock();
  111. $file->method('getSlug')->willReturn('foo');
  112. $file->method('getMimeType')->willReturn('image/jpeg');
  113. $imageUtils = $this->imageUtils;
  114. $imageUtils->method('formatImage')->with($file)->willReturn('12345679');
  115. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  116. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $imageUtils])
  117. ->setMethodsExcept(['read'])
  118. ->getMock();
  119. $this->filesystem->method('read')->with('foo')->willReturn('12345679');
  120. $this->assertEquals(
  121. '12345679',
  122. $fileStorage->read($file)
  123. );
  124. }
  125. /**
  126. * @see LocalStorage::prepareFile()
  127. */
  128. public function testPrepareFile(): void
  129. {
  130. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  131. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
  132. ->setMethodsExcept(['prepareFile'])
  133. ->getMock();
  134. $owner = $this->getMockBuilder(Organization::class)->getMock();
  135. $author = $this->getMockBuilder(Access::class)->getMock();
  136. $author->method('getId')->willReturn(123);
  137. $fileStorage->method('getOrganizationAndPersonFromOwner')->with($owner)->willReturn([$owner, null]);
  138. $this->entityManager->expects(self::once())->method('persist');
  139. $this->entityManager->expects(self::once())->method('flush');
  140. $file = $fileStorage->prepareFile(
  141. $owner,
  142. 'file.ext',
  143. FileTypeEnum::LICENCE_CMF(),
  144. $author,
  145. true,
  146. 'ONLY_ORGANIZATION',
  147. 'application/pdf'
  148. );
  149. $this->assertEquals($owner, $file->getOrganization());
  150. $this->assertEquals(null, $file->getPerson());
  151. $this->assertEquals('file.ext', $file->getName());
  152. $this->assertEquals(null, $file->getSlug());
  153. $this->assertEquals(FileTypeEnum::LICENCE_CMF()->getValue(), $file->getType());
  154. $this->assertTrue($file->getIsTemporaryFile());
  155. $this->assertEquals('ONLY_ORGANIZATION', $file->getVisibility());
  156. $this->assertEquals('application/pdf', $file->getMimeType());
  157. $this->assertEquals(123, $file->getCreatedBy());
  158. }
  159. /**
  160. * @see LocalStorage::prepareFile()
  161. */
  162. public function testPrepareFileDefaultValues(): void
  163. {
  164. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  165. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
  166. ->setMethodsExcept(['prepareFile'])
  167. ->getMock();
  168. $owner = $this->getMockBuilder(Person::class)->getMock();
  169. $author = $this->getMockBuilder(Access::class)->getMock();
  170. $fileStorage->method('getOrganizationAndPersonFromOwner')->with($owner)->willReturn([null, $owner]);
  171. $this->entityManager->expects(self::once())->method('persist');
  172. $this->entityManager->expects(self::once())->method('flush');
  173. $file = $fileStorage->prepareFile($owner, 'file.txt', FileTypeEnum::NONE(), $author);
  174. $this->assertEquals(null, $file->getOrganization());
  175. $this->assertEquals($owner, $file->getPerson());
  176. $this->assertEquals('file.txt', $file->getName());
  177. $this->assertEquals(FileTypeEnum::NONE()->getValue(), $file->getType());
  178. $this->assertFalse($file->getIsTemporaryFile());
  179. $this->assertEquals('NOBODY', $file->getVisibility());
  180. $this->assertEquals('text/plain', $file->getMimeType());
  181. }
  182. /**
  183. * @see LocalStorage::prepareFile()
  184. */
  185. public function testPrepareFileNoFlush(): void
  186. {
  187. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  188. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
  189. ->setMethodsExcept(['prepareFile'])
  190. ->getMock();
  191. $owner = $this->getMockBuilder(Organization::class)->getMock();
  192. $author = $this->getMockBuilder(Access::class)->getMock();
  193. $fileStorage->method('getOrganizationAndPersonFromOwner')->with($owner)->willReturn([$owner, null]);
  194. $this->entityManager->expects(self::once())->method('persist');
  195. $this->entityManager->expects(self::never())->method('flush');
  196. $fileStorage->prepareFile(
  197. $owner,
  198. 'file.txt',
  199. FileTypeEnum::NONE(),
  200. $author,
  201. false,
  202. 'NOBODY',
  203. null,
  204. false
  205. );
  206. }
  207. /**
  208. * @see LocalStorage::writeFile()
  209. */
  210. public function testWriteFileNewFile(): void {
  211. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  212. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
  213. ->setMethodsExcept(['writeFile'])
  214. ->getMock();
  215. $organization = $this->getMockBuilder(Organization::class)->getMock();
  216. $author = $this->getMockBuilder(Access::class)->getMock();
  217. $author->method('getId')->willReturn(123);
  218. $file = $this->getMockBuilder(File::class)->getMock();
  219. $file->method('getName')->willReturn('foo.txt');
  220. $file->method('getOrganization')->willReturn($organization);
  221. $file->method('getPerson')->willReturn(null);
  222. $file->method('getIsTemporaryFile')->willReturn(false);
  223. $file->method('getSlug')->willReturn(null);
  224. $file->method('getType')->willReturn(FileTypeEnum::NONE()->getValue());
  225. $fileStorage
  226. ->method('getPrefix')
  227. ->with($organization, false, FileTypeEnum::NONE()->getValue())
  228. ->willReturn('prefix/');
  229. $content = '123456789';
  230. $size = strlen($content);
  231. $this->filesystem
  232. ->expects(self::once())
  233. ->method('write')
  234. ->with(self::matchesRegularExpression('/^prefix\/\w{16,24}\/foo.txt/'), $content, true)
  235. ->willReturn($size);
  236. $file->expects(self::once())->method('setSize')->with($size)->willReturnSelf();
  237. $file->expects(self::once())->method('setStatus')->with(FileStatusEnum::READY()->getValue())->willReturnSelf();
  238. $file->expects(self::once())
  239. ->method('setSlug')
  240. ->with(self::matchesRegularExpression('/^prefix\/\w{16,24}\/foo.txt/'))
  241. ->willReturnSelf();
  242. $file->expects(self::once())->method('setCreateDate')->with(self::isInstanceOf(DateTime::class))->willReturnSelf();
  243. $file->expects(self::once())->method('setCreatedBy')->with(123)->willReturnSelf();
  244. $file->expects(self::never())->method('setUpdateDate');
  245. $file->expects(self::never())->method('setUpdatedBy');
  246. $this->entityManager->expects(self::once())->method('flush');
  247. $returned = $fileStorage->writeFile($file, $content, $author);
  248. $this->assertEquals($file, $returned);
  249. }
  250. /**
  251. * @see LocalStorage::writeFile()
  252. */
  253. public function testWriteFileExistingFile(): void {
  254. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  255. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
  256. ->setMethodsExcept(['writeFile'])
  257. ->getMock();
  258. $person = $this->getMockBuilder(Person::class)->getMock();
  259. $author = $this->getMockBuilder(Access::class)->getMock();
  260. $author->method('getId')->willReturn(123);
  261. $key = 'prefix/uid/bar.txt';
  262. $file = $this->getMockBuilder(File::class)->getMock();
  263. $file->method('getName')->willReturn('bar.txt');
  264. $file->method('getOrganization')->willReturn(null);
  265. $file->method('getPerson')->willReturn($person);
  266. $file->method('getIsTemporaryFile')->willReturn(true);
  267. $file->method('getSlug')->willReturn($key);
  268. $file->method('getType')->willReturn(FileTypeEnum::NONE()->getValue());
  269. $fileStorage->expects(self::never())->method('getPrefix');
  270. $content = '123 Soleil';
  271. $size = strlen($content);
  272. $this->filesystem
  273. ->expects(self::once())
  274. ->method('write')
  275. ->with($key, $content, true)
  276. ->willReturn($size);
  277. $this->filesystem->method('has')->with($key)->willReturn(true);
  278. $file->expects(self::once())->method('setSize')->with($size)->willReturnSelf();
  279. $file->expects(self::once())->method('setStatus')->with(FileStatusEnum::READY()->getValue())->willReturnSelf();
  280. $file->expects(self::never())->method('setSlug');
  281. $file->expects(self::never())->method('setCreateDate');
  282. $file->expects(self::never())->method('setCreatedBy');
  283. $file->expects(self::once())->method('setUpdateDate')->with(self::isInstanceOf(DateTime::class))->willReturnSelf();
  284. $file->expects(self::once())->method('setUpdatedBy')->with(123)->willReturnSelf();
  285. $this->entityManager->expects(self::once())->method('flush');
  286. $returned = $fileStorage->writeFile($file, $content, $author);
  287. $this->assertEquals($file, $returned);
  288. }
  289. /**
  290. * @see LocalStorage::writeFile()
  291. */
  292. public function testWriteFileExistingButMissingFile(): void
  293. {
  294. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  295. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
  296. ->setMethodsExcept(['writeFile'])
  297. ->getMock();
  298. $person = $this->getMockBuilder(Person::class)->getMock();
  299. $author = $this->getMockBuilder(Access::class)->getMock();
  300. $key = 'prefix/uid/bar.txt';
  301. $file = $this->getMockBuilder(File::class)->getMock();
  302. $file->method('getName')->willReturn('bar.txt');
  303. $file->method('getOrganization')->willReturn(null);
  304. $file->method('getPerson')->willReturn($person);
  305. $file->method('getIsTemporaryFile')->willReturn(true);
  306. $file->method('getSlug')->willReturn($key);
  307. $file->method('getType')->willReturn(FileTypeEnum::NONE()->getValue());
  308. $this->filesystem->expects(self::never())->method('write');
  309. $this->entityManager->expects(self::never())->method('flush');
  310. $this->filesystem->method('has')->with($key)->willReturn(false);
  311. $this->expectException(RuntimeException::class);
  312. $this->expectExceptionMessage('The file `' . $key . '` does not exist in the file storage');
  313. $fileStorage->writeFile($file, '12346', $author);
  314. }
  315. /**
  316. * @see LocalStorage::writeFile()
  317. */
  318. public function testWriteFileWithAccessOwner(): void {
  319. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  320. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
  321. ->setMethodsExcept(['writeFile'])
  322. ->getMock();
  323. $access = $this->getMockBuilder(Access::class)->getMock();
  324. $person = $this->getMockBuilder(Person::class)->getMock();
  325. $organization = $this->getMockBuilder(Organization::class)->getMock();
  326. $author = $this->getMockBuilder(Access::class)->getMock();
  327. $author->method('getId')->willReturn(123);
  328. $file = $this->getMockBuilder(File::class)->getMock();
  329. $file->method('getName')->willReturn('bar.txt');
  330. $file->method('getOrganization')->willReturn($organization);
  331. $file->method('getPerson')->willReturn($person);
  332. $file->method('getIsTemporaryFile')->willReturn(true);
  333. $file->method('getSlug')->willReturn(null);
  334. $file->method('getType')->willReturn(FileTypeEnum::NONE()->getValue());
  335. $this->accessRepository
  336. ->expects(self::once())
  337. ->method('findOneBy')
  338. ->with(['organization' => $organization, 'person' => $person])
  339. ->willReturn($access);
  340. $fileStorage
  341. ->expects(self::once())
  342. ->method('getPrefix')
  343. ->with($access, true, FileTypeEnum::NONE()->getValue())
  344. ->willReturn('prefix/');
  345. $content = '1';
  346. $this->filesystem->method('write')->willReturn(1);
  347. $fileStorage->writeFile($file, $content, $author);
  348. }
  349. /**
  350. * @see LocalStorage::writeFile()
  351. */
  352. public function testWriteFileWithNoName(): void
  353. {
  354. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  355. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
  356. ->setMethodsExcept(['writeFile'])
  357. ->getMock();
  358. $author = $this->getMockBuilder(Access::class)->getMock();
  359. $file = $this->getMockBuilder(File::class)->getMock();
  360. $file->method('getName')->willReturn('');
  361. $this->expectException(RuntimeException::class);
  362. $this->expectExceptionMessage('File has no filename');
  363. $fileStorage->writeFile($file, '...', $author);
  364. }
  365. /**
  366. * @see LocalStorage::makeFile()
  367. */
  368. public function testMakeFile(): void {
  369. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  370. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
  371. ->setMethodsExcept(['makeFile'])
  372. ->getMock();
  373. $organization = $this->getMockBuilder(Organization::class)->getMock();
  374. $author = $this->getMockBuilder(Access::class)->getMock();
  375. $file = $this->getMockBuilder(File::class)->getMock();
  376. $fileStorage
  377. ->expects(self::once())
  378. ->method('prepareFile')
  379. ->with($organization, 'foo.txt', FileTypeEnum::NONE(), $author, true, 'ONLY_ORGANIZATION', 'mime/type')
  380. ->willReturn($file);
  381. $fileStorage
  382. ->expects(self::once())
  383. ->method('writeFile')
  384. ->with($file, '...', $author)
  385. ->willReturn($file);
  386. $fileStorage->makeFile(
  387. $organization,
  388. 'foo.txt',
  389. FileTypeEnum::NONE(),
  390. '...',
  391. $author,
  392. true,
  393. 'ONLY_ORGANIZATION',
  394. 'mime/type');
  395. }
  396. /**
  397. * @see LocalStorage::softDelete()
  398. */
  399. public function testSoftdelete(): void {
  400. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  401. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
  402. ->setMethodsExcept(['softDelete'])
  403. ->getMock();
  404. $author = $this->getMockBuilder(Access::class)->getMock();
  405. $author->method('getId')->willReturn(123);
  406. $file = $this->getMockBuilder(File::class)->getMock();
  407. $file->method('getSlug')->willReturn('key');
  408. $file->expects(self::once())->method('setStatus')->with(FileStatusEnum::DELETED()->getValue())->willReturnSelf();
  409. $file->expects(self::once())->method('setSize')->with(0)->willReturnSelf();
  410. $file->expects(self::once())->method('setUpdatedBy')->with(123)->willReturnSelf();
  411. $returned = $fileStorage->softDelete($file, $author);
  412. $this->assertEquals($file, $returned);
  413. }
  414. /**
  415. * @see LocalStorage::hardDelete()
  416. */
  417. public function testHardDelete(): void {
  418. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  419. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
  420. ->setMethodsExcept(['hardDelete'])
  421. ->getMock();
  422. $file = $this->getMockBuilder(File::class)->getMock();
  423. $file->method('getSlug')->willReturn('key');
  424. $this->filesystem->expects(self::once())->method('delete')->with('key')->willReturn(true);
  425. $fileStorage->hardDelete($file);
  426. }
  427. /**
  428. * @see LocalStorage::hardDelete()
  429. */
  430. public function testHardDeleteFailed(): void {
  431. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  432. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
  433. ->setMethodsExcept(['hardDelete'])
  434. ->getMock();
  435. $file = $this->getMockBuilder(File::class)->getMock();
  436. $file->method('getSlug')->willReturn('key');
  437. $this->filesystem->expects(self::once())->method('delete')->with('key')->willReturn(false);
  438. $this->expectException(RuntimeException::class);
  439. $this->expectExceptionMessage('File `' . $file->getSlug() . '` could\'nt be deleted');
  440. $fileStorage->hardDelete($file);
  441. }
  442. /**
  443. * @see LocalStorage::getPrefix()
  444. */
  445. public function testGetPrefixAccess(): void {
  446. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  447. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
  448. ->setMethodsExcept(['getPrefix'])
  449. ->getMock();
  450. $organization = $this->getMockBuilder(Organization::class)->getMock();
  451. $organization->method('getId')->willReturn(2);
  452. $access = $this->getMockBuilder(Access::class)->getMock();
  453. $access->method('getOrganization')->willReturn($organization);
  454. $access->method('getId')->willReturn(1);
  455. $prefix = $fileStorage->getPrefix($access, false);
  456. $this->assertEquals('organization/2/1', $prefix);
  457. }
  458. /**
  459. * @see LocalStorage::getPrefix()
  460. */
  461. public function testGetPrefixOrganization(): void {
  462. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  463. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
  464. ->setMethodsExcept(['getPrefix'])
  465. ->getMock();
  466. $organization = $this->getMockBuilder(Organization::class)->getMock();
  467. $organization->method('getId')->willReturn(1);
  468. $prefix = $fileStorage->getPrefix($organization, false);
  469. $this->assertEquals('organization/1', $prefix);
  470. }
  471. /**
  472. * @see LocalStorage::getPrefix()
  473. */
  474. public function testGetPrefixPerson(): void {
  475. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  476. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
  477. ->setMethodsExcept(['getPrefix'])
  478. ->getMock();
  479. $person = $this->getMockBuilder(Person::class)->getMock();
  480. $person->method('getId')->willReturn(1);
  481. $prefix = $fileStorage->getPrefix($person, false);
  482. $this->assertEquals('person/1', $prefix);
  483. }
  484. /**
  485. * @see LocalStorage::getPrefix()
  486. */
  487. public function testGetPrefixTemp(): void {
  488. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  489. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
  490. ->setMethodsExcept(['getPrefix'])
  491. ->getMock();
  492. $organization = $this->getMockBuilder(Organization::class)->getMock();
  493. $organization->method('getId')->willReturn(1);
  494. $prefix = $fileStorage->getPrefix($organization, true);
  495. $this->assertEquals('temp/organization/1', $prefix);
  496. }
  497. /**
  498. * @see LocalStorage::getPrefix()
  499. */
  500. public function testGetPrefixWithType(): void {
  501. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  502. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
  503. ->setMethodsExcept(['getPrefix'])
  504. ->getMock();
  505. $organization = $this->getMockBuilder(Organization::class)->getMock();
  506. $organization->method('getId')->willReturn(1);
  507. $prefix = $fileStorage->getPrefix($organization, false, FileTypeEnum::LICENCE_CMF()->getValue());
  508. $this->assertEquals('organization/1/licence_cmf', $prefix);
  509. }
  510. /**
  511. * @see LocalStorage::getOrganizationAndPersonFromOwner()
  512. */
  513. public function testGetOrganizationAndPersonFromOwner(): void {
  514. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  515. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
  516. ->setMethodsExcept(['getOrganizationAndPersonFromOwner'])
  517. ->getMock();
  518. $organization = $this->getMockBuilder(Organization::class)->getMock();
  519. $organization->method('getId')->willReturn(2);
  520. $person = $this->getMockBuilder(Person::class)->getMock();
  521. $person->method('getId')->willReturn(1);
  522. $access = $this->getMockBuilder(Access::class)->getMock();
  523. $access->method('getOrganization')->willReturn($organization);
  524. $access->method('getPerson')->willReturn($person);
  525. $access->method('getId')->willReturn(1);
  526. $this->assertEquals([$organization, $person], $fileStorage->getOrganizationAndPersonFromOwner($access));
  527. $this->assertEquals([$organization, null], $fileStorage->getOrganizationAndPersonFromOwner($organization));
  528. $this->assertEquals([null, $person], $fileStorage->getOrganizationAndPersonFromOwner($person));
  529. }
  530. }