如何在单元测试Laravel中获取电子邮件验证链接

guykilcj  于 2023-11-20  发布在  其他
关注(0)|答案(4)|浏览(103)

我正在开发一个Laravel应用程序。我正在对我的应用程序进行单元测试。现在我在测试验证过程中遇到了麻烦。
我现在要做的是注册一个用户,然后测试验证邮件是否发送,然后我将获得该注册的验证链接,然后我将对该链接做一些事情。
1.第一个问题是电子邮件没有发送。
1.第二个问题是,我不知道如何检索验证电子邮件链接?
这是我的考验

public function test_user_can_be_verified_and_redirected_based_on_role()
{
    Notification::fake();
    $user = $this->registerUser();
    Notification::assertSentTo($user, SendEmailVerificationNotification::class);

}

protected function registerUser()
{
    $user = factory(User::class)->make();

    $this->post(route('register'), [
        'name' => $user->name,
        'email' => $user->email,
        'password' => 'testing',
        'password_confirmation' => 'testing',
    ])->assertRedirect();

    return User::whereEmail($user->email)->first();
}

字符串
但问题是,即使我从浏览器注册时发送通知,也不会发送通知。我也喜欢检索验证链接并做一些事情。如何才能做到这一点?

8hhllhi2

8hhllhi21#

不是一个完美的解决方案,但它确实有用,如果有人以某种方式破坏了这个功能,我会知道的。
首先覆盖VerifyEmail通知的受保护方法verificationUri并将其公开

class EmailVerificationNotification extends VerifyEmail
{
    public function verificationUrl($notifiable) {
        return parent::verificationUrl($notifiable);
    }
}

字符串
然后使用它来生成一个链接并对其进行Assert。

/** @test */
    public function an_user_can_verify_his_email_address()
    {
        $notification = new EmailVerificationNotification();

        $user = factory(User::class)->create(['email_verified_at' => null]);

        $uri = $notification->verificationUrl($user);

        $this->assertSame(null, $user->email_verified_at);

        $this->actingAs($user)->get($uri);

        $this->assertNotNull($user->email_verified_at);
    }

zy1mlcev

zy1mlcev2#

只需运行以下命令,即可在VerifyEmail通知之外轻松生成验证URL:

$verificationUrl = URL::temporarySignedRoute(
        'verification.verify',
        now()->addMinutes(60),
        ['id' => $user->id, 'hash' => sha1($user->email)]
    );

字符串
无需将verificationUrl()方法设为public。

jyztefdp

jyztefdp3#

以下是我的解决方案:

public function testVerifyEmailValidatesUser(): void
{
    // VerifyEmail extends Illuminate\Auth\Notifications\VerifyEmail in this example
    $notification = new VerifyEmail();
    $user = factory(User::class)->create();

    // New user should not has verified their email yet
    $this->assertFalse($user->hasVerifiedEmail());

    $mail = $notification->toMail($user);
    $uri = $mail->actionUrl;

    // Simulate clicking on the validation link
    $this->actingAs($user)
        ->get($uri);

    // User should have verified their email
    $this->assertTrue(User::find($user->id)->hasVerifiedEmail());
}

字符串

e0bqpujr

e0bqpujr4#

@florent的回答很好,但是对于Laravel 5.7.29,我发现使用工厂创建的新用户自动设置了email_verified_at标志,因此第一个测试-用户尚未验证-失败。
通过添加数组将email_verified_at设置为null,可以观察到正确的操作:

public function testVerifyEmailValidatesUser(): void
{
    // VerifyEmail extends Illuminate\Auth\Notifications\VerifyEmail in this example
    $notification = new VerifyEmail();
    $user = factory(User::class)->create([ 'email_verified_at' => null ]);

    // New user should not has verified their email yet
    $this->assertFalse($user->hasVerifiedEmail());

    $mail = $notification->toMail($user);
    $uri = $mail->actionUrl;

    // Simulate clicking on the validation link
    $this->actingAs($user)
        ->get($uri);

    // User should have verified their email
    $this->assertTrue(User::find($user->id)->hasVerifiedEmail());
}

字符串

相关问题