LocalStorageTest.php 25 KB

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