Files
Toiminnanohjaus/tests/Unit/Modules/Jobs/Application/TaskUseCasesTest.php
T
petriandClaude Opus 5.5 3922c744a7 Add time tracking on tasks with a weekly sheet
Phase 1, item 8 (last of phase 1). New TimeTracking module:
- One entry per user, task and day (0.25-24 h in quarters, with a note);
  the job and work type are copied from the task. A user's day cannot
  exceed 24 h, and hours on a done, invoiced, billed or archived job
  cannot be added or changed.
- Weekly sheet /time: tasks as rows (own open tasks, tasks with hours
  that week, tasks added by search), days as columns, row, day and week
  totals, week navigation. A cell saves on change and an empty cell
  removes the entry; on phones one day at a time. Saves and errors are
  announced, errors are tied to their cell.
- Task booking page /time/tasks/{task}: date, hours and note, added to
  the day's entry; own entries with edit and remove.
- Anyone but subcontractors may book on any unlocked task;
  subcontractors only on jobs they are attached to.

Jobs module:
- JobStatus::locksTime(), BookableTask and task queries for booking.
- A task with time entries cannot be removed (TaskUsage contract,
  provided by TimeTracking); the job page links tasks to booking.
- The "involved in a job" rule is shared in JobInvolvement.

Spec and CLAUDE.md updated.

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

139 lines
6.9 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\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\CreateCustomerJob;
use App\Modules\Jobs\Application\UseCases\RemoveTask;
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\Exceptions\InvalidTaskReference;
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskTitle;
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 Tests\Fakes\FakeCatalogQueries;
use Tests\Fakes\FakeClock;
use Tests\Fakes\FakeCustomerQueries;
use Tests\Fakes\FakeTaskUsage;
use Tests\Fakes\FakeTransactionManager;
use Tests\Fakes\FakeUserAccountQueries;
use Tests\Fakes\InMemoryCustomerJobRepository;
use Tests\Fakes\InMemoryTaskRepository;
beforeEach(function () {
$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->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->addTask = new AddTask($this->jobs, $this->tasks, $checker, $transactions);
$this->updateTask = new UpdateTask($this->jobs, $this->tasks, $checker, $transactions);
$this->changeTaskStatus = new ChangeTaskStatus($this->tasks, $transactions);
$this->usage = new FakeTaskUsage;
$this->removeTask = new RemoveTask($this->tasks, $this->usage, $transactions);
$this->changeJobStatus = new ChangeJobStatus($this->jobs, $transactions);
});
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', assigneeUserIds: [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?->assigneeUserIds())->toBe([10])
->and($this->tasks->tasks)->toHaveCount(2);
});
it('rejects a work type of another unit and an inactive assignee', function (int $workTypeId, array $assignees, string $field) {
try {
$this->addTask->execute($this->jobId, new TaskData('Työ', $workTypeId, 'waiting', assigneeUserIds: $assignees), 10);
$this->fail('No exception');
} catch (InvalidTaskReference $exception) {
expect($exception->field)->toBe($field);
}
})->with([
'work type of another unit' => [21, [], 'work_type'],
'inactive assignee' => [11, [12], 'assignees'],
]);
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'));
$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'));
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')),
'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();
});