Files
Toiminnanohjaus/tests/Unit/Modules/Catalog/Application/WorkflowTemplateUseCasesTest.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

137 lines
6.6 KiB
PHP

<?php
use App\Modules\Catalog\Application\DTOs\WorkflowStepData;
use App\Modules\Catalog\Application\DTOs\WorkflowTemplateData;
use App\Modules\Catalog\Application\Services\WorkflowStepBuilder;
use App\Modules\Catalog\Application\UseCases\AddServiceUnit;
use App\Modules\Catalog\Application\UseCases\AddWorkflowTemplate;
use App\Modules\Catalog\Application\UseCases\AddWorkType;
use App\Modules\Catalog\Application\UseCases\SetCatalogItemActive;
use App\Modules\Catalog\Application\UseCases\SetWorkflowTemplateActive;
use App\Modules\Catalog\Application\UseCases\UpdateWorkflowTemplate;
use App\Modules\Catalog\Domain\Entities\ServiceUnit;
use App\Modules\Catalog\Domain\Enums\CatalogLevel;
use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound;
use App\Modules\Catalog\Domain\Exceptions\InvalidWorkflowStepTitle;
use App\Modules\Catalog\Domain\Exceptions\WorkflowStepWorkTypeNotAllowed;
use App\Modules\Catalog\Domain\Exceptions\WorkflowTemplateNameTaken;
use App\Modules\Catalog\Domain\Exceptions\WorkflowTemplateNeedsSteps;
use App\Modules\Catalog\Domain\Exceptions\WorkflowTemplateNotFound;
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
use Tests\Fakes\FakeTransactionManager;
use Tests\Fakes\InMemoryServiceRepository;
use Tests\Fakes\InMemoryServiceUnitRepository;
use Tests\Fakes\InMemoryWorkflowTemplateRepository;
use Tests\Fakes\InMemoryWorkTypeRepository;
beforeEach(function () {
$units = new InMemoryServiceUnitRepository;
$services = new InMemoryServiceRepository;
$this->workTypes = new InMemoryWorkTypeRepository;
$this->templates = new InMemoryWorkflowTemplateRepository;
$transactions = new FakeTransactionManager;
$builder = new WorkflowStepBuilder($this->workTypes, $units);
$this->add = new AddWorkflowTemplate($this->templates, $this->workTypes, $builder, $transactions);
$this->update = new UpdateWorkflowTemplate($this->templates, $this->workTypes, $builder, $transactions);
$this->setActive = new SetWorkflowTemplateActive($this->templates, $transactions);
$this->setWorkTypeActive = new SetCatalogItemActive($units, $services, $this->workTypes, $transactions);
$addUnit = new AddServiceUnit($units, $transactions);
$addWorkType = new AddWorkType($units, $services, $this->workTypes, $transactions);
$media = $addUnit->execute('Mediatuotanto');
$marketing = $addUnit->execute('Markkinointi');
$shared = (int) $units->save(ServiceUnit::create(new CatalogName('Kaikille yhteiset'), 3, shared: true))->id();
$this->videography = $addWorkType->execute($media, null, 'videokuvaus');
$this->editing = $addWorkType->execute($media, null, 'editointi');
$this->graphics = $addWorkType->execute($marketing, null, 'graafinen työ');
$this->planning = $addWorkType->execute($shared, null, 'suunnittelu');
$this->data = fn (string $name, array $steps, ?int $workTypeId = null): WorkflowTemplateData => new WorkflowTemplateData(
$name,
$workTypeId ?? $this->videography,
array_map(fn (array $step): WorkflowStepData => new WorkflowStepData(...$step), $steps),
);
});
it('adds a template with steps in order and their own work types', function () {
$id = $this->add->execute(($this->data)('Lyhyt video', [
['Suunnittelu', null, $this->planning],
[' Kuvaus ', 'Kuvauspäivä asiakkaalla'],
['Editointi', '', $this->editing],
]));
$template = $this->templates->findById($id);
expect($template?->name()->value)->toBe('Lyhyt video')
->and($template?->isActive())->toBeTrue()
->and(array_map(fn ($step) => [$step->title->value, $step->description, $step->workTypeId], $template?->steps() ?? []))->toBe([
['Suunnittelu', null, $this->planning],
['Kuvaus', 'Kuvauspäivä asiakkaalla', null],
['Editointi', null, $this->editing],
]);
});
it('needs at least one step with a title, and tells which step is wrong', function () {
expect(fn () => $this->add->execute(($this->data)('Tyhjä', [])))->toThrow(WorkflowTemplateNeedsSteps::class);
try {
$this->add->execute(($this->data)('Lyhyt video', [['Kuvaus'], [' ']]));
$this->fail('The empty title was accepted.');
} catch (InvalidWorkflowStepTitle $exception) {
expect($exception->stepIndex)->toBe(1);
}
});
it('rejects a name the work type already has, but allows it for another work type', function () {
$this->add->execute(($this->data)('Lyhyt video', [['Kuvaus']]));
$this->add->execute(($this->data)('lyhyt video', [['Kuvaus']], $this->editing));
expect(fn () => $this->add->execute(($this->data)('LYHYT VIDEO', [['Kuvaus']])))->toThrow(WorkflowTemplateNameTaken::class);
});
it('allows a step work type only from the same unit or the shared unit, and in use', function () {
try {
$this->add->execute(($this->data)('Lyhyt video', [['Kuvaus'], ['Grafiikka', null, $this->graphics]]));
$this->fail('A work type of another unit was accepted.');
} catch (WorkflowStepWorkTypeNotAllowed $exception) {
expect($exception->stepIndex)->toBe(1);
}
$this->setWorkTypeActive->execute(CatalogLevel::WorkType, $this->editing, false);
expect(fn () => $this->add->execute(($this->data)('Lyhyt video', [['Editointi', null, $this->editing]])))
->toThrow(WorkflowStepWorkTypeNotAllowed::class);
});
it('needs a template work type in use', function () {
$this->setWorkTypeActive->execute(CatalogLevel::WorkType, $this->videography, false);
$this->add->execute(($this->data)('Lyhyt video', [['Kuvaus']]));
})->throws(CatalogItemNotFound::class);
it('updates name and steps, keeping step work types taken out of use since', function () {
$id = $this->add->execute(($this->data)('Lyhyt video', [['Kuvaus'], ['Editointi', null, $this->editing]]));
$this->setWorkTypeActive->execute(CatalogLevel::WorkType, $this->editing, false);
$this->update->execute($id, ($this->data)('Lyhyt video 2', [['Editointi', null, $this->editing], ['Toimitus']]));
$template = $this->templates->findById($id);
expect($template?->name()->value)->toBe('Lyhyt video 2')
->and(array_map(fn ($step) => $step->title->value, $template?->steps() ?? []))->toBe(['Editointi', 'Toimitus']);
expect(fn () => $this->update->execute(999, ($this->data)('X', [['Kuvaus']])))->toThrow(WorkflowTemplateNotFound::class);
});
it('takes a template out of use and back', function () {
$id = $this->add->execute(($this->data)('Lyhyt video', [['Kuvaus']]));
$this->setActive->execute($id, false);
expect($this->templates->findById($id)?->isActive())->toBeFalse();
$this->setActive->execute($id, true);
expect($this->templates->findById($id)?->isActive())->toBeTrue();
});