diff --git a/app/Actions/UpdateUser.php b/app/Actions/UpdateUser.php index 5e96d42..38bc839 100644 --- a/app/Actions/UpdateUser.php +++ b/app/Actions/UpdateUser.php @@ -13,11 +13,12 @@ */ public function handle(User $user, array $attributes): void { - $email = $attributes['email'] ?? null; + $emailChanged = array_key_exists('email', $attributes) && $user->email !== $attributes['email']; - $user->update([ - ...$attributes, - ...$user->email === $email ? [] : ['email_verified_at' => null], - ]); + $user->update([...$attributes, ...($emailChanged ? ['email_verified_at' => null] : [])]); + + if (! $user->hasVerifiedEmail()) { + $user->sendEmailVerificationNotification(); + } } } diff --git a/app/Rules/ValidEmail.php b/app/Rules/ValidEmail.php index 34aab35..99aa6ea 100644 --- a/app/Rules/ValidEmail.php +++ b/app/Rules/ValidEmail.php @@ -22,7 +22,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void assert(is_string($value)); if (in_array(preg_match(self::REGEX, $value), [0, false], true)) { - $fail('The :attribute must be a valid email address.'); + $fail(__('The :attribute must be a valid email address.')); } } } diff --git a/tests/Unit/Actions/UpdateUserTest.php b/tests/Unit/Actions/UpdateUserTest.php index 671ba5c..0e854a0 100644 --- a/tests/Unit/Actions/UpdateUserTest.php +++ b/tests/Unit/Actions/UpdateUserTest.php @@ -4,8 +4,11 @@ use App\Actions\UpdateUser; use App\Models\User; +use Illuminate\Auth\Notifications\VerifyEmail; +use Illuminate\Support\Facades\Notification; it('may update a user', function (): void { + Notification::fake(); $user = User::factory()->create([ 'name' => 'Old Name', 'email' => 'old@email.com', @@ -19,6 +22,7 @@ expect($user->refresh()->name)->toBe('New Name') ->and($user->email)->toBe('old@email.com'); + Notification::assertSentTo($user, VerifyEmail::class); }); it('resets email verification when email changes', function (): void {