Files
Toiminnanohjaus/tests/Feature/Modules/Jobs/TasksTest.php
T
petriandClaude Opus 5.5 18598cc08e Give tasks one assignee and members, and schedule template tasks to due dates
- A task now has one optional assignee (tasks.assignee_user_id), whose
  calendar row it is on and whose days its estimate books, and members
  (task_members) who see it among their own jobs and in the weekly sheet
  but get no bookings. A migration moves existing assignees over: the one
  with most booked time becomes the assignee, the others members.
- Adding tasks from a workflow template asks for each step's assignee and,
  by default, schedules tasks with a due date to end on it, booking the
  estimate backwards from the due date (not before today); what does not
  fit runs past the due date and is reported as late.
- The task calendar leaves quick sale tasks out of the unscheduled list,
  shows a bar's job details in a popover on hover and focus, and shows the
  day load on its own grid line.
- The template table stacks its cells on narrow screens to reflow at 320 px.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-24 19:01:11 +02:00

154 lines
6.8 KiB
PHP

<?php
use App\Models\User;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
use App\Modules\Jobs\Application\Contracts\TaskQueries;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->employee = User::factory()->create(['name' => 'Eero Työntekijä']);
$this->actingAs($this->employee);
$this->marketing = ServiceUnitModel::query()->where('name', 'Markkinointi')->sole();
$this->job = CustomerJobModel::factory()->create(['name' => 'Kampanja', 'status' => 'order', 'responsible_user_id' => $this->employee->id]);
DB::table('customer_job_service_unit')->where('customer_job_id', $this->job->id)->update(['service_unit_id' => $this->marketing->id]);
$this->graphic = WorkTypeModel::query()->where('name', 'graafinen työ')->sole();
$this->planning = WorkTypeModel::query()->where('name', 'suunnittelu')->sole();
});
it('adds a task with work types of the job and the shared ones on offer', function () {
$this->get(route('jobs.tasks.create', $this->job->id))
->assertOk()
->assertSee('graafinen työ')
->assertSee('suunnittelu')
->assertDontSee('painotyö');
Livewire::test('jobs::task-form', ['job' => $this->job->id])
->assertSet('form.assignee_user_id', (string) $this->employee->id)
->assertSet('form.member_user_ids', [])
->set('form.title', 'Mainoskuvat')
->set('form.work_type_id', (string) $this->graphic->id)
->set('form.due_on', '2026-05-20')
->call('save')
->assertHasNoErrors()
->assertRedirect(route('jobs.show', $this->job->id).'#tasks');
$task = TaskModel::query()->sole();
expect($task->work_type_id)->toBe($this->graphic->id)
->and($task->assignee_user_id)->toBe($this->employee->id)
->and(DB::table('task_members')->where('task_id', $task->id)->count())->toBe(0);
$this->get(route('jobs.show', $this->job->id))
->assertSee('Mainoskuvat')
->assertSee('graafinen työ')
->assertSee('Eero Työntekijä');
});
it('shows Finnish errors on the task form', function () {
Livewire::test('jobs::task-form', ['job' => $this->job->id])
->call('save')
->assertHasErrors(['form.title', 'form.work_type_id'])
->assertSee('Anna tehtävän nimi.')
->assertSee('Valitse työtehtävä.');
});
it('does not add tasks before the job is a quote request', function () {
$this->job->update(['status' => 'tentative']);
$this->get(route('jobs.tasks.create', $this->job->id))->assertNotFound();
$this->get(route('jobs.show', $this->job->id))
->assertSee('Tehtäviä voi lisätä, kun työ on tarjouspyyntö-tilassa')
->assertDontSee('Lisää tehtävä');
});
it('edits, changes the status of and removes a task on the job page', function () {
$task = TaskModel::factory()->create(['customer_job_id' => $this->job->id, 'work_type_id' => $this->graphic->id, 'title' => 'Mainoskuvat']);
Livewire::test('jobs::task-form', ['job' => $this->job->id, 'task' => $task->id])
->assertSet('form.title', 'Mainoskuvat')
->set('form.work_type_id', (string) $this->planning->id)
->call('save')
->assertHasNoErrors();
Livewire::test('jobs::show', ['job' => $this->job->id])
->call('changeTaskStatus', $task->id, 'in_review')
->assertSee('Tehtävän Mainoskuvat tila on nyt Tarkastettavana.')
->call('confirmTaskRemoval', $task->id)
->assertSee('Poistetaanko tehtävä Mainoskuvat?')
->call('removeTask')
->assertSee('Tehtävä Mainoskuvat poistettu.');
expect(TaskModel::query()->count())->toBe(0);
});
it('counts task assignees as members in my jobs', function () {
$colleague = User::factory()->create();
TaskModel::factory()->assignedTo([$colleague->id])->create(['customer_job_id' => $this->job->id, 'work_type_id' => $this->graphic->id]);
$this->actingAs($colleague);
Livewire::test('jobs::index')->assertSee('Kampanja');
});
it('lets a subcontractor change task statuses on their jobs, but not add or change tasks', function () {
$subcontractor = User::factory()->subcontractor()->create();
$task = TaskModel::factory()->assignedTo([$subcontractor->id])->create(['customer_job_id' => $this->job->id, 'work_type_id' => $this->graphic->id]);
$this->actingAs($subcontractor);
$this->get(route('jobs.show', $this->job->id))->assertOk()->assertDontSee('Lisää tehtävä');
$this->get(route('jobs.tasks.create', $this->job->id))->assertForbidden();
$this->get(route('jobs.tasks.edit', [$this->job->id, $task->id]))->assertForbidden();
Livewire::test('jobs::show', ['job' => $this->job->id])
->call('changeTaskStatus', $task->id, 'done')
->assertHasNoErrors()
->call('confirmTaskRemoval', $task->id)
->assertForbidden();
expect($task->fresh()->status->value)->toBe('done');
});
it('returns 404 for a task of another job', function () {
$other = TaskModel::factory()->create();
$this->get(route('jobs.tasks.edit', [$this->job->id, $other->id]))->assertNotFound();
});
it('saves a task assignee and members apart, and the members find the job among their own', function () {
$colleague = User::factory()->create(['name' => 'Aino Ahkera']);
$intern = User::factory()->create(['name' => 'Ville Harjoittelija']);
Livewire::test('jobs::task-form', ['job' => $this->job->id])
->set('form.title', 'Mainoskuvat')
->set('form.work_type_id', (string) $this->graphic->id)
->set('form.assignee_user_id', (string) $colleague->id)
->set('form.member_user_ids', [(string) $intern->id, (string) $colleague->id])
->call('save')
->assertHasNoErrors();
$task = TaskModel::query()->sole();
expect($task->assignee_user_id)->toBe($colleague->id)
->and(DB::table('task_members')->where('task_id', $task->id)->pluck('user_id')->all())->toBe([$intern->id]);
$this->get(route('jobs.show', $this->job->id))
->assertSeeInOrder(['Tekijä', 'Aino Ahkera', 'Jäsenet', 'Ville Harjoittelija']);
Livewire::test('jobs::task-form', ['job' => $this->job->id, 'task' => $task->id])
->assertSet('form.assignee_user_id', (string) $colleague->id)
->assertSet('form.member_user_ids', [(string) $intern->id])
->set('form.assignee_user_id', '')
->call('save')
->assertHasNoErrors();
expect($task->refresh()->assignee_user_id)->toBeNull()
->and(app(TaskQueries::class)->assignedOpenTaskIds($intern->id))->toBe([$task->id]);
});