Files
Toiminnanohjaus/tests/Feature/Modules/Jobs/TaskCalendarTest.php
T
petriandClaude Sonnet 5.5 eb9346d31c Fill the task calendar's working days in order instead of evenly
A task's estimate now takes each working day of its planned period up to
the assignee's free capacity before the next day gets anything, so 9 h
is booked as 7.5 h + 1.5 h rather than 4.5 h + 4.5 h. What does not fit
in the period still moves past its end and makes the task end later.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-11-25 12:10:43 +02:00

193 lines
10 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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 Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->travelTo(new DateTimeImmutable('2026-12-21 09:00'));
$this->employee = User::factory()->create(['name' => 'Eero Työntekijä']);
$this->other = User::factory()->create(['name' => 'Aino Ahkera']);
$this->actingAs($this->employee);
$marketing = ServiceUnitModel::query()->where('name', 'Markkinointi')->sole();
$this->job = CustomerJobModel::factory()->create(['name' => 'Kampanja', 'status' => 'order', 'responsible_user_id' => $this->employee->id]);
DB::table('customer_job_service_unit')->where('customer_job_id', $this->job->id)->update(['service_unit_id' => $marketing->id]);
$this->graphic = WorkTypeModel::query()->where('name', 'graafinen työ')->sole();
});
function calendarTaskModel(array $attributes, array $assignees): TaskModel
{
return TaskModel::factory()->assignedTo($assignees)->create($attributes);
}
it('shows the two weeks from today with holidays, rows and bars', function () {
calendarTaskModel(['customer_job_id' => test()->job->id, 'work_type_id' => test()->graphic->id, 'title' => 'Mainoskuvat', 'planned_from' => '2026-12-22', 'planned_to' => '2026-12-23'], [test()->other->id]);
calendarTaskModel(['customer_job_id' => test()->job->id, 'work_type_id' => test()->graphic->id, 'title' => 'Ilman aikataulua'], [test()->employee->id]);
$this->get(route('jobs.calendar'))
->assertOk()
->assertSee('Tehtäväkalenteri')
->assertSee('21.12.2026–3.1.2027')
->assertSee('Jouluaatto')
->assertSee('Uudenvuodenpäivä')
->assertSeeInOrder(['Eero Työntekijä', 'Aino Ahkera', 'Ei tekijää'])
->assertSee('Mainoskuvat')
->assertSee('Aikatauluttamattomat tehtävät')
->assertSee('Ilman aikataulua');
});
it('moves a task to another person by dropping it and books their days', function () {
$task = calendarTaskModel(['customer_job_id' => $this->job->id, 'work_type_id' => $this->graphic->id, 'title' => 'Mainoskuvat', 'estimated_hours' => 12], [$this->employee->id]);
Livewire::test('jobs::calendar')
->call('reschedule', $task->id, $this->job->id, $this->employee->id, $this->other->id, '2026-12-28', null)
->assertSee('Tehtävä Mainoskuvat: Aino Ahkera 28.12.2026–29.12.2026.');
expect($task->refresh()->planned_from?->format('Y-m-d'))->toBe('2026-12-28')
->and($task->planned_to?->format('Y-m-d'))->toBe('2026-12-29')
->and($task->assignee_user_id)->toBe($this->other->id)
->and(DB::table('task_bookings')->where('task_id', $task->id)->orderBy('day')->pluck('minutes', 'day')->all())->toBe(['2026-12-28' => 450, '2026-12-29' => 270]);
});
it('continues a task on the next working day when the day is full', function () {
calendarTaskModel(['customer_job_id' => $this->job->id, 'work_type_id' => $this->graphic->id, 'title' => 'Kuvaus', 'estimated_hours' => 6], [$this->employee->id]);
$first = TaskModel::query()->sole();
Livewire::test('jobs::calendar')->call('reschedule', $first->id, $this->job->id, $this->employee->id, $this->employee->id, '2026-12-23', '2026-12-23');
$task = calendarTaskModel(['customer_job_id' => $this->job->id, 'work_type_id' => $this->graphic->id, 'title' => 'Taitto', 'estimated_hours' => 4], [$this->employee->id]);
// 24.–27.12. are holidays and a weekend, so the rest goes to Monday.
Livewire::test('jobs::calendar')
->call('reschedule', $task->id, $this->job->id, $this->employee->id, $this->employee->id, '2026-12-23', '2026-12-23')
->assertSee('Päivät olivat täynnä, joten tehtävä jatkuu 28.12.2026 asti.')
->assertSee('7,5 / 7,5 h');
expect($task->refresh()->planned_to?->format('Y-m-d'))->toBe('2026-12-28');
});
it('changes the schedule and status in the dialog and takes a task out of the calendar', function () {
$task = calendarTaskModel(['customer_job_id' => $this->job->id, 'work_type_id' => $this->graphic->id, 'title' => 'Mainoskuvat', 'planned_from' => '2026-12-22', 'planned_to' => '2026-12-22'], [$this->employee->id]);
Livewire::test('jobs::calendar')
->call('openDialog', $task->id, $this->employee->id)
->assertSet('plannedFrom', '2026-12-22')
->set('plannedFrom', '2026-12-29')
->set('plannedTo', '2026-12-28')
->call('saveDialog')
->assertHasErrors('plannedTo')
->set('plannedTo', '2026-12-30')
->set('status', 'in_progress')
->call('saveDialog')
->assertHasNoErrors()
->assertSee('Tehtävän Mainoskuvat tila on nyt Työn alla.')
->call('openDialog', $task->id, $this->employee->id)
->call('clearSchedule')
->assertSee('Tehtävän Mainoskuvat aikataulu poistettu.');
expect($task->refresh()->status->value)->toBe('in_progress')
->and($task->planned_from)->toBeNull();
});
it('shows the task due date in the dialog', function () {
$due = calendarTaskModel(['customer_job_id' => $this->job->id, 'work_type_id' => $this->graphic->id, 'title' => 'Mainoskuvat', 'due_on' => '2026-12-24', 'planned_from' => '2026-12-22', 'planned_to' => '2026-12-22'], [$this->employee->id]);
$open = calendarTaskModel(['customer_job_id' => $this->job->id, 'work_type_id' => $this->graphic->id, 'title' => 'Taitto', 'due_on' => null, 'planned_from' => '2026-12-22', 'planned_to' => '2026-12-22'], [$this->employee->id]);
Livewire::test('jobs::calendar')
->call('openDialog', $due->id, $this->employee->id)
->assertSet('dialogDueOn', '24.12.2026')
->assertSee('Määräaika')
->call('openDialog', $open->id, $this->employee->id)
->assertSet('dialogDueOn', __('jobs.not_set'));
});
it('shows a subcontractor only their own row and lets them change only the status', function () {
$subcontractor = User::factory()->subcontractor()->create(['name' => 'Ville Alihankkija']);
DB::table('customer_job_members')->insert(['customer_job_id' => $this->job->id, 'user_id' => $subcontractor->id]);
$own = calendarTaskModel(['customer_job_id' => $this->job->id, 'work_type_id' => $this->graphic->id, 'title' => 'Oma tehtävä', 'planned_from' => '2026-12-22', 'planned_to' => '2026-12-22'], [$subcontractor->id]);
calendarTaskModel(['customer_job_id' => $this->job->id, 'work_type_id' => $this->graphic->id, 'title' => 'Toisen tehtävä', 'planned_from' => '2026-12-22', 'planned_to' => '2026-12-22'], [$this->employee->id]);
$this->actingAs($subcontractor);
$this->get(route('jobs.calendar'))
->assertOk()
->assertSee('Oma tehtävä')
->assertDontSee('Toisen tehtävä')
->assertDontSee('id="row-'.$this->employee->id.'-name"', escape: false)
->assertDontSee('Ei tekijää');
Livewire::test('jobs::calendar')
->call('reschedule', $own->id, $this->job->id, $subcontractor->id, $subcontractor->id, '2026-12-28', null)
->assertForbidden();
Livewire::test('jobs::calendar')
->call('openDialog', $own->id, $subcontractor->id)
->set('status', 'done')
->call('saveDialog')
->assertSee('Tehtävän Oma tehtävä tila on nyt Valmis.');
expect($own->refresh()->status->value)->toBe('done')
->and($own->planned_from?->format('Y-m-d'))->toBe('2026-12-22');
});
it('adds a schedule on the task form', function () {
Livewire::test('jobs::task-form', ['job' => $this->job->id])
->set('form.title', 'Mainoskuvat')
->set('form.work_type_id', (string) $this->graphic->id)
->set('form.estimated_hours', '4')
->set('form.planned_from', '2026-12-28')
->call('save')
->assertHasNoErrors();
$task = TaskModel::query()->sole();
expect($task->planned_to?->format('Y-m-d'))->toBe('2026-12-28')
->and(DB::table('task_bookings')->where('task_id', $task->id)->sum('minutes'))->toBe(240);
});
it('shows a task only on its assignee row and books nothing for its members', function () {
$task = TaskModel::factory()->assignedTo([$this->other->id, $this->employee->id])->create(['customer_job_id' => $this->job->id, 'work_type_id' => $this->graphic->id, 'title' => 'Mainoskuvat', 'estimated_hours' => 4]);
Livewire::test('jobs::calendar')->call('reschedule', $task->id, $this->job->id, $this->other->id, $this->other->id, '2026-12-22', '2026-12-22');
expect(DB::table('task_bookings')->where('task_id', $task->id)->pluck('user_id')->unique()->values()->all())->toBe([$this->other->id]);
Livewire::test('jobs::calendar')
->set('mine', true)
->assertDontSee('Mainoskuvat');
});
it('leaves the tasks of quick sale jobs out of the unscheduled tasks', function () {
$quickSales = CustomerJobModel::factory()->create(['name' => 'Pikamyynnit 2026', 'status' => 'in_production', 'quick_sale_year' => 2026]);
calendarTaskModel(['customer_job_id' => $quickSales->id, 'work_type_id' => $this->graphic->id, 'title' => 'Passikuvat'], []);
calendarTaskModel(['customer_job_id' => $this->job->id, 'work_type_id' => $this->graphic->id, 'title' => 'Mainoskuvat'], [$this->employee->id]);
Livewire::test('jobs::calendar')
->assertSee('Mainoskuvat')
->assertDontSee('Passikuvat');
});
it('shows the job details in the info popover of a bar', function () {
$this->job->update(['due_on' => '2026-12-31']);
calendarTaskModel(['customer_job_id' => $this->job->id, 'work_type_id' => $this->graphic->id, 'title' => 'Mainoskuvat', 'description' => 'Kolme kuvaa somekanaviin', 'planned_from' => '2026-12-22', 'planned_to' => '2026-12-23'], [$this->other->id]);
$task = TaskModel::query()->sole();
Livewire::test('jobs::calendar')
->assertSee('aria-describedby="bar-info-'.$task->id.'-'.$this->other->id.'"', escape: false)
->assertSeeInOrder([
'Työ', $this->job->number, 'Kampanja',
'Asiakas', CustomerModel::query()->findOrFail($this->job->customer_id)->name,
'Työn tila', 'Tilaus',
'Työn määräpäivä', '31.12.2026',
'Vastuuhenkilö', 'Eero Työntekijä',
'Työtehtävä', 'graafinen työ',
'Kolme kuvaa somekanaviin',
]);
});