Files
AFC-Demo/tests/Feature/Auth/EmailVerificationTest.php
T

68 lines
1.9 KiB
PHP
Raw Normal View History

2025-10-16 14:22:46 +02:00
<?php
use App\Models\User;
use Illuminate\Auth\Events\Verified;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\URL;
2025-10-16 14:22:58 +02:00
test('email verification screen can be rendered', function () {
$user = User::factory()->unverified()->create();
2025-10-16 14:22:46 +02:00
2025-10-16 14:22:58 +02:00
$response = $this->actingAs($user)->get(route('verification.notice'));
2025-10-16 14:22:46 +02:00
2025-10-16 14:22:58 +02:00
$response->assertStatus(200);
});
2025-10-16 14:22:46 +02:00
2025-10-16 14:22:58 +02:00
test('email can be verified', function () {
$user = User::factory()->unverified()->create();
2025-10-16 14:22:46 +02:00
2025-10-16 14:22:58 +02:00
Event::fake();
2025-10-16 14:22:46 +02:00
2025-10-16 14:22:58 +02:00
$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['id' => $user->id, 'hash' => sha1($user->email)]
);
2025-10-16 14:22:46 +02:00
2025-10-16 14:22:58 +02:00
$response = $this->actingAs($user)->get($verificationUrl);
2025-10-16 14:22:46 +02:00
2025-10-16 14:22:58 +02:00
Event::assertDispatched(Verified::class);
2025-10-16 14:22:46 +02:00
2025-10-16 14:22:58 +02:00
expect($user->fresh()->hasVerifiedEmail())->toBeTrue();
$response->assertRedirect(route('dashboard', absolute: false).'?verified=1');
});
2025-10-16 14:22:46 +02:00
2025-10-16 14:22:58 +02:00
test('email is not verified with invalid hash', function () {
$user = User::factory()->unverified()->create();
2025-10-16 14:22:46 +02:00
2025-10-16 14:22:58 +02:00
$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['id' => $user->id, 'hash' => sha1('wrong-email')]
);
2025-10-16 14:22:46 +02:00
2025-10-16 14:22:58 +02:00
$this->actingAs($user)->get($verificationUrl);
2025-10-16 14:22:46 +02:00
2025-10-16 14:22:58 +02:00
expect($user->fresh()->hasVerifiedEmail())->toBeFalse();
});
2025-10-16 14:22:46 +02:00
2025-10-16 14:22:58 +02:00
test('already verified user visiting verification link is redirected without firing event again', function () {
$user = User::factory()->create([
'email_verified_at' => now(),
]);
2025-10-16 14:22:46 +02:00
2025-10-16 14:22:58 +02:00
Event::fake();
2025-10-16 14:22:46 +02:00
2025-10-16 14:22:58 +02:00
$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['id' => $user->id, 'hash' => sha1($user->email)]
);
2025-10-16 14:22:46 +02:00
2025-10-16 14:22:58 +02:00
$this->actingAs($user)->get($verificationUrl)
->assertRedirect(route('dashboard', absolute: false).'?verified=1');
2025-10-16 14:22:46 +02:00
2025-10-16 14:22:58 +02:00
expect($user->fresh()->hasVerifiedEmail())->toBeTrue();
Event::assertNotDispatched(Verified::class);
2025-10-20 22:10:08 +02:00
});