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, ]) ->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'); }); 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']); });