Files
Toiminnanohjaus/tests/Unit/Modules/TimeTracking/TimeTrackingTest.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

122 lines
5.4 KiB
PHP

<?php
use App\Modules\Jobs\Domain\Enums\JobStatus;
use App\Modules\TimeTracking\Application\Services\TimeBooking;
use App\Modules\TimeTracking\Application\UseCases\AddTaskHours;
use App\Modules\TimeTracking\Application\UseCases\RemoveTimeEntry;
use App\Modules\TimeTracking\Application\UseCases\SetDayHours;
use App\Modules\TimeTracking\Application\UseCases\UpdateTimeEntry;
use App\Modules\TimeTracking\Domain\Entities\TimeEntry;
use App\Modules\TimeTracking\Domain\Exceptions\DailyHoursExceeded;
use App\Modules\TimeTracking\Domain\Exceptions\InvalidWorkedHours;
use App\Modules\TimeTracking\Domain\Exceptions\TaskNotBookable;
use App\Modules\TimeTracking\Domain\Exceptions\TimeEntryNotFound;
use App\Modules\TimeTracking\Domain\Exceptions\TimeLocked;
use App\Modules\TimeTracking\Domain\ValueObjects\WorkedHours;
use Tests\Fakes\FakeTaskQueries;
use Tests\Fakes\FakeTransactionManager;
use Tests\Fakes\InMemoryTimeEntryRepository;
beforeEach(function () {
$this->tasks = new FakeTaskQueries;
$this->entries = new InMemoryTimeEntryRepository;
$booking = new TimeBooking($this->tasks, $this->entries);
$transactions = new FakeTransactionManager;
$this->setDayHours = new SetDayHours($this->entries, $booking, $transactions);
$this->addTaskHours = new AddTaskHours($this->entries, $booking, $transactions);
$this->updateEntry = new UpdateTimeEntry($this->entries, $booking, $transactions);
$this->removeEntry = new RemoveTimeEntry($this->entries, $booking, $transactions);
$this->tasks->addTask(1);
$this->tasks->addTask(2);
$this->tasks->addTask(3, jobStatus: JobStatus::Done);
});
it('parses hours in quarters, with a comma or a point', function (string $input, ?string $formatted) {
expect(WorkedHours::parse($input)?->format())->toBe($formatted);
})->with([
'quarter' => ['0,25', '0,25'],
'comma' => ['7,5', '7,5'],
'point' => ['7.50', '7,5'],
'whole day' => ['24', '24'],
'empty' => ['', null],
]);
it('rejects hours that are not quarters or out of range', function (string $input) {
WorkedHours::parse($input);
})->throws(InvalidWorkedHours::class)->with(['0,1', '24,25', '-1', 'kaksi', '0']);
it('adds hours and continues the note of a day entry', function () {
$entry = TimeEntry::create(10, 1, 1, 7, new DateTimeImmutable('2026-05-18'), new WorkedHours(8), 'Luonnokset');
$entry->addHours(new WorkedHours(2), 'Korjaukset');
$entry->addHours(new WorkedHours(1), ' ');
expect($entry->hours()->format())->toBe('2,75')
->and($entry->note())->toBe("Luonnokset\nKorjaukset");
});
it('sets a cell, changes it and clears it', function () {
$this->setDayHours->execute(10, 1, '2026-05-18', '2,5');
$this->setDayHours->execute(10, 1, '2026-05-18', '3');
expect($this->entries->entries)->toHaveCount(1)
->and(array_values($this->entries->entries)[0]->hours()->format())->toBe('3')
->and(array_values($this->entries->entries)[0]->workTypeId())->toBe(7);
$this->setDayHours->execute(10, 1, '2026-05-18', '0');
expect($this->entries->entries)->toBeEmpty();
$this->setDayHours->execute(10, 1, '2026-05-18', '');
expect($this->entries->entries)->toBeEmpty();
});
it('adds hours from the task page to the day entry', function () {
$this->addTaskHours->execute(10, 1, '2026-05-18', '2', 'Aamu');
$total = $this->addTaskHours->execute(10, 1, '2026-05-18', '1,5', 'Iltapäivä');
expect($total)->toBe('3,5')
->and($this->entries->entries)->toHaveCount(1);
});
it('keeps a day within 24 hours across tasks', function () {
$this->setDayHours->execute(10, 1, '2026-05-18', '20');
expect(fn () => $this->setDayHours->execute(10, 2, '2026-05-18', '4,25'))->toThrow(DailyHoursExceeded::class);
$this->setDayHours->execute(10, 2, '2026-05-18', '4');
$this->setDayHours->execute(10, 1, '2026-05-18', '19');
$this->setDayHours->execute(11, 1, '2026-05-18', '8');
expect($this->entries->quartersOn(10, new DateTimeImmutable('2026-05-18')))->toBe(92);
});
it('does not record or change hours on a locked job or an unknown task', function () {
expect(fn () => $this->setDayHours->execute(10, 3, '2026-05-18', '1'))->toThrow(TimeLocked::class)
->and(fn () => $this->addTaskHours->execute(10, 99, '2026-05-18', '1'))->toThrow(TaskNotBookable::class);
$this->setDayHours->execute(10, 1, '2026-05-18', '2');
$entryId = array_key_first($this->entries->entries);
$this->tasks->addTask(1, jobStatus: JobStatus::Invoiced);
expect(fn () => $this->updateEntry->execute(10, $entryId, '3', null))->toThrow(TimeLocked::class)
->and(fn () => $this->removeEntry->execute(10, $entryId))->toThrow(TimeLocked::class)
->and($this->entries->entries)->toHaveCount(1);
});
it('lets users change only their own entries', function () {
$this->setDayHours->execute(10, 1, '2026-05-18', '2');
$entryId = array_key_first($this->entries->entries);
expect(fn () => $this->updateEntry->execute(11, $entryId, '3', null))->toThrow(TimeEntryNotFound::class)
->and(fn () => $this->removeEntry->execute(11, $entryId))->toThrow(TimeEntryNotFound::class);
$this->updateEntry->execute(10, $entryId, '3,75', 'Muutettu');
expect($this->entries->entries[$entryId]->hours()->format())->toBe('3,75')
->and($this->entries->entries[$entryId]->note())->toBe('Muutettu');
$this->removeEntry->execute(10, $entryId);
expect($this->entries->entries)->toBeEmpty();
});