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']); } }