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