Files
petriandClaude Sonnet 5.5 4fb8ea1c5f 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]>
2026-11-25 07:31:53 +02:00

149 lines
5.9 KiB
PHP

<?php
use App\Models\User;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskCommentModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->employee = User::factory()->create(['name' => 'Eetu Esimerkki']);
$this->actingAs($this->employee);
$this->job = CustomerJobModel::factory()->create(['status' => 'order', 'responsible_user_id' => $this->employee->id]);
$workType = WorkTypeModel::query()->where('name', 'graafinen työ')->sole();
$this->task = TaskModel::factory()->create(['customer_job_id' => $this->job->id, 'work_type_id' => $workType->id, 'title' => 'Mainoskuvat']);
$this->params = ['jobId' => $this->job->id, 'taskId' => $this->task->id, 'taskTitle' => 'Mainoskuvat'];
});
function taskComment(int $taskId, int $userId, string $body): TaskCommentModel
{
return TaskCommentModel::query()->create(['task_id' => $taskId, 'user_id' => $userId, 'body' => $body]);
}
it('shows the comments with author, time and their number on the job page', function () {
$this->travelTo(now()->setTimezone('Europe/Helsinki')->setDate(2026, 11, 25)->setTime(14, 5));
taskComment($this->task->id, $this->employee->id, 'Valot ok.');
taskComment($this->task->id, $this->employee->id, 'Mikit puuttuu.');
$this->get(route('jobs.show', $this->job->id))
->assertOk()
->assertSee('Kommentit:')
->assertSeeInOrder(['Eetu Esimerkki', '25.11.2026 klo 14.05', 'Valot ok.', 'Mikit puuttuu.']);
});
it('adds, edits and removes the user\'s own comment', function () {
$component = Livewire::test('jobs::task-comments', $this->params)
->set('newComment', " Valot ok.\nMikit puuttuu. ")
->call('add')
->assertHasNoErrors()
->assertSet('newComment', '')
->assertSee('Kommentti lisätty.')
->assertDispatched('task-comments-changed');
$comment = TaskCommentModel::query()->sole();
expect($comment->body)->toBe("Valot ok.\nMikit puuttuu.")
->and($comment->user_id)->toBe($this->employee->id);
$component->call('startEdit', $comment->id)
->assertSet('editingBody', "Valot ok.\nMikit puuttuu.")
->set('editingBody', 'Kaikki ok.')
->call('saveEdit')
->assertSee('Kommentti tallennettu.')
->assertSee('muokattu')
->assertSet('editingCommentId', null);
expect($comment->fresh()->body)->toBe('Kaikki ok.')
->and($comment->fresh()->edited_at)->not->toBeNull();
$component->call('confirmRemoval', $comment->id)
->assertSet('removalCommentId', $comment->id)
->call('remove')
->assertSee('Kommentti poistettu.');
expect(TaskCommentModel::query()->count())->toBe(0);
});
it('shows Finnish errors for empty and too long comments', function () {
Livewire::test('jobs::task-comments', $this->params)
->call('add')
->assertHasErrors('newComment')
->assertSee('Kirjoita kommentti.')
->set('newComment', str_repeat('a', 5001))
->call('add')
->assertSee('Kommentti voi olla enintään 5000 merkkiä.');
expect(TaskCommentModel::query()->count())->toBe(0);
});
it('does not let an employee edit or remove another user\'s comment', function () {
$comment = taskComment($this->task->id, User::factory()->create()->id, 'Toisen kommentti');
Livewire::test('jobs::task-comments', $this->params)
->assertDontSee('Muokkaa kommenttia')
->assertDontSee('Poista kommentti')
->call('startEdit', $comment->id)
->assertSet('editingCommentId', null)
->assertSee('Voit muokata ja poistaa vain omia kommenttejasi.')
->call('confirmRemoval', $comment->id)
->assertSet('removalCommentId', null);
expect($comment->fresh())->not->toBeNull();
});
it('lets management remove anyone\'s comment but not edit it', function () {
$comment = taskComment($this->task->id, $this->employee->id, 'Työntekijän kommentti');
$this->actingAs(User::factory()->management()->create());
Livewire::test('jobs::task-comments', $this->params)
->assertSee('Poista kommentti')
->assertDontSee('Muokkaa kommenttia')
->call('confirmRemoval', $comment->id)
->call('remove')
->assertSee('Kommentti poistettu.');
expect($comment->fresh())->toBeNull();
});
it('does not change comments of another task through this one', function () {
$other = taskComment(TaskModel::factory()->create()->id, $this->employee->id, 'Vieras');
Livewire::test('jobs::task-comments', $this->params)
->call('confirmRemoval', $other->id)
->assertSee('Kommenttia ei löytynyt. Päivitä sivu.')
->assertSet('removalCommentId', null);
expect($other->fresh())->not->toBeNull();
});
it('lets an attached subcontractor comment', function () {
$subcontractor = User::factory()->subcontractor()->create();
TaskModel::factory()->assignedTo([$subcontractor->id])->create(['customer_job_id' => $this->job->id, 'work_type_id' => $this->task->work_type_id]);
$this->actingAs($subcontractor);
Livewire::test('jobs::task-comments', $this->params)
->set('newComment', 'Aineisto toimitettu.')
->call('add')
->assertSee('Kommentti lisätty.');
expect(TaskCommentModel::query()->sole()->user_id)->toBe($subcontractor->id);
});
it('forbids the comments of a job the subcontractor is not attached to', function () {
$this->actingAs(User::factory()->subcontractor()->create());
Livewire::test('jobs::task-comments', $this->params)->assertForbidden();
});
it('removes the comments with their task', function () {
taskComment($this->task->id, $this->employee->id, 'Valot ok.');
$this->task->delete();
expect(TaskCommentModel::query()->count())->toBe(0);
});