Files
Toiminnanohjaus/tests/Feature/Modules/TimeTracking/TimeEntriesTest.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

62 lines
2.4 KiB
PHP

<?php
use App\Models\User;
use App\Modules\Jobs\Application\UseCases\RemoveTask;
use App\Modules\Jobs\Domain\Exceptions\TaskHasTimeEntries;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
use App\Modules\TimeTracking\Infrastructure\Persistence\Eloquent\TimeEntryModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->employee = User::factory()->create();
$this->actingAs($this->employee);
$this->task = TaskModel::factory()->assignedTo([$this->employee->id])->create(['title' => 'Mainoskuvat']);
$this->monday = now()->startOfWeek()->toDateString();
});
it('saves and clears a cell of the weekly view', function () {
$this->get(route('time.week'))->assertOk()->assertSee('Mainoskuvat');
Livewire::test('time::week')
->call('saveCell', $this->task->id, $this->monday, '2,5')
->assertSee('2,5 h.')
->call('saveCell', $this->task->id, $this->monday, '1,1')
->assertHasErrors(["cells.{$this->task->id}.{$this->monday}"]);
expect((float) TimeEntryModel::query()->sole()->hours)->toBe(2.5);
Livewire::test('time::week')->call('saveCell', $this->task->id, $this->monday, '');
expect(TimeEntryModel::query()->count())->toBe(0);
});
it('adds hours from the task page to the day entry and locks done jobs', function () {
Livewire::test('time::task', ['task' => $this->task->id])
->set('workedOn', $this->monday)
->set('hours', '2')
->call('save')
->set('hours', '1,5')
->set('note', 'Lisää kuvia')
->call('save')
->assertSee('kirjaus on nyt 3,5 h.');
expect(TimeEntryModel::query()->count())->toBe(1);
$this->task->customer_job_id && DB::table('customer_jobs')->where('id', $this->task->customer_job_id)->update(['status' => 'done']);
Livewire::test('time::task', ['task' => $this->task->id])->assertSee('tunnit on lukittu');
});
it('keeps subcontractors to their own jobs and blocks removing tasks with hours', function () {
TimeEntryModel::factory()->create(['task_id' => $this->task->id, 'user_id' => $this->employee->id]);
expect(fn () => app(RemoveTask::class)->execute($this->task->customer_job_id, $this->task->id))
->toThrow(TaskHasTimeEntries::class);
$this->actingAs(User::factory()->subcontractor()->create());
$this->get(route('time.task', $this->task->id))->assertForbidden();
});