feat(comments) : add file attachments on comments

Comments can now have documents attached via multipart/form-data upload.
New endpoint GET /api/documents/comment/{id} to list a comment's files.
Document entity gains a comment relation with cascade remove.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Matthieu
2026-03-24 08:49:46 +01:00
parent 4468fd7cdf
commit 330b9376f6
7 changed files with 315 additions and 9 deletions

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Tests\Api\Controller;
use App\Tests\AbstractApiTestCase;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* @internal
@@ -147,4 +148,99 @@ class CommentControllerTest extends AbstractApiTestCase
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['count' => 2]);
}
public function testCreateCommentJsonReturnsDocumentsArray(): void
{
$machine = $this->createMachine('Machine A');
$client = $this->createViewerClient();
$response = $client->request('POST', '/api/comments', [
'json' => [
'content' => 'No files',
'entityType' => 'machine',
'entityId' => $machine->getId(),
],
]);
$this->assertResponseStatusCodeSame(201);
$data = $response->toArray();
$this->assertSame([], $data['documents']);
}
public function testCreateCommentWithFile(): void
{
$machine = $this->createMachine('Machine A');
$client = $this->createViewerClient();
$tmpFile = tempnam(sys_get_temp_dir(), 'test_');
file_put_contents($tmpFile, 'test file content');
$uploadedFile = new UploadedFile(
$tmpFile,
'test-doc.pdf',
'application/pdf',
null,
true,
);
$client->request('POST', '/api/comments', [
'extra' => [
'parameters' => [
'content' => 'Comment with file',
'entityType' => 'machine',
'entityId' => $machine->getId(),
'entityName' => 'Machine A',
],
'files' => [
'files' => [$uploadedFile],
],
],
]);
$this->assertResponseStatusCodeSame(201);
$data = json_decode($client->getResponse()->getContent(), true);
$this->assertSame('Comment with file', $data['content']);
$this->assertCount(1, $data['documents']);
$this->assertSame('test-doc.pdf', $data['documents'][0]['filename']);
$this->assertArrayHasKey('fileUrl', $data['documents'][0]);
$this->assertArrayHasKey('downloadUrl', $data['documents'][0]);
@unlink($tmpFile);
}
public function testCreateCommentWithMultipleFiles(): void
{
$machine = $this->createMachine('Machine A');
$client = $this->createViewerClient();
$tmpFile1 = tempnam(sys_get_temp_dir(), 'test_');
file_put_contents($tmpFile1, 'content 1');
$tmpFile2 = tempnam(sys_get_temp_dir(), 'test_');
file_put_contents($tmpFile2, 'content 2');
$file1 = new UploadedFile($tmpFile1, 'doc1.pdf', 'application/pdf', null, true);
$file2 = new UploadedFile($tmpFile2, 'doc2.png', 'image/png', null, true);
$client->request('POST', '/api/comments', [
'extra' => [
'parameters' => [
'content' => 'Multiple files',
'entityType' => 'machine',
'entityId' => $machine->getId(),
],
'files' => [
'files' => [$file1, $file2],
],
],
]);
$this->assertResponseStatusCodeSame(201);
$data = json_decode($client->getResponse()->getContent(), true);
$this->assertCount(2, $data['documents']);
@unlink($tmpFile1);
@unlink($tmpFile2);
}
}