|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Tests\Integration\Auth; |
| 4 | + |
| 5 | +use Illuminate\Auth\Notifications\ResetPassword; |
| 6 | +use Illuminate\Notifications\Messages\MailMessage; |
| 7 | +use Illuminate\Support\Facades\Notification; |
| 8 | +use Illuminate\Support\Facades\Password; |
| 9 | +use Illuminate\Tests\Integration\Auth\Fixtures\AuthenticationTestUser; |
| 10 | +use Orchestra\Testbench\Factories\UserFactory; |
| 11 | +use Orchestra\Testbench\TestCase; |
| 12 | + |
| 13 | +class ForgotPasswordWithoutDefaultRoutesTest extends TestCase |
| 14 | +{ |
| 15 | + protected function tearDown(): void |
| 16 | + { |
| 17 | + ResetPassword::$createUrlCallback = null; |
| 18 | + ResetPassword::$toMailCallback = null; |
| 19 | + |
| 20 | + parent::tearDown(); |
| 21 | + } |
| 22 | + |
| 23 | + protected function defineEnvironment($app) |
| 24 | + { |
| 25 | + $app['config']->set('auth.providers.users.model', AuthenticationTestUser::class); |
| 26 | + } |
| 27 | + |
| 28 | + protected function defineDatabaseMigrations() |
| 29 | + { |
| 30 | + $this->loadLaravelMigrations(); |
| 31 | + } |
| 32 | + |
| 33 | + protected function defineRoutes($router) |
| 34 | + { |
| 35 | + $router->get('custom/password/reset/{token}', function ($token) { |
| 36 | + return 'Custom reset password!'; |
| 37 | + })->name('custom.password.reset'); |
| 38 | + } |
| 39 | + |
| 40 | + /** @test */ |
| 41 | + public function it_cannot_send_forgot_password_email() |
| 42 | + { |
| 43 | + $this->expectException('Symfony\Component\Routing\Exception\RouteNotFoundException'); |
| 44 | + $this->expectExceptionMessage('Route [password.reset] not defined.'); |
| 45 | + |
| 46 | + Notification::fake(); |
| 47 | + |
| 48 | + UserFactory::new()->create(); |
| 49 | + |
| 50 | + $user = AuthenticationTestUser::first(); |
| 51 | + |
| 52 | + Password::broker()->sendResetLink([ |
| 53 | + 'email' => $user->email, |
| 54 | + ]); |
| 55 | + |
| 56 | + Notification::assertSentTo( |
| 57 | + $user, |
| 58 | + function (ResetPassword $notification, $channels) use ($user) { |
| 59 | + $message = $notification->toMail($user); |
| 60 | + |
| 61 | + return ! is_null($notification->token) |
| 62 | + && $message->actionUrl === route('custom.password.reset', ['token' => $notification->token, 'email' => $user->email]); |
| 63 | + } |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + /** @test */ |
| 68 | + public function it_can_send_forgot_password_email_via_create_url_using() |
| 69 | + { |
| 70 | + Notification::fake(); |
| 71 | + |
| 72 | + ResetPassword::createUrlUsing(function ($user, string $token) { |
| 73 | + return route('custom.password.reset', $token); |
| 74 | + }); |
| 75 | + |
| 76 | + UserFactory::new()->create(); |
| 77 | + |
| 78 | + $user = AuthenticationTestUser::first(); |
| 79 | + |
| 80 | + Password::broker()->sendResetLink([ |
| 81 | + 'email' => $user->email, |
| 82 | + ]); |
| 83 | + |
| 84 | + Notification::assertSentTo( |
| 85 | + $user, |
| 86 | + function (ResetPassword $notification, $channels) use ($user) { |
| 87 | + $message = $notification->toMail($user); |
| 88 | + |
| 89 | + return ! is_null($notification->token) |
| 90 | + && $message->actionUrl === route('custom.password.reset', ['token' => $notification->token]); |
| 91 | + } |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + /** @test */ |
| 96 | + public function it_can_send_forgot_password_email_via_to_mail_using() |
| 97 | + { |
| 98 | + Notification::fake(); |
| 99 | + |
| 100 | + ResetPassword::toMailUsing(function ($notifiable, $token) { |
| 101 | + return (new MailMessage) |
| 102 | + ->subject(__('Reset Password Notification')) |
| 103 | + ->line(__('You are receiving this email because we received a password reset request for your account.')) |
| 104 | + ->action(__('Reset Password'), route('custom.password.reset', $token)) |
| 105 | + ->line(__('If you did not request a password reset, no further action is required.')); |
| 106 | + }); |
| 107 | + |
| 108 | + UserFactory::new()->create(); |
| 109 | + |
| 110 | + $user = AuthenticationTestUser::first(); |
| 111 | + |
| 112 | + Password::broker()->sendResetLink([ |
| 113 | + 'email' => $user->email, |
| 114 | + ]); |
| 115 | + |
| 116 | + Notification::assertSentTo( |
| 117 | + $user, |
| 118 | + function (ResetPassword $notification, $channels) use ($user) { |
| 119 | + $message = $notification->toMail($user); |
| 120 | + |
| 121 | + return ! is_null($notification->token) |
| 122 | + && $message->actionUrl === route('custom.password.reset', ['token' => $notification->token]); |
| 123 | + } |
| 124 | + ); |
| 125 | + } |
| 126 | +} |
0 commit comments