From 1b16d4fe3bc8da05f68184a9bd58f39d7f657379 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 11 Jul 2024 14:48:53 +0200 Subject: [PATCH] Flush entities when storing document using webdav / put operation The WebdavController has been updated to flush the EntityManager after writing a document, while its tests have been adjusted correspondingly. A new test for the document PUT operation has also been added, which ensures the EntityManager flushes and the StoredObjectManager writes to this document. --- .../Controller/WebdavController.php | 4 ++ .../Tests/Controller/WebdavControllerTest.php | 37 ++++++++++++++++--- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/Bundle/ChillDocStoreBundle/Controller/WebdavController.php b/src/Bundle/ChillDocStoreBundle/Controller/WebdavController.php index 6e105f9ee..022d544cb 100644 --- a/src/Bundle/ChillDocStoreBundle/Controller/WebdavController.php +++ b/src/Bundle/ChillDocStoreBundle/Controller/WebdavController.php @@ -16,6 +16,7 @@ use Chill\DocStoreBundle\Dav\Response\DavResponse; use Chill\DocStoreBundle\Entity\StoredObject; use Chill\DocStoreBundle\Security\Authorization\StoredObjectRoleEnum; use Chill\DocStoreBundle\Service\StoredObjectManagerInterface; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; @@ -42,6 +43,7 @@ final readonly class WebdavController private \Twig\Environment $engine, private StoredObjectManagerInterface $storedObjectManager, private Security $security, + private EntityManagerInterface $entityManager, ) { $this->requestAnalyzer = new PropfindRequestAnalyzer(); } @@ -201,6 +203,8 @@ final readonly class WebdavController $this->storedObjectManager->write($storedObject, $request->getContent()); + $this->entityManager->flush(); + return new DavResponse('', Response::HTTP_NO_CONTENT); } diff --git a/src/Bundle/ChillDocStoreBundle/Tests/Controller/WebdavControllerTest.php b/src/Bundle/ChillDocStoreBundle/Tests/Controller/WebdavControllerTest.php index 5d6565108..cf1d8c814 100644 --- a/src/Bundle/ChillDocStoreBundle/Tests/Controller/WebdavControllerTest.php +++ b/src/Bundle/ChillDocStoreBundle/Tests/Controller/WebdavControllerTest.php @@ -40,17 +40,20 @@ class WebdavControllerTest extends KernelTestCase $this->engine = self::getContainer()->get(\Twig\Environment::class); } - private function buildController(EntityManagerInterface $entityManager = null): WebdavController + private function buildController(?EntityManagerInterface $entityManager = null, ?StoredObjectManagerInterface $storedObjectManager = null): WebdavController { - $storedObjectManager = new MockedStoredObjectManager(); - $security = $this->prophesize(Security::class); - $security->isGranted(Argument::in(['EDIT', 'SEE']), Argument::type(StoredObject::class)) - ->willReturn(true); + if (null === $storedObjectManager) { + $storedObjectManager = new MockedStoredObjectManager(); + } if (null === $entityManager) { $entityManager = $this->createMock(EntityManagerInterface::class); } + $security = $this->prophesize(Security::class); + $security->isGranted(Argument::in(['SEE_AND_EDIT', 'SEE']), Argument::type(StoredObject::class)) + ->willReturn(true); + return new WebdavController($this->engine, $storedObjectManager, $security->reveal(), $entityManager); } @@ -165,6 +168,30 @@ class WebdavControllerTest extends KernelTestCase self::assertEquals(5, $response->headers->get('content-length')); } + public function testPutDocument(): void + { + $document = $this->buildDocument(); + $entityManager = $this->createMock(EntityManagerInterface::class); + $storedObjectManager = $this->createMock(StoredObjectManagerInterface::class); + + // entity manager must be flushed + $entityManager->expects($this->once()) + ->method('flush'); + + // object must be written by StoredObjectManager + $storedObjectManager->expects($this->once()) + ->method('write') + ->with($this->identicalTo($document), $this->identicalTo('1234')); + + $controller = $this->buildController($entityManager, $storedObjectManager); + + $request = new Request(content: '1234'); + $response = $controller->putDocument($document, $request); + + self::assertEquals(204, $response->getStatusCode()); + self::assertEquals('', $response->getContent()); + } + public static function generateDataPropfindDocument(): iterable { $content =