3
votes

I have a user controller with the following validation rules:

public function store(Request $request)
{
    ...
    $this->validate($request, [
        'name' => 'required',
        'email' => 'email|required|unique:users',
        'password' => 'confirmed|max:32|min:8|required',
        'roles' => 'exists:roles,id|required',
    ]);

    $user = new User();
    $user->fill($request->all());
   ...
}

My User.php model defines the fillable properties as:

protected $fillable = ['name', 'email'];

To pass the confirmed validation, I have to send both password and password_confirmation fields in the POST request.

During development everything works fine, but in unit tests I'm getting a database error. It tries to insert data into a password_confirmation column. It's like it ignores the $fillable array.

I know about the "laravel losts event handlers between tests" bug/issue (https://github.com/laravel/framework/issues/1181). So I think that maybe I'm missing to call some model function aside from Model::boot() (I'm calling User::boot() in the test's setUp() function).

Thanks,

Edit

Reading the Model.php source, I've found that someone is calling Model::unguard() https://github.com/laravel/framework/blob/5.1/src/Illuminate/Database/Eloquent/Model.php#L2180 after the setUp() function and before the test. If I call User::reguard() at the beggining of the test it passes, but (I don't know why), the unguard() and reguard() functions get called multiple times and the test gets really slow.

2

2 Answers

4
votes

Found the problem: the base seeder in v5.0.x only called Model::unguard() (https://github.com/laravel/laravel/blob/v5.0.22/database/seeds/DatabaseSeeder.php#L15) while v5.1.x was updated and added a call to Model::reguard() (https://github.com/laravel/laravel/blob/v5.1.0/database/seeds/DatabaseSeeder.php#L19) (I was using v5.0.22).

1
votes

Fillable only applies to MassAssignment. When you're creating a new instance, like what you're doing above, you're not triggering the mass assignment event.

You could do something like this: If you're creating the user anyway, you might as well do:

$user = User::create($request->all);

If you just want to instantiate the user, without persisting the data, you could do something like this:

$user = new User($request);