From 513820b9e54af3a307eb55bdcab228c62df16957 Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Thu, 30 Apr 2026 08:40:00 +0700 Subject: [PATCH] test: add feature tests for general settings module authorization and updates --- .../Admin/System/GeneralSettingTest.php | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 tests/Feature/Admin/System/GeneralSettingTest.php diff --git a/tests/Feature/Admin/System/GeneralSettingTest.php b/tests/Feature/Admin/System/GeneralSettingTest.php new file mode 100644 index 0000000..429b269 --- /dev/null +++ b/tests/Feature/Admin/System/GeneralSettingTest.php @@ -0,0 +1,103 @@ +assertRedirect(route('login')); + }); + + it('returns 403 when user has no permission to view settings', function () { + actingAs(createUnauthorizedUser()) + ->get(route('system.settings.index')) + ->assertStatus(403); + }); +}); + +describe('General Setting Module - Authorized Actions', function () { + beforeEach(function () { + $user = createAuthorizedUser([ + 'View:Setting', + 'Edit:Setting', + ]); + actingAs($user); + }); + + it('can access settings index page', function () { + get(route('system.settings.index')) + ->assertOk() + ->assertInertia(fn ($page) => $page + ->component('admin/system/settings/index') + ->has('setting') + ); + }); + + it('can update general settings with logo and icon', function () { + Storage::fake('public'); + + // Ensure a setting exists first for testing update logic in Controller + $setting = GeneralSetting::create([ + 'name' => 'Old Name', + 'description' => 'Old Description', + 'address' => 'Old Address', + 'phone' => '000', + ]); + + $data = [ + 'name' => 'VNGrup Dress', + 'description' => 'Toko baju premium', + 'address' => 'Jakarta, Indonesia', + 'phone' => '081234567890', + 'logo' => UploadedFile::fake()->image('logo.png'), + 'icon' => UploadedFile::fake()->image('icon.png'), + ]; + + postJson(route('system.settings.update'), $data) + ->assertRedirect() + ->assertSessionHas('success'); + + assertDatabaseHas('general_settings', [ + 'id' => $setting->id, + 'name' => 'VNGrup Dress', + 'phone' => '081234567890', + ]); + + $setting->refresh(); + expect($setting->getFirstMediaUrl('logo'))->not->toBeEmpty(); + expect($setting->getFirstMediaUrl('icon'))->not->toBeEmpty(); + }); + + it('validates settings update', function () { + // If setting doesn't exist, logo and icon are required + GeneralSetting::truncate(); + + postJson(route('system.settings.update'), []) + ->assertStatus(422) + ->assertJsonValidationErrors(['name', 'description', 'address', 'phone', 'logo', 'icon']); + }); +}); + +describe('General Setting Module - Unauthorized Actions', function () { + beforeEach(function () { + actingAs(createUnauthorizedUser()); + }); + + it('cannot update settings without permission', function () { + postJson(route('system.settings.update'), ['name' => 'Unauthorized']) + ->assertStatus(403); + }); +});