266 lines
8.4 KiB
PHP
266 lines
8.4 KiB
PHP
<?php
|
|
|
|
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
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_profile_page_is_displayed()
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
$response = $this
|
|
->actingAs($user)
|
|
->get(route('profile.edit'));
|
|
|
|
$response->assertOk();
|
|
}
|
|
|
|
public function test_profile_information_can_be_updated()
|
|
{
|
|
Storage::fake('public');
|
|
|
|
$user = User::factory()->create();
|
|
|
|
$response = $this
|
|
->actingAs($user)
|
|
->patch(route('profile.update'), [
|
|
'name' => 'Test User',
|
|
'email' => 'test@example.com',
|
|
]);
|
|
|
|
$response
|
|
->assertSessionHasNoErrors()
|
|
->assertRedirect(route('profile.edit'));
|
|
|
|
$user->refresh();
|
|
|
|
$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
|
|
->actingAs($user)
|
|
->patch(route('profile.update'), [
|
|
'name' => 'Test User',
|
|
'email' => $user->email,
|
|
]);
|
|
|
|
$response
|
|
->assertSessionHasNoErrors()
|
|
->assertRedirect(route('profile.edit'));
|
|
|
|
$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_user_can_delete_their_uploaded_profile_picture()
|
|
{
|
|
Storage::fake('public');
|
|
|
|
$user = User::factory()->create([
|
|
'name' => 'Upload User',
|
|
'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)
|
|
->delete(route('profile.avatar.destroy'));
|
|
|
|
$response
|
|
->assertSessionHasNoErrors()
|
|
->assertRedirect(route('profile.edit'));
|
|
|
|
$user->refresh();
|
|
|
|
$this->assertTrue($user->avatar_is_generated);
|
|
$this->assertSame('#06B6D4', $user->avatar_color);
|
|
$this->assertNotSame('avatars/uploads/current.png', $user->avatar_path);
|
|
$this->assertStringStartsWith('avatars/generated/', $user->avatar_path);
|
|
Storage::disk('public')->assertMissing('avatars/uploads/current.png');
|
|
Storage::disk('public')->assertExists($user->avatar_path);
|
|
$this->assertStringContainsString('UU', Storage::disk('public')->get($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)
|
|
->delete(route('profile.destroy'), [
|
|
'password' => 'password',
|
|
]);
|
|
|
|
$response
|
|
->assertSessionHasNoErrors()
|
|
->assertRedirect(route('home'));
|
|
|
|
$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()
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
$response = $this
|
|
->actingAs($user)
|
|
->from(route('profile.edit'))
|
|
->delete(route('profile.destroy'), [
|
|
'password' => 'wrong-password',
|
|
]);
|
|
|
|
$response
|
|
->assertSessionHasErrors('password')
|
|
->assertRedirect(route('profile.edit'));
|
|
|
|
$this->assertNotNull($user->fresh());
|
|
}
|
|
}
|