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ä:')->assertSee('3:00') ->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('board', 'all')->call('setView', 'full') ->call('moveDropped', 'job-'.$job->id, 0, '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('board', 'all')->call('setView', 'full')->call('moveDropped', 'job-'.$job->id, 0, 'in_production'); expect($job->refresh()->status)->toBe(JobStatus::InProduction); $this->get(route('jobs.show', $job->id))->assertSee(__('jobs.history.unlocked')); });