From 5ec6e49af2a1bf31492382897e1712c31c91a104 Mon Sep 17 00:00:00 2001 From: Matthieu Date: Mon, 23 Mar 2026 15:20:39 +0100 Subject: [PATCH] feat(documents) : accept type on upload + expose in query controller + PATCH tests Co-Authored-By: Claude Opus 4.6 (1M context) --- src/Controller/DocumentQueryController.php | 1 + src/State/DocumentUploadProcessor.php | 5 +++ tests/Api/Entity/DocumentTest.php | 52 ++++++++++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/src/Controller/DocumentQueryController.php b/src/Controller/DocumentQueryController.php index 0055d20..55824bf 100644 --- a/src/Controller/DocumentQueryController.php +++ b/src/Controller/DocumentQueryController.php @@ -121,6 +121,7 @@ class DocumentQueryController extends AbstractController 'composantId' => $document->getComposant()?->getId(), 'pieceId' => $document->getPiece()?->getId(), 'productId' => $document->getProduct()?->getId(), + 'type' => $document->getType()->value, 'createdAt' => $document->getCreatedAt()->format(DATE_ATOM), 'updatedAt' => $document->getUpdatedAt()->format(DATE_ATOM), ]; diff --git a/src/State/DocumentUploadProcessor.php b/src/State/DocumentUploadProcessor.php index 2d67088..62215db 100644 --- a/src/State/DocumentUploadProcessor.php +++ b/src/State/DocumentUploadProcessor.php @@ -7,6 +7,7 @@ namespace App\State; use ApiPlatform\Metadata\Operation; use ApiPlatform\State\ProcessorInterface; use App\Entity\Document; +use App\Enum\DocumentType; use App\Service\DocumentStorageService; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\DependencyInjection\Attribute\Autowire; @@ -76,6 +77,10 @@ final class DocumentUploadProcessor implements ProcessorInterface $document->setMimeType($mimeType); $document->setSize((int) $size); + $typeValue = $request->request->get('type', 'documentation'); + $docType = DocumentType::tryFrom($typeValue) ?? DocumentType::DOCUMENTATION; + $document->setType($docType); + // Handle entity relations from form fields $this->setRelationsFromRequest($document, $request); diff --git a/tests/Api/Entity/DocumentTest.php b/tests/Api/Entity/DocumentTest.php index 91b1a1c..adfdbd4 100644 --- a/tests/Api/Entity/DocumentTest.php +++ b/tests/Api/Entity/DocumentTest.php @@ -114,6 +114,58 @@ class DocumentTest extends AbstractApiTestCase $this->assertJsonContains(['totalItems' => 1]); } + public function testPatchType(): void + { + $doc = $this->createDocumentInDb(); + + $client = $this->createGestionnaireClient(); + $client->request('PATCH', self::iri('documents', $doc->getId()), [ + 'headers' => ['Content-Type' => 'application/merge-patch+json'], + 'json' => ['type' => 'devis'], + ]); + + $this->assertResponseIsSuccessful(); + $this->assertJsonContains(['type' => 'devis']); + } + + public function testPatchNameAndType(): void + { + $doc = $this->createDocumentInDb(); + + $client = $this->createGestionnaireClient(); + $client->request('PATCH', self::iri('documents', $doc->getId()), [ + 'headers' => ['Content-Type' => 'application/merge-patch+json'], + 'json' => ['name' => 'new-name', 'type' => 'facture'], + ]); + + $this->assertResponseIsSuccessful(); + $this->assertJsonContains(['name' => 'new-name', 'type' => 'facture']); + } + + public function testGetItemIncludesType(): void + { + $doc = $this->createDocumentInDb(); + + $client = $this->createViewerClient(); + $client->request('GET', self::iri('documents', $doc->getId())); + + $this->assertResponseIsSuccessful(); + $this->assertJsonContains(['type' => 'documentation']); + } + + public function testViewerCannotPatch(): void + { + $doc = $this->createDocumentInDb(); + + $client = $this->createViewerClient(); + $client->request('PATCH', self::iri('documents', $doc->getId()), [ + 'headers' => ['Content-Type' => 'application/merge-patch+json'], + 'json' => ['type' => 'devis'], + ]); + + $this->assertResponseStatusCodeSame(403); + } + private function createDocumentInDb(?string $machineId = null, ?string $siteId = null): Document { $doc = new Document();