Files
Toiminnanohjaus/tests/Feature/Modules/Catalog/WorkflowTemplatesTest.php
petriandClaude Opus 5.5 b4f2f1cfaa Add the task calendar, holidays, workflow templates and customer removal
Task calendar (/jobs/calendar) replaces per-unit task Kanban columns
(K34): a two-week Gantt of tasks per person. Bars move, resize and
change person by dragging or through a keyboard dialog. Each task's
estimate is booked evenly over working days into task_bookings, and
what exceeds the daily maximum (jobs.daily_capacity_hours, 8 h) moves
to the next working day. Tasks get planned_from/planned_to on the form.

Holidays (/settings/holidays): Finnish public holidays calculated per
year, which the admin can take out of use, plus company days off.

Also includes the earlier uncommitted work: workflow templates with
adding tasks from a template, and removing customers no module refers to.

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

161 lines
7.5 KiB
PHP

<?php
use App\Models\User;
use App\Modules\Catalog\Application\Contracts\WorkflowTemplateQueries;
use App\Modules\Catalog\Application\DTOs\WorkflowStepData;
use App\Modules\Catalog\Application\DTOs\WorkflowTemplateData;
use App\Modules\Catalog\Application\UseCases\AddWorkflowTemplate;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceModel;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Features\SupportLockedProperties\CannotUpdateLockedPropertyException;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->actingAs(User::factory()->management()->create());
$this->workType = fn (string $name, string $unit = 'Mediatuotanto', ?string $service = 'Videotuotanto') => WorkTypeModel::query()
->where('name', $name)
->where('service_unit_id', ServiceUnitModel::query()->where('name', $unit)->sole()->id)
->where('service_id', $service === null ? null : ServiceModel::query()->where('name', $service)->sole()->id)
->sole();
$this->videography = ($this->workType)('videokuvaus');
$this->editing = ($this->workType)('editointi');
$this->planning = ($this->workType)('suunnittelu', 'Kaikille yhteiset', null);
$this->addTemplate = 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),
));
});
it('shows the template pages to admins and management only', function (string $role, int $status) {
$this->actingAs(User::factory()->{$role}()->create());
$this->get(route('catalog.workflow-templates'))->assertStatus($status);
$this->get(route('catalog.workflow-templates.create'))->assertStatus($status);
})->with([
'admin' => ['admin', 200],
'management' => ['management', 200],
'employee' => ['employee', 403],
'subcontractor' => ['subcontractor', 403],
]);
it('links the templates from the catalog page', function () {
$this->get(route('catalog.index'))->assertSee(route('catalog.workflow-templates'));
});
it('adds a template with steps added, moved and removed on the form', function () {
Livewire::test('catalog::workflow-template-form')
->set('form.name', 'Lyhyt video')
->set('form.work_type_id', (string) $this->videography->id)
->assertSee('editointi')
->assertSee('suunnittelu')
->set('form.steps.0.title', 'Kuvaus')
->call('addStep')
->assertSee('Vaihe 2 lisätty.')
->set('form.steps.1.title', 'Suunnittelu')
->set('form.steps.1.work_type_id', (string) $this->planning->id)
->call('moveStep', 1, 'up')
->assertSee('Vaihe 2 siirretty kohtaan 1.')
->call('addStep')
->set('form.steps.2.title', 'Turha')
->call('removeStep', 2)
->assertSee('Vaihe 3 poistettu.')
->call('addStep')
->set('form.steps.2.title', 'Editointi')
->set('form.steps.2.work_type_id', (string) $this->editing->id)
->set('form.steps.2.description', 'Leikkaus ja värimäärittely')
->call('save')
->assertHasNoErrors()
->assertRedirect(route('catalog.workflow-templates'));
$template = app(WorkflowTemplateQueries::class)->all()[0];
$details = app(WorkflowTemplateQueries::class)->find($template->id);
expect($template->name)->toBe('Lyhyt video')
->and($template->workTypeName)->toBe('videokuvaus')
->and($template->serviceUnitName)->toBe('Mediatuotanto')
->and($template->stepCount)->toBe(3)
->and(array_map(fn ($step) => [$step->title, $step->workTypeId, $step->effectiveWorkTypeId, $step->workTypeName, $step->description], $details->steps))->toBe([
['Suunnittelu', $this->planning->id, $this->planning->id, 'suunnittelu', null],
['Kuvaus', null, $this->videography->id, 'videokuvaus', null],
['Editointi', $this->editing->id, $this->editing->id, 'editointi', 'Leikkaus ja värimäärittely'],
]);
$this->get(route('catalog.workflow-templates'))->assertSee('Lyhyt video')->assertSee('videokuvaus');
});
it('shows Finnish errors on the right fields', function () {
($this->addTemplate)('Lyhyt video', $this->videography->id, [['Kuvaus']]);
Livewire::test('catalog::workflow-template-form')
->call('save')
->assertHasErrors(['form.name', 'form.work_type_id', 'form.steps.0.title'])
->assertSee('Anna nimi.')
->assertSee('Valitse työtehtävä.')
->assertSee('Anna tehtävän nimi.');
Livewire::test('catalog::workflow-template-form')
->set('form.name', 'lyhyt video')
->set('form.work_type_id', (string) $this->videography->id)
->set('form.steps.0.title', 'Kuvaus')
->call('save')
->assertHasErrors(['form.name'])
->assertSee('Työtehtävällä on jo tämänniminen pohja.');
Livewire::test('catalog::workflow-template-form')
->set('form.name', 'Video ja grafiikka')
->set('form.work_type_id', (string) $this->videography->id)
->set('form.steps.0.title', 'Grafiikka')
->set('form.steps.0.work_type_id', (string) ($this->workType)('graafinen työ', 'Markkinointi', null)->id)
->call('save')
->assertHasErrors(['form.steps.0.work_type_id'])
->assertSee('Valitse käytössä oleva työtehtävä pohjan palveluyksiköstä tai kaikille yhteisistä.');
});
it('edits a template and keeps tasks offered only while it is in use', function () {
$id = ($this->addTemplate)('Lyhyt video', $this->videography->id, [['Kuvaus'], ['Editointi', null, $this->editing->id]]);
Livewire::test('catalog::workflow-template-form', ['template' => $id])
->assertSet('form.name', 'Lyhyt video')
->assertSet('form.steps.1.work_type_id', (string) $this->editing->id)
->call('removeStep', 0)
->set('form.name', 'Pelkkä editointi')
->call('save')
->assertHasNoErrors();
expect(array_map(fn ($step) => $step->title, app(WorkflowTemplateQueries::class)->find($id)->steps))->toBe(['Editointi']);
$media = ServiceUnitModel::query()->where('name', 'Mediatuotanto')->sole()->id;
$marketing = ServiceUnitModel::query()->where('name', 'Markkinointi')->sole()->id;
expect(array_map(fn ($template) => $template->name, app(WorkflowTemplateQueries::class)->activeFor([$media])))->toBe(['Pelkkä editointi'])
->and(app(WorkflowTemplateQueries::class)->activeFor([$marketing]))->toBe([]);
Livewire::test('catalog::workflow-templates')
->call('toggleActive', $id)
->assertSee('Pelkkä editointi poistettu käytöstä.');
expect(app(WorkflowTemplateQueries::class)->activeFor([$media]))->toBe([]);
});
it('offers shared work type templates to every job', function () {
($this->addTemplate)('Työpaja', $this->planning->id, [['Valmistelu'], ['Työpaja']]);
$marketing = ServiceUnitModel::query()->where('name', 'Markkinointi')->sole()->id;
expect(array_map(fn ($template) => $template->name, app(WorkflowTemplateQueries::class)->activeFor([$marketing])))->toBe(['Työpaja']);
});
it('returns 404 for an unknown template', function () {
$this->get(route('catalog.workflow-templates.edit', 999))->assertNotFound();
});
it('locks the template id', function () {
Livewire::test('catalog::workflow-template-form')->set('templateId', 1);
})->throws(CannotUpdateLockedPropertyException::class);