81 lines
2.8 KiB
PHP
81 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Http\Client;
|
|
|
|
use App\Models\Medium;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Tests\TestCase;
|
|
|
|
class ProfileControllerTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
Storage::fake('public');
|
|
}
|
|
|
|
public function test_update_requires_authentication(): void
|
|
{
|
|
$this->post(route('dashboard.profile.update'))->assertRedirect(route('login'));
|
|
}
|
|
|
|
public function test_update_saves_profile_fields(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
$this->actingAs($user)->from('/dashboard')->post(route('dashboard.profile.update'), [
|
|
'fullname' => 'نام جدید',
|
|
'national_code' => '0012345678',
|
|
'email' => 'new@example.com',
|
|
'city' => 'تهران',
|
|
])->assertRedirect('/dashboard')->assertSessionHas('success');
|
|
|
|
$this->assertDatabaseHas('users', ['id' => $user->id, 'fullname' => 'نام جدید', 'national_code' => '0012345678', 'email' => 'new@example.com', 'city' => 'تهران']);
|
|
}
|
|
|
|
public function test_update_stores_avatar_and_sets_profile_url(): void
|
|
{
|
|
$user = User::factory()->create(['profile_url' => null]);
|
|
|
|
$this->actingAs($user)->post(route('dashboard.profile.update'), [
|
|
'fullname' => 'n',
|
|
'national_code' => '1',
|
|
'avatar' => UploadedFile::fake()->image('me.png'),
|
|
]);
|
|
|
|
$medium = Medium::first();
|
|
$this->assertNotNull($medium);
|
|
$this->assertStringStartsWith('profile/', $medium->path);
|
|
$this->assertSame($medium->url, $user->fresh()->profile_url);
|
|
Storage::disk('public')->assertExists($medium->path);
|
|
}
|
|
|
|
public function test_update_replaces_previous_avatar(): void
|
|
{
|
|
$old = Medium::factory()->public()->create(['path' => 'profile/old.png', 'url' => 'http://example.test/storage/profile/old.png']);
|
|
Storage::disk('public')->put($old->path, 'old');
|
|
$user = User::factory()->create(['profile_url' => $old->url]);
|
|
|
|
$this->actingAs($user)->post(route('dashboard.profile.update'), [
|
|
'fullname' => 'n',
|
|
'national_code' => '1',
|
|
'avatar' => UploadedFile::fake()->image('new.png'),
|
|
]);
|
|
|
|
$this->assertDatabaseMissing('media', ['id' => $old->id]);
|
|
Storage::disk('public')->assertMissing('profile/old.png');
|
|
$this->assertNotSame($old->url, $user->fresh()->profile_url);
|
|
}
|
|
|
|
public function test_update_validates_required_fields(): void
|
|
{
|
|
$this->actingAs(User::factory()->create())->post(route('dashboard.profile.update'), [])->assertSessionHasErrors(['fullname', 'national_code']);
|
|
}
|
|
}
|