Refactor StoredObject normalization handling

Deprecate and remove specific context constants from StoredObjectNormalizer. Update object properties for better clarity and add permissions handling. Introduce related tests and adjust other files relying on the old context constants.
This commit is contained in:
2024-08-28 23:19:24 +02:00
parent 00cc3b7806
commit b6edbb3eed
7 changed files with 211 additions and 29 deletions

View File

@@ -0,0 +1,98 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\DocStoreBundle\Tests\Serializer\Normalizer;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\DocStoreBundle\Security\Authorization\StoredObjectRoleEnum;
use Chill\DocStoreBundle\Security\Guard\JWTDavTokenProviderInterface;
use Chill\DocStoreBundle\Serializer\Normalizer\StoredObjectNormalizer;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* @internal
*
* @coversNothing
*/
class StoredObjectNormalizerTest extends TestCase
{
public function testNormalize(): void
{
$storedObject = new StoredObject();
$storedObject->setTitle('test');
$reflection = new \ReflectionClass(StoredObject::class);
$idProperty = $reflection->getProperty('id');
$idProperty->setValue($storedObject, 1);
$jwtProvider = $this->createMock(JWTDavTokenProviderInterface::class);
$jwtProvider->expects($this->once())->method('createToken')->withAnyParameters()->willReturn('token');
$jwtProvider->expects($this->once())->method('getTokenExpiration')->with('token')->willReturn($d = new \DateTimeImmutable());
$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
$urlGenerator->expects($this->once())->method('generate')
->with(
'chill_docstore_dav_document_get',
[
'uuid' => $storedObject->getUuid(),
'access_token' => 'token',
],
UrlGeneratorInterface::ABSOLUTE_URL,
)
->willReturn($davLink = 'http://localhost/dav/token');
$security = $this->createMock(Security::class);
$security->expects($this->exactly(2))->method('isGranted')
->with(
$this->logicalOr(StoredObjectRoleEnum::EDIT->value, StoredObjectRoleEnum::SEE->value),
$storedObject
)
->willReturn(true);
$globalNormalizer = $this->createMock(NormalizerInterface::class);
$globalNormalizer->expects($this->exactly(3))->method('normalize')
->withAnyParameters()
->willReturnCallback(function (?object $object, string $format, array $context) {
if (null === $object) {
return null;
}
return ['sub' => 'sub'];
});
$normalizer = new StoredObjectNormalizer($jwtProvider, $urlGenerator, $security);
$normalizer->setNormalizer($globalNormalizer);
$actual = $normalizer->normalize($storedObject, 'json');
self::assertArrayHasKey('id', $actual);
self::assertEquals(1, $actual['id']);
self::assertArrayHasKey('title', $actual);
self::assertEquals('test', $actual['title']);
self::assertArrayHasKey('uuid', $actual);
self::assertArrayHasKey('prefix', $actual);
self::assertArrayHaskey('status', $actual);
self::assertArrayHasKey('currentVersion', $actual);
self::assertEquals(null, $actual['currentVersion']);
self::assertArrayHasKey('totalVersions', $actual);
self::assertEquals(0, $actual['totalVersions']);
self::assertArrayHasKey('datas', $actual);
self::assertArrayHasKey('createdAt', $actual);
self::assertArrayHasKey('createdBy', $actual);
self::assertArrayHasKey('_permissions', $actual);
self::assertEqualsCanonicalizing(['canEdit' => true, 'canSee' => true], $actual['_permissions']);
self::assertArrayHaskey('_links', $actual);
self::assertArrayHasKey('dav_link', $actual['_links']);
self::assertEqualsCanonicalizing(['href' => $davLink, 'expiration' => $d->getTimestamp()], $actual['_links']['dav_link']);
}
}

View File

@@ -0,0 +1,61 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\DocStoreBundle\Tests\Serializer\Normalizer;
use Chill\DocStoreBundle\Entity\StoredObject;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* @internal
*
* @coversNothing
*/
class StoredObjectVersionNormalizerTest extends KernelTestCase
{
private NormalizerInterface $normalizer;
protected function setUp(): void
{
self::bootKernel();
$this->normalizer = self::getContainer()->get(NormalizerInterface::class);
}
public function testNormalize(): void
{
$storedObject = new StoredObject();
$version = $storedObject->registerVersion(
iv: [1, 2, 3, 4],
keyInfos: ['someKey' => 'someKey'],
type: 'text/text',
);
$reflection = new \ReflectionClass($version);
$idProperty = $reflection->getProperty('id');
$idProperty->setValue($version, 1);
$actual = $this->normalizer->normalize($version, 'json', ['groups' => ['read']]);
self::assertEqualsCanonicalizing(
[
'id' => 1,
'version' => 0,
'filename' => $version->getFilename(),
'iv' => [1, 2, 3, 4],
'keyInfos' => ['someKey' => 'someKey'],
'type' => 'text/text',
'createdAt' => null,
'createdBy' => null,
],
$actual
);
}
}