Laravel test failed
I am using the permission pack from space.
It works, but my test keeps failing.
My test looks like this:
/** @test */
public function a_user_with_right_permission_can_update_company_profile()
{
$role = create(Role::class, ['name' => 'super-admin']);
$permission = create(Permission::class, ['name' => 'bewerk-bedrijf-instellingen']);
$role->givePermissionTo($permission->name);
$user = create(User::class)->assignRole($role->name);
$user = $user->fresh();
$this->signIn($user);
$settings = [
'name' => 'Company',
'email' => 'info@example.com',
'phone' => '0623844932',
'website' => 'http://www.example.com',
'description' => 'A short description',
];
$this->patch('/instellingen/bedrijf/profiel', $settings)
->assertSessionHas('status', 'Bedrijfsprofiel geupdatet!');
$this->assertDatabaseHas('settings', $settings);
}
I get this error message:
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'Bedrijfsprofiel geupdatet!'
+'Niet geautoriseerd!'
So it tells me that I am not authorized to do this! But I have the correct permission. Since this is middleware in this controller:
public function __construct()
{
$this->middleware('permission:bewerk-bedrijf-instellingen');
}
When I remove middleware the test works. What can happen here, cache issue?
- Edit -
/** @test */
public function a_user_with_right_permission_can_update_company_profile()
{
$permission = Permission::create([
'name' => 'bewerk-bedrijf-instellingen'
]);
$role = Role::create([
'name' => 'super-admin'
]);
$role->givePermissionTo($permission->name);
$user = User::create([
'name' => 'First',
'last_name' => 'Last',
'email' => 'info@example.com',
'password' => 'secret',
'password_confirmation' => 'secret',
'function' => 'Developer',
]);
$user = $user->fresh();
$this->signIn($user);
$settings = [
'name' => 'Company',
'email' => 'info@example
.com',
'phone' => '0623844932',
'website' => 'http://www.example.com',
'description' => 'A short description',
];
$this->patch('/instellingen/bedrijf/profiel', $settings)
->assertSessionHas('status', 'Bedrijfsprofiel geupdatet!');
$this->assertDatabaseHas('settings', $settings);
}
Middleware
public function handle($request, Closure $next, $permission)
{
if (Auth::guest()) {
return redirect('/');
}
if (!$request->user()->hasPermissionTo($permission)) {
return back()
->with('status', 'Niet geautoriseerd!');
}
return $next($request);
}
When I am dd($request->user()->hasPermissionTo($permission));
in middleware I get false. When I am dd($request->user()->roles()->pluck('name'));
, I receive super-admin
.
+3
source to share
No one has answered this question yet
Check out similar questions: