diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.php index 234efc935..73e39ba36 100644 --- a/app/Http/Controllers/ProfileController.php +++ b/app/Http/Controllers/ProfileController.php @@ -45,13 +45,17 @@ public function update(ProfileUpdateRequest $request): RedirectResponse $user->fill($request->validated()); - if ($user->isDirty('email')) { - $user->email_verified_at = null; - } + $emailChanged = $user->isDirty('email'); $user->save(); - if (! $user->is_uploaded_avatar) { + if ($emailChanged) { + $user->email_verified_at = null; + $user->save(); + $user->sendEmailVerificationNotification(); + } + + if (! $user->is_uploaded_avatar && $emailChanged) { UpdateUserAvatar::dispatch($user); } diff --git a/app/Jobs/UpdateUserAvatar.php b/app/Jobs/UpdateUserAvatar.php index f7e3d694c..8200af8c5 100644 --- a/app/Jobs/UpdateUserAvatar.php +++ b/app/Jobs/UpdateUserAvatar.php @@ -49,7 +49,7 @@ public function handle(): void Storage::disk('public')->put($avatar, $contents, 'public'); $this->resizer()->read($disk->path($avatar)) - ->resize(200, 200) + ->coverDown(200, 200) ->save(); $this->user->update([ diff --git a/app/Livewire/Links/Create.php b/app/Livewire/Links/Create.php index 38e8b5d4d..8417de506 100644 --- a/app/Livewire/Links/Create.php +++ b/app/Livewire/Links/Create.php @@ -4,7 +4,6 @@ namespace App\Livewire\Links; -use App\Jobs\UpdateUserAvatar; use App\Models\User; use Illuminate\Http\Request; use Illuminate\Support\Str; @@ -53,10 +52,6 @@ public function store(Request $request): void $user->links()->create($validated); - if (! $user->is_uploaded_avatar) { - dispatch(new UpdateUserAvatar($user)); - } - $this->description = ''; $this->url = ''; diff --git a/tests/Http/Profile/EditTest.php b/tests/Http/Profile/EditTest.php index 7078122f2..9c068f45b 100644 --- a/tests/Http/Profile/EditTest.php +++ b/tests/Http/Profile/EditTest.php @@ -2,7 +2,9 @@ declare(strict_types=1); +use App\Jobs\UpdateUserAvatar; use App\Models\User; +use Illuminate\Auth\Notifications\VerifyEmail; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Storage; @@ -123,6 +125,40 @@ $this->assertNotNull($user->refresh()->email_verified_at); }); +test('email verification job sent & status reset when the email address is changed', function () { + Notification::fake(); + $user = User::factory()->create(); + + $this->actingAs($user) + ->patch('/profile', [ + 'name' => $user->name, + 'username' => 'valid_username', + 'email' => 'new@email.address', + 'prefers_anonymous_questions' => false, + ]) + ->assertSessionHasNoErrors(); + + expect($user->email_verified_at)->toBeNull(); + + Notification::assertSentTo($user, VerifyEmail::class); +}); + +test('only updates avatar if email changes & avatar not been uploaded', function () { + Queue::fake(); + $user = User::factory()->create(); + + $this->actingAs($user) + ->patch('/profile', [ + 'name' => $user->name, + 'username' => 'valid_username', + 'email' => $user->email, + 'prefers_anonymous_questions' => false, + ]) + ->assertSessionHasNoErrors(); + + Queue::assertNotPushed(UpdateUserAvatar::class); +}); + test('password can be updated', function () { $user = User::factory()->create();