0
votes

I am using PHPUnit to test. Here is my class with the failing test in bold:

    //Check that the database is clean
    public function testNoUsers(){
        $users = User::all();
        $this->assertEquals(count($users), 0);
    }

    //Test that users can login and a find and search works.
    public function testUserRegistrationAndSave(){

        User::create(array(
            'email' => '[email protected]',
            'password' => Hash::make('first_password')
        ));

        $user = User::where('email', '=', '[email protected]')->first();
        $this->assertEquals('[email protected]', $user->email);
    }


    //Make sure that the email is a proper email address
    public function testShouldNotSaveWithInvalidEmail(){
        $invalidUser = new User(array(
            'email' => 'thisisatest',
            'password' => Hash::make('first_password')
        ));

        $this->assertEquals($invalidUser->save(), false, 'Incorrect email address');
    }

    //Make sure that if the email is not filled out, it can't be saved.
    public function testEmailIsFilled(){
        $invalidUser = new User(array(
            'password' => Hash::make('first_password')
        ));

        $this->assertEquals($invalidUser->save(), false, 'Email not filled out!');
    }

    public function testUserActive(){
        $invalidUser = new User(array(
            'email' => '[email protected]',
            'password' => Hash::make('first_password')
        ));

        $invalidUser->save();

        //Has active = 1
        $validUser = new User(array(
            'email' => '[email protected]',
            'password' => Hash::make('first_password'),
            'active' => 1
        ));
        $validUser->save();




        //User with no active can't login
        $login = $invalidUser->login();
        $this->assertEquals(false, $login, 'Inactive user logged in');

        var_dump($validUser->active); //This outputs 1
        //Reset password to plaintext
        $validUser->password = "first_password";
        $login = $validUser->login();
        **$this->assertEquals(true, $login, 'Active user logged in');** //Error is here
    }

}

And here is my login method. This returns FALSE when I add active => 1, but returns true when I remove the active => 1 part:

use Illuminate\Auth\UserInterface; use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Model implements UserInterface, RemindableInterface {

protected $fillable = array('email', 'password');
protected $softDelete = true;

/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'users';

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = array('password');

protected static $rules = array(
    'email' => 'required|email|unique:users|min:4'
);

/**
 * Get the unique identifier for the user.
 *
 * @return mixed
 */
public function getAuthIdentifier()
{
    return $this->getKey();
}

/**
 * Get the password for the user.
 *
 * @return string
 */
public function getAuthPassword()
{
    return $this->password;
}

/**
 * Get the token value for the "remember me" session.
 *
 * @return string
 */
public function getRememberToken()
{
    return $this->remember_token;
}

/**
 * Set the token value for the "remember me" session.
 *
 * @param  string  $value
 * @return void
 */
public function setRememberToken($value)
{
    $this->remember_token = $value;
}

/**
 * Get the column name for the "remember me" token.
 *
 * @return string
 */
public function getRememberTokenName()
{
    return 'remember_token';
}

/**
 * Get the e-mail address where password reminders are sent.
 *
 * @return string
 */
public function getReminderEmail()
{
    return $this->email;
}

**public function login(){
    if(Auth::attempt(array('email' => $this->email, 'password' => $this->password, 'active' => 1), true)){
        return true;
    }else{
        return false;
    }
}**

}

I am using PHPUnit 4 running tests with SQLite. The migration adds the active field with a default value of 1, so I am not sure why this is not working.

1
Very stupid mistake. Both $invalidUser and $validUser had the same email address...meaning they didn't save and so the only record available had no active set. - user3545987

1 Answers

0
votes

You will have to do it manually, but it's easy:

public function login()
{
    $loggable = $user = User::where('email', $this->email) && 
                Hash::check($this->password, $user->password) &&
                $user->active;

    if ($loggable)
    {
        Auth::login($user);
    }

    return $loggable;
}