Files

156 lines
6.6 KiB
PHP

<?php
use App\Models\User;
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->employee = User::factory()->create();
$this->actingAs($this->employee);
$this->job = CustomerJobModel::factory()->create(['name' => 'Kylkiteippaukset', 'status' => 'order', 'responsible_user_id' => $this->employee->id]);
});
it('opens a job from the board and saves its details without leaving the board', function () {
Livewire::test('jobs::board')
->call('openJob', $this->job->id)
->assertDispatched('open-job-editor', jobId: $this->job->id);
Livewire::test('jobs::job-editor')
->dispatch('open-job-editor', jobId: $this->job->id)
->assertSet('form.name', 'Kylkiteippaukset')
->assertSee($this->job->number)
->set('form.name', 'Pakettiauton kylkiteippaukset')
->set('form.instructions', 'Asiakkaan logo vektorina')
->call('save')
->assertHasNoErrors()
->assertDispatched('job-saved')
->assertDispatched('job-editor-saved');
expect($this->job->fresh()->name)->toBe('Pakettiauton kylkiteippaukset')
->and($this->job->fresh()->instructions)->toBe('Asiakkaan logo vektorina');
});
it('changes the status and priority at once and records them', function () {
Livewire::test('jobs::job-editor')
->dispatch('open-job-editor', jobId: $this->job->id)
->call('changeStatus', 'in_production')
->assertSee('tila: Tuotannossa')
->call('changePriority', 'urgent')
->assertSee('prioriteetti: Kiire');
expect($this->job->fresh()->status->value)->toBe('in_production')
->and($this->job->fresh()->priority->value)->toBe('urgent')
->and(DB::table('customer_job_changes')->where('customer_job_id', $this->job->id)->whereIn('field', ['status', 'priority'])->count())->toBe(2);
});
it('keeps an employee from billing statuses and from opening the lock', function () {
$this->job->update(['status' => 'done']);
Livewire::test('jobs::job-editor')
->dispatch('open-job-editor', jobId: $this->job->id)
->assertSee('tunnit ja kulut on lukittu')
->assertDontSee('Avaa lukitus (johto)')
->call('changeStatus', 'invoiced')
->assertHasErrors('form.status')
->call('changeStatus', 'in_production')
->assertHasErrors('form.status');
expect($this->job->fresh()->status->value)->toBe('done');
$this->actingAs(User::factory()->management()->create());
Livewire::test('jobs::job-editor')
->dispatch('open-job-editor', jobId: $this->job->id)
->call('unlock');
expect($this->job->fresh()->status->value)->toBe('in_production');
});
it('edits, adds and marks tasks done in the editor', function () {
$task = TaskModel::factory()->create(['customer_job_id' => $this->job->id, 'title' => 'Vedos', 'status' => 'waiting']);
$editor = Livewire::test('jobs::job-editor')
->dispatch('open-job-editor', jobId: $this->job->id)
->call('updateTaskField', $task->id, 'title', 'Vedos asiakkaalle')
->call('updateTaskField', $task->id, 'due_on', '2026-12-16')
->call('setTaskDone', $task->id, true)
->set('newTaskTitle', 'Asennus')
->call('addTask')
->assertHasNoErrors()
->assertSee('Tehtävä Asennus lisätty.');
expect($task->fresh()->title)->toBe('Vedos asiakkaalle')
->and($task->fresh()->due_on?->format('Y-m-d'))->toBe('2026-12-16')
->and($task->fresh()->status->value)->toBe('done')
->and(TaskModel::query()->where('customer_job_id', $this->job->id)->where('title', 'Asennus')->value('assignee_user_id'))->toBe($this->employee->id);
$editor->call('updateTaskField', $task->id, 'nonsense', 'x');
expect($task->fresh()->title)->toBe('Vedos asiakkaalle');
});
it('hides quote amounts without the quotes permission', function () {
$this->job->update(['quoted_amount_cents' => 123400]);
Livewire::test('jobs::job-editor')
->dispatch('open-job-editor', jobId: $this->job->id)
->assertSee('Tarjous- ja laskutussummat on piilotettu')
->assertDontSeeHtml('id="job-editor-quoted"');
$this->actingAs(User::factory()->management()->create());
Livewire::test('jobs::job-editor')
->dispatch('open-job-editor', jobId: $this->job->id)
->assertSeeHtml('id="job-editor-quoted"');
});
it('lets a subcontractor only look at their own job and tick its tasks', function () {
$subcontractor = User::factory()->subcontractor()->create();
$theirs = CustomerJobModel::factory()->withMembers([$subcontractor->id])->create(['status' => 'in_production']);
$task = TaskModel::factory()->create(['customer_job_id' => $theirs->id, 'status' => 'in_progress']);
$this->actingAs($subcontractor);
Livewire::test('jobs::job-editor')
->dispatch('open-job-editor', jobId: $this->job->id)
->assertForbidden();
Livewire::test('jobs::job-editor')
->dispatch('open-job-editor', jobId: $theirs->id)
->assertSee('Voit kuitata tehtäviä')
->call('setTaskDone', $task->id, true)
->call('save')
->assertForbidden();
expect($task->fresh()->status->value)->toBe('done');
});
it('shows a new customer colour at once when the board saves it', function () {
$editor = Livewire::test('jobs::job-editor')
->dispatch('open-job-editor', jobId: $this->job->id)
->assertSeeHtml('background-color: #23476a');
Livewire::test('jobs::board')
->call('setCustomerColor', $this->job->customer_id, '#991b1b')
->assertDispatched('customer-color-changed', customerId: $this->job->customer_id);
$editor->dispatch('customer-color-changed', customerId: $this->job->customer_id)
->assertSeeHtml('background-color: #991b1b');
});
it('has comments and images for each task', function () {
$task = TaskModel::factory()->create(['customer_job_id' => $this->job->id, 'title' => 'Vedos']);
Livewire::test('jobs::job-editor')
->dispatch('open-job-editor', jobId: $this->job->id)
->assertSee('Tarkistuslista')
->assertSee('Kommentit ja kuvat (kommentit 0, kuvat 0)')
->assertSeeLivewire('jobs::task-checklist')
->assertSeeLivewire('jobs::task-comments')
->assertSeeLivewire('jobs::task-images')
->assertSeeHtml('editor-task-'.$task->id.'-tabs-tab-comments')
->assertSeeHtml('editor-task-'.$task->id.'-tabs-tab-images');
});