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(); $printing = WorkTypeModel::query()->where('name', 'painotyö')->sole(); $add = fn (string $name, int $workTypeId, array $steps): int => app(AddWorkflowTemplate::class)->execute(new WorkflowTemplateData( $name, $workTypeId, array_map(fn (array $step): WorkflowStepData => new WorkflowStepData(...$step), $steps), )); $this->campaign = $add('Somekampanja', $this->graphic->id, [['Suunnittelu', 'Kohderyhmä ja viestit', $this->planning->id], ['Kuvat'], ['Julkaisu']]); $this->print = $add('Esite', $printing->id, [['Painatus']]); }); it('adds the steps of a template as tasks of the job', function () { $this->get(route('jobs.show', $this->job->id))->assertSee(route('jobs.tasks.from-template', $this->job->id)); Livewire::test('jobs::tasks-from-template', ['job' => $this->job->id]) ->assertSee('Somekampanja (graafinen työ)') ->assertSee('Vaiheet: Suunnittelu (suunnittelu) → Kuvat → Julkaisu') ->assertDontSee('Esite') ->set('templateId', (string) $this->campaign) ->call('add') ->assertHasNoErrors() ->assertRedirect(route('jobs.show', $this->job->id).'#tasks'); $tasks = TaskModel::query()->orderBy('id')->get(); expect($tasks->map(fn ($task) => [$task->title, $task->work_type_id, $task->status->value, $task->description])->all())->toBe([ ['Suunnittelu', $this->planning->id, 'waiting', 'Kohderyhmä ja viestit'], ['Kuvat', $this->graphic->id, 'waiting', null], ['Julkaisu', $this->graphic->id, 'waiting', null], ]) ->and($tasks->pluck('assignee_user_id')->unique()->values()->all())->toBe([$this->employee->id]); $this->get(route('jobs.show', $this->job->id))->assertSee('Lisättiin 3 tehtävää pohjasta Somekampanja (graafinen työ).'); }); it('needs a template that is still available', function () { Livewire::test('jobs::tasks-from-template', ['job' => $this->job->id]) ->call('add') ->assertHasErrors(['templateId']) ->assertSee('Valitse pohja.'); $component = Livewire::test('jobs::tasks-from-template', ['job' => $this->job->id])->set('templateId', (string) $this->campaign); app(SetWorkflowTemplateActive::class)->execute($this->campaign, false); $component->call('add') ->assertHasErrors(['templateId']) ->assertSee('Pohja ei ole enää käytettävissä tälle työlle. Valitse toinen.'); Livewire::test('jobs::tasks-from-template', ['job' => $this->job->id]) ->set('templateId', (string) $this->print) ->call('add') ->assertHasErrors(['templateId']); expect(TaskModel::query()->count())->toBe(0); }); it('is not for subcontractors, jobs before a quote request or archived jobs', function () { $this->job->update(['status' => 'tentative']); $this->get(route('jobs.tasks.from-template', $this->job->id))->assertNotFound(); $this->job->update(['status' => 'archived']); $this->get(route('jobs.tasks.from-template', $this->job->id))->assertNotFound(); $this->job->update(['status' => 'order']); $subcontractor = User::factory()->subcontractor()->create(); DB::table('customer_job_members')->insert(['customer_job_id' => $this->job->id, 'user_id' => $subcontractor->id]); $this->actingAs($subcontractor)->get(route('jobs.tasks.from-template', $this->job->id))->assertForbidden(); }); it('locks the job id', function () { Livewire::test('jobs::tasks-from-template', ['job' => $this->job->id])->set('jobId', 999); })->throws(CannotUpdateLockedPropertyException::class); it('shows a table of the chosen template tasks and saves their due dates and estimates', function () { Livewire::test('jobs::tasks-from-template', ['job' => $this->job->id]) ->assertDontSee('Pohjan Somekampanja tehtävät') ->set('templateId', (string) $this->campaign) ->assertSet('rows', array_fill(0, 3, ['due_on' => '', 'estimated_hours' => '', 'assignee_user_id' => (string) $this->employee->id])) ->assertSet('scheduleToDueDates', true) ->assertSee('Tekijä: Kuvat') ->assertSee('Pohjan Somekampanja tehtävät') ->assertSee('Määräpäivä: Kuvat') ->assertSee('Arvioitu aika (h): Julkaisu') ->set('rows.0.due_on', '2026-06-01') ->set('rows.0.estimated_hours', '1,5') ->set('rows.2.estimated_hours', '2') ->set('scheduleToDueDates', false) ->call('add') ->assertHasNoErrors(); $tasks = TaskModel::query()->orderBy('id')->get(); expect($tasks->map(fn ($task) => [$task->title, $task->due_on?->format('Y-m-d'), $task->estimated_hours === null ? null : (float) $task->estimated_hours])->all())->toBe([ ['Suunnittelu', '2026-06-01', 1.5], ['Kuvat', null, null], ['Julkaisu', null, 2.0], ]); }); it('shows errors in the table cells and adds nothing', function () { Livewire::test('jobs::tasks-from-template', ['job' => $this->job->id]) ->set('templateId', (string) $this->campaign) ->set('rows.1.due_on', '1.6.2026') ->set('rows.2.estimated_hours', 'paljon') ->call('add') ->assertHasErrors(['rows.1.due_on', 'rows.2.estimated_hours']) ->assertSee('aria-invalid="true"', escape: false); expect(TaskModel::query()->count())->toBe(0); }); it('empties the rows when another template is chosen', function () { Livewire::test('jobs::tasks-from-template', ['job' => $this->job->id]) ->set('templateId', (string) $this->campaign) ->set('rows.0.estimated_hours', '3') ->set('templateId', '') ->assertSet('rows', []) ->set('templateId', (string) $this->campaign) ->assertSet('rows.0.estimated_hours', ''); }); it('schedules the tasks to their due dates for the chosen assignees, backwards from the due date', function () { $this->travelTo(new DateTimeImmutable('2026-06-01 09:00')); $colleague = User::factory()->create(['name' => 'Aino Ahkera']); Livewire::test('jobs::tasks-from-template', ['job' => $this->job->id]) ->set('templateId', (string) $this->campaign) ->set('rows.0.due_on', '2026-06-10') ->set('rows.0.estimated_hours', '12') ->set('rows.0.assignee_user_id', (string) $colleague->id) ->set('rows.1.due_on', '2026-06-10') ->set('rows.1.estimated_hours', '4') ->set('rows.1.assignee_user_id', '') ->call('add') ->assertHasNoErrors(); $tasks = TaskModel::query()->orderBy('id')->get(); expect($tasks->map(fn ($task) => [$task->assignee_user_id, $task->planned_from?->format('Y-m-d'), $task->planned_to?->format('Y-m-d')])->all())->toBe([ [$colleague->id, '2026-06-09', '2026-06-10'], [null, '2026-06-10', '2026-06-10'], [$this->employee->id, null, null], ]) ->and(DB::table('task_bookings')->where('task_id', $tasks[0]->id)->orderBy('day')->pluck('minutes', 'day')->all())->toBe(['2026-06-09' => 240, '2026-06-10' => 480]) ->and(DB::table('task_bookings')->where('task_id', '!=', $tasks[0]->id)->count())->toBe(0); $this->get(route('jobs.show', $this->job->id))->assertSee('2 tehtävää aikataulutettiin määräpäivilleen.'); });