Add comments to tasks

Everyone who may change a task's status (attached subcontractors too)
comments on it on the job page. Authors edit and remove their own
comments; admin and management may remove anyone's
(jobs.remove-any-task-comment). Edited comments show when they were
edited, and task cards show the number of comments.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-25 07:31:53 +02:00
co-authored by Claude Sonnet 5.5
parent 60cab4b871
commit 4fb8ea1c5f
29 changed files with 1354 additions and 4 deletions
@@ -0,0 +1,83 @@
<?php
use App\Modules\Jobs\Application\Services\TaskCommentFinder;
use App\Modules\Jobs\Application\UseCases\AddTaskComment;
use App\Modules\Jobs\Application\UseCases\EditTaskComment;
use App\Modules\Jobs\Application\UseCases\RemoveTaskComment;
use App\Modules\Jobs\Domain\Entities\Task;
use App\Modules\Jobs\Domain\Enums\TaskStatus;
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskCommentBody;
use App\Modules\Jobs\Domain\Exceptions\NotTaskCommentAuthor;
use App\Modules\Jobs\Domain\Exceptions\TaskCommentNotFound;
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
use App\Modules\Jobs\Domain\ValueObjects\TaskCommentBody;
use App\Modules\Jobs\Domain\ValueObjects\TaskTitle;
use Tests\Fakes\FakeClock;
use Tests\Fakes\FakeTransactionManager;
use Tests\Fakes\InMemoryTaskCommentRepository;
use Tests\Fakes\InMemoryTaskRepository;
beforeEach(function () {
$tasks = new InMemoryTaskRepository;
$this->comments = new InMemoryTaskCommentRepository;
$transactions = new FakeTransactionManager;
$finder = new TaskCommentFinder($tasks, $this->comments);
$this->clock = new FakeClock(new DateTimeImmutable('2026-11-25'));
$this->jobId = 5;
$this->taskId = (int) $tasks->save(Task::reconstitute(1, $this->jobId, 11, new TaskTitle('Kuvaus'), null, TaskStatus::Waiting, null, null, null, [], 10))->id();
$this->add = new AddTaskComment($finder, $this->comments, $transactions, $this->clock);
$this->edit = new EditTaskComment($finder, $this->comments, $transactions, $this->clock);
$this->remove = new RemoveTaskComment($finder, $this->comments, $transactions);
});
it('keeps line breaks of a comment and rejects empty and too long comments', function () {
expect((new TaskCommentBody(" Valot ok.\r\nMikit puuttuu. \n"))->value)->toBe("Valot ok.\nMikit puuttuu.")
->and(fn () => new TaskCommentBody(" \n "))->toThrow(InvalidTaskCommentBody::class)
->and(fn () => new TaskCommentBody(str_repeat('a', 5001)))->toThrow(InvalidTaskCommentBody::class);
});
it('adds a comment with its author and time', function () {
$commentId = $this->add->execute($this->jobId, $this->taskId, 10, 'Valot ok.');
$comment = $this->comments->comments[$commentId];
expect($comment->authorId())->toBe(10)
->and($comment->body()->value)->toBe('Valot ok.')
->and($comment->createdAt()->format('Y-m-d H:i'))->toBe('2026-11-25 12:00')
->and($comment->editedAt())->toBeNull();
});
it('lets only the author edit a comment and marks it edited', function () {
$commentId = $this->add->execute($this->jobId, $this->taskId, 10, 'Valot ok.');
$this->edit->execute($this->jobId, $this->taskId, $commentId, 10, 'Valot ok.');
expect($this->comments->comments[$commentId]->editedAt())->toBeNull();
$this->clock->today = new DateTimeImmutable('2026-11-26');
$this->edit->execute($this->jobId, $this->taskId, $commentId, 10, 'Valot ja mikit ok.');
expect($this->comments->comments[$commentId]->body()->value)->toBe('Valot ja mikit ok.')
->and($this->comments->comments[$commentId]->editedAt()?->format('Y-m-d'))->toBe('2026-11-26')
->and(fn () => $this->edit->execute($this->jobId, $this->taskId, $commentId, 12, 'Muutettu'))->toThrow(NotTaskCommentAuthor::class);
});
it('lets the author remove a comment, and others only when they may remove anyone\'s', function () {
$own = $this->add->execute($this->jobId, $this->taskId, 10, 'Oma');
$other = $this->add->execute($this->jobId, $this->taskId, 10, 'Toisen');
expect(fn () => $this->remove->execute($this->jobId, $this->taskId, $other, 12, false))->toThrow(NotTaskCommentAuthor::class);
$this->remove->execute($this->jobId, $this->taskId, $own, 10, false);
$this->remove->execute($this->jobId, $this->taskId, $other, 12, true);
expect($this->comments->comments)->toBe([]);
});
it('does not touch comments of a task through another job', function () {
$commentId = $this->add->execute($this->jobId, $this->taskId, 10, 'Valot ok.');
expect(fn () => $this->add->execute(99, $this->taskId, 10, 'Vieras'))->toThrow(TaskNotFound::class)
->and(fn () => $this->edit->execute(99, $this->taskId, $commentId, 10, 'Vieras'))->toThrow(TaskNotFound::class)
->and(fn () => $this->remove->execute($this->jobId, $this->taskId, 404, 10, true))->toThrow(TaskCommentNotFound::class);
});