EmailVerificationTest.php 1.8 KB

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