AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

EmailVerificationTest.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 Laravel\Fortify\Features;
  9. use Tests\TestCase;
  10. class EmailVerificationTest extends TestCase
  11. {
  12. use RefreshDatabase;
  13. protected function setUp(): void
  14. {
  15. parent::setUp();
  16. $this->skipUnlessFortifyHas(Features::emailVerification());
  17. }
  18. public function test_email_verification_screen_can_be_rendered()
  19. {
  20. $user = User::factory()->unverified()->create();
  21. $response = $this->actingAs($user)->get(route('verification.notice'));
  22. $response->assertOk();
  23. }
  24. public function test_unverified_users_are_redirected_to_the_email_verification_prompt(): void
  25. {
  26. $user = User::factory()->unverified()->create();
  27. $response = $this->actingAs($user)->get(route('appearance.edit'));
  28. $response->assertRedirect(route('verification.notice'));
  29. }
  30. public function test_email_can_be_verified()
  31. {
  32. $user = User::factory()->unverified()->create();
  33. Event::fake();
  34. $verificationUrl = URL::temporarySignedRoute(
  35. 'verification.verify',
  36. now()->addMinutes(60),
  37. ['id' => $user->id, 'hash' => sha1($user->email)],
  38. );
  39. $response = $this->actingAs($user)->get($verificationUrl);
  40. Event::assertDispatched(Verified::class);
  41. $this->assertTrue($user->fresh()->hasVerifiedEmail());
  42. $response->assertRedirect(route('dashboard', absolute: false).'?verified=1');
  43. }
  44. public function test_email_is_not_verified_with_invalid_hash()
  45. {
  46. $user = User::factory()->unverified()->create();
  47. Event::fake();
  48. $verificationUrl = URL::temporarySignedRoute(
  49. 'verification.verify',
  50. now()->addMinutes(60),
  51. ['id' => $user->id, 'hash' => sha1('wrong-email')],
  52. );
  53. $this->actingAs($user)->get($verificationUrl);
  54. Event::assertNotDispatched(Verified::class);
  55. $this->assertFalse($user->fresh()->hasVerifiedEmail());
  56. }
  57. public function test_email_is_not_verified_with_invalid_user_id(): void
  58. {
  59. $user = User::factory()->unverified()->create();
  60. Event::fake();
  61. $verificationUrl = URL::temporarySignedRoute(
  62. 'verification.verify',
  63. now()->addMinutes(60),
  64. ['id' => 123, 'hash' => sha1($user->email)],
  65. );
  66. $this->actingAs($user)->get($verificationUrl);
  67. Event::assertNotDispatched(Verified::class);
  68. $this->assertFalse($user->fresh()->hasVerifiedEmail());
  69. }
  70. public function test_verified_user_is_redirected_to_dashboard_from_verification_prompt(): void
  71. {
  72. $user = User::factory()->create();
  73. Event::fake();
  74. $response = $this->actingAs($user)->get(route('verification.notice'));
  75. Event::assertNotDispatched(Verified::class);
  76. $response->assertRedirect(route('dashboard', absolute: false));
  77. }
  78. public function test_already_verified_user_visiting_verification_link_is_redirected_without_firing_event_again(): void
  79. {
  80. $user = User::factory()->create();
  81. Event::fake();
  82. $verificationUrl = URL::temporarySignedRoute(
  83. 'verification.verify',
  84. now()->addMinutes(60),
  85. ['id' => $user->id, 'hash' => sha1($user->email)],
  86. );
  87. $this->actingAs($user)->get($verificationUrl)
  88. ->assertRedirect(route('dashboard', absolute: false).'?verified=1');
  89. Event::assertNotDispatched(Verified::class);
  90. $this->assertTrue($user->fresh()->hasVerifiedEmail());
  91. }
  92. }