ML's avatar
Level 2

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!

0 likes
2 replies
ML's avatar
Level 2

Well. this solves it... I just had to pull the value from the DB! Doh!

  1. add Eloquent reference to the test use Illuminate\Database\Eloquent\Model;

  2. Complete the code like this:

    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');
    }
    
tomturton's avatar

I am interested in this problem too. While your solution should work, it's not as neat as I'd like it to be. Thanks for the idea though.

Please or to participate in this conversation.