Files
Toiminnanohjaus/tests/Unit/Modules/Jobs/Application/TaskUseCasesTest.php
T
petriandClaude Sonnet 5.5 f906f9a43f Add notifications: header bell, task start run and assignment notices
A new Notifications module shows a bell beside the profile menu with the
number of unseen notifications and a panel of the last 7 days; opening
it marks them seen, and each notification links to its job or task.

- A daily run at 14.00 (notifications:task-starts) tells the assignee
  and members of tasks that start by the next working day, skipping
  holidays and days off, done tasks and archived jobs.
- Jobs publishes PeopleAddedToTask and PeopleAddedToJob when someone
  else makes a user a task's assignee or member or gives them a role on
  a job; those users are notified at once. UpdateTask and ScheduleTask
  now take the acting user.
- A notification link to #task-<id> opens that task card on the job page.

Also: simplifying job fields per service unit (K31) moves to the end of
phase 4, and the workflow template browser test waits for Livewire's
progress bar before its last accessibility check.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-11-25 08:28:01 +02:00

275 lines
15 KiB
PHP

<?php
use App\Modules\Jobs\Application\DTOs\CustomerJobData;
use App\Modules\Jobs\Application\DTOs\TaskData;
use App\Modules\Jobs\Application\Services\JobDetailsFactory;
use App\Modules\Jobs\Application\Services\TaskPlanner;
use App\Modules\Jobs\Application\Services\TaskReferenceChecker;
use App\Modules\Jobs\Application\UseCases\AddTask;
use App\Modules\Jobs\Application\UseCases\ChangeJobStatus;
use App\Modules\Jobs\Application\UseCases\ChangeTaskStatus;
use App\Modules\Jobs\Application\UseCases\ClearTaskSchedule;
use App\Modules\Jobs\Application\UseCases\CreateCustomerJob;
use App\Modules\Jobs\Application\UseCases\RemoveTask;
use App\Modules\Jobs\Application\UseCases\ScheduleTask;
use App\Modules\Jobs\Application\UseCases\UpdateTask;
use App\Modules\Jobs\Domain\Enums\JobStatus;
use App\Modules\Jobs\Domain\Enums\TaskStatus;
use App\Modules\Jobs\Domain\Events\PeopleAddedToTask;
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskReference;
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskTitle;
use App\Modules\Jobs\Domain\Exceptions\PlanEndsBeforeStart;
use App\Modules\Jobs\Domain\Exceptions\TaskHasTimeEntries;
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
use App\Modules\Jobs\Domain\Exceptions\TasksNotAllowedYet;
use App\Modules\Jobs\Domain\ValueObjects\TaskTitle;
use App\Modules\Shared\Application\UseCases\HolidayCalendar;
use Tests\Fakes\FakeCatalogQueries;
use Tests\Fakes\FakeClock;
use Tests\Fakes\FakeCustomerQueries;
use Tests\Fakes\FakeEventDispatcher;
use Tests\Fakes\FakeSettings;
use Tests\Fakes\FakeTaskImageFiles;
use Tests\Fakes\FakeTaskUsage;
use Tests\Fakes\FakeTransactionManager;
use Tests\Fakes\FakeUserAccountQueries;
use Tests\Fakes\InMemoryCustomerJobRepository;
use Tests\Fakes\InMemoryHolidayOverrides;
use Tests\Fakes\InMemoryTaskBookingRepository;
use Tests\Fakes\InMemoryTaskImageRepository;
use Tests\Fakes\InMemoryTaskRepository;
beforeEach(function () {
$this->events = new FakeEventDispatcher;
$this->jobs = new InMemoryCustomerJobRepository;
$this->tasks = new InMemoryTaskRepository;
$customers = new FakeCustomerQueries;
$this->catalog = new FakeCatalogQueries;
$this->users = new FakeUserAccountQueries;
$transactions = new FakeTransactionManager;
$customers->customers = [1 => 'Mainos Oy'];
$this->catalog->addServiceUnit(1, 'Markkinointi');
$this->catalog->addServiceUnit(2, 'Painotuotanto');
$this->catalog->addServiceUnit(9, 'Kaikille yhteiset', shared: true);
$this->catalog->addWorkType(11, 1, 'graafinen työ');
$this->catalog->addWorkType(21, 2, 'painotyö');
$this->catalog->addWorkType(91, 9, 'suunnittelu');
$this->users->addUser(10, 'Eero');
$this->users->addUser(12, 'Pois', active: false);
$createJob = new CreateCustomerJob($this->jobs, $this->jobs, new JobDetailsFactory($customers, $this->catalog, $this->users), $transactions, new FakeClock(new DateTimeImmutable('2026-05-01')), $this->events);
$this->jobId = $createJob->execute(new CustomerJobData('Kampanja', 1, [1], 10, 'one_off', 'quote_request', 'normal'), 10);
$this->tentativeJobId = $createJob->execute(new CustomerJobData('Alustava', 1, [1], 10, 'one_off', 'tentative', 'normal'), 10);
$checker = new TaskReferenceChecker($this->catalog, $this->users);
$this->bookings = new InMemoryTaskBookingRepository($this->tasks);
$this->planner = new TaskPlanner($this->bookings, new HolidayCalendar(new InMemoryHolidayOverrides), new FakeSettings);
$this->addTask = new AddTask($this->jobs, $this->tasks, $checker, $this->planner, $transactions, $this->events);
$this->updateTask = new UpdateTask($this->jobs, $this->tasks, $checker, $this->planner, $transactions, $this->events);
$this->scheduleTask = new ScheduleTask($this->jobs, $this->tasks, $checker, $this->planner, $transactions, $this->events);
$this->clearSchedule = new ClearTaskSchedule($this->tasks, $this->planner, $transactions);
$this->changeTaskStatus = new ChangeTaskStatus($this->tasks, $transactions);
$this->usage = new FakeTaskUsage;
$this->removeTask = new RemoveTask($this->tasks, $this->usage, $transactions, new InMemoryTaskImageRepository, new FakeTaskImageFiles);
$this->changeJobStatus = new ChangeJobStatus($this->jobs, $transactions, new FakeClock(new DateTimeImmutable('2026-05-01')));
});
it('allows tasks from a quote request on, not before and not when archived', function () {
expect(JobStatus::Tentative->allowsTasks())->toBeFalse()
->and(JobStatus::Prospect->allowsTasks())->toBeFalse()
->and(JobStatus::Archived->allowsTasks())->toBeFalse()
->and(JobStatus::QuoteRequest->allowsTasks())->toBeTrue()
->and(JobStatus::Billed->allowsTasks())->toBeTrue()
->and(fn () => new TaskTitle(' '))->toThrow(InvalidTaskTitle::class);
expect(fn () => $this->addTask->execute($this->tentativeJobId, new TaskData('Luonnos', 11, 'waiting'), 10))
->toThrow(TasksNotAllowedYet::class);
});
it('adds a task with a work type of the job or a shared one', function () {
$taskId = $this->addTask->execute($this->jobId, new TaskData('Mainoskuvat', 11, 'waiting', dueOn: '2026-05-20', estimatedHours: '3,5', assigneeUserId: 10), 10);
$this->addTask->execute($this->jobId, new TaskData('Suunnittelupalaveri', 91, 'waiting'), 10);
$task = $this->tasks->findForJob($this->jobId, $taskId);
expect($task?->dueOn()?->format('Y-m-d'))->toBe('2026-05-20')
->and($task?->estimatedHours()?->format())->toBe('3.5')
->and($task?->assigneeUserId())->toBe(10)
->and($this->tasks->tasks)->toHaveCount(2);
});
it('rejects a work type of another unit and an inactive assignee or member', function (int $workTypeId, ?int $assignee, array $members, string $field) {
try {
$this->addTask->execute($this->jobId, new TaskData('Työ', $workTypeId, 'waiting', assigneeUserId: $assignee, memberUserIds: $members), 10);
$this->fail('No exception');
} catch (InvalidTaskReference $exception) {
expect($exception->field)->toBe($field);
}
})->with([
'work type of another unit' => [21, null, [], 'work_type'],
'inactive assignee' => [11, 12, [], 'assignee'],
'inactive member' => [11, 10, [12], 'members'],
]);
it('changes, moves through statuses and removes a task', function () {
$taskId = $this->addTask->execute($this->jobId, new TaskData('Mainoskuvat', 11, 'waiting'), 10);
$this->updateTask->execute($this->jobId, $taskId, new TaskData('Mainoskuvat ja banneri', 11, 'in_progress'), 10);
$this->changeTaskStatus->execute($this->jobId, $taskId, TaskStatus::Done);
$task = $this->tasks->findForJob($this->jobId, $taskId);
expect($task?->title()->value)->toBe('Mainoskuvat ja banneri')
->and($task?->status())->toBe(TaskStatus::Done);
$this->removeTask->execute($this->jobId, $taskId);
expect($this->tasks->tasks)->toBeEmpty();
});
it('can still change a task after the job moved back to an earlier status', function () {
$taskId = $this->addTask->execute($this->jobId, new TaskData('Mainoskuvat', 11, 'waiting'), 10);
$this->changeJobStatus->execute($this->jobId, JobStatus::Tentative, 10);
$this->updateTask->execute($this->jobId, $taskId, new TaskData('Mainoskuvat', 11, 'in_review'), 10);
expect($this->tasks->findForJob($this->jobId, $taskId)?->status())->toBe(TaskStatus::InReview);
});
it('does not touch a task of another job', function (string $action) {
$taskId = $this->addTask->execute($this->jobId, new TaskData('Mainoskuvat', 11, 'waiting'), 10);
match ($action) {
'update' => $this->updateTask->execute($this->tentativeJobId, $taskId, new TaskData('X', 11, 'done'), 10),
'status' => $this->changeTaskStatus->execute($this->tentativeJobId, $taskId, TaskStatus::Done),
'remove' => $this->removeTask->execute($this->tentativeJobId, $taskId),
};
})->throws(TaskNotFound::class)->with(['update', 'status', 'remove']);
it('does not remove a task with time entries', function () {
$taskId = $this->addTask->execute($this->jobId, new TaskData('Mainoskuvat', 11, 'waiting'), 10);
$this->usage->tasksWithTime = [$taskId];
expect(fn () => $this->removeTask->execute($this->jobId, $taskId))->toThrow(TaskHasTimeEntries::class)
->and($this->tasks->tasks)->toHaveCount(1)
->and(JobStatus::Done->locksTime())->toBeTrue()
->and(JobStatus::OnHold->locksTime())->toBeFalse();
});
it('books a scheduled task evenly from the working days of its period', function () {
$taskId = $this->addTask->execute($this->jobId, new TaskData('Taitto', 11, 'waiting', estimatedHours: '12', assigneeUserId: 10, plannedFrom: '2026-12-04', plannedTo: '2026-12-07'), 10);
$oneDayId = $this->addTask->execute($this->jobId, new TaskData('Palaveri', 91, 'waiting', assigneeUserId: 10, plannedFrom: '2026-12-08'), 10);
expect($this->bookings->of($taskId, 10))->toBe(['2026-12-04' => 360, '2026-12-07' => 360])
->and($this->tasks->findForJob($this->jobId, $oneDayId)?->plannedTo()?->format('Y-m-d'))->toBe('2026-12-08')
->and($this->bookings->of($oneDayId, 10))->toBe([]);
});
it('moves what does not fit in a full day to the next working day', function () {
$this->addTask->execute($this->jobId, new TaskData('Kuvaus', 11, 'waiting', estimatedHours: '6', assigneeUserId: 10, plannedFrom: '2026-12-04'), 10);
$taskId = $this->addTask->execute($this->jobId, new TaskData('Taitto', 11, 'waiting', estimatedHours: '4', assigneeUserId: 10), 10);
$result = $this->scheduleTask->execute($this->jobId, $taskId, 10, 10, '2026-12-04', '2026-12-04', 10);
expect($result->extended)->toBeTrue()
->and($result->plannedTo)->toBe('2026-12-07')
->and($this->bookings->of($taskId, 10))->toBe(['2026-12-04' => 120, '2026-12-07' => 120]);
});
it('does not count done tasks in the day load', function () {
$doneId = $this->addTask->execute($this->jobId, new TaskData('Kuvaus', 11, 'waiting', estimatedHours: '8', assigneeUserId: 10, plannedFrom: '2026-12-04'), 10);
$this->changeTaskStatus->execute($this->jobId, $doneId, TaskStatus::Done);
$taskId = $this->addTask->execute($this->jobId, new TaskData('Taitto', 11, 'waiting', estimatedHours: '8', assigneeUserId: 10, plannedFrom: '2026-12-04'), 10);
expect($this->bookings->of($taskId, 10))->toBe(['2026-12-04' => 480]);
});
it('moves a task to another person and gives it the days its estimate needs', function () {
$this->users->addUser(11, 'Aino');
$taskId = $this->addTask->execute($this->jobId, new TaskData('Taitto', 11, 'waiting', estimatedHours: '20', assigneeUserId: 10, plannedFrom: '2026-12-01'), 10);
$result = $this->scheduleTask->execute($this->jobId, $taskId, 10, 11, '2026-12-11', null, 10);
expect($this->tasks->findForJob($this->jobId, $taskId)?->assigneeUserId())->toBe(11)
->and($result->plannedFrom)->toBe('2026-12-11')
->and($result->plannedTo)->toBe('2026-12-15')
->and($result->extended)->toBeFalse()
->and($this->bookings->of($taskId, 10))->toBe([])
->and(array_sum($this->bookings->of($taskId, 11)))->toBe(1200);
});
it('rejects moving a task to an inactive person or ending it before it starts', function () {
$taskId = $this->addTask->execute($this->jobId, new TaskData('Taitto', 11, 'waiting', assigneeUserId: 10), 10);
expect(fn () => $this->scheduleTask->execute($this->jobId, $taskId, 10, 12, '2026-12-07', '2026-12-07', 10))->toThrow(InvalidTaskReference::class)
->and(fn () => $this->scheduleTask->execute($this->jobId, $taskId, 10, 10, '2026-12-07', '2026-12-04', 10))->toThrow(PlanEndsBeforeStart::class);
});
it('takes a task out of the calendar', function () {
$taskId = $this->addTask->execute($this->jobId, new TaskData('Taitto', 11, 'waiting', estimatedHours: '4', assigneeUserId: 10, plannedFrom: '2026-12-07'), 10);
$this->clearSchedule->execute($this->jobId, $taskId);
expect($this->tasks->findForJob($this->jobId, $taskId)?->isScheduled())->toBeFalse()
->and($this->bookings->of($taskId, 10))->toBe([]);
});
it('books only the assignee, not the members, and keeps the assignee out of the members', function () {
$this->users->addUser(11, 'Aino');
$taskId = $this->addTask->execute($this->jobId, new TaskData('Taitto', 11, 'waiting', estimatedHours: '4', assigneeUserId: 10, memberUserIds: [11, 10], plannedFrom: '2026-12-07'), 10);
$task = $this->tasks->findForJob($this->jobId, $taskId);
expect($task?->memberUserIds())->toBe([11])
->and($this->bookings->of($taskId, 10))->toBe(['2026-12-07' => 240])
->and($this->bookings->of($taskId, 11))->toBe([]);
});
it('moves a task to a member, who then is the assignee and no longer a member', function () {
$this->users->addUser(11, 'Aino');
$this->users->addUser(13, 'Ville');
$taskId = $this->addTask->execute($this->jobId, new TaskData('Taitto', 11, 'waiting', estimatedHours: '4', assigneeUserId: 10, memberUserIds: [11, 13], plannedFrom: '2026-12-07'), 10);
$this->scheduleTask->execute($this->jobId, $taskId, 10, 11, '2026-12-07', '2026-12-07', 10);
$task = $this->tasks->findForJob($this->jobId, $taskId);
expect($task?->assigneeUserId())->toBe(11)
->and($task?->memberUserIds())->toBe([13])
->and($this->bookings->of($taskId, 10))->toBe([])
->and($this->bookings->of($taskId, 11))->toBe(['2026-12-07' => 240]);
});
it('books nothing for a task without an assignee', function () {
$taskId = $this->addTask->execute($this->jobId, new TaskData('Taitto', 11, 'waiting', estimatedHours: '4', memberUserIds: [10], plannedFrom: '2026-12-07'), 10);
expect($this->bookings->of($taskId, 10))->toBe([])
->and($this->tasks->findForJob($this->jobId, $taskId)?->plannedTo()?->format('Y-m-d'))->toBe('2026-12-07');
});
it('publishes who was added to a task: the new assignee and new members, not those who already were', function () {
$this->users->addUser(11, 'Aino');
$this->users->addUser(13, 'Ville');
$taskId = $this->addTask->execute($this->jobId, new TaskData('Mainoskuvat', 11, 'waiting', assigneeUserId: 10, memberUserIds: [11]), 10);
$added = end($this->events->dispatched);
expect($added)->toBeInstanceOf(PeopleAddedToTask::class)
->and($added->taskId)->toBe($taskId)
->and($added->taskTitle)->toBe('Mainoskuvat')
->and($added->jobName)->toBe('Kampanja')
->and($added->newAssigneeId)->toBe(10)
->and($added->newMemberIds)->toBe([11])
->and($added->actorUserId)->toBe(10);
$count = count($this->events->dispatched);
$this->updateTask->execute($this->jobId, $taskId, new TaskData('Mainoskuvat', 11, 'waiting', assigneeUserId: 10, memberUserIds: [11]), 10);
expect($this->events->dispatched)->toHaveCount($count);
$this->updateTask->execute($this->jobId, $taskId, new TaskData('Mainoskuvat', 11, 'waiting', assigneeUserId: 10, memberUserIds: [11, 13]), 11);
expect(end($this->events->dispatched)->newAssigneeId)->toBeNull()
->and(end($this->events->dispatched)->newMemberIds)->toBe([13])
->and(end($this->events->dispatched)->actorUserId)->toBe(11);
$this->scheduleTask->execute($this->jobId, $taskId, 10, 13, '2026-12-07', '2026-12-07', 10);
expect(end($this->events->dispatched)->newAssigneeId)->toBe(13)
->and(end($this->events->dispatched)->newMemberIds)->toBe([]);
});