LocalStorageTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. <?php
  2. namespace App\Tests\Unit\Service\File\Storage;
  3. use ApiPlatform\Core\Api\IriConverterInterface;
  4. use App\Entity\Access\Access;
  5. use App\Entity\Core\File;
  6. use App\Entity\Organization\Organization;
  7. use App\Entity\Person\Person;
  8. use App\Enum\Core\FileStatusEnum;
  9. use App\Enum\Core\FileTypeEnum;
  10. use App\Repository\Access\AccessRepository;
  11. use App\Service\File\Storage\LocalStorage;
  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 IriConverterInterface $iriConverter;
  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->iriConverter = $this->getMockBuilder(IriConverterInterface::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->iriConverter])
  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->iriConverter])
  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->iriConverter])
  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 testRead(): void
  91. {
  92. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  93. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
  94. ->setMethodsExcept(['read'])
  95. ->getMock();
  96. $file = $this->getMockBuilder(File::class)->getMock();
  97. $file->method('getSlug')->willReturn('foo');
  98. $this->filesystem->method('read')->with('foo')->willReturn('12345679');
  99. $this->assertEquals(
  100. '12345679',
  101. $fileStorage->read($file)
  102. );
  103. }
  104. /**
  105. * @see LocalStorage::prepareFile()
  106. */
  107. public function testPrepareFile(): void
  108. {
  109. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  110. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
  111. ->setMethodsExcept(['prepareFile'])
  112. ->getMock();
  113. $owner = $this->getMockBuilder(Organization::class)->getMock();
  114. $author = $this->getMockBuilder(Access::class)->getMock();
  115. $author->method('getId')->willReturn(123);
  116. $fileStorage->method('getOrganizationAndPersonFromOwner')->with($owner)->willReturn([$owner, null]);
  117. $this->entityManager->expects(self::once())->method('persist');
  118. $this->entityManager->expects(self::once())->method('flush');
  119. $file = $fileStorage->prepareFile(
  120. $owner,
  121. 'file.ext',
  122. FileTypeEnum::LICENCE_CMF(),
  123. $author,
  124. true,
  125. 'ONLY_ORGANIZATION',
  126. 'application/pdf'
  127. );
  128. $this->assertEquals($owner, $file->getOrganization());
  129. $this->assertEquals(null, $file->getPerson());
  130. $this->assertEquals('file.ext', $file->getName());
  131. $this->assertEquals(null, $file->getSlug());
  132. $this->assertEquals(FileTypeEnum::LICENCE_CMF()->getValue(), $file->getType());
  133. $this->assertTrue($file->getIsTemporaryFile());
  134. $this->assertEquals('ONLY_ORGANIZATION', $file->getVisibility());
  135. $this->assertEquals('application/pdf', $file->getMimeType());
  136. $this->assertEquals(123, $file->getCreatedBy());
  137. }
  138. /**
  139. * @see LocalStorage::prepareFile()
  140. */
  141. public function testPrepareFileDefaultValues(): void
  142. {
  143. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  144. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
  145. ->setMethodsExcept(['prepareFile'])
  146. ->getMock();
  147. $owner = $this->getMockBuilder(Person::class)->getMock();
  148. $author = $this->getMockBuilder(Access::class)->getMock();
  149. $fileStorage->method('getOrganizationAndPersonFromOwner')->with($owner)->willReturn([null, $owner]);
  150. $this->entityManager->expects(self::once())->method('persist');
  151. $this->entityManager->expects(self::once())->method('flush');
  152. $file = $fileStorage->prepareFile($owner, 'file.txt', FileTypeEnum::NONE(), $author);
  153. $this->assertEquals(null, $file->getOrganization());
  154. $this->assertEquals($owner, $file->getPerson());
  155. $this->assertEquals('file.txt', $file->getName());
  156. $this->assertEquals(FileTypeEnum::NONE()->getValue(), $file->getType());
  157. $this->assertFalse($file->getIsTemporaryFile());
  158. $this->assertEquals('NOBODY', $file->getVisibility());
  159. $this->assertEquals('text/plain', $file->getMimeType());
  160. }
  161. /**
  162. * @see LocalStorage::prepareFile()
  163. */
  164. public function testPrepareFileNoFlush(): void
  165. {
  166. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  167. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
  168. ->setMethodsExcept(['prepareFile'])
  169. ->getMock();
  170. $owner = $this->getMockBuilder(Organization::class)->getMock();
  171. $author = $this->getMockBuilder(Access::class)->getMock();
  172. $fileStorage->method('getOrganizationAndPersonFromOwner')->with($owner)->willReturn([$owner, null]);
  173. $this->entityManager->expects(self::once())->method('persist');
  174. $this->entityManager->expects(self::never())->method('flush');
  175. $fileStorage->prepareFile(
  176. $owner,
  177. 'file.txt',
  178. FileTypeEnum::NONE(),
  179. $author,
  180. false,
  181. 'NOBODY',
  182. null,
  183. false
  184. );
  185. }
  186. /**
  187. * @see LocalStorage::writeFile()
  188. */
  189. public function testWriteFileNewFile(): void {
  190. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  191. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
  192. ->setMethodsExcept(['writeFile'])
  193. ->getMock();
  194. $organization = $this->getMockBuilder(Organization::class)->getMock();
  195. $author = $this->getMockBuilder(Access::class)->getMock();
  196. $author->method('getId')->willReturn(123);
  197. $file = $this->getMockBuilder(File::class)->getMock();
  198. $file->method('getName')->willReturn('foo.txt');
  199. $file->method('getOrganization')->willReturn($organization);
  200. $file->method('getPerson')->willReturn(null);
  201. $file->method('getIsTemporaryFile')->willReturn(false);
  202. $file->method('getSlug')->willReturn(null);
  203. $file->method('getType')->willReturn(FileTypeEnum::NONE()->getValue());
  204. $fileStorage
  205. ->method('getPrefix')
  206. ->with($organization, false, FileTypeEnum::NONE()->getValue())
  207. ->willReturn('prefix/');
  208. $content = '123456789';
  209. $size = strlen($content);
  210. $this->filesystem
  211. ->expects(self::once())
  212. ->method('write')
  213. ->with(self::matchesRegularExpression('/^prefix\/\w{16,24}\/foo.txt/'), $content, true)
  214. ->willReturn($size);
  215. $file->expects(self::once())->method('setSize')->with($size)->willReturnSelf();
  216. $file->expects(self::once())->method('setStatus')->with(FileStatusEnum::READY()->getValue())->willReturnSelf();
  217. $file->expects(self::once())
  218. ->method('setSlug')
  219. ->with(self::matchesRegularExpression('/^prefix\/\w{16,24}\/foo.txt/'))
  220. ->willReturnSelf();
  221. $file->expects(self::once())->method('setCreateDate')->with(self::isInstanceOf(DateTime::class))->willReturnSelf();
  222. $file->expects(self::once())->method('setCreatedBy')->with(123)->willReturnSelf();
  223. $file->expects(self::never())->method('setUpdateDate');
  224. $file->expects(self::never())->method('setUpdatedBy');
  225. $this->entityManager->expects(self::once())->method('flush');
  226. $returned = $fileStorage->writeFile($file, $content, $author);
  227. $this->assertEquals($file, $returned);
  228. }
  229. /**
  230. * @see LocalStorage::writeFile()
  231. */
  232. public function testWriteFileExistingFile(): void {
  233. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  234. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
  235. ->setMethodsExcept(['writeFile'])
  236. ->getMock();
  237. $person = $this->getMockBuilder(Person::class)->getMock();
  238. $author = $this->getMockBuilder(Access::class)->getMock();
  239. $author->method('getId')->willReturn(123);
  240. $key = 'prefix/uid/bar.txt';
  241. $file = $this->getMockBuilder(File::class)->getMock();
  242. $file->method('getName')->willReturn('bar.txt');
  243. $file->method('getOrganization')->willReturn(null);
  244. $file->method('getPerson')->willReturn($person);
  245. $file->method('getIsTemporaryFile')->willReturn(true);
  246. $file->method('getSlug')->willReturn($key);
  247. $file->method('getType')->willReturn(FileTypeEnum::NONE()->getValue());
  248. $fileStorage->expects(self::never())->method('getPrefix');
  249. $content = '123 Soleil';
  250. $size = strlen($content);
  251. $this->filesystem
  252. ->expects(self::once())
  253. ->method('write')
  254. ->with($key, $content, true)
  255. ->willReturn($size);
  256. $this->filesystem->method('has')->with($key)->willReturn(true);
  257. $file->expects(self::once())->method('setSize')->with($size)->willReturnSelf();
  258. $file->expects(self::once())->method('setStatus')->with(FileStatusEnum::READY()->getValue())->willReturnSelf();
  259. $file->expects(self::never())->method('setSlug');
  260. $file->expects(self::never())->method('setCreateDate');
  261. $file->expects(self::never())->method('setCreatedBy');
  262. $file->expects(self::once())->method('setUpdateDate')->with(self::isInstanceOf(DateTime::class))->willReturnSelf();
  263. $file->expects(self::once())->method('setUpdatedBy')->with(123)->willReturnSelf();
  264. $this->entityManager->expects(self::once())->method('flush');
  265. $returned = $fileStorage->writeFile($file, $content, $author);
  266. $this->assertEquals($file, $returned);
  267. }
  268. /**
  269. * @see LocalStorage::writeFile()
  270. */
  271. public function testWriteFileExistingButMissingFile(): void
  272. {
  273. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  274. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
  275. ->setMethodsExcept(['writeFile'])
  276. ->getMock();
  277. $person = $this->getMockBuilder(Person::class)->getMock();
  278. $author = $this->getMockBuilder(Access::class)->getMock();
  279. $key = 'prefix/uid/bar.txt';
  280. $file = $this->getMockBuilder(File::class)->getMock();
  281. $file->method('getName')->willReturn('bar.txt');
  282. $file->method('getOrganization')->willReturn(null);
  283. $file->method('getPerson')->willReturn($person);
  284. $file->method('getIsTemporaryFile')->willReturn(true);
  285. $file->method('getSlug')->willReturn($key);
  286. $file->method('getType')->willReturn(FileTypeEnum::NONE()->getValue());
  287. $this->filesystem->expects(self::never())->method('write');
  288. $this->entityManager->expects(self::never())->method('flush');
  289. $this->filesystem->method('has')->with($key)->willReturn(false);
  290. $this->expectException(RuntimeException::class);
  291. $this->expectExceptionMessage('The file `' . $key . '` does not exist in the file storage');
  292. $fileStorage->writeFile($file, '12346', $author);
  293. }
  294. /**
  295. * @see LocalStorage::writeFile()
  296. */
  297. public function testWriteFileWithAccessOwner(): void {
  298. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  299. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
  300. ->setMethodsExcept(['writeFile'])
  301. ->getMock();
  302. $access = $this->getMockBuilder(Access::class)->getMock();
  303. $person = $this->getMockBuilder(Person::class)->getMock();
  304. $organization = $this->getMockBuilder(Organization::class)->getMock();
  305. $author = $this->getMockBuilder(Access::class)->getMock();
  306. $author->method('getId')->willReturn(123);
  307. $file = $this->getMockBuilder(File::class)->getMock();
  308. $file->method('getName')->willReturn('bar.txt');
  309. $file->method('getOrganization')->willReturn($organization);
  310. $file->method('getPerson')->willReturn($person);
  311. $file->method('getIsTemporaryFile')->willReturn(true);
  312. $file->method('getSlug')->willReturn(null);
  313. $file->method('getType')->willReturn(FileTypeEnum::NONE()->getValue());
  314. $this->accessRepository
  315. ->expects(self::once())
  316. ->method('findOneBy')
  317. ->with(['organization' => $organization, 'person' => $person])
  318. ->willReturn($access);
  319. $fileStorage
  320. ->expects(self::once())
  321. ->method('getPrefix')
  322. ->with($access, true, FileTypeEnum::NONE()->getValue())
  323. ->willReturn('prefix/');
  324. $content = '1';
  325. $this->filesystem->method('write')->willReturn(1);
  326. $fileStorage->writeFile($file, $content, $author);
  327. }
  328. /**
  329. * @see LocalStorage::writeFile()
  330. */
  331. public function testWriteFileWithNoName(): void
  332. {
  333. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  334. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
  335. ->setMethodsExcept(['writeFile'])
  336. ->getMock();
  337. $author = $this->getMockBuilder(Access::class)->getMock();
  338. $file = $this->getMockBuilder(File::class)->getMock();
  339. $file->method('getName')->willReturn('');
  340. $this->expectException(RuntimeException::class);
  341. $this->expectExceptionMessage('File has no filename');
  342. $fileStorage->writeFile($file, '...', $author);
  343. }
  344. /**
  345. * @see LocalStorage::makeFile()
  346. */
  347. public function testMakeFile(): void {
  348. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  349. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
  350. ->setMethodsExcept(['makeFile'])
  351. ->getMock();
  352. $organization = $this->getMockBuilder(Organization::class)->getMock();
  353. $author = $this->getMockBuilder(Access::class)->getMock();
  354. $file = $this->getMockBuilder(File::class)->getMock();
  355. $fileStorage
  356. ->expects(self::once())
  357. ->method('prepareFile')
  358. ->with($organization, 'foo.txt', FileTypeEnum::NONE(), $author, true, 'ONLY_ORGANIZATION', 'mime/type')
  359. ->willReturn($file);
  360. $fileStorage
  361. ->expects(self::once())
  362. ->method('writeFile')
  363. ->with($file, '...', $author)
  364. ->willReturn($file);
  365. $fileStorage->makeFile(
  366. $organization,
  367. 'foo.txt',
  368. FileTypeEnum::NONE(),
  369. '...',
  370. $author,
  371. true,
  372. 'ONLY_ORGANIZATION',
  373. 'mime/type');
  374. }
  375. /**
  376. * @see LocalStorage::delete()
  377. */
  378. public function testDelete(): void {
  379. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  380. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
  381. ->setMethodsExcept(['delete'])
  382. ->getMock();
  383. $author = $this->getMockBuilder(Access::class)->getMock();
  384. $author->method('getId')->willReturn(123);
  385. $file = $this->getMockBuilder(File::class)->getMock();
  386. $file->method('getSlug')->willReturn('key');
  387. $this->filesystem->expects(self::once())->method('delete')->with('key')->willReturn(true);
  388. $file->expects(self::once())->method('setStatus')->with(FileStatusEnum::DELETED()->getValue())->willReturnSelf();
  389. $file->expects(self::once())->method('setSize')->with(0)->willReturnSelf();
  390. $file->expects(self::once())->method('setUpdatedBy')->with(123)->willReturnSelf();
  391. $returned = $fileStorage->delete($file, $author);
  392. $this->assertEquals($file, $returned);
  393. }
  394. /**
  395. * @see LocalStorage::delete()
  396. */
  397. public function testDeleteFailed(): void {
  398. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  399. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
  400. ->setMethodsExcept(['delete'])
  401. ->getMock();
  402. $author = $this->getMockBuilder(Access::class)->getMock();
  403. $author->method('getId')->willReturn(123);
  404. $file = $this->getMockBuilder(File::class)->getMock();
  405. $file->method('getSlug')->willReturn('key');
  406. $this->filesystem->expects(self::once())->method('delete')->with('key')->willReturn(false);
  407. $file->expects(self::never())->method('setStatus');
  408. $file->expects(self::never())->method('setSize');
  409. $file->expects(self::never())->method('setUpdatedBy');
  410. $this->expectException(RuntimeException::class);
  411. $this->expectExceptionMessage('File `' . $file->getSlug() . '` could\'nt be deleted');
  412. $fileStorage->delete($file, $author);
  413. }
  414. /**
  415. * @see LocalStorage::getPrefix()
  416. */
  417. public function testGetPrefixAccess(): void {
  418. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  419. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
  420. ->setMethodsExcept(['getPrefix'])
  421. ->getMock();
  422. $organization = $this->getMockBuilder(Organization::class)->getMock();
  423. $organization->method('getId')->willReturn(2);
  424. $access = $this->getMockBuilder(Access::class)->getMock();
  425. $access->method('getOrganization')->willReturn($organization);
  426. $access->method('getId')->willReturn(1);
  427. $prefix = $fileStorage->getPrefix($access, false);
  428. $this->assertEquals('organization/2/1', $prefix);
  429. }
  430. /**
  431. * @see LocalStorage::getPrefix()
  432. */
  433. public function testGetPrefixOrganization(): void {
  434. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  435. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
  436. ->setMethodsExcept(['getPrefix'])
  437. ->getMock();
  438. $organization = $this->getMockBuilder(Organization::class)->getMock();
  439. $organization->method('getId')->willReturn(1);
  440. $prefix = $fileStorage->getPrefix($organization, false);
  441. $this->assertEquals('organization/1', $prefix);
  442. }
  443. /**
  444. * @see LocalStorage::getPrefix()
  445. */
  446. public function testGetPrefixPerson(): void {
  447. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  448. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
  449. ->setMethodsExcept(['getPrefix'])
  450. ->getMock();
  451. $person = $this->getMockBuilder(Person::class)->getMock();
  452. $person->method('getId')->willReturn(1);
  453. $prefix = $fileStorage->getPrefix($person, false);
  454. $this->assertEquals('person/1', $prefix);
  455. }
  456. /**
  457. * @see LocalStorage::getPrefix()
  458. */
  459. public function testGetPrefixTemp(): void {
  460. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  461. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
  462. ->setMethodsExcept(['getPrefix'])
  463. ->getMock();
  464. $organization = $this->getMockBuilder(Organization::class)->getMock();
  465. $organization->method('getId')->willReturn(1);
  466. $prefix = $fileStorage->getPrefix($organization, true);
  467. $this->assertEquals('temp/organization/1', $prefix);
  468. }
  469. /**
  470. * @see LocalStorage::getPrefix()
  471. */
  472. public function testGetPrefixWithType(): void {
  473. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  474. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
  475. ->setMethodsExcept(['getPrefix'])
  476. ->getMock();
  477. $organization = $this->getMockBuilder(Organization::class)->getMock();
  478. $organization->method('getId')->willReturn(1);
  479. $prefix = $fileStorage->getPrefix($organization, false, FileTypeEnum::LICENCE_CMF()->getValue());
  480. $this->assertEquals('organization/1/licence_cmf', $prefix);
  481. }
  482. /**
  483. * @see LocalStorage::getOrganizationAndPersonFromOwner()
  484. */
  485. public function testGetOrganizationAndPersonFromOwner(): void {
  486. $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
  487. ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
  488. ->setMethodsExcept(['getOrganizationAndPersonFromOwner'])
  489. ->getMock();
  490. $organization = $this->getMockBuilder(Organization::class)->getMock();
  491. $organization->method('getId')->willReturn(2);
  492. $person = $this->getMockBuilder(Person::class)->getMock();
  493. $person->method('getId')->willReturn(1);
  494. $access = $this->getMockBuilder(Access::class)->getMock();
  495. $access->method('getOrganization')->willReturn($organization);
  496. $access->method('getPerson')->willReturn($person);
  497. $access->method('getId')->willReturn(1);
  498. $this->assertEquals([$organization, $person], $fileStorage->getOrganizationAndPersonFromOwner($access));
  499. $this->assertEquals([$organization, null], $fileStorage->getOrganizationAndPersonFromOwner($organization));
  500. $this->assertEquals([null, $person], $fileStorage->getOrganizationAndPersonFromOwner($person));
  501. }
  502. }