EmailVerificationTest.php 1.7 KB

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