非活动用户的Laravel单元测试登录

时间:2018-10-10 12:59:27

标签: php unit-testing laravel-5 login

我想测试用户在db中被标记为非活动的情况,这是LoginController中的方法

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware(['guest'])->except('logout'); //, 'checkup'
    }

    /**
     * The user has been authenticated.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  mixed $user
     * @return mixed
     */
    protected function authenticated(Request $request, $user)
    {
        if ($user->roles->count() === 0 ) {
            $this->guard()->logout();
            flash('There is no role specified please contact the system Administrator!')->error();
            return back()->withErrors(['There is no role specified']);
        }
    }

    /**
     * @param Request $request
     * @return array
     */
    protected function credentials(Request $request)
    {
        $credentials = $request->only($this->username(), 'password');
        $credentials['status'] = "A";

        return $credentials;
    }
}

因此,我创建了一个假用户,并使用以下测试来掩盖案件

 $this->faker = Faker::create();
    $password = $this->faker->password;
    $user = factory(User::class)->create([
        'username' => $this->faker->username,
        'firstname' => $this->faker->firstName,
        'lastname' => $this->faker->lastName,
        'status'=> 'I',
        'password' => bcrypt($password),
        'email' => $this->faker->email
    ]);

    $response = $this->call('POST', '/login', [
       'email' => $user->email,
       'password' =>$password,
        '_token' => csrf_token()
    ]);

    $response->assertRedirect('/login');

但是用户每次都会登录。

使用laravel黄昏测试相同的情况,将会通过

public function testNotActiveUserLogin()
    {
        $faker = Faker\Factory::create();
        $password = $faker->password;
        $user = factory(User::class)->create([
            'username' => $faker->username,
            'firstname' => $faker->firstName,
            'lastname' => $faker->lastName,
            'status' => 'I',
            'password' => bcrypt($password),
            'email' => $faker->email
        ]);

        $this->browse(function (Browser $browser) use ($user, $password) {
            $browser->visit('/')
                ->type('email', $user->email)
                ->type('password', $password)
                ->press('Sign In')
                ->assertPathIs('/login');
        });
    }

在这种情况下我想念什么?

1 个答案:

答案 0 :(得分:0)

该用户将始终被视为活动用户,因为使用以下方法从数据库中获取用户后,您将覆盖状态:$credentials['status'] = "A";。如果删除此声明,则凭据应正确。