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); });