Files
Toiminnanohjaus/tests/Feature/Modules/Jobs/QuickIntakeTest.php
T
petriandClaude Opus 5.5 b3d8e66477 Add quick job intake for phones
Phase 1, item 5:
- New page /jobs/quick (Pikavastaanotto): customer (search or quick
  add), name, short description, service unit, responsible person and
  starting status (Alustava or Prospekti) in one column. The job is
  created with CreateCustomerJob, so numbering, reference checks and
  history are the same as on the full form.
- After saving, the form clears for the next job (responsible person
  and status stay), and a status message with the job number and links
  to the job and its edit page takes focus.
- The customer search of the job form moves to a PicksCustomer trait and
  an <x-jobs.customer-picker> component shared by both pages.
- Menu link and a list button for users who may open jobs; nav items
  accept several route patterns.
- PHPStan ignores unused-trait only for Livewire Concerns, which are
  used from excluded single-file components.
- Spec and CLAUDE.md updated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-23 19:07:25 +02:00

91 lines
3.8 KiB
PHP

<?php
use App\Models\User;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel;
use App\Modules\Customers\Infrastructure\Persistence\Eloquent\CustomerModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->employee = User::factory()->create(['name' => 'Eero Työntekijä']);
$this->actingAs($this->employee);
$this->customer = CustomerModel::factory()->create(['name' => 'Mainos Oy']);
$this->unit = ServiceUnitModel::query()->where('name', 'Markkinointi')->sole();
});
it('opens a job with the required fields and the defaults, then clears the form', function () {
$this->get(route('jobs.quick'))->assertOk()->assertSee('Pikavastaanotto');
Livewire::test('jobs::quick')
->assertSet('form.responsible_user_id', (string) $this->employee->id)
->call('selectCustomer', $this->customer->id)
->set('form.name', 'Kevätkampanja')
->set('form.description', 'Some-mainokset huhtikuulle.')
->set('form.service_unit_id', (string) $this->unit->id)
->set('form.status', 'prospect')
->call('save')
->assertHasNoErrors()
->assertSee('Asiakastyö '.now()->year.'-0001 Kevätkampanja luotu.')
->assertSee('Avaa työ '.now()->year.'-0001')
->assertSee('Täydennä tietoja')
->assertSet('form.name', '')
->assertSet('form.customer_id', '')
->assertSet('form.status', 'prospect')
->assertSet('form.responsible_user_id', (string) $this->employee->id);
$job = CustomerJobModel::query()->sole();
expect($job->status->value)->toBe('prospect')
->and($job->priority->value)->toBe('normal')
->and($job->kind->value)->toBe('one_off')
->and($job->customer_id)->toBe($this->customer->id)
->and(DB::table('customer_job_service_unit')->where('customer_job_id', $job->id)->pluck('service_unit_id')->all())->toBe([$this->unit->id])
->and(DB::table('customer_job_changes')->where('customer_job_id', $job->id)->value('field'))->toBe('created');
});
it('adds a new customer during intake', function () {
Livewire::test('jobs::quick')
->set('customerSearch', 'Kahvila Kulma')
->call('createCustomer')
->assertSee('Asiakas Kahvila Kulma lisätty ja valittu.')
->set('form.name', 'Ruokalistan kuvaus')
->set('form.service_unit_id', (string) $this->unit->id)
->call('save')
->assertHasNoErrors();
expect(CustomerJobModel::query()->sole()->customer_id)
->toBe(CustomerModel::query()->where('name', 'Kahvila Kulma')->sole()->id);
});
it('shows Finnish errors for missing fields and does not accept other statuses', function () {
Livewire::test('jobs::quick')
->set('form.responsible_user_id', '')
->call('save')
->assertHasErrors(['form.customer_id', 'form.name', 'form.service_unit_id', 'form.responsible_user_id'])
->assertSee('Valitse asiakas tai lisää uusi.')
->assertSee('Valitse palveluyksikkö.');
Livewire::test('jobs::quick')
->call('selectCustomer', $this->customer->id)
->set('form.name', 'Työ')
->set('form.service_unit_id', (string) $this->unit->id)
->set('form.status', 'billed')
->call('save')
->assertHasErrors(['form.status']);
expect(CustomerJobModel::query()->count())->toBe(0);
});
it('keeps subcontractors out and shows the menu link only to those who may open jobs', function () {
$this->get(route('jobs.index'))->assertSee('Pikavastaanotto');
$this->actingAs(User::factory()->subcontractor()->create());
$this->get(route('jobs.quick'))->assertForbidden();
$this->get(route('jobs.index'))->assertDontSee('Pikavastaanotto');
});