Files
Toiminnanohjaus/tests/Unit/Modules/TimeTracking/ApprovalTest.php
petriandClaude Opus 5.5 d5cbf9d6eb Add approval of hours and lock opening for management
- Time entries store who approved them and when. Approved hours are
  locked: cells, task page edits and removals are refused.
- "Hyväksy viikko" in the weekly view approves the user's own week;
  subcontractors' hours are approved by management.
- Management gets an approval page (unapproved weeks by person, recently
  approved weeks) and can open anyone's week read-only to approve it or
  cancel its approval, which only management may do.
- Only management may move a done job back to an earlier status, which
  opens the lock on its hours and expenses; the history marks it.
- The Clock contract gets now() for approval times.

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

79 lines
3.6 KiB
PHP

<?php
use App\Modules\TimeTracking\Application\Services\TimeBooking;
use App\Modules\TimeTracking\Application\UseCases\AddTaskHours;
use App\Modules\TimeTracking\Application\UseCases\ApproveWeek;
use App\Modules\TimeTracking\Application\UseCases\CancelWeekApproval;
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\Exceptions\TimeApproved;
use Tests\Fakes\FakeClock;
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->approveWeek = new ApproveWeek($this->entries, new FakeClock(new DateTimeImmutable('2026-11-30')), $transactions);
$this->cancelApproval = new CancelWeekApproval($this->entries, $transactions);
$this->tasks->addTask(1);
$this->tasks->addTask(2);
// Week 48: Monday 23.11.2026.
$this->setDayHours->execute(10, 1, '2026-11-23', '4');
$this->setDayHours->execute(10, 1, '2026-11-29', '2');
$this->setDayHours->execute(10, 1, '2026-11-30', '3');
$this->setDayHours->execute(11, 1, '2026-11-23', '5');
});
it('approves the user\'s unapproved hours of the week only', function () {
expect($this->approveWeek->execute(10, '2026-11-23', approverId: 99))->toBe(2);
$approved = array_filter($this->entries->entries, fn ($entry) => $entry->isApproved());
expect(array_map(fn ($entry) => [$entry->userId(), $entry->workedOn()->format('Y-m-d'), $entry->approvedBy()], array_values($approved)))->toBe([
[10, '2026-11-23', 99],
[10, '2026-11-29', 99],
])->and($this->approveWeek->execute(10, '2026-11-23', 99))->toBe(0);
});
it('locks approved hours against every change', function (Closure $change) {
$this->approveWeek->execute(10, '2026-11-23', 10);
expect(fn () => $change->call($this))->toThrow(TimeApproved::class);
})->with([
'week cell' => [fn () => $this->setDayHours->execute(10, 1, '2026-11-23', '5')],
'emptied cell' => [fn () => $this->setDayHours->execute(10, 1, '2026-11-23', '')],
'task page' => [fn () => $this->addTaskHours->execute(10, 1, '2026-11-23', '1')],
'edit' => [fn () => $this->updateEntry->execute(10, 1, '1', null)],
'remove' => [fn () => $this->removeEntry->execute(10, 1)],
]);
it('lets new hours be added to an approved week on another task', function () {
$this->approveWeek->execute(10, '2026-11-23', 10);
$this->setDayHours->execute(10, 2, '2026-11-23', '1');
expect($this->entries->findForDay(10, 2, new DateTimeImmutable('2026-11-23'))?->isApproved())->toBeFalse();
});
it('cancels the approval of a week so the hours can be changed again', function () {
$this->approveWeek->execute(10, '2026-11-23', 99);
expect($this->cancelApproval->execute(10, '2026-11-23'))->toBe(2);
$this->setDayHours->execute(10, 1, '2026-11-23', '5');
expect($this->entries->findForDay(10, 1, new DateTimeImmutable('2026-11-23'))?->hours()->format())->toBe('5');
});