dress/tests/Feature/Admin/System/GeneralSettingTest.php

104 lines
3.3 KiB
PHP

<?php
use App\Models\GeneralSetting;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use function Pest\Laravel\actingAs;
use function Pest\Laravel\assertDatabaseHas;
use function Pest\Laravel\get;
use function Pest\Laravel\postJson;
/*
|--------------------------------------------------------------------------
| General Setting Module Tests
|--------------------------------------------------------------------------
*/
describe('General Setting Module - Authorization', function () {
it('redirects to login when accessing settings unauthenticated', function () {
get(route('system.settings.index'))
->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);
});
});