b2in/tests/Feature/CabinetQuickStatusTest.php
2026-04-10 17:18:17 +02:00

113 lines
3.7 KiB
PHP

<?php
use App\Livewire\Cabinet\QuickStatus;
use App\Models\CabinetTabletSetting;
use Livewire\Livewire;
const VALID_KEY = 'test-secret-key-123';
beforeEach(function () {
config(['domains.cabinet_status_key' => VALID_KEY]);
});
test('page is accessible without login', function () {
$this->get('/info/status?k='.VALID_KEY)
->assertSuccessful()
->assertSeeLivewire(QuickStatus::class);
});
test('page shows forbidden when key is missing', function () {
Livewire::test(QuickStatus::class, ['k' => ''])
->assertSet('authorized', false)
->assertSee('Kein Zugriff');
});
test('page shows forbidden when key is wrong', function () {
Livewire::test(QuickStatus::class, ['k' => 'wrong-key'])
->assertSet('authorized', false)
->assertSee('Kein Zugriff');
});
test('page loads current status when key is correct', function () {
CabinetTabletSetting::factory()->create([
'store_status' => 'notice',
'notice_headline' => 'Testhinweis',
]);
Livewire::test(QuickStatus::class, ['k' => VALID_KEY])
->assertSet('authorized', true)
->assertSet('storeStatus', 'notice')
->assertSet('noticeHeadline', 'Testhinweis');
});
test('selectStatus changes storeStatus', function () {
Livewire::test(QuickStatus::class, ['k' => VALID_KEY])
->call('selectStatus', 'closed')
->assertSet('storeStatus', 'closed');
});
test('selectStatus ignores unknown values', function () {
Livewire::test(QuickStatus::class, ['k' => VALID_KEY])
->call('selectStatus', 'invalid')
->assertSet('storeStatus', 'auto');
});
test('save persists status and headline to database', function () {
Livewire::test(QuickStatus::class, ['k' => VALID_KEY])
->set('storeStatus', 'notice')
->set('noticeHeadline', 'Sonderöffnung')
->set('noticeSubtext', 'Heute nur bis 14 Uhr')
->call('save')
->assertHasNoErrors()
->assertSet('saved', true);
$settings = CabinetTabletSetting::current();
expect($settings->store_status)->toBe('notice');
expect($settings->notice_headline)->toBe('Sonderöffnung');
expect($settings->notice_subtext)->toBe('Heute nur bis 14 Uhr');
});
test('save clears headline when switching to auto', function () {
CabinetTabletSetting::factory()->create([
'store_status' => 'notice',
'notice_headline' => 'Alter Text',
]);
Livewire::test(QuickStatus::class, ['k' => VALID_KEY])
->set('storeStatus', 'auto')
->call('save')
->assertHasNoErrors();
expect(CabinetTabletSetting::current()->store_status)->toBe('auto');
expect(CabinetTabletSetting::current()->notice_headline)->toBeNull();
});
test('save validates headline max length', function () {
Livewire::test(QuickStatus::class, ['k' => VALID_KEY])
->set('storeStatus', 'notice')
->set('noticeHeadline', str_repeat('A', 41))
->call('save')
->assertHasErrors(['noticeHeadline']);
});
test('save validates subtext max length', function () {
Livewire::test(QuickStatus::class, ['k' => VALID_KEY])
->set('storeStatus', 'notice')
->set('noticeSubtext', str_repeat('A', 81))
->call('save')
->assertHasErrors(['noticeSubtext']);
});
test('unauthorized user cannot save', function () {
$component = Livewire::test(QuickStatus::class, ['k' => 'wrong'])
->set('storeStatus', 'closed')
->call('save');
expect(CabinetTabletSetting::count())->toBe(0);
});
test('all four status options are available', function () {
$component = Livewire::test(QuickStatus::class, ['k' => VALID_KEY]);
expect($component->get('statusOptions'))->toHaveKeys(['auto', 'closed', 'notice', 'warning']);
});