Files

147 lines
4.3 KiB
PHP
Raw Permalink Normal View History

2026-02-06 14:33:11 +02:00
<?php
use App\Models\User;
use App\Models\Role;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
uses(RefreshDatabase::class);
beforeEach(function () {
Storage::fake('public');
});
test('users can view a profile', function () {
$user = User::factory()->create([
'name' => 'John Doe',
'bio' => 'Test bio',
]);
$response = $this->get(route('profile.show', $user));
$response->assertStatus(200);
$response->assertSee('John Doe');
$response->assertSee('Test bio');
});
test('authenticated users can view their edit profile page', function () {
$user = User::factory()->create();
$response = $this->actingAs($user)->get(route('profile.edit'));
$response->assertStatus(200);
$response->assertSee('Edit Profile');
});
test('guests cannot view edit profile page', function () {
$response = $this->get(route('profile.edit'));
$response->assertRedirect(route('login'));
});
test('users can update their profile', function () {
$user = User::factory()->create();
$response = $this->actingAs($user)->put(route('profile.update'), [
'name' => 'Updated Name',
'email' => $user->email,
'bio' => 'Updated bio',
'website' => 'https://example.com',
'twitter' => 'twitter_handle',
'github' => 'github_handle',
'linkedin' => 'linkedin_handle',
]);
$response->assertRedirect(route('profile.show', $user));
$response->assertSessionHas('success');
$user->refresh();
expect($user->name)->toBe('Updated Name');
expect($user->bio)->toBe('Updated bio');
expect($user->website)->toBe('https://example.com');
expect($user->twitter)->toBe('twitter_handle');
});
test('users can upload an avatar', function () {
$user = User::factory()->create();
$file = UploadedFile::fake()->image('avatar.jpg');
$response = $this->actingAs($user)->put(route('profile.update'), [
'name' => $user->name,
'email' => $user->email,
'avatar' => $file,
]);
$response->assertRedirect(route('profile.show', $user));
$user->refresh();
expect($user->avatar)->not->toBeNull();
Storage::disk('public')->assertExists('avatars/' . $user->avatar);
});
test('avatar must be an image', function () {
$user = User::factory()->create();
$file = UploadedFile::fake()->create('document.pdf');
$response = $this->actingAs($user)->put(route('profile.update'), [
'name' => $user->name,
'email' => $user->email,
'avatar' => $file,
]);
$response->assertSessionHasErrors('avatar');
});
test('avatar cannot exceed 2MB', function () {
$user = User::factory()->create();
$file = UploadedFile::fake()->image('avatar.jpg')->size(3000);
$response = $this->actingAs($user)->put(route('profile.update'), [
'name' => $user->name,
'email' => $user->email,
'avatar' => $file,
]);
$response->assertSessionHasErrors('avatar');
});
test('users can delete their avatar', function () {
Storage::fake('public');
$user = User::factory()->create(['avatar' => 'test_avatar.jpg']);
Storage::disk('public')->put('avatars/test_avatar.jpg', 'fake content');
$response = $this->actingAs($user)->delete(route('profile.avatar.delete'));
$response->assertRedirect(route('profile.edit'));
$response->assertSessionHas('success');
$user->refresh();
expect($user->avatar)->toBeNull();
Storage::disk('public')->assertMissing('avatars/test_avatar.jpg');
});
test('email must be unique when updating profile', function () {
$existingUser = User::factory()->create(['email' => 'existing@example.com']);
$user = User::factory()->create(['email' => 'user@example.com']);
$response = $this->actingAs($user)->put(route('profile.update'), [
'name' => $user->name,
'email' => 'existing@example.com',
]);
$response->assertSessionHasErrors('email');
});
test('website must be a valid url', function () {
$user = User::factory()->create();
$response = $this->actingAs($user)->put(route('profile.update'), [
'name' => $user->name,
'email' => $user->email,
'website' => 'not-a-valid-url',
]);
$response->assertSessionHasErrors('website');
});