use mailable
This commit is contained in:
parent
ca684d14d8
commit
209d74ab7a
|
|
@ -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) {
|
||||
|
|
|
|||
44
app/Mail/ForgotPassword.php
Normal file
44
app/Mail/ForgotPassword.php
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
namespace App\Mail;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Mail\Mailable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class ForgotPassword extends Mailable
|
||||
{
|
||||
use Queueable, SerializesModels;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $reset_url = '';
|
||||
|
||||
/**
|
||||
* Create a new message instance.
|
||||
*
|
||||
* @param int $uid
|
||||
* @param string $token
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(int $uid, string $token)
|
||||
{
|
||||
$this->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');
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user