| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629 |
- <?php
- namespace App\Tests\Unit\Service\File\Storage;
- use App\Entity\Access\Access;
- use App\Entity\Core\File;
- use App\Entity\Organization\Organization;
- use App\Entity\Person\Person;
- use App\Enum\Core\FileStatusEnum;
- use App\Enum\Core\FileTypeEnum;
- use App\Enum\Core\FileVisibilityEnum;
- use App\Repository\Access\AccessRepository;
- use App\Service\File\Factory\ImageFactory;
- use App\Service\File\Storage\LocalStorage;
- use App\Service\Utils\FileUtils;
- use App\Service\Utils\UrlBuilder;
- use Doctrine\ORM\EntityManagerInterface;
- use Gaufrette\Filesystem;
- use JetBrains\PhpStorm\Pure;
- use Knp\Bundle\GaufretteBundle\FilesystemMap;
- use Liip\ImagineBundle\Imagine\Cache\CacheManager;
- use Liip\ImagineBundle\Imagine\Data\DataManager;
- use PHPUnit\Framework\MockObject\MockObject;
- use PHPUnit\Framework\TestCase;
- class TestableLocalStorage extends LocalStorage
- {
- public const FS_KEY = parent::FS_KEY;
- public function getPrefix(mixed $owner, bool $isTemporary, ?FileTypeEnum $type = null): string
- {
- return parent::getPrefix($owner, $isTemporary, $type);
- }
- #[Pure]
- public function getOrganizationAndPersonFromOwner(mixed $owner): array
- {
- return parent::getOrganizationAndPersonFromOwner($owner);
- }
- }
- class LocalStorageTest extends TestCase
- {
- private FilesystemMap $filesystemMap;
- private EntityManagerInterface $entityManager;
- private AccessRepository $accessRepository;
- private DataManager $dataManager;
- private CacheManager $cacheManager;
- private ImageFactory $imageFactory;
- private Filesystem $filesystem;
- private FileUtils $fileUtils;
- private UrlBuilder $urlBuilder;
- public function setUp(): void
- {
- $this->filesystemMap = $this->getMockBuilder(FilesystemMap::class)->disableOriginalConstructor()->getMock();
- $this->entityManager = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
- $this->accessRepository = $this->getMockBuilder(AccessRepository::class)->disableOriginalConstructor()->getMock();
- $this->dataManager = $this->getMockBuilder(DataManager::class)->disableOriginalConstructor()->getMock();
- $this->cacheManager = $this->getMockBuilder(CacheManager::class)->disableOriginalConstructor()->getMock();
- $this->imageFactory = $this->getMockBuilder(ImageFactory::class)->disableOriginalConstructor()->getMock();
- $this->fileUtils = $this->getMockBuilder(FileUtils::class)->disableOriginalConstructor()->getMock();
- $this->urlBuilder = $this->getMockBuilder(UrlBuilder::class)->disableOriginalConstructor()->getMock();
- $this->filesystem = $this->getMockBuilder(Filesystem::class)->disableOriginalConstructor()->getMock();
- $this->filesystemMap->method('get')->with(TestableLocalStorage::FS_KEY)->willReturn($this->filesystem);
- }
- public function getMockForMethod(string $methodName): TestableLocalStorage|MockObject
- {
- return $this->getMockBuilder(TestableLocalStorage::class)
- ->setConstructorArgs([
- $this->filesystemMap,
- $this->entityManager,
- $this->accessRepository,
- $this->dataManager,
- $this->cacheManager,
- $this->imageFactory,
- $this->fileUtils,
- $this->urlBuilder,
- '/file/storage/dir/'
- ])
- ->setMethodsExcept([$methodName])
- ->getMock();
- }
- /**
- * @see LocalStorage::exists()
- */
- public function testExists(): void
- {
- $fileStorage = $this->getMockForMethod('exists');
- $file = $this->getMockBuilder(File::class)->getMock();
- $file->method('getSlug')->willReturn('foo');
- $this->filesystem->expects(self::once())->method('has')->with('foo')->willReturn(true);
- $this->assertTrue($fileStorage->exists($file));
- }
- /**
- * @see LocalStorage::exists()
- */
- public function testExistsInexistant(): void
- {
- $fileStorage = $this->getMockForMethod('exists');
- $file = $this->getMockBuilder(File::class)->getMock();
- $file->method('getSlug')->willReturn('foo');
- $this->filesystem->expects(self::once())->method('has')->with('foo')->willReturn(false);
- $this->assertFalse($fileStorage->exists($file));
- }
- /**
- * @see LocalStorage::listByOwner()
- */
- public function testListByOwner(): void
- {
- $fileStorage = $this->getMockForMethod('listByOwner');
- $owner = $this->getMockBuilder(Organization::class)->getMock();
- $fileStorage->method('getPrefix')->with($owner, false, FileTypeEnum::LICENCE_CMF)->willReturn('foo');
- $this->filesystem->method('listKeys')->with('foo')->willReturn(['foo/a.txt', 'foo/b.pdf']);
- $this->assertEquals(
- ['foo/a.txt', 'foo/b.pdf'],
- $fileStorage->listByOwner($owner, FileTypeEnum::LICENCE_CMF)
- );
- }
- /**
- * @see LocalStorage::read()
- */
- public function testReadPdf(): void
- {
- $fileStorage = $this->getMockForMethod('read');
- $file = $this->getMockBuilder(File::class)->getMock();
- $file->method('getSlug')->willReturn('foo');
- $this->filesystem->method('read')->with('foo')->willReturn('12345679');
- $this->assertEquals(
- '12345679',
- $fileStorage->read($file)
- );
- }
- /**
- * @see LocalStorage::prepareFile()
- */
- public function testPrepareFile(): void
- {
- $fileStorage = $this->getMockForMethod('prepareFile');
- $owner = $this->getMockBuilder(Organization::class)->getMock();
- $author = $this->getMockBuilder(Access::class)->getMock();
- $author->method('getId')->willReturn(123);
- $fileStorage->method('getOrganizationAndPersonFromOwner')->with($owner)->willReturn([$owner, null]);
- $this->entityManager->expects(self::once())->method('persist');
- $this->entityManager->expects(self::once())->method('flush');
- $file = $fileStorage->prepareFile(
- $owner,
- 'file.ext',
- FileTypeEnum::LICENCE_CMF,
- $author,
- true,
- FileVisibilityEnum::ONLY_ORGANIZATION,
- 'application/pdf'
- );
- $this->assertEquals($owner, $file->getOrganization());
- $this->assertEquals(null, $file->getPerson());
- $this->assertEquals('file.ext', $file->getName());
- $this->assertEquals(null, $file->getSlug());
- $this->assertEquals(FileTypeEnum::LICENCE_CMF, $file->getType());
- $this->assertTrue($file->getIsTemporaryFile());
- $this->assertEquals(FileVisibilityEnum::ONLY_ORGANIZATION, $file->getVisibility());
- $this->assertEquals('application/pdf', $file->getMimeType());
- $this->assertEquals(123, $file->getCreatedBy());
- }
- /**
- * @see LocalStorage::prepareFile()
- */
- public function testPrepareFileDefaultValues(): void
- {
- $fileStorage = $this->getMockForMethod('prepareFile');
- $owner = $this->getMockBuilder(Person::class)->getMock();
- $author = $this->getMockBuilder(Access::class)->getMock();
- $fileStorage->method('getOrganizationAndPersonFromOwner')->with($owner)->willReturn([null, $owner]);
- $this->entityManager->expects(self::once())->method('persist');
- $this->entityManager->expects(self::once())->method('flush');
- $this->fileUtils->method('guessMimeTypeFromFilename')->with('file.txt')->willReturn('text/plain');
- $file = $fileStorage->prepareFile($owner, 'file.txt', FileTypeEnum::NONE, $author);
- $this->assertEquals(null, $file->getOrganization());
- $this->assertEquals($owner, $file->getPerson());
- $this->assertEquals('file.txt', $file->getName());
- $this->assertEquals(FileTypeEnum::NONE, $file->getType());
- $this->assertFalse($file->getIsTemporaryFile());
- $this->assertEquals(FileVisibilityEnum::NOBODY, $file->getVisibility());
- $this->assertEquals('text/plain', $file->getMimeType());
- }
- /**
- * @see LocalStorage::prepareFile()
- */
- public function testPrepareFileNoFlush(): void
- {
- $fileStorage = $this->getMockForMethod('prepareFile');
- $owner = $this->getMockBuilder(Organization::class)->getMock();
- $author = $this->getMockBuilder(Access::class)->getMock();
- $fileStorage->method('getOrganizationAndPersonFromOwner')->with($owner)->willReturn([$owner, null]);
- $this->entityManager->expects(self::once())->method('persist');
- $this->entityManager->expects(self::never())->method('flush');
- $this->fileUtils->method('guessMimeTypeFromFilename')->willReturn('text/plain');
- $fileStorage->prepareFile(
- $owner,
- 'file.txt',
- FileTypeEnum::NONE,
- $author,
- false,
- FileVisibilityEnum::NOBODY,
- 'text/plain',
- false
- );
- }
- /**
- * @see LocalStorage::write()
- */
- public function testWriteNewFile(): void
- {
- $fileStorage = $this->getMockForMethod('write');
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $author = $this->getMockBuilder(Access::class)->getMock();
- $author->method('getId')->willReturn(123);
- $file = $this->getMockBuilder(File::class)->getMock();
- $file->method('getName')->willReturn('foo.txt');
- $file->method('getOrganization')->willReturn($organization);
- $file->method('getPerson')->willReturn(null);
- $file->method('getIsTemporaryFile')->willReturn(false);
- $file->method('getSlug')->willReturn(null);
- $file->method('getType')->willReturn(FileTypeEnum::NONE);
- $fileStorage
- ->method('getPrefix')
- ->with($organization, false, FileTypeEnum::NONE)
- ->willReturn('prefix/');
- $content = '123456789';
- $size = strlen($content);
- $this->filesystem
- ->expects(self::once())
- ->method('write')
- ->with(self::matchesRegularExpression('/^prefix\/\w{16,24}\/foo.txt/'), $content, true)
- ->willReturn($size);
- $file->expects(self::once())->method('setSize')->with($size)->willReturnSelf();
- $file->expects(self::once())->method('setStatus')->with(FileStatusEnum::READY)->willReturnSelf();
- $file->expects(self::once())
- ->method('setSlug')
- ->with(self::matchesRegularExpression('/^prefix\/\w{16,24}\/foo.txt/'))
- ->willReturnSelf();
- $file->expects(self::once())->method('setCreateDate')->with(self::isInstanceOf(\DateTime::class))->willReturnSelf();
- $file->expects(self::once())->method('setCreatedBy')->with(123)->willReturnSelf();
- $file->expects(self::never())->method('setUpdateDate');
- $file->expects(self::never())->method('setUpdatedBy');
- $this->entityManager->expects(self::once())->method('flush');
- $returned = $fileStorage->write($file, $content, $author);
- $this->assertSame($file, $returned);
- }
- /**
- * @see LocalStorage::write()
- */
- public function testWriteExistingFile(): void
- {
- $fileStorage = $this->getMockForMethod('write');
- $person = $this->getMockBuilder(Person::class)->getMock();
- $author = $this->getMockBuilder(Access::class)->getMock();
- $author->method('getId')->willReturn(123);
- $key = 'prefix/uid/bar.txt';
- $file = $this->getMockBuilder(File::class)->getMock();
- $file->method('getName')->willReturn('bar.txt');
- $file->method('getOrganization')->willReturn(null);
- $file->method('getPerson')->willReturn($person);
- $file->method('getIsTemporaryFile')->willReturn(true);
- $file->method('getSlug')->willReturn($key);
- $file->method('getType')->willReturn(FileTypeEnum::NONE);
- $fileStorage->expects(self::never())->method('getPrefix');
- $content = '123 Soleil';
- $size = strlen($content);
- $this->filesystem
- ->expects(self::once())
- ->method('write')
- ->with($key, $content, true)
- ->willReturn($size);
- $this->filesystem->method('has')->with($key)->willReturn(true);
- $file->expects(self::once())->method('setSize')->with($size)->willReturnSelf();
- $file->expects(self::once())->method('setStatus')->with(FileStatusEnum::READY)->willReturnSelf();
- $file->expects(self::never())->method('setSlug');
- $file->expects(self::never())->method('setCreateDate');
- $file->expects(self::never())->method('setCreatedBy');
- $file->expects(self::once())->method('setUpdateDate')->with(self::isInstanceOf(\DateTime::class))->willReturnSelf();
- $file->expects(self::once())->method('setUpdatedBy')->with(123)->willReturnSelf();
- $this->entityManager->expects(self::once())->method('flush');
- $returned = $fileStorage->write($file, $content, $author);
- $this->assertEquals($file, $returned);
- }
- /**
- * @see LocalStorage::write()
- */
- public function testWriteExistingButMissingFile(): void
- {
- $fileStorage = $this->getMockForMethod('write');
- $person = $this->getMockBuilder(Person::class)->getMock();
- $author = $this->getMockBuilder(Access::class)->getMock();
- $key = 'prefix/uid/bar.txt';
- $file = $this->getMockBuilder(File::class)->getMock();
- $file->method('getName')->willReturn('bar.txt');
- $file->method('getOrganization')->willReturn(null);
- $file->method('getPerson')->willReturn($person);
- $file->method('getIsTemporaryFile')->willReturn(true);
- $file->method('getSlug')->willReturn($key);
- $file->method('getType')->willReturn(FileTypeEnum::NONE);
- $this->filesystem->expects(self::never())->method('write');
- $this->entityManager->expects(self::never())->method('flush');
- $this->filesystem->method('has')->with($key)->willReturn(false);
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage('The file `'.$key.'` does not exist in the file storage');
- $fileStorage->write($file, '12346', $author);
- }
- /**
- * @see LocalStorage::write()
- */
- public function testWriteWithAccessOwner(): void
- {
- $fileStorage = $this->getMockForMethod('write');
- $access = $this->getMockBuilder(Access::class)->getMock();
- $person = $this->getMockBuilder(Person::class)->getMock();
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $author = $this->getMockBuilder(Access::class)->getMock();
- $author->method('getId')->willReturn(123);
- $file = $this->getMockBuilder(File::class)->getMock();
- $file->method('getName')->willReturn('bar.txt');
- $file->method('getOrganization')->willReturn($organization);
- $file->method('getPerson')->willReturn($person);
- $file->method('getIsTemporaryFile')->willReturn(true);
- $file->method('getSlug')->willReturn(null);
- $file->method('getType')->willReturn(FileTypeEnum::NONE);
- $this->accessRepository
- ->expects(self::once())
- ->method('findOneBy')
- ->with(['organization' => $organization, 'person' => $person])
- ->willReturn($access);
- $fileStorage
- ->expects(self::once())
- ->method('getPrefix')
- ->with($access, true, FileTypeEnum::NONE)
- ->willReturn('prefix/');
- $content = '1';
- $this->filesystem->method('write')->willReturn(1);
- $fileStorage->write($file, $content, $author);
- }
- /**
- * @see LocalStorage::write()
- */
- public function testWriteWithNoName(): void
- {
- $fileStorage = $this->getMockForMethod('write');
- $author = $this->getMockBuilder(Access::class)->getMock();
- $file = $this->getMockBuilder(File::class)->getMock();
- $file->method('getName')->willReturn('');
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage('File has no filename');
- $fileStorage->write($file, '...', $author);
- }
- /**
- * @see LocalStorage::makeFile()
- */
- public function testMakeFile(): void
- {
- $fileStorage = $this->getMockForMethod('makeFile');
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $author = $this->getMockBuilder(Access::class)->getMock();
- $file = $this->getMockBuilder(File::class)->getMock();
- $fileStorage
- ->expects(self::once())
- ->method('prepareFile')
- ->with($organization, 'foo.txt', FileTypeEnum::NONE, $author, true, FileVisibilityEnum::ONLY_ORGANIZATION, 'mime/type')
- ->willReturn($file);
- $fileStorage
- ->expects(self::once())
- ->method('write')
- ->with($file, '...', $author)
- ->willReturn($file);
- $fileStorage->makeFile(
- $organization,
- 'foo.txt',
- FileTypeEnum::NONE,
- '...',
- $author,
- true,
- FileVisibilityEnum::ONLY_ORGANIZATION,
- 'mime/type');
- }
- /**
- * @see LocalStorage::softDelete()
- */
- public function testSoftdelete(): void
- {
- $fileStorage = $this->getMockForMethod('softDelete');
- $author = $this->getMockBuilder(Access::class)->getMock();
- $author->method('getId')->willReturn(123);
- $file = $this->getMockBuilder(File::class)->getMock();
- $file->method('getSlug')->willReturn('key');
- $file->expects(self::once())->method('setStatus')->with(FileStatusEnum::DELETED)->willReturnSelf();
- $file->expects(self::once())->method('setSize')->with(0)->willReturnSelf();
- $file->expects(self::once())->method('setUpdatedBy')->with(123)->willReturnSelf();
- $returned = $fileStorage->softDelete($file, $author);
- $this->assertEquals($file, $returned);
- }
- /**
- * @see LocalStorage::hardDelete()
- */
- public function testHardDelete(): void
- {
- $fileStorage = $this->getMockForMethod('hardDelete');
- $file = $this->getMockBuilder(File::class)->getMock();
- $file->method('getSlug')->willReturn('key');
- $this->filesystem->expects(self::once())->method('delete')->with('key')->willReturn(true);
- $fileStorage->hardDelete($file);
- }
- /**
- * @see LocalStorage::hardDelete()
- */
- public function testHardDeleteFailed(): void
- {
- $fileStorage = $this->getMockForMethod('hardDelete');
- $file = $this->getMockBuilder(File::class)->getMock();
- $file->method('getSlug')->willReturn('key');
- $this->filesystem->expects(self::once())->method('delete')->with('key')->willReturn(false);
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage('File `'.$file->getSlug().'` could\'nt be deleted');
- $fileStorage->hardDelete($file);
- }
- /**
- * @see LocalStorage::getPrefix()
- */
- public function testGetPrefixAccess(): void
- {
- $fileStorage = $this->getMockForMethod('getPrefix');
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organization->method('getId')->willReturn(2);
- $access = $this->getMockBuilder(Access::class)->getMock();
- $access->method('getOrganization')->willReturn($organization);
- $access->method('getId')->willReturn(1);
- $prefix = $fileStorage->getPrefix($access, false);
- $this->assertEquals('organization/2/1', $prefix);
- }
- /**
- * @see LocalStorage::getPrefix()
- */
- public function testGetPrefixOrganization(): void
- {
- $fileStorage = $this->getMockForMethod('getPrefix');
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organization->method('getId')->willReturn(1);
- $prefix = $fileStorage->getPrefix($organization, false);
- $this->assertEquals('organization/1', $prefix);
- }
- /**
- * @see LocalStorage::getPrefix()
- */
- public function testGetPrefixPerson(): void
- {
- $fileStorage = $this->getMockForMethod('getPrefix');
- $person = $this->getMockBuilder(Person::class)->getMock();
- $person->method('getId')->willReturn(1);
- $prefix = $fileStorage->getPrefix($person, false);
- $this->assertEquals('person/1', $prefix);
- }
- /**
- * @see LocalStorage::getPrefix()
- */
- public function testGetPrefixTemp(): void
- {
- $fileStorage = $this->getMockForMethod('getPrefix');
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organization->method('getId')->willReturn(1);
- $prefix = $fileStorage->getPrefix($organization, true);
- $this->assertEquals('temp/organization/1', $prefix);
- }
- /**
- * @see LocalStorage::getPrefix()
- */
- public function testGetPrefixWithType(): void
- {
- $fileStorage = $this->getMockForMethod('getPrefix');
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organization->method('getId')->willReturn(1);
- $prefix = $fileStorage->getPrefix($organization, false, FileTypeEnum::LICENCE_CMF);
- $this->assertEquals('organization/1/licence_cmf', $prefix);
- }
- /**
- * @see LocalStorage::getOrganizationAndPersonFromOwner()
- */
- public function testGetOrganizationAndPersonFromOwner(): void
- {
- $fileStorage = $this->getMockForMethod('getOrganizationAndPersonFromOwner');
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organization->method('getId')->willReturn(2);
- $person = $this->getMockBuilder(Person::class)->getMock();
- $person->method('getId')->willReturn(1);
- $access = $this->getMockBuilder(Access::class)->getMock();
- $access->method('getOrganization')->willReturn($organization);
- $access->method('getPerson')->willReturn($person);
- $access->method('getId')->willReturn(1);
- $this->assertEquals([$organization, $person], $fileStorage->getOrganizationAndPersonFromOwner($access));
- $this->assertEquals([$organization, null], $fileStorage->getOrganizationAndPersonFromOwner($organization));
- $this->assertEquals([null, $person], $fileStorage->getOrganizationAndPersonFromOwner($person));
- }
- }
|