Users can now upload an avatar from Profile settings.

This commit is contained in:
2026-05-16 04:00:06 +03:30
parent f46f32aa4c
commit 61b4cf9252
13 changed files with 274 additions and 19 deletions

View File

@@ -4,6 +4,9 @@ namespace Tests\Feature\Settings;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Inertia\Testing\AssertableInertia as Assert;
use Tests\TestCase;
class ProfileUpdateTest extends TestCase
@@ -21,6 +24,22 @@ class ProfileUpdateTest extends TestCase
$response->assertOk();
}
public function test_profile_page_exposes_user_avatar_url(): void
{
$user = User::factory()->create([
'avatar_path' => 'users/demo/avatar.jpg',
]);
$this
->actingAs($user)
->get(route('profile.edit'))
->assertOk()
->assertInertia(fn (Assert $page) => $page
->component('settings/profile')
->where('auth.user.avatar', '/storage/users/demo/avatar.jpg'),
);
}
public function test_profile_information_can_be_updated()
{
$user = User::factory()->create();
@@ -61,9 +80,67 @@ class ProfileUpdateTest extends TestCase
$this->assertNotNull($user->refresh()->email_verified_at);
}
public function test_user_can_update_their_avatar(): void
{
Storage::fake('public');
$user = User::factory()->create([
'avatar_path' => 'users/demo/old-avatar.jpg',
]);
Storage::disk('public')->put($user->avatar_path, 'old avatar');
$this
->actingAs($user)
->post(route('profile.update'), [
'_method' => 'PATCH',
'name' => $user->name,
'email' => $user->email,
'avatar' => UploadedFile::fake()->image('avatar.png', 400, 400),
])
->assertRedirect(route('profile.edit'))
->assertSessionHasNoErrors();
$user->refresh();
$this->assertNotSame('users/demo/old-avatar.jpg', $user->avatar_path);
$this->assertStringStartsWith("users/{$user->id}/avatars/", $user->avatar_path);
Storage::disk('public')->assertMissing('users/demo/old-avatar.jpg');
Storage::disk('public')->assertExists($user->avatar_path);
}
public function test_user_avatar_must_be_an_image(): void
{
Storage::fake('public');
$user = User::factory()->create([
'avatar_path' => 'users/demo/current-avatar.jpg',
]);
$this
->actingAs($user)
->from(route('profile.edit'))
->post(route('profile.update'), [
'_method' => 'PATCH',
'name' => $user->name,
'email' => $user->email,
'avatar' => UploadedFile::fake()->create('avatar.txt', 8, 'text/plain'),
])
->assertRedirect(route('profile.edit'))
->assertSessionHasErrors('avatar');
$this->assertSame('users/demo/current-avatar.jpg', $user->refresh()->avatar_path);
}
public function test_user_can_delete_their_account()
{
$user = User::factory()->create();
Storage::fake('public');
$user = User::factory()->create([
'avatar_path' => 'users/demo/avatar.jpg',
]);
Storage::disk('public')->put($user->avatar_path, 'avatar');
$response = $this
->actingAs($user)
@@ -77,6 +154,7 @@ class ProfileUpdateTest extends TestCase
$this->assertGuest();
$this->assertNull($user->fresh());
Storage::disk('public')->assertMissing('users/demo/avatar.jpg');
}
public function test_correct_password_must_be_provided_to_delete_account()

View File

@@ -30,7 +30,9 @@ class ViewerInteractionTest extends TestCase
public function test_authenticated_user_can_chat_while_channel_is_live(): void
{
$viewer = User::factory()->create();
$viewer = User::factory()->create([
'avatar_path' => 'users/demo/avatar.jpg',
]);
$channel = Channel::factory()->create(['slug' => 'demo', 'is_live' => true]);
$broadcast = Broadcast::factory()->for($channel)->live()->create();
$channel->forceFill(['live_broadcast_id' => $broadcast->id])->save();
@@ -47,6 +49,14 @@ class ViewerInteractionTest extends TestCase
'user_id' => $viewer->id,
'body' => 'Hello chat',
]);
$this->actingAs($viewer)
->get(route('channels.show', $channel))
->assertOk()
->assertInertia(fn (Assert $page) => $page
->component('channels/show')
->where('chatMessages.0.user.avatar_url', '/storage/users/demo/avatar.jpg'),
);
}
public function test_anonymous_users_can_watch_channel_page_but_not_chat(): void