简体   繁体   中英

Laravel 5.1 Testing - getting row from database to complete test

I am building a testing routine in Laravel 5.1 to run through the login process I have just created. I am wanting, as part of the testing, to test the password reset and change process.

The issue is that the password reset process generates and row in a table with a timestamp and uuid. The link remains valid for 1 hour for the reset to work.

The flow is:

1- Reset password by entering email address

2- System generates email with uuid link and sends to the user (currently it appears in the Laravel log).

3- User clicks on link from email, and if within one hour since it was generated, the user is presented with a password change screen. The link is also deleted from the table.

So now for my test code:

public function testSendPasswordLink()
{
    $this->visit('/login')
        ->click('Forgot Your Password?')
        ->seePageIs('/forgot-password')
        ->type('test@test.com','email')
        ->press('Send Password Reset Link')
        ->seePageIs('/login')
        ->see('A password reset link was sent to the email address supplied.')
        ->seeInDatabase('password_resets', ['email' => 'test@test.com']);
}

I would like to:

       ->getFromDatabase('password_resets', 'uuid')
       ->visit('/reset-password/'.$uuid)
       ->see(....

Is there a way of doing the above? I know how to see in the table but not how to get from the table in the test.

Alternatively is there a way to accomplish this via a different set of steps?

Thanks!

Wow, insanity setting in... well, I am new at this so I will not execute myself as yet...! The answer was rather obvious (after sleeping on it!)

All I had to do is query the table and retrieve the token to perform the password reset test... below is the finished code:

  1. Add Eloquent to the test php file

    use Illuminate\\Database\\Eloquent\\Model;

  2. Below is the complete function:

    public function testChangePassword()

     $this->visit('/login') ->click('Forgot Your Password?') ->seePageIs('/forgot-password') ->type('test@test.com','email') ->press('Send Password Reset Link') ->seePageIs('/login') ->see('A password reset link was sent to the email address supplied.') ->seeInDatabase('password_resets', ['email' => 'test@test.com']); $uuid = DB::table('password_resets') ->where('email', '=', 'test@test.com') ->value('token'); $this->visit('/reset-password/' . $uuid) ->type('bbbbbbbb','password') ->type('bbbbbbbb','password_confirmation') ->press('Change Password') ->see('Your password was reset.') ->seePageIs('/login') ->type('test@test.com','email') ->type('bbbbbbbb','password') ->press('Sign In') ->seePageIs('/welcome') ->click('Logout') ->seePageIs('/login'); // Change password back $this->visit('/login') ->click('Forgot Your Password?') ->seePageIs('/forgot-password') ->type('test@test.com','email') ->press('Send Password Reset Link') ->seePageIs('/login') ->see('A password reset link was sent to the email address supplied.') ->seeInDatabase('password_resets', ['email' => 'test@test.com']); $uuid = DB::table('password_resets') ->where('email', '=', 'test@test.com') ->value('token'); $this->visit('/reset-password/' . $uuid) ->type('abcd1234','password') ->type('abcd1234','password_confirmation') ->press('Change Password') ->see('Your password was reset.') ->seePageIs('/login') ->type('test@test.com','email') ->type('abcd1234','password') ->press('Sign In') ->seePageIs('/welcome') ->click('Logout') ->seePageIs('/login'); 

By the way, I cannot seem to format the code with the {} brackets, so apologies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM