首页 文章

将active添加到Auth :: try会返回false,其中active => 1但是没有active => 1则没有问题?

提问于
浏览
0

我正在使用PHPUnit进行测试 . 这是我的课程,以粗体显示失败的测试:

//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' => 'thisisatest@gmail.com',
            'password' => Hash::make('first_password')
        ));

        $user = User::where('email', '=', 'thisisatest@gmail.com')->first();
        $this->assertEquals('thisisatest@gmail.com', $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' => 'test@test.com',
            'password' => Hash::make('first_password')
        ));

        $invalidUser->save();

        //Has active = 1
        $validUser = new User(array(
            'email' => 'test@test.com',
            '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
    }

}

这是我的登录方法 . 当我添加active => 1时返回FALSE,但是当我删除active => 1部分时返回true:

使用Illuminate \ Auth \ UserInterface;使用Illuminate \ Auth \ Reminders \ RemindableInterface;

class User extends Model实现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;
    }
}**

}

我正在使用PHPUnit 4运行SQLite测试 . 迁移会将活动字段添加为默认值1,因此我不确定为什么这不起作用 .

1 回答

  • 0

    您必须手动完成,但这很容易:

    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;
    }
    

相关问题