121 lines
3.9 KiB
PHP
121 lines
3.9 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Tests\Feature\Admin;
|
||
|
|
|
||
|
|
use App\Models\Role;
|
||
|
|
use App\Models\Setting;
|
||
|
|
use App\Models\User;
|
||
|
|
use App\Models\Permission;
|
||
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
|
|
use Tests\TestCase;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Functional test for Site Settings management.
|
||
|
|
*/
|
||
|
|
class SettingManagementTest extends TestCase
|
||
|
|
{
|
||
|
|
use RefreshDatabase;
|
||
|
|
|
||
|
|
protected User $admin;
|
||
|
|
|
||
|
|
protected function setUp(): void
|
||
|
|
{
|
||
|
|
parent::setUp();
|
||
|
|
|
||
|
|
// Create Admin role and user
|
||
|
|
$adminRole = Role::create(['name' => 'Admin', 'slug' => 'admin']);
|
||
|
|
$this->admin = User::factory()->create();
|
||
|
|
$this->admin->roles()->attach($adminRole);
|
||
|
|
|
||
|
|
// Ensure settings permissions exist (though Admin bypasses)
|
||
|
|
Permission::create(['name' => 'View Settings', 'slug' => 'view-settings', 'resource' => 'settings', 'action' => 'view']);
|
||
|
|
Permission::create(['name' => 'Update Settings', 'slug' => 'update-settings', 'resource' => 'settings', 'action' => 'update']);
|
||
|
|
|
||
|
|
// Seed basic settings
|
||
|
|
$this->seed(\Database\Seeders\SettingSeeder::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Test that admin can view the settings page.
|
||
|
|
*/
|
||
|
|
public function test_admin_can_view_settings_page()
|
||
|
|
{
|
||
|
|
$response = $this->actingAs($this->admin)->get(route('admin.settings.index'));
|
||
|
|
|
||
|
|
$response->assertStatus(200);
|
||
|
|
$response->assertViewHas('settings');
|
||
|
|
$response->assertSee('SiteWeaver CMS');
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Test that admin can update site settings.
|
||
|
|
*/
|
||
|
|
public function test_admin_can_update_settings()
|
||
|
|
{
|
||
|
|
$payload = [
|
||
|
|
'site_title' => 'Updated Site Title',
|
||
|
|
'seo_description' => 'New SEO Description',
|
||
|
|
'seo_keywords' => ['updated', 'cms'],
|
||
|
|
'supported_languages' => [
|
||
|
|
['name' => 'English', 'abbreviation' => 'en'],
|
||
|
|
['name' => 'Spanish', 'abbreviation' => 'es']
|
||
|
|
],
|
||
|
|
'default_locale' => 'en',
|
||
|
|
'translation_driver' => 'mock',
|
||
|
|
'google_translate_key' => ''
|
||
|
|
];
|
||
|
|
|
||
|
|
$response = $this->actingAs($this->admin)
|
||
|
|
->postJson(route('admin.settings.update'), $payload);
|
||
|
|
|
||
|
|
$response->assertStatus(200);
|
||
|
|
|
||
|
|
$this->assertEquals('Updated Site Title', Setting::get('site_title'));
|
||
|
|
$this->assertEquals('New SEO Description', Setting::get('seo_description'));
|
||
|
|
$this->assertEquals(['updated', 'cms'], Setting::get('seo_keywords'));
|
||
|
|
|
||
|
|
$languages = Setting::get('supported_languages');
|
||
|
|
$this->assertCount(2, $languages);
|
||
|
|
$this->assertEquals('es', $languages[1]['abbreviation']);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Test that validation is enforced for settings.
|
||
|
|
*/
|
||
|
|
public function test_settings_validation()
|
||
|
|
{
|
||
|
|
$payload = [
|
||
|
|
'site_title' => '', // Required
|
||
|
|
'supported_languages' => [], // Min 1
|
||
|
|
'default_locale' => 'e', // Size 2
|
||
|
|
'translation_driver' => 'invalid' // In mock,google
|
||
|
|
];
|
||
|
|
|
||
|
|
$response = $this->actingAs($this->admin)
|
||
|
|
->postJson(route('admin.settings.update'), $payload);
|
||
|
|
|
||
|
|
$response->assertStatus(422);
|
||
|
|
$response->assertJsonValidationErrors(['site_title', 'supported_languages', 'default_locale', 'translation_driver']);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Test that locale middleware uses dynamic settings.
|
||
|
|
*/
|
||
|
|
public function test_locale_middleware_uses_dynamic_settings()
|
||
|
|
{
|
||
|
|
// Update supported languages to include Spanish
|
||
|
|
Setting::set('supported_languages', [
|
||
|
|
['name' => 'English', 'abbreviation' => 'en'],
|
||
|
|
['name' => 'Spanish', 'abbreviation' => 'es']
|
||
|
|
], 'localization');
|
||
|
|
|
||
|
|
// Clear cache since we modified DB directly
|
||
|
|
\Illuminate\Support\Facades\Cache::flush();
|
||
|
|
|
||
|
|
// Request with Spanish prefix should work now
|
||
|
|
$response = $this->get('/es/any-page');
|
||
|
|
|
||
|
|
$this->assertEquals('es', app()->getLocale());
|
||
|
|
}
|
||
|
|
}
|