mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-22 15:43:51 +00:00
Merge remote-tracking branch 'origin/master' into upgrade-sf5
This commit is contained in:
@@ -0,0 +1,414 @@
|
||||
<?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\Controller;
|
||||
|
||||
use Chill\DocStoreBundle\Controller\WebdavController;
|
||||
use Chill\DocStoreBundle\Entity\StoredObject;
|
||||
use Chill\DocStoreBundle\Service\StoredObjectManagerInterface;
|
||||
use Prophecy\Argument;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @coversNothing
|
||||
*/
|
||||
class WebdavControllerTest extends KernelTestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
private \Twig\Environment $engine;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
self::bootKernel();
|
||||
|
||||
$this->engine = self::$container->get(\Twig\Environment::class);
|
||||
}
|
||||
|
||||
private function buildController(): WebdavController
|
||||
{
|
||||
$storedObjectManager = new MockedStoredObjectManager();
|
||||
$security = $this->prophesize(Security::class);
|
||||
$security->isGranted(Argument::in(['EDIT', 'SEE']), Argument::type(StoredObject::class))
|
||||
->willReturn(true);
|
||||
|
||||
return new WebdavController($this->engine, $storedObjectManager, $security->reveal());
|
||||
}
|
||||
|
||||
private function buildDocument(): StoredObject
|
||||
{
|
||||
$object = (new StoredObject())
|
||||
->setType('application/vnd.oasis.opendocument.text');
|
||||
|
||||
$reflectionObject = new \ReflectionClass($object);
|
||||
$reflectionObjectUuid = $reflectionObject->getProperty('uuid');
|
||||
|
||||
$reflectionObjectUuid->setValue($object, Uuid::fromString('716e6688-4579-4938-acf3-c4ab5856803b'));
|
||||
|
||||
return $object;
|
||||
}
|
||||
|
||||
public function testGet(): void
|
||||
{
|
||||
$controller = $this->buildController();
|
||||
|
||||
$response = $controller->getDocument($this->buildDocument());
|
||||
|
||||
self::assertEquals(200, $response->getStatusCode());
|
||||
self::assertEquals('abcde', $response->getContent());
|
||||
self::assertContains('etag', $response->headers->keys());
|
||||
self::assertStringContainsString('ab56b4', $response->headers->get('etag'));
|
||||
}
|
||||
|
||||
public function testOptionsOnDocument(): void
|
||||
{
|
||||
$controller = $this->buildController();
|
||||
|
||||
$response = $controller->optionsDocument($this->buildDocument());
|
||||
|
||||
self::assertEquals(200, $response->getStatusCode());
|
||||
self::assertContains('allow', $response->headers->keys());
|
||||
|
||||
foreach (explode(',', 'OPTIONS,GET,HEAD,PROPFIND') as $method) {
|
||||
self::assertStringContainsString($method, $response->headers->get('allow'));
|
||||
}
|
||||
|
||||
self::assertContains('dav', $response->headers->keys());
|
||||
self::assertStringContainsString('1', $response->headers->get('dav'));
|
||||
}
|
||||
|
||||
public function testOptionsOnDirectory(): void
|
||||
{
|
||||
$controller = $this->buildController();
|
||||
|
||||
$response = $controller->optionsDirectory($this->buildDocument());
|
||||
|
||||
self::assertEquals(200, $response->getStatusCode());
|
||||
self::assertContains('allow', $response->headers->keys());
|
||||
|
||||
foreach (explode(',', 'OPTIONS,GET,HEAD,PROPFIND') as $method) {
|
||||
self::assertStringContainsString($method, $response->headers->get('allow'));
|
||||
}
|
||||
|
||||
self::assertContains('dav', $response->headers->keys());
|
||||
self::assertStringContainsString('1', $response->headers->get('dav'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider generateDataPropfindDocument
|
||||
*/
|
||||
public function testPropfindDocument(string $requestContent, int $expectedStatusCode, string $expectedXmlResponse, string $message): void
|
||||
{
|
||||
$controller = $this->buildController();
|
||||
|
||||
$request = new Request([], [], [], [], [], [], $requestContent);
|
||||
$request->setMethod('PROPFIND');
|
||||
$response = $controller->propfindDocument($this->buildDocument(), '1234', $request);
|
||||
|
||||
self::assertEquals($expectedStatusCode, $response->getStatusCode());
|
||||
self::assertContains('content-type', $response->headers->keys());
|
||||
self::assertStringContainsString('text/xml', $response->headers->get('content-type'));
|
||||
self::assertTrue((new \DOMDocument())->loadXML($response->getContent()), $message.' test that the xml response is a valid xml');
|
||||
self::assertXmlStringEqualsXmlString($expectedXmlResponse, $response->getContent(), $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider generateDataPropfindDirectory
|
||||
*/
|
||||
public function testPropfindDirectory(string $requestContent, int $expectedStatusCode, string $expectedXmlResponse, string $message): void
|
||||
{
|
||||
$controller = $this->buildController();
|
||||
|
||||
$request = new Request([], [], [], [], [], [], $requestContent);
|
||||
$request->setMethod('PROPFIND');
|
||||
$request->headers->add(['Depth' => '0']);
|
||||
$response = $controller->propfindDirectory($this->buildDocument(), '1234', $request);
|
||||
|
||||
self::assertEquals($expectedStatusCode, $response->getStatusCode());
|
||||
self::assertContains('content-type', $response->headers->keys());
|
||||
self::assertStringContainsString('text/xml', $response->headers->get('content-type'));
|
||||
self::assertTrue((new \DOMDocument())->loadXML($response->getContent()), $message.' test that the xml response is a valid xml');
|
||||
self::assertXmlStringEqualsXmlString($expectedXmlResponse, $response->getContent(), $message);
|
||||
}
|
||||
|
||||
public function testHeadDocument(): void
|
||||
{
|
||||
$controller = $this->buildController();
|
||||
$response = $controller->headDocument($this->buildDocument());
|
||||
|
||||
self::assertEquals(200, $response->getStatusCode());
|
||||
self::assertContains('content-length', $response->headers->keys());
|
||||
self::assertContains('content-type', $response->headers->keys());
|
||||
self::assertContains('etag', $response->headers->keys());
|
||||
self::assertEquals('ab56b4d92b40713acc5af89985d4b786', $response->headers->get('etag'));
|
||||
self::assertEquals('application/vnd.oasis.opendocument.text', $response->headers->get('content-type'));
|
||||
self::assertEquals(5, $response->headers->get('content-length'));
|
||||
}
|
||||
|
||||
public static function generateDataPropfindDocument(): iterable
|
||||
{
|
||||
$content =
|
||||
<<<'XML'
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<propfind xmlns="DAV:"><prop><resourcetype xmlns="DAV:"/><IsReadOnly xmlns="http://ucb.openoffice.org/dav/props/"/><getcontenttype xmlns="DAV:"/><supportedlock xmlns="DAV:"/></prop></propfind>
|
||||
XML;
|
||||
|
||||
$response =
|
||||
<<<'XML'
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<d:multistatus xmlns:d="DAV:" >
|
||||
<d:response>
|
||||
<d:href>/dav/1234/get/716e6688-4579-4938-acf3-c4ab5856803b/d</d:href>
|
||||
<d:propstat>
|
||||
<d:prop>
|
||||
<d:resourcetype/>
|
||||
<d:getcontenttype>application/vnd.oasis.opendocument.text</d:getcontenttype>
|
||||
</d:prop>
|
||||
<d:status>HTTP/1.1 200 OK</d:status>
|
||||
</d:propstat>
|
||||
<d:propstat>
|
||||
<d:prop xmlns:ns0="http://ucb.openoffice.org/dav/props/">
|
||||
<ns0:IsReadOnly/>
|
||||
</d:prop>
|
||||
<d:status>HTTP/1.1 404 Not Found</d:status>
|
||||
</d:propstat>
|
||||
</d:response>
|
||||
</d:multistatus>
|
||||
XML;
|
||||
|
||||
yield [$content, 207, $response, 'get IsReadOnly and contenttype from server'];
|
||||
|
||||
$content =
|
||||
<<<'XML'
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<propfind xmlns="DAV:">
|
||||
<prop>
|
||||
<IsReadOnly xmlns="http://ucb.openoffice.org/dav/props/"/>
|
||||
</prop>
|
||||
</propfind>
|
||||
XML;
|
||||
|
||||
$response =
|
||||
<<<'XML'
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<d:multistatus xmlns:d="DAV:">
|
||||
<d:response>
|
||||
<d:href>/dav/1234/get/716e6688-4579-4938-acf3-c4ab5856803b/d</d:href>
|
||||
<d:propstat>
|
||||
<d:prop xmlns:ns0="http://ucb.openoffice.org/dav/props/">
|
||||
<ns0:IsReadOnly/>
|
||||
</d:prop>
|
||||
<d:status>HTTP/1.1 404 Not Found</d:status>
|
||||
</d:propstat>
|
||||
</d:response>
|
||||
</d:multistatus>
|
||||
XML;
|
||||
|
||||
yield [$content, 207, $response, 'get property IsReadOnly'];
|
||||
|
||||
yield [
|
||||
<<<'XML'
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<propfind xmlns="DAV:">
|
||||
<prop>
|
||||
<BaseURI xmlns="http://ucb.openoffice.org/dav/props/"/>
|
||||
</prop>
|
||||
</propfind>
|
||||
XML,
|
||||
207,
|
||||
<<<'XML'
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<d:multistatus xmlns:d="DAV:">
|
||||
<d:response>
|
||||
<d:href>/dav/1234/get/716e6688-4579-4938-acf3-c4ab5856803b/d</d:href>
|
||||
<d:propstat>
|
||||
<d:prop xmlns:ns0="http://ucb.openoffice.org/dav/props/">
|
||||
<ns0:BaseURI/>
|
||||
</d:prop>
|
||||
<d:status>HTTP/1.1 404 Not Found</d:status>
|
||||
</d:propstat>
|
||||
</d:response>
|
||||
</d:multistatus>
|
||||
XML,
|
||||
'Test requesting an unknow property',
|
||||
];
|
||||
|
||||
yield [
|
||||
<<<'XML'
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<propfind xmlns="DAV:">
|
||||
<prop>
|
||||
<getlastmodified xmlns="DAV:"/>
|
||||
</prop>
|
||||
</propfind>
|
||||
XML,
|
||||
207,
|
||||
<<<'XML'
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<d:multistatus xmlns:d="DAV:">
|
||||
<d:response>
|
||||
<d:href>/dav/1234/get/716e6688-4579-4938-acf3-c4ab5856803b/d</d:href>
|
||||
<d:propstat>
|
||||
<d:prop>
|
||||
<!-- the date scraped from a webserver is >Sun, 10 Sep 2023 14:10:23 GMT -->
|
||||
<d:getlastmodified>Wed, 13 Sep 2023 14:15:00 +0200</d:getlastmodified>
|
||||
</d:prop>
|
||||
<d:status>HTTP/1.1 200 OK</d:status>
|
||||
</d:propstat>
|
||||
</d:response>
|
||||
</d:multistatus>
|
||||
XML,
|
||||
'test getting the last modified date',
|
||||
];
|
||||
|
||||
yield [
|
||||
<<<'XML'
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<propfind xmlns="DAV:">
|
||||
<propname/>
|
||||
</propfind>
|
||||
XML,
|
||||
207,
|
||||
<<<'XML'
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<d:multistatus xmlns:d="DAV:">
|
||||
<d:response>
|
||||
<d:href>/dav/1234/get/716e6688-4579-4938-acf3-c4ab5856803b/d</d:href>
|
||||
<d:propstat>
|
||||
<d:prop>
|
||||
<d:resourcetype/>
|
||||
<d:creationdate/>
|
||||
<d:getlastmodified>Wed, 13 Sep 2023 14:15:00 +0200</d:getlastmodified>
|
||||
<!-- <d:getcontentlength/> -->
|
||||
<d:getcontentlength>5</d:getcontentlength>
|
||||
<!-- <d:getlastmodified/> -->
|
||||
<d:getetag>"ab56b4d92b40713acc5af89985d4b786"</d:getetag>
|
||||
<!--
|
||||
<d:supportedlock/>
|
||||
<d:lockdiscovery/>
|
||||
-->
|
||||
<!-- <d:getcontenttype/> -->
|
||||
<d:getcontenttype>application/vnd.oasis.opendocument.text</d:getcontenttype>
|
||||
</d:prop>
|
||||
<d:status>HTTP/1.1 200 OK</d:status>
|
||||
</d:propstat>
|
||||
</d:response>
|
||||
</d:multistatus>
|
||||
XML,
|
||||
'test finding all properties',
|
||||
];
|
||||
}
|
||||
|
||||
public static function generateDataPropfindDirectory(): iterable
|
||||
{
|
||||
yield [
|
||||
<<<'XML'
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<propfind xmlns="DAV:"><prop><resourcetype xmlns="DAV:"/><IsReadOnly xmlns="http://ucb.openoffice.org/dav/props/"/><getcontenttype xmlns="DAV:"/><supportedlock xmlns="DAV:"/></prop></propfind>
|
||||
XML,
|
||||
207,
|
||||
<<<'XML'
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<d:multistatus xmlns:d="DAV:">
|
||||
<d:response>
|
||||
<d:href>/dav/1234/get/716e6688-4579-4938-acf3-c4ab5856803b/</d:href>
|
||||
<d:propstat>
|
||||
<d:prop>
|
||||
<d:resourcetype><d:collection/></d:resourcetype>
|
||||
<d:getcontenttype>httpd/unix-directory</d:getcontenttype>
|
||||
<!--
|
||||
<d:supportedlock>
|
||||
<d:lockentry>
|
||||
<d:lockscope><d:exclusive/></d:lockscope>
|
||||
<d:locktype><d:write/></d:locktype>
|
||||
</d:lockentry>
|
||||
<d:lockentry>
|
||||
<d:lockscope><d:shared/></d:lockscope>
|
||||
<d:locktype><d:write/></d:locktype>
|
||||
</d:lockentry>
|
||||
</d:supportedlock>
|
||||
-->
|
||||
</d:prop>
|
||||
<d:status>HTTP/1.1 200 OK</d:status>
|
||||
</d:propstat>
|
||||
<d:propstat>
|
||||
<d:prop xmlns:ns0="http://ucb.openoffice.org/dav/props/">
|
||||
<ns0:IsReadOnly/>
|
||||
</d:prop>
|
||||
<d:status>HTTP/1.1 404 Not Found</d:status>
|
||||
</d:propstat>
|
||||
</d:response>
|
||||
</d:multistatus>
|
||||
XML,
|
||||
'test resourceType and IsReadOnly ',
|
||||
];
|
||||
|
||||
yield [
|
||||
<<<'XML'
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<propfind xmlns="DAV:"><prop><CreatableContentsInfo xmlns="http://ucb.openoffice.org/dav/props/"/></prop></propfind>
|
||||
XML,
|
||||
207,
|
||||
<<<'XML'
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<d:multistatus xmlns:d="DAV:">
|
||||
<d:response>
|
||||
<d:href>/dav/1234/get/716e6688-4579-4938-acf3-c4ab5856803b/</d:href>
|
||||
<d:propstat>
|
||||
<d:prop xmlns:ns0="http://ucb.openoffice.org/dav/props/" >
|
||||
<ns0:CreatableContentsInfo/>
|
||||
</d:prop>
|
||||
<d:status>HTTP/1.1 404 Not Found</d:status>
|
||||
</d:propstat>
|
||||
</d:response>
|
||||
</d:multistatus>
|
||||
XML,
|
||||
'test creatableContentsInfo',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
class MockedStoredObjectManager implements StoredObjectManagerInterface
|
||||
{
|
||||
public function getLastModified(StoredObject $document): \DateTimeInterface
|
||||
{
|
||||
return new \DateTimeImmutable('2023-09-13T14:15');
|
||||
}
|
||||
|
||||
public function getContentLength(StoredObject $document): int
|
||||
{
|
||||
return 5;
|
||||
}
|
||||
|
||||
public function read(StoredObject $document): string
|
||||
{
|
||||
return 'abcde';
|
||||
}
|
||||
|
||||
public function write(StoredObject $document, string $clearContent): void
|
||||
{
|
||||
}
|
||||
|
||||
public function etag(StoredObject $document): string
|
||||
{
|
||||
return 'ab56b4d92b40713acc5af89985d4b786';
|
||||
}
|
||||
|
||||
public function clearCache(): void
|
||||
{
|
||||
}
|
||||
}
|
@@ -0,0 +1,134 @@
|
||||
<?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\Dav\Request;
|
||||
|
||||
use Chill\DocStoreBundle\Dav\Request\PropfindRequestAnalyzer;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @coversNothing
|
||||
*/
|
||||
class PropfindRequestAnalyzerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideRequestedProperties
|
||||
*/
|
||||
public function testGetRequestedProperties(string $xml, array $expected): void
|
||||
{
|
||||
$analyzer = new PropfindRequestAnalyzer();
|
||||
|
||||
$request = new \DOMDocument();
|
||||
$request->loadXML($xml);
|
||||
$actual = $analyzer->getRequestedProperties($request);
|
||||
|
||||
foreach ($expected as $key => $value) {
|
||||
if ('unknowns' === $key) {
|
||||
continue;
|
||||
}
|
||||
|
||||
self::assertArrayHasKey($key, $actual, "Check that key {$key} does exists in list of expected values");
|
||||
self::assertEquals($value, $actual[$key], "Does the value match expected for key {$key}");
|
||||
}
|
||||
|
||||
if (array_key_exists('unknowns', $expected)) {
|
||||
self::assertEquals(count($expected['unknowns']), count($actual['unknowns']));
|
||||
self::assertEqualsCanonicalizing($expected['unknowns'], $actual['unknowns']);
|
||||
}
|
||||
}
|
||||
|
||||
public function provideRequestedProperties(): iterable
|
||||
{
|
||||
yield [
|
||||
<<<'XML'
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<propfind xmlns="DAV:">
|
||||
<prop>
|
||||
<BaseURI xmlns="http://ucb.openoffice.org/dav/props/"/>
|
||||
</prop>
|
||||
</propfind>
|
||||
XML,
|
||||
[
|
||||
'resourceType' => false,
|
||||
'contentType' => false,
|
||||
'lastModified' => false,
|
||||
'creationDate' => false,
|
||||
'contentLength' => false,
|
||||
'etag' => false,
|
||||
'supportedLock' => false,
|
||||
'unknowns' => [
|
||||
['xmlns' => 'http://ucb.openoffice.org/dav/props/', 'prop' => 'BaseURI'],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
yield [
|
||||
<<<'XML'
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<propfind xmlns="DAV:">
|
||||
<propname/>
|
||||
</propfind>
|
||||
XML,
|
||||
[
|
||||
'resourceType' => true,
|
||||
'contentType' => true,
|
||||
'lastModified' => true,
|
||||
'creationDate' => true,
|
||||
'contentLength' => true,
|
||||
'etag' => true,
|
||||
'supportedLock' => true,
|
||||
'unknowns' => [],
|
||||
],
|
||||
];
|
||||
|
||||
yield [
|
||||
<<<'XML'
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<propfind xmlns="DAV:">
|
||||
<prop>
|
||||
<getlastmodified xmlns="DAV:"/>
|
||||
</prop>
|
||||
</propfind>
|
||||
XML,
|
||||
[
|
||||
'resourceType' => false,
|
||||
'contentType' => false,
|
||||
'lastModified' => true,
|
||||
'creationDate' => false,
|
||||
'contentLength' => false,
|
||||
'etag' => false,
|
||||
'supportedLock' => false,
|
||||
'unknowns' => [],
|
||||
],
|
||||
];
|
||||
|
||||
yield [
|
||||
<<<'XML'
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<propfind xmlns="DAV:"><prop><resourcetype xmlns="DAV:"/><IsReadOnly xmlns="http://ucb.openoffice.org/dav/props/"/><getcontenttype xmlns="DAV:"/><supportedlock xmlns="DAV:"/></prop></propfind>
|
||||
XML,
|
||||
[
|
||||
'resourceType' => true,
|
||||
'contentType' => true,
|
||||
'lastModified' => false,
|
||||
'creationDate' => false,
|
||||
'contentLength' => false,
|
||||
'etag' => false,
|
||||
'supportedLock' => false,
|
||||
'unknowns' => [
|
||||
['xmlns' => 'http://ucb.openoffice.org/dav/props/', 'prop' => 'IsReadOnly'],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
@@ -0,0 +1,105 @@
|
||||
<?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\Form;
|
||||
|
||||
use Chill\DocStoreBundle\Entity\StoredObject;
|
||||
use Chill\DocStoreBundle\Form\DataMapper\StoredObjectDataMapper;
|
||||
use Chill\DocStoreBundle\Form\DataTransformer\StoredObjectDataTransformer;
|
||||
use Chill\DocStoreBundle\Form\StoredObjectType;
|
||||
use Chill\DocStoreBundle\Security\Authorization\StoredObjectRoleEnum;
|
||||
use Chill\DocStoreBundle\Security\Guard\JWTDavTokenProviderInterface;
|
||||
use Chill\DocStoreBundle\Serializer\Normalizer\StoredObjectNormalizer;
|
||||
use Prophecy\Argument;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
use Symfony\Component\Form\PreloadedExtension;
|
||||
use Symfony\Component\Form\Test\TypeTestCase;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Symfony\Component\Serializer\Encoder\JsonEncoder;
|
||||
use Symfony\Component\Serializer\Serializer;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @coversNothing
|
||||
*/
|
||||
class StoredObjectTypeTest extends TypeTestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
public function testChangeTitleValue(): void
|
||||
{
|
||||
$formData = ['title' => $newTitle = 'new title', 'stored_object' => <<<'JSON'
|
||||
{"datas":[],"filename":"","id":null,"iv":[],"keyInfos":[],"title":"","type":"","uuid":"3c6a28fe-f913-40b9-a201-5eccc4f2d312","status":"ready","createdAt":null,"createdBy":null,"creationDate":null,"_links":{"dav_link":{"href":"http:\/\/url\/fake","expiration":"1716889578"}}}
|
||||
JSON];
|
||||
$model = new StoredObject();
|
||||
$form = $this->factory->create(StoredObjectType::class, $model, ['has_title' => true]);
|
||||
|
||||
$form->submit($formData);
|
||||
|
||||
$this->assertTrue($form->isSynchronized());
|
||||
|
||||
$this->assertEquals($newTitle, $model->getTitle());
|
||||
}
|
||||
|
||||
public function testReplaceByAnotherObject(): void
|
||||
{
|
||||
$formData = ['title' => $newTitle = 'new title', 'stored_object' => <<<'JSON'
|
||||
{"filename":"abcdef","iv":[10, 15, 20, 30],"keyInfos":[],"type":"text/html","status":"object_store_created"}
|
||||
JSON];
|
||||
$model = new StoredObject();
|
||||
$originalObjectId = spl_object_id($model);
|
||||
$form = $this->factory->create(StoredObjectType::class, $model, ['has_title' => true]);
|
||||
|
||||
$form->submit($formData);
|
||||
|
||||
$this->assertTrue($form->isSynchronized());
|
||||
$model = $form->getData();
|
||||
$this->assertNotEquals($originalObjectId, spl_object_hash($model));
|
||||
$this->assertEquals('abcdef', $model->getFilename());
|
||||
$this->assertEquals([10, 15, 20, 30], $model->getIv());
|
||||
$this->assertEquals('text/html', $model->getType());
|
||||
$this->assertEquals($newTitle, $model->getTitle());
|
||||
}
|
||||
|
||||
protected function getExtensions()
|
||||
{
|
||||
$jwtTokenProvider = $this->prophesize(JWTDavTokenProviderInterface::class);
|
||||
$jwtTokenProvider->createToken(Argument::type(StoredObject::class), Argument::type(StoredObjectRoleEnum::class))
|
||||
->willReturn('token');
|
||||
$jwtTokenProvider->getTokenExpiration('token')->willReturn(new \DateTimeImmutable());
|
||||
$urlGenerator = $this->prophesize(UrlGeneratorInterface::class);
|
||||
$urlGenerator->generate('chill_docstore_dav_document_get', Argument::type('array'), UrlGeneratorInterface::ABSOLUTE_URL)
|
||||
->willReturn('http://url/fake');
|
||||
|
||||
$serializer = new Serializer(
|
||||
[
|
||||
new StoredObjectNormalizer(
|
||||
$jwtTokenProvider->reveal(),
|
||||
$urlGenerator->reveal(),
|
||||
),
|
||||
],
|
||||
[
|
||||
new JsonEncoder(),
|
||||
]
|
||||
);
|
||||
$dataTransformer = new StoredObjectDataTransformer($serializer);
|
||||
$dataMapper = new StoredObjectDataMapper();
|
||||
$type = new StoredObjectType(
|
||||
$dataTransformer,
|
||||
$dataMapper,
|
||||
);
|
||||
|
||||
return [
|
||||
new PreloadedExtension([$type], []),
|
||||
];
|
||||
}
|
||||
}
|
@@ -0,0 +1,123 @@
|
||||
<?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\Security\Authorization;
|
||||
|
||||
use Chill\DocStoreBundle\Entity\StoredObject;
|
||||
use Chill\DocStoreBundle\Security\Authorization\StoredObjectRoleEnum;
|
||||
use Chill\DocStoreBundle\Security\Authorization\StoredObjectVoter;
|
||||
use Chill\DocStoreBundle\Security\Guard\DavTokenAuthenticationEventSubscriber;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @coversNothing
|
||||
*/
|
||||
class StoredObjectVoterTest extends TestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
/**
|
||||
* @dataProvider provideDataVote
|
||||
*/
|
||||
public function testVote(TokenInterface $token, ?object $subject, string $attribute, mixed $expected): void
|
||||
{
|
||||
$voter = new StoredObjectVoter();
|
||||
|
||||
self::assertEquals($expected, $voter->vote($token, $subject, [$attribute]));
|
||||
}
|
||||
|
||||
public function provideDataVote(): iterable
|
||||
{
|
||||
yield [
|
||||
$this->buildToken(StoredObjectRoleEnum::EDIT, new StoredObject()),
|
||||
new \stdClass(),
|
||||
'SOMETHING',
|
||||
VoterInterface::ACCESS_ABSTAIN,
|
||||
];
|
||||
|
||||
yield [
|
||||
$this->buildToken(StoredObjectRoleEnum::EDIT, $so = new StoredObject()),
|
||||
$so,
|
||||
'SOMETHING',
|
||||
VoterInterface::ACCESS_ABSTAIN,
|
||||
];
|
||||
|
||||
yield [
|
||||
$this->buildToken(StoredObjectRoleEnum::EDIT, $so = new StoredObject()),
|
||||
$so,
|
||||
StoredObjectRoleEnum::SEE->value,
|
||||
VoterInterface::ACCESS_GRANTED,
|
||||
];
|
||||
|
||||
yield [
|
||||
$this->buildToken(StoredObjectRoleEnum::EDIT, $so = new StoredObject()),
|
||||
$so,
|
||||
StoredObjectRoleEnum::EDIT->value,
|
||||
VoterInterface::ACCESS_GRANTED,
|
||||
];
|
||||
|
||||
yield [
|
||||
$this->buildToken(StoredObjectRoleEnum::SEE, $so = new StoredObject()),
|
||||
$so,
|
||||
StoredObjectRoleEnum::EDIT->value,
|
||||
VoterInterface::ACCESS_DENIED,
|
||||
];
|
||||
|
||||
yield [
|
||||
$this->buildToken(StoredObjectRoleEnum::SEE, $so = new StoredObject()),
|
||||
$so,
|
||||
StoredObjectRoleEnum::SEE->value,
|
||||
VoterInterface::ACCESS_GRANTED,
|
||||
];
|
||||
|
||||
yield [
|
||||
$this->buildToken(null, null),
|
||||
new StoredObject(),
|
||||
StoredObjectRoleEnum::SEE->value,
|
||||
VoterInterface::ACCESS_DENIED,
|
||||
];
|
||||
|
||||
yield [
|
||||
$this->buildToken(null, null),
|
||||
new StoredObject(),
|
||||
StoredObjectRoleEnum::SEE->value,
|
||||
VoterInterface::ACCESS_DENIED,
|
||||
];
|
||||
}
|
||||
|
||||
private function buildToken(?StoredObjectRoleEnum $storedObjectRoleEnum = null, ?StoredObject $storedObject = null): TokenInterface
|
||||
{
|
||||
$token = $this->prophesize(TokenInterface::class);
|
||||
|
||||
if (null !== $storedObjectRoleEnum) {
|
||||
$token->hasAttribute(DavTokenAuthenticationEventSubscriber::ACTIONS)->willReturn(true);
|
||||
$token->getAttribute(DavTokenAuthenticationEventSubscriber::ACTIONS)->willReturn($storedObjectRoleEnum);
|
||||
} else {
|
||||
$token->hasAttribute(DavTokenAuthenticationEventSubscriber::ACTIONS)->willReturn(false);
|
||||
$token->getAttribute(DavTokenAuthenticationEventSubscriber::ACTIONS)->willThrow(new \InvalidArgumentException());
|
||||
}
|
||||
|
||||
if (null !== $storedObject) {
|
||||
$token->hasAttribute(DavTokenAuthenticationEventSubscriber::STORED_OBJECT)->willReturn(true);
|
||||
$token->getAttribute(DavTokenAuthenticationEventSubscriber::STORED_OBJECT)->willReturn($storedObject->getUuid()->toString());
|
||||
} else {
|
||||
$token->hasAttribute(DavTokenAuthenticationEventSubscriber::STORED_OBJECT)->willReturn(false);
|
||||
$token->getAttribute(DavTokenAuthenticationEventSubscriber::STORED_OBJECT)->willThrow(new \InvalidArgumentException());
|
||||
}
|
||||
|
||||
return $token->reveal();
|
||||
}
|
||||
}
|
@@ -0,0 +1,55 @@
|
||||
<?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\Security\Guard;
|
||||
|
||||
use Chill\DocStoreBundle\Security\Guard\DavOnUrlTokenExtractor;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
use Psr\Log\NullLogger;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @coversNothing
|
||||
*/
|
||||
class DavOnUrlTokenExtractorTest extends TestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
/**
|
||||
* @dataProvider provideDataUri
|
||||
*/
|
||||
public function testExtract(string $uri, false|string $expected): void
|
||||
{
|
||||
$request = $this->prophesize(Request::class);
|
||||
$request->getRequestUri()->willReturn($uri);
|
||||
|
||||
$extractor = new DavOnUrlTokenExtractor(new NullLogger());
|
||||
|
||||
$actual = $extractor->extract($request->reveal());
|
||||
|
||||
self::assertEquals($expected, $actual);
|
||||
}
|
||||
|
||||
/**
|
||||
* @phpstan-pure
|
||||
*/
|
||||
public static function provideDataUri(): iterable
|
||||
{
|
||||
yield ['/dav/123456789/get/d07d2230-5326-11ee-8fd4-93696acf5ea1/d', '123456789'];
|
||||
yield ['/dav/123456789', '123456789'];
|
||||
yield ['/not-dav/123456978', false];
|
||||
yield ['/dav', false];
|
||||
yield ['/', false];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user