PM_Console/backend/tests/Feature/SettingTest.php

115 خطوط
3.8 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
class SettingTest extends TestCase
{
use RefreshDatabase;
public function test_authenticated_user_can_create_and_update_real_settings(): void
{
$user = User::factory()->create(['status' => 'active']);
$this->grantAdminRole($user);
$token = $user->createToken('api-token')->plainTextToken;
$this->withToken($token)
->postJson('/api/settings', [
'key' => 'default_task_statuses',
'value' => ['todo', 'in_progress', 'done'],
'group' => 'task',
'type' => 'list',
])
->assertCreated()
->assertJsonPath('data.value.0', 'todo')
->assertJsonPath('data.type', 'list');
$setting = Setting::where('key', 'default_task_statuses')->firstOrFail();
$this->withToken($token)
->putJson("/api/settings/{$setting->id}", [
'value' => ['todo', 'review', 'done'],
'group' => 'task',
'type' => 'list',
])
->assertOk()
->assertJsonPath('data.value.1', 'review');
$this->assertSame(['todo', 'review', 'done'], $setting->fresh()->value);
}
public function test_settings_support_plain_strings_and_json_objects(): void
{
$user = User::factory()->create(['status' => 'active']);
$this->grantAdminRole($user);
$token = $user->createToken('api-token')->plainTextToken;
$plain = Setting::create([
'key' => 'company_name',
'value' => 'شرکت پیشگام',
'group' => 'general',
'type' => 'string',
]);
$this->withToken($token)
->getJson('/api/settings')
->assertOk()
->assertJsonFragment([
'key' => 'company_name',
'value' => 'شرکت پیشگام',
]);
$this->withToken($token)
->putJson("/api/settings/{$plain->id}", [
'value' => [
'host' => 'smtp.example.com',
'port' => '587',
'encryption' => 'tls',
],
'group' => 'mail',
'type' => 'json',
])
->assertOk()
->assertJsonPath('data.value.host', 'smtp.example.com');
$this->assertSame('smtp.example.com', $plain->fresh()->value['host']);
}
public function test_company_logo_can_be_uploaded_exposed_as_branding_and_removed(): void
{
Storage::fake('public');
$user = User::factory()->create(['status' => 'active']);
$this->grantAdminRole($user);
$token = $user->createToken('api-token')->plainTextToken;
$response = $this->withToken($token)
->postJson('/api/settings/company-logo', [
'logo' => UploadedFile::fake()->image('company-logo.png', 240, 240),
])
->assertCreated()
->assertJsonPath('data.setting.key', 'company_logo');
$path = $response->json('data.setting.value');
Storage::disk('public')->assertExists($path);
$brandingResponse = $this->withToken($token)
->getJson('/api/branding')
->assertOk()
->assertJsonPath('data.logo_path', $path);
$this->assertStringEndsWith('/storage/'.$path, $brandingResponse->json('data.logo_url'));
$this->withToken($token)
->deleteJson('/api/settings/company-logo')
->assertOk()
->assertJsonPath('data.branding.logo_url', null);
Storage::disk('public')->assertMissing($path);
}
}