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

127 lines
5.8 KiB
PHP

<?php
use App\Models\User;
use App\Modules\Jobs\Domain\Enums\JobStatus;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
use App\Modules\TimeTracking\Infrastructure\Persistence\Eloquent\TimeEntryModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Gate;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->employee = User::factory()->employee()->create(['name' => 'Eero Työntekijä']);
$this->task = TaskModel::factory()->assignedTo([$this->employee->id])->create(['title' => 'Mainoskuvat']);
$this->monday = now()->startOfWeek()->toDateString();
$this->entry = TimeEntryModel::factory()->create(['task_id' => $this->task->id, 'user_id' => $this->employee->id, 'worked_on' => $this->monday, 'hours' => 3]);
});
it('decides who may approve whose hours', function () {
$management = User::factory()->management()->create();
$subcontractor = User::factory()->subcontractor()->create();
expect(Gate::forUser($this->employee)->allows('time.approve', $this->employee->id))->toBeTrue()
->and(Gate::forUser($this->employee)->allows('time.approve', $management->id))->toBeFalse()
->and(Gate::forUser($subcontractor)->allows('time.approve', $subcontractor->id))->toBeFalse()
->and(Gate::forUser($management)->allows('time.approve', $subcontractor->id))->toBeTrue()
->and(Gate::forUser($this->employee)->allows('time.cancel-approval'))->toBeFalse();
});
it('approves the own week in the weekly view, which locks its cells', function () {
$this->actingAs($this->employee);
Livewire::test('time::week')
->assertSee('Hyväksymättä 3 h.')
->call('approveWeek')
->assertSet('statusMessage', fn (string $message) => str_contains($message, 'hyväksytty. Hyväksytyt tunnit on lukittu.'))
->assertDontSee('id="cell-'.$this->task->id.'-'.$this->monday.'"', false)
->assertDontSee(__('time.approval.cancel'))
->call('saveCell', $this->task->id, $this->monday, '5');
$entry = $this->entry->refresh();
expect($entry->approved_by)->toBe($this->employee->id)
->and($entry->approved_at)->not->toBeNull()
->and((float) $entry->hours)->toBe(3.0);
});
it('does not let a subcontractor approve own hours', function () {
$subcontractor = User::factory()->subcontractor()->create();
$job = CustomerJobModel::factory()->withMembers([$subcontractor->id])->create(['status' => JobStatus::Order]);
$task = TaskModel::factory()->assignedTo([$subcontractor->id])->create(['customer_job_id' => $job->id]);
TimeEntryModel::factory()->create(['task_id' => $task->id, 'user_id' => $subcontractor->id, 'worked_on' => $this->monday]);
$this->actingAs($subcontractor);
Livewire::test('time::week')
->assertSee(__('time.approval.management_approves'))
->call('approveWeek')
->assertForbidden();
});
it('lets management open, approve and cancel another user\'s week read-only', function () {
$this->actingAs(User::factory()->management()->create(['name' => 'Johtaja']));
$this->get(route('time.week', ['kayttaja' => $this->employee->id]))
->assertOk()
->assertSee('Tuntikirjaus: Eero Työntekijä')
->assertDontSee('id="cell-', false)
->assertDontSee('id="task-search"', false);
Livewire::withQueryParams(['kayttaja' => (string) $this->employee->id])
->test('time::week')
->call('saveCell', $this->task->id, $this->monday, '5')
->call('approveWeek')
->assertSee('Johtaja')
->call('cancelApproval')
->assertSet('statusMessage', fn (string $message) => str_ends_with($message, 'hyväksyntä peruttu.'));
expect((float) $this->entry->refresh()->hours)->toBe(3.0)
->and($this->entry->approved_at)->toBeNull();
});
it('keeps employees out of other users\' weeks and the approval page', function () {
$this->actingAs(User::factory()->employee()->create());
$this->get(route('time.week', ['kayttaja' => $this->employee->id]))->assertForbidden();
$this->get(route('time.approvals'))->assertForbidden();
});
it('lists unapproved and approved weeks for management and approves from the list', function () {
$colleague = User::factory()->employee()->create(['name' => 'Kaisa Kollega']);
TimeEntryModel::factory()->approved()->create(['task_id' => TaskModel::factory()->assignedTo([$colleague->id])->create()->id, 'user_id' => $colleague->id, 'worked_on' => $this->monday, 'hours' => 1.5]);
$this->actingAs(User::factory()->management()->create());
Livewire::test('time::approvals')
->assertSeeInOrder(['Hyväksymättä', 'Eero Työntekijä', 'Hyväksytyt', 'Kaisa Kollega'])
->call('approve', $this->employee->id, $this->monday)
->assertSet('statusMessage', fn (string $message) => str_starts_with($message, 'Eero Työntekijä:'))
->assertSee(__('time.approvals.none_pending'));
expect($this->entry->refresh()->approved_at)->not->toBeNull();
});
it('lets only management move a done job back, which opens its lock', function () {
$job = CustomerJobModel::factory()->create(['status' => JobStatus::Done]);
$this->actingAs($this->employee);
Livewire::test('jobs::board')
->set('mine', false)
->call('moveJob', $job->id, 'in_production')
->assertSet('statusMessage', fn (string $message) => str_contains($message, 'lukituksen voi avata vain johto'));
expect($job->refresh()->status)->toBe(JobStatus::Done);
$this->actingAs(User::factory()->management()->create());
Livewire::test('jobs::board')->set('mine', false)->call('moveJob', $job->id, 'in_production');
expect($job->refresh()->status)->toBe(JobStatus::InProduction);
$this->get(route('jobs.show', $job->id))->assertSee(__('jobs.history.unlocked'));
});