2024-07-18 00:11:32 -04:00
|
|
|
<?php
|
|
|
|
|
2025-01-30 00:23:55 -05:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2024-07-18 00:11:32 -04:00
|
|
|
use App\Models\User;
|
|
|
|
use Illuminate\Auth\Events\Verified;
|
|
|
|
use Illuminate\Support\Facades\Event;
|
|
|
|
use Illuminate\Support\Facades\URL;
|
|
|
|
use Laravel\Fortify\Features;
|
|
|
|
|
2025-01-30 00:23:55 -05:00
|
|
|
test('email verification screen can be rendered', function (): void {
|
2024-07-18 00:11:32 -04:00
|
|
|
$user = User::factory()->create([
|
|
|
|
'email_verified_at' => null,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response = $this->actingAs($user)->get('/email/verify');
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
2025-01-30 00:23:55 -05:00
|
|
|
})->skip(fn (): bool => ! Features::enabled(Features::emailVerification()), 'Email verification not enabled.');
|
2024-07-18 00:11:32 -04:00
|
|
|
|
2025-01-30 00:23:55 -05:00
|
|
|
test('email can be verified', function (): void {
|
2024-07-18 00:11:32 -04:00
|
|
|
Event::fake();
|
|
|
|
|
|
|
|
$user = User::factory()->create([
|
|
|
|
'email_verified_at' => null,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$verificationUrl = URL::temporarySignedRoute(
|
|
|
|
'verification.verify',
|
|
|
|
now()->addMinutes(60),
|
2025-01-30 00:23:55 -05:00
|
|
|
['id' => $user->id, 'hash' => sha1((string) $user->email)]
|
2024-07-18 00:11:32 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
$response = $this->actingAs($user)->get($verificationUrl);
|
|
|
|
|
|
|
|
Event::assertDispatched(Verified::class);
|
|
|
|
|
|
|
|
expect($user->fresh()->hasVerifiedEmail())->toBeTrue();
|
|
|
|
$response->assertRedirect(route('dashboard', absolute: false).'?verified=1');
|
2025-01-30 00:23:55 -05:00
|
|
|
})->skip(fn (): bool => ! Features::enabled(Features::emailVerification()), 'Email verification not enabled.');
|
2024-07-18 00:11:32 -04:00
|
|
|
|
2025-01-30 00:23:55 -05:00
|
|
|
test('email can not verified with invalid hash', function (): void {
|
2024-07-18 00:11:32 -04:00
|
|
|
$user = User::factory()->create([
|
|
|
|
'email_verified_at' => null,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$verificationUrl = URL::temporarySignedRoute(
|
|
|
|
'verification.verify',
|
|
|
|
now()->addMinutes(60),
|
|
|
|
['id' => $user->id, 'hash' => sha1('wrong-email')]
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->actingAs($user)->get($verificationUrl);
|
|
|
|
|
|
|
|
expect($user->fresh()->hasVerifiedEmail())->toBeFalse();
|
2025-01-30 00:23:55 -05:00
|
|
|
})->skip(fn (): bool => ! Features::enabled(Features::emailVerification()), 'Email verification not enabled.');
|