EmailVerificationTest.php (1858B)
1 <?php 2 3 namespace Tests\Feature\Auth; 4 5 use App\Models\User; 6 use App\Providers\RouteServiceProvider; 7 use Illuminate\Auth\Events\Verified; 8 use Illuminate\Foundation\Testing\RefreshDatabase; 9 use Illuminate\Support\Facades\Event; 10 use Illuminate\Support\Facades\URL; 11 use Tests\TestCase; 12 13 class EmailVerificationTest extends TestCase 14 { 15 use RefreshDatabase; 16 17 public function test_email_verification_screen_can_be_rendered(): void 18 { 19 $user = User::factory()->create([ 20 'email_verified_at' => null, 21 ]); 22 23 $response = $this->actingAs($user)->get('/verify-email'); 24 25 $response 26 ->assertSeeVolt('pages.auth.verify-email') 27 ->assertStatus(200); 28 } 29 30 public function test_email_can_be_verified(): void 31 { 32 $user = User::factory()->create([ 33 'email_verified_at' => null, 34 ]); 35 36 Event::fake(); 37 38 $verificationUrl = URL::temporarySignedRoute( 39 'verification.verify', 40 now()->addMinutes(60), 41 ['id' => $user->id, 'hash' => sha1($user->email)] 42 ); 43 44 $response = $this->actingAs($user)->get($verificationUrl); 45 46 Event::assertDispatched(Verified::class); 47 $this->assertTrue($user->fresh()->hasVerifiedEmail()); 48 $response->assertRedirect(RouteServiceProvider::HOME.'?verified=1'); 49 } 50 51 public function test_email_is_not_verified_with_invalid_hash(): void 52 { 53 $user = User::factory()->create([ 54 'email_verified_at' => null, 55 ]); 56 57 $verificationUrl = URL::temporarySignedRoute( 58 'verification.verify', 59 now()->addMinutes(60), 60 ['id' => $user->id, 'hash' => sha1('wrong-email')] 61 ); 62 63 $this->actingAs($user)->get($verificationUrl); 64 65 $this->assertFalse($user->fresh()->hasVerifiedEmail()); 66 } 67 }