Files
Toiminnanohjaus/tests/Feature/Modules/Reports/HoursReportTest.php
T
petriandClaude Sonnet 5.5 64f1e7c54a Add Reports module with the hours by person report
Management's reports (spec 13) get their own module that reads the other
modules through ports. The first report sums hours by person in a period,
narrowed by service unit or customer, with breakdowns, a chart and CSV.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-11-25 08:39:19 +02:00

133 lines
6.3 KiB
PHP

<?php
use App\Models\User;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
use App\Modules\Customers\Infrastructure\Persistence\Eloquent\CustomerModel;
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\DB;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->manager = User::factory()->management()->create(['name' => 'Maija Johto']);
$this->actingAs($this->manager);
$this->worker = User::factory()->employee()->create(['name' => 'Aino Tekijä']);
$this->editing = WorkTypeModel::factory()->create(['name' => 'Editointi']);
$this->customer = CustomerModel::factory()->company()->create(['name' => 'Asiakas Oy']);
$this->customerTask = TaskModel::factory()->create([
'customer_job_id' => CustomerJobModel::factory()->create(['customer_id' => $this->customer->id, 'name' => 'Video'])->id,
'work_type_id' => $this->editing->id,
]);
$internalId = CustomerModel::query()->where('is_internal', true)->value('id');
$this->internalTask = TaskModel::factory()->create([
'customer_job_id' => CustomerJobModel::factory()->create(['customer_id' => $internalId, 'name' => 'Hallinto'])->id,
'work_type_id' => $this->editing->id,
]);
});
/**
* @return list<list<string>>
*/
function hoursCsvRows(string $content): array
{
$handle = fopen('php://memory', 'r+');
fwrite($handle, $content);
rewind($handle);
$rows = [];
while (($row = fgetcsv($handle, null, ';', '"', '')) !== false) {
$rows[] = $row;
}
return $rows;
}
it('sums hours by person on customer and internal work in the period', function () {
TimeEntryModel::factory()->approved()->create(['user_id' => $this->worker->id, 'task_id' => $this->customerTask->id, 'worked_on' => '2026-11-02', 'hours' => 2.5]);
TimeEntryModel::factory()->create(['user_id' => $this->worker->id, 'task_id' => $this->customerTask->id, 'worked_on' => '2026-11-30', 'hours' => 1]);
TimeEntryModel::factory()->approved()->create(['user_id' => $this->worker->id, 'task_id' => $this->internalTask->id, 'worked_on' => '2026-11-10', 'hours' => 0.75]);
TimeEntryModel::factory()->create(['user_id' => $this->worker->id, 'task_id' => $this->customerTask->id, 'worked_on' => '2026-12-01', 'hours' => 8]);
TimeEntryModel::factory()->create(['user_id' => $this->manager->id, 'task_id' => $this->customerTask->id, 'worked_on' => '2026-10-31', 'hours' => 8]);
$report = Livewire::withQueryParams(['alku' => '2026-11-01', 'loppu' => '2026-11-30'])
->test('reports::hours')
->assertOk()
->assertSee('Aino Tekijä')
->assertDontSee('Maija Johto')
->instance()
->report();
expect($report->people)->toHaveCount(1)
->and($report->people[0]->quarters)->toBe(17)
->and($report->people[0]->customerQuarters)->toBe(14)
->and($report->people[0]->internalQuarters)->toBe(3)
->and($report->people[0]->unapprovedQuarters)->toBe(4)
->and($report->people[0]->byWorkType[0]->label)->toBe('Editointi')
->and($report->people[0]->byJob[0]->quarters)->toBe(14)
->and($report->total->quarters)->toBe(17);
});
it('narrows the hours to a customer or a service unit', function () {
TimeEntryModel::factory()->create(['user_id' => $this->worker->id, 'task_id' => $this->customerTask->id, 'worked_on' => '2026-11-02', 'hours' => 2]);
TimeEntryModel::factory()->create(['user_id' => $this->worker->id, 'task_id' => $this->internalTask->id, 'worked_on' => '2026-11-02', 'hours' => 1]);
$otherUnit = ServiceUnitModel::factory()->create();
DB::table('customer_job_service_unit')->where('customer_job_id', $this->internalTask->customer_job_id)->update(['service_unit_id' => $otherUnit->id]);
$page = Livewire::withQueryParams(['alku' => '2026-11-01', 'loppu' => '2026-11-30'])->test('reports::hours');
$page->set('customer', (string) $this->customer->id);
expect($page->instance()->report()->total->quarters)->toBe(8);
$page->set('customer', '')->set('serviceUnit', (string) $otherUnit->id);
expect($page->instance()->report()->total->quarters)->toBe(4);
});
it('starts with the current month and says when the period is not valid', function () {
$this->travelTo('2026-11-25 12:00');
Livewire::test('reports::hours')
->assertSet('from', '2026-11-01')
->assertSet('to', '2026-11-30')
->call('choosePeriod', 'previous_year')
->assertSet('from', '2025-01-01')
->assertSet('to', '2025-12-31')
->set('from', '2026-01-02')
->set('to', '2026-01-01')
->assertSee(__('reports.period.invalid'));
});
it('downloads the hours as CSV', function () {
TimeEntryModel::factory()->create(['user_id' => $this->worker->id, 'task_id' => $this->customerTask->id, 'worked_on' => '2026-11-02', 'hours' => 2.5]);
$response = $this->get(route('reports.csv', ['report' => 'hours', 'alku' => '2026-11-01', 'loppu' => '2026-11-30']))
->assertOk()
->assertDownload('tunnit-2026-11-01-2026-11-30.csv');
expect($response->headers->get('Cache-Control'))->toContain('no-store');
$rows = hoursCsvRows(substr($response->streamedContent(), 3));
expect($rows[0])->toBe(array_values(__('reports.hours.csv')))
->and($rows)->toHaveCount(2)
->and($rows[1][0])->toBe('Aino Tekijä')
->and($rows[1][3])->toBe('Asiakas Oy')
->and($rows[1][4])->toBe('ei')
->and($rows[1][6])->toBe('Editointi')
->and(array_slice($rows[1], 7))->toBe(['2,5', '2,5', '0']);
});
it('is only for admin and management', function () {
$this->actingAs(User::factory()->employee()->create());
$this->get(route('reports.hours'))->assertForbidden();
$this->get(route('reports.csv', ['report' => 'hours', 'alku' => '2026-11-01', 'loppu' => '2026-11-30']))->assertForbidden();
$this->actingAs(User::factory()->admin()->create());
$this->get(route('reports.hours'))->assertOk();
$this->get(route('reports.csv', ['report' => 'hours', 'alku' => 'x', 'loppu' => '2026-11-30']))->assertNotFound();
});