From 209d74ab7a58864942ad996dad0458605334c713 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Sun, 15 Jul 2018 17:42:03 +0800 Subject: [PATCH] use mailable --- app/Http/Controllers/AuthController.php | 11 ++----- app/Mail/ForgotPassword.php | 44 +++++++++++++++++++++++++ tests/AuthControllerTest.php | 34 +++---------------- 3 files changed, 51 insertions(+), 38 deletions(-) create mode 100644 app/Mail/ForgotPassword.php diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index d2a6b063..64226415 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -11,6 +11,7 @@ use Option; use Session; use App\Events; use App\Models\User; +use App\Mail\ForgotPassword; use Illuminate\Http\Request; use App\Exceptions\PrettyPageException; use App\Services\Repositories\UserRepository; @@ -180,19 +181,11 @@ class AuthController extends Controller if (! $user) return json(trans('auth.forgot.unregistered'), 1); - $uid = $user->uid; // Generate token for password resetting $token = base64_encode($user->getToken().substr(time(), 4, 6).str_random(16)); - $url = Option::get('site_url')."/auth/reset?uid=$uid&token=$token"; - try { - Mail::send('auth.mail', ['reset_url' => $url], function ($m) use ($request) { - $site_name = Option::get('site_name'); - - $m->from(config('mail.username'), $site_name); - $m->to($request->input('email'))->subject(trans('auth.mail.title', ['sitename' => $site_name])); - }); + Mail::to($request->input('email'))->send(new ForgotPassword($user->uid, $token)); Log::info("[Password Reset] Mail has been sent to [{$request->input('email')}] with token [$token]"); } catch(\Exception $e) { diff --git a/app/Mail/ForgotPassword.php b/app/Mail/ForgotPassword.php new file mode 100644 index 00000000..86a3939f --- /dev/null +++ b/app/Mail/ForgotPassword.php @@ -0,0 +1,44 @@ +reset_url = option('site_url')."/auth/reset?uid=$uid&token=$token"; + } + + /** + * Build the message. + * + * @return $this + */ + public function build() + { + $site_name = option('site_name'); + + return $this->from(config('mail.username'), $site_name) + ->subject(trans('auth.mail.title', ['sitename' => $site_name])) + ->view('auth.mail'); + } +} diff --git a/tests/AuthControllerTest.php b/tests/AuthControllerTest.php index dc40ff12..1f22edfa 100644 --- a/tests/AuthControllerTest.php +++ b/tests/AuthControllerTest.php @@ -2,6 +2,7 @@ use App\Events; use App\Models\User; +use App\Mail\ForgotPassword; use App\Services\Facades\Option; use Illuminate\Support\Facades\Mail; use Illuminate\Foundation\Testing\WithoutMiddleware; @@ -445,34 +446,6 @@ class AuthControllerTest extends TestCase $user->getToken().substr(time(), 4, 6).str_random(16) ); $url = Option::get('site_url')."/auth/reset?uid=$uid&token=$token"; - // @see https://stackoverflow.com/questions/31120567/unittesting-laravel-5-mail-using-mock - Mail::shouldReceive('send') - ->once() - ->with( - 'auth.mail', - \Mockery::on(function ($actual) use ($url) { - $this->assertEquals(0, stristr($url, $actual['reset_url'])); - return true; - }), - \Mockery::on(function (\Closure $closure) use ($user) { - $mock = \Mockery::mock(Illuminate\Mail\Message::class); - - $mock->shouldReceive('from') - ->once() - ->with(option('mail.username'), option_localized('site_name')); - - $mock->shouldReceive('to') - ->once() - ->with($user->email) - ->andReturnSelf(); - - $mock->shouldReceive('subject') - ->once() - ->with(trans('auth.mail.title', ['sitename' => option_localized('site_name')])); - $closure($mock); - return true; - }) - ); $this->postJson('/auth/forgot', [ 'email' => $user->email, 'captcha' => 'a' @@ -480,9 +453,12 @@ class AuthControllerTest extends TestCase 'errno' => 0, 'msg' => trans('auth.mail.success') ])->assertSessionHas('last_mail_time'); + Mail::assertSent(ForgotPassword::class, function ($mail) use ($url, $user) { + return stristr($url, $mail->reset_url) == 0 && $mail->hasTo($user->email); + }); // Should handle exception when sending email - Mail::shouldReceive('send') + Mail::shouldReceive('to') ->once() ->andThrow(new \Mockery\Exception('A fake exception.')); $this->flushSession();