Files
Toiminnanohjaus/tests/Feature/Modules/TimeTracking/TimeEntriesTest.php
T
petriandClaude Opus 5.5 38c5b43018 Include the last day of a date range in expense and time entry queries
SQLite stores date casts as "Y-m-d 00:00:00", so a between ending at
"Y-m-d" left out the month's last day and the week's Sunday (the own
expenses test failed on the 30th). Ranges now end before the next day.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-30 10:47:01 +02:00

179 lines
7.2 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 Illuminate\Support\Facades\DB;
use Livewire\Features\SupportLockedProperties\CannotUpdateLockedPropertyException;
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:30.')
->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('takes hours and minutes, minutes and 15 minute steps in a cell', function () {
$week = Livewire::test('time::week')
->call('saveCell', $this->task->id, $this->monday, '1:30')
->call('adjustCell', $this->task->id, $this->monday, 1);
expect((float) TimeEntryModel::query()->sole()->hours)->toBe(1.75);
$week->call('saveCell', $this->task->id, $this->monday, '90');
expect((float) TimeEntryModel::query()->sole()->hours)->toBe(1.5);
$week->call('saveCell', $this->task->id, $this->monday, '1:10')
->assertHasErrors(["cells.{$this->task->id}.{$this->monday}"]);
foreach (range(1, 6) as $step) {
$week->call('adjustCell', $this->task->id, $this->monday, -1);
}
expect(TimeEntryModel::query()->count())->toBe(0);
});
it('stores the billing basis, billing note and internal comment of an entry', function () {
Livewire::test('time::week')
->call('saveCell', $this->task->id, $this->monday, '2')
->call('openEntry', $this->task->id, $this->monday)
->assertSet('entryBillingModel', 'hourly')
->set('entryBillingModel', 'non_billable')
->set('entryNote', 'Kuvien rajaus')
->set('entryComment', 'Asiakas toivoi kiirettä')
->call('saveEntry')
->assertSee('Kirjaus tallennettu');
$entry = TimeEntryModel::query()->sole();
expect($entry->note)->toBe('Kuvien rajaus')
->and($entry->internal_comment)->toBe('Asiakas toivoi kiirettä')
->and($entry->billing_model)->toBe('non_billable');
});
it('blocks approving a week without billing notes and lists the entries', function () {
Livewire::test('time::week')
->call('saveCell', $this->task->id, $this->monday, '2')
->assertSee('1 selite puuttuu')
->call('approveWeek')
->assertSee('Viikkoa ei voi vielä hyväksyä');
expect(TimeEntryModel::query()->sole()->approved_at)->toBeNull();
});
it('pins tasks to the pinned part, keeps them on later weeks and unpins them', function () {
$other = TaskModel::factory()->create(['title' => 'Kehitystyöt']);
$week = Livewire::test('time::week')
->call('pinTasks', [$other->id])
->assertSee('Tehtävä Kehitystyöt kiinnitetty');
expect($week->instance()->sheet->row($other->id)?->pinned)->toBeTrue()
->and($week->instance()->sheet->pinnedTaskCount())->toBe(1);
$week->call('showWeek', 'next');
expect($week->instance()->sheet->pinnedTaskCount())->toBe(1);
$week->call('unpinTask', $other->id);
expect($week->instance()->sheet->pinnedTaskCount())->toBe(0);
$week->call('pinTasks', [$other->id])->call('clearPins')->assertSee('1 kiinnitys poistettu.');
});
it('lets a pin lapse after the job is done, except on weeks with hours', function () {
$week = Livewire::test('time::week')
->call('pinTasks', [$this->task->id])
->call('saveCell', $this->task->id, $this->monday, '1');
DB::table('customer_jobs')->where('id', $this->task->customer_job_id)->update(['status' => 'done']);
$week->call('showWeek', 'this');
expect($week->instance()->sheet->pinnedTaskCount())->toBe(1);
$week->call('showWeek', 'next');
expect($week->instance()->sheet->pinnedTaskCount())->toBe(0)
->and($week->instance()->sheet->scheduledTaskCount())->toBe(0);
});
it('saves the timer time rounded to quarter hours on today', function () {
Livewire::test('time::week')
->call('saveTimer', $this->task->id, 50 * 60, 'Ottelun striimaus')
->assertSee('0:45 kirjattu tehtävälle Mainoskuvat');
$entry = TimeEntryModel::query()->sole();
expect((float) $entry->hours)->toBe(0.75)
->and($entry->worked_on->toDateString())->toBe(now()->toDateString())
->and($entry->note)->toBe('Ottelun striimaus');
});
it('takes the week only from the URL and the week buttons', function () {
$this->get(route('time.week', ['viikko' => "abc'); alert(1); ('"]))->assertOk()->assertSee('Mainoskuvat');
$week = Livewire::test('time::week');
expect(fn () => $week->set('week', '2026-05-20'))->toThrow(CannotUpdateLockedPropertyException::class)
->and(fn () => $week->set('selectedDay', "x'); alert(1); ('"))->toThrow(CannotUpdateLockedPropertyException::class);
$week->call('selectDay', "x'); alert(1); ('")->assertSet('selectedDay', now()->toDateString());
});
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();
});
it('counts the hours of Sunday in the week', function () {
$sunday = now()->startOfWeek()->addDays(6)->toDateString();
TimeEntryModel::factory()->create(['task_id' => $this->task->id, 'user_id' => $this->employee->id, 'worked_on' => $sunday, 'hours' => 2]);
$sheet = Livewire::test('time::week')->instance()->sheet;
expect($sheet->totalQuarters)->toBe(8)
->and($sheet->dayTotals[$sunday])->toBe(8);
});