Add profile avatar uploads
This commit is contained in:
@@ -4,6 +4,8 @@ namespace Tests\Feature\Settings;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ProfileUpdateTest extends TestCase
|
||||
@@ -23,6 +25,8 @@ class ProfileUpdateTest extends TestCase
|
||||
|
||||
public function test_profile_information_can_be_updated()
|
||||
{
|
||||
Storage::fake('public');
|
||||
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this
|
||||
@@ -41,10 +45,15 @@ class ProfileUpdateTest extends TestCase
|
||||
$this->assertSame('Test User', $user->name);
|
||||
$this->assertSame('test@example.com', $user->email);
|
||||
$this->assertNull($user->email_verified_at);
|
||||
$this->assertTrue($user->avatar_is_generated);
|
||||
$this->assertNotNull($user->avatar_path);
|
||||
Storage::disk('public')->assertExists($user->avatar_path);
|
||||
}
|
||||
|
||||
public function test_email_verification_status_is_unchanged_when_the_email_address_is_unchanged()
|
||||
{
|
||||
Storage::fake('public');
|
||||
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this
|
||||
@@ -61,9 +70,134 @@ class ProfileUpdateTest extends TestCase
|
||||
$this->assertNotNull($user->refresh()->email_verified_at);
|
||||
}
|
||||
|
||||
public function test_user_can_upload_a_profile_picture()
|
||||
{
|
||||
Storage::fake('public');
|
||||
|
||||
$user = User::factory()->create([
|
||||
'avatar_path' => 'avatars/generated/old.svg',
|
||||
'avatar_color' => '#F97316',
|
||||
'avatar_is_generated' => true,
|
||||
]);
|
||||
Storage::disk('public')->put($user->avatar_path, '<svg>OU</svg>');
|
||||
|
||||
$response = $this
|
||||
->actingAs($user)
|
||||
->post(route('profile.update'), [
|
||||
'_method' => 'PATCH',
|
||||
'name' => $user->name,
|
||||
'email' => $user->email,
|
||||
'avatar' => UploadedFile::fake()->image('avatar.png'),
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertSessionHasNoErrors()
|
||||
->assertRedirect(route('profile.edit'));
|
||||
|
||||
$user->refresh();
|
||||
|
||||
$this->assertFalse($user->avatar_is_generated);
|
||||
$this->assertSame('#F97316', $user->avatar_color);
|
||||
$this->assertNotSame('avatars/generated/old.svg', $user->avatar_path);
|
||||
$this->assertStringStartsWith('avatars/uploads/', $user->avatar_path);
|
||||
Storage::disk('public')->assertMissing('avatars/generated/old.svg');
|
||||
Storage::disk('public')->assertExists($user->avatar_path);
|
||||
}
|
||||
|
||||
public function test_name_change_regenerates_the_default_profile_picture()
|
||||
{
|
||||
Storage::fake('public');
|
||||
|
||||
$user = User::factory()->create([
|
||||
'name' => 'Old User',
|
||||
'email' => 'old-user@example.com',
|
||||
'avatar_path' => 'avatars/generated/old.svg',
|
||||
'avatar_color' => '#22C55E',
|
||||
'avatar_is_generated' => true,
|
||||
]);
|
||||
Storage::disk('public')->put($user->avatar_path, '<svg>OU</svg>');
|
||||
|
||||
$response = $this
|
||||
->actingAs($user)
|
||||
->patch(route('profile.update'), [
|
||||
'name' => 'Fresh Person',
|
||||
'email' => 'old-user@example.com',
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertSessionHasNoErrors()
|
||||
->assertRedirect(route('profile.edit'));
|
||||
|
||||
$user->refresh();
|
||||
|
||||
$this->assertTrue($user->avatar_is_generated);
|
||||
$this->assertSame('#22C55E', $user->avatar_color);
|
||||
$this->assertNotSame('avatars/generated/old.svg', $user->avatar_path);
|
||||
Storage::disk('public')->assertMissing('avatars/generated/old.svg');
|
||||
Storage::disk('public')->assertExists($user->avatar_path);
|
||||
$this->assertStringContainsString('FP', Storage::disk('public')->get($user->avatar_path));
|
||||
}
|
||||
|
||||
public function test_name_change_keeps_an_uploaded_profile_picture()
|
||||
{
|
||||
Storage::fake('public');
|
||||
|
||||
$user = User::factory()->create([
|
||||
'name' => 'Old User',
|
||||
'email' => 'old-user@example.com',
|
||||
'avatar_path' => 'avatars/uploads/current.png',
|
||||
'avatar_color' => '#06B6D4',
|
||||
'avatar_is_generated' => false,
|
||||
]);
|
||||
Storage::disk('public')->put($user->avatar_path, 'uploaded image');
|
||||
|
||||
$response = $this
|
||||
->actingAs($user)
|
||||
->patch(route('profile.update'), [
|
||||
'name' => 'Fresh Person',
|
||||
'email' => 'old-user@example.com',
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertSessionHasNoErrors()
|
||||
->assertRedirect(route('profile.edit'));
|
||||
|
||||
$user->refresh();
|
||||
|
||||
$this->assertFalse($user->avatar_is_generated);
|
||||
$this->assertSame('avatars/uploads/current.png', $user->avatar_path);
|
||||
Storage::disk('public')->assertExists('avatars/uploads/current.png');
|
||||
}
|
||||
|
||||
public function test_profile_picture_must_be_an_image()
|
||||
{
|
||||
Storage::fake('public');
|
||||
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this
|
||||
->actingAs($user)
|
||||
->post(route('profile.update'), [
|
||||
'_method' => 'PATCH',
|
||||
'name' => $user->name,
|
||||
'email' => $user->email,
|
||||
'avatar' => UploadedFile::fake()->create('avatar.pdf', 10, 'application/pdf'),
|
||||
]);
|
||||
|
||||
$response->assertSessionHasErrors('avatar');
|
||||
}
|
||||
|
||||
public function test_user_can_delete_their_account()
|
||||
{
|
||||
Storage::fake('public');
|
||||
|
||||
$user = User::factory()->create();
|
||||
$user->forceFill([
|
||||
'avatar_path' => 'avatars/generated/delete-me.svg',
|
||||
'avatar_color' => '#EC4899',
|
||||
'avatar_is_generated' => true,
|
||||
])->save();
|
||||
Storage::disk('public')->put($user->avatar_path, '<svg>DU</svg>');
|
||||
|
||||
$response = $this
|
||||
->actingAs($user)
|
||||
@@ -77,6 +211,7 @@ class ProfileUpdateTest extends TestCase
|
||||
|
||||
$this->assertGuest();
|
||||
$this->assertNull($user->fresh());
|
||||
Storage::disk('public')->assertMissing('avatars/generated/delete-me.svg');
|
||||
}
|
||||
|
||||
public function test_correct_password_must_be_provided_to_delete_account()
|
||||
|
||||
Reference in New Issue
Block a user