CRM/backend/tests/Feature/SettingsBehaviorTest.php

69 خطوط
2.9 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\Setting;
use App\Models\User;
use App\Services\VoIP\DisabledProvider;
use App\Support\PasswordPolicy;
use App\Support\SettingsCatalog;
use Database\Seeders\RolePermissionSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Validator;
use Tests\TestCase;
class SettingsBehaviorTest extends TestCase
{
use RefreshDatabase;
public function test_settings_api_only_exposes_runtime_enforced_options(): void
{
$this->seed(RolePermissionSeeder::class);
$admin = User::factory()->create(['is_active' => true]);
$admin->assignRole('admin');
$response = $this->actingAs($admin)->getJson('/api/settings')->assertOk();
$items = collect($response->json())->flatten(1);
$this->assertTrue($items->contains('key', 'company_name'));
$this->assertFalse($items->contains('key', 'two_factor_enabled'));
$this->assertFalse($items->contains(fn (array $setting) => ! $setting['is_runtime_enforced']));
$this->assertCount(collect(SettingsCatalog::definitions())->where('is_runtime_enforced', true)->count(), $items);
$this->assertFalse($items->firstWhere('key', 'voip_provider')['allowed_values'] === ['mock', 'ami', 'api', 'socket']);
}
public function test_saved_security_settings_change_runtime_password_validation(): void
{
$this->seed(RolePermissionSeeder::class);
$admin = User::factory()->create(['is_active' => true]);
$admin->assignRole('admin');
$this->actingAs($admin)->putJson('/api/settings', ['settings' => [
['key' => 'password_min_length', 'value' => '12', 'group' => 'security', 'type' => 'integer'],
['key' => 'password_require_numbers', 'value' => 'true', 'group' => 'security', 'type' => 'boolean'],
]])->assertOk();
$this->assertDatabaseHas('settings', ['key' => 'password_min_length', 'value' => '12']);
$this->assertTrue(Validator::make(['password' => 'longpassword'], ['password' => PasswordPolicy::rules()])->fails());
$this->assertFalse(Validator::make(['password' => 'longpassword1'], ['password' => PasswordPolicy::rules()])->fails());
}
public function test_production_voip_does_not_report_a_fake_success_when_unconfigured(): void
{
Setting::updateOrCreate(['key' => 'voip_provider'], ['value' => 'none', 'group' => 'voip', 'type' => 'string']);
$result = (new DisabledProvider)->testConnection();
$this->assertFalse($result['ok']);
$this->assertStringContainsString('پیکربندی نشده', $result['message']);
}
public function test_every_exposed_setting_declares_its_runtime_consumer(): void
{
$definitions = collect(SettingsCatalog::definitions())->where('is_runtime_enforced', true);
$this->assertNotEmpty($definitions);
$this->assertEmpty($definitions->filter(fn (array $setting) => empty($setting['used_by']))->all());
}
}