Files
famous-ly4-ev/app-modules/identity/tests/Feature/ManageAppSettingsTest.php
T
2026-08-23 20:44:52 +07:00

93 lines
3.4 KiB
PHP

<?php
use App\Models\User;
use Livewire\Livewire;
use Modules\Identity\Database\Seeders\RolePermissionSeeder;
use Modules\Identity\Filament\Pages\ManageAppSettings;
use Modules\Shared\Support\EnvFileWriter;
beforeEach(function () {
$this->seed(RolePermissionSeeder::class);
// Never let a test write to the real project .env — bind the writer to
// a throwaway temp file instead.
$this->envPath = sys_get_temp_dir().'/manage-app-settings-test-'.uniqid().'.env';
file_put_contents($this->envPath, "APP_NAME=Laravel\n");
app()->instance(EnvFileWriter::class, new EnvFileWriter($this->envPath));
});
afterEach(function () {
@unlink($this->envPath);
});
test('an admin without manage_settings is forbidden from the app settings page', function () {
$support = User::factory()->create();
$support->assignRole('support');
$this->actingAs($support)->get('/admin/manage-app-settings')->assertForbidden();
});
test('a super_admin can view and save app settings, writing them to .env', function () {
$superAdmin = User::factory()->create();
$superAdmin->assignRole('super_admin');
$this->actingAs($superAdmin);
Livewire::test(ManageAppSettings::class)
->assertOk()
->fillForm([
'site_name' => 'EV Booking Co',
'support_email' => 'help@evbooking.test',
'support_phone' => '+95912345678',
'timezone' => 'Asia/Yangon',
'currency' => 'MMK',
'back_seat_enabled' => false,
'whole_vehicle_enabled' => true,
'front_seat_max_per_booking' => 2,
'booking_admin_emails' => ['ops@evbooking.test', 'dispatch@evbooking.test'],
'sms_enabled' => true,
'sms_server' => 'https://sms.example.test/send',
'sms_token' => 'secret-token',
'sms_sender' => 'EVBooking',
])
->call('save')
->assertHasNoFormErrors();
$contents = file_get_contents($this->envPath);
expect($contents)
->toContain('APP_NAME="EV Booking Co"')
->toContain('SUPPORT_EMAIL=help@evbooking.test')
->toContain('APP_TIMEZONE=Asia/Yangon')
->toContain('APP_CURRENCY=MMK')
->toContain('BOOKING_BACK_SEAT_ENABLED=false')
->toContain('BOOKING_WHOLE_VEHICLE_ENABLED=true')
->toContain('BOOKING_FRONT_SEAT_MAX_PER_BOOKING=2')
->toContain('BOOKING_ADMIN_EMAILS=ops@evbooking.test,dispatch@evbooking.test')
->toContain('SMS_ENABLED=true')
->toContain('SMS_SERVER=https://sms.example.test/send')
->toContain('SMS_TOKEN=secret-token')
->toContain('SMS_SENDER=EVBooking');
});
test('sms server and token are required once sms is enabled', function () {
$superAdmin = User::factory()->create();
$superAdmin->assignRole('super_admin');
$this->actingAs($superAdmin);
Livewire::test(ManageAppSettings::class)
->fillForm(['sms_enabled' => true, 'sms_server' => '', 'sms_token' => ''])
->call('save')
->assertHasFormErrors(['sms_server', 'sms_token']);
});
test('front seat max per booking must be at least 1', function () {
$superAdmin = User::factory()->create();
$superAdmin->assignRole('super_admin');
$this->actingAs($superAdmin);
Livewire::test(ManageAppSettings::class)
->fillForm(['front_seat_max_per_booking' => 0])
->call('save')
->assertHasFormErrors(['front_seat_max_per_booking']);
});