Files
Toiminnanohjaus/tests/Feature/Modules/Machines/MaintenanceTest.php
T
petriandClaude Sonnet 5.5 ef400054b0 Return stock of cancelled maintenance, throttle the PIN page's password
Cancelling a maintenance record now reverses the stock it used, through
Inventory's new ReverseMaintenanceStockUse behind the MaintenanceMaterials
port, in the same transaction as the cancellation.

The PIN page allows 5 wrong current passwords a minute per user, as the
login form does, so a signed-in session cannot be used to guess the
password.

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

163 lines
7.9 KiB
PHP

<?php
use App\Models\User;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel;
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockItemModel;
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockMovementModel;
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineCounterModel;
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineCounterReadingModel;
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineModel;
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MaintenanceItemModel;
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MaintenanceRecordModel;
use App\Modules\Notifications\Infrastructure\Persistence\Eloquent\NotificationModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->travelTo(now()->setTimezone(config('app.business_timezone'))->setTime(10, 0));
$this->manager = User::factory()->management()->create(['name' => 'Maija Johto']);
$this->employee = User::factory()->employee()->create(['name' => 'Eero Työntekijä']);
$print = ServiceUnitModel::query()->where('name', 'Painotuotanto')->sole();
$print->update(['uses_stock' => true]);
$this->machine = MachineModel::factory()->inUnits([$print->id])->create(['name' => 'Suurkuvatulostin', 'responsible_user_id' => $this->employee->id]);
$this->prints = MachineCounterModel::query()->create(['machine_id' => $this->machine->id, 'name' => 'Tulosteet', 'unit' => 'piece', 'sort_order' => 1]);
$this->drum = StockItemModel::factory()->create(['name' => 'Vaihtorumpu']);
});
it('adds maintenance items with a counter interval, time intervals or both, and needs one of them', function () {
$this->actingAs($this->manager);
Livewire::test('machines::maintenance-item-form', ['machine' => $this->machine->id])
->set('name', 'Pään puhdistus')
->call('save')
->assertHasErrors('period')
->set('period', 'day')
->call('save')
->assertHasNoErrors();
Livewire::test('machines::maintenance-item-form', ['machine' => $this->machine->id])
->set('name', 'Rummun vaihto')
->set('counterId', (string) $this->prints->id)
->set('interval', '20000')
->set('period', 'month')
->set('periodCount', '6')
->set('stockItemId', (string) $this->drum->id)
->call('save')
->assertHasNoErrors();
expect(MaintenanceItemModel::query()->where('name', 'Rummun vaihto')->sole()->only(['interval_milli', 'period_count', 'stock_quantity_milli']))
->toBe(['interval_milli' => 20_000_000, 'period_count' => 6, 'stock_quantity_milli' => 1_000]);
$this->actingAs($this->employee);
$this->get(route('machines.maintenance.create', $this->machine->id))->assertForbidden();
});
it('adds the usage of a job to the cumulative counter and refuses an empty or zero usage', function () {
$this->actingAs($this->employee);
Livewire::test('machines::maintenance', ['machineId' => $this->machine->id])
->set('usage.'.$this->prints->id, '350')
->call('saveUsage', $this->prints->id)
->assertHasNoErrors()
->set('usage.'.$this->prints->id, '1 000')
->call('saveUsage', $this->prints->id)
->assertSet('message', "Laskuriin Tulosteet lisätty 1\u{00A0}000 kpl, laskuri nyt 1\u{00A0}350 kpl.")
->set('usage.'.$this->prints->id, '0')
->call('saveUsage', $this->prints->id)
->assertHasErrors('usage.'.$this->prints->id)
->set('usage.'.$this->prints->id, '')
->call('saveUsage', $this->prints->id)
->assertHasErrors('usage.'.$this->prints->id);
expect(MachineCounterReadingModel::query()->orderBy('id')->pluck('reading_milli')->all())->toBe([350_000, 1_350_000]);
});
it('hides the maintenance sections of a machine without counters and maintenance items from users', function () {
$this->prints->delete();
$this->actingAs($this->employee);
Livewire::test('machines::maintenance', ['machineId' => $this->machine->id])
->assertDontSee(__('machines.maintenance.items'))
->assertDontSee(__('machines.maintenance.history'));
$this->actingAs($this->manager);
Livewire::test('machines::maintenance', ['machineId' => $this->machine->id])
->assertSee(__('machines.maintenance.add'))
->assertDontSee(__('machines.maintenance.history'));
});
it('shows a maintenance request when due and resets the interval with a maintenance record that uses the stock item', function () {
$item = MaintenanceItemModel::query()->create([
'machine_id' => $this->machine->id, 'name' => 'Rummun vaihto', 'machine_counter_id' => $this->prints->id, 'interval_milli' => 20_000_000,
'counter_baseline_milli' => 0, 'period' => 'week', 'period_count' => 1, 'starts_on' => now()->toDateString(), 'warning_percent' => 90,
'stock_item_id' => $this->drum->id, 'stock_quantity_milli' => 1_000,
]);
$this->actingAs($this->employee);
Livewire::test('machines::maintenance', ['machineId' => $this->machine->id])
->assertSee('Kunnossa')
->set('usage.'.$this->prints->id, '19 000')
->call('saveUsage', $this->prints->id)
->assertSee('Huolto lähestyy')
->assertSee("jäljellä 1\u{00A0}000 kpl", false);
$this->travel(8)->days();
Livewire::test('machines::maintenance', ['machineId' => $this->machine->id])
->assertSee('Huolto myöhässä')
->call('startRecord', $item->id)
->set('recordNote', 'Rumpu vaihdettu')
->call('record')
->assertSet('message', 'Huolto Rummun vaihto kirjattu.')
->assertSee('Kunnossa')
->assertSee("jäljellä 20\u{00A0}000 kpl", false)
->assertSee('Rumpu vaihdettu');
$record = MaintenanceRecordModel::query()->sole();
$use = StockMovementModel::query()->sole();
expect($record->counter_reading_milli)->toBe(19_000_000)
->and($use->only(['stock_item_id', 'machine_id', 'maintenance_record_id', 'quantity_milli']))
->toBe(['stock_item_id' => $this->drum->id, 'machine_id' => $this->machine->id, 'maintenance_record_id' => $record->id, 'quantity_milli' => -1_000]);
Livewire::test('machines::maintenance', ['machineId' => $this->machine->id])->call('confirmCancel', $record->id)->assertForbidden();
$this->actingAs($this->manager);
Livewire::test('machines::maintenance', ['machineId' => $this->machine->id])
->call('confirmCancel', $record->id)
->call('cancelRecord')
->assertSee('Peruttu')
->assertSee('Huolto myöhässä');
$reversal = StockMovementModel::query()->where('reverses_movement_id', $use->id)->sole();
expect($reversal->quantity_milli)->toBe(1_000)
->and($reversal->user_id)->toBe($this->manager->id)
->and((int) StockMovementModel::query()->where('stock_item_id', $this->drum->id)->sum('quantity_milli'))->toBe(0);
});
it('notifies the responsible person of a maintenance request once', function () {
MaintenanceItemModel::query()->create([
'machine_id' => $this->machine->id, 'name' => 'Pään puhdistus', 'period' => 'day', 'period_count' => 1,
'starts_on' => now()->subDay()->toDateString(), 'warning_percent' => 90,
]);
$this->artisan('notifications:maintenance-due')->assertSuccessful();
$this->artisan('notifications:maintenance-due')->assertSuccessful();
$notification = NotificationModel::query()->sole();
expect($notification->user_id)->toBe($this->employee->id)
->and($notification->type->value)->toBe('maintenance_due');
$this->actingAs($this->employee);
Livewire::test('notifications::bell')->assertSee('Koneen Suurkuvatulostin huolto Pään puhdistus on myöhässä.');
$this->actingAs($this->manager);
$this->get(route('home'))->assertOk();
Livewire::test('dashboard::management')->assertSee('1 konehuolto myöhässä')->assertSee('Suurkuvatulostin: Pään puhdistus');
});