diff --git a/tests/BladeTest.php b/tests/BladeTest.php index 02a899083..18d17a113 100644 --- a/tests/BladeTest.php +++ b/tests/BladeTest.php @@ -27,8 +27,10 @@ public function all_blade_directives_will_evaluate_false_when_there_is_nobody_lo $role = 'writer'; $roles = [$role]; $elserole = 'na'; + $elsepermission = 'na'; $this->assertEquals('does not have permission', $this->renderView('can', ['permission' => $permission])); + $this->assertEquals('does not have permission', $this->renderView('haspermission', compact('permission', 'elsepermission'))); $this->assertEquals('does not have role', $this->renderView('role', compact('role', 'elserole'))); $this->assertEquals('does not have role', $this->renderView('hasRole', compact('role', 'elserole'))); $this->assertEquals('does not have all of the given roles', $this->renderView('hasAllRoles', compact('roles'))); @@ -44,10 +46,12 @@ public function all_blade_directives_will_evaluate_false_when_somebody_without_r $role = 'writer'; $roles = 'writer'; $elserole = 'na'; + $elsepermission = 'na'; auth()->setUser($this->testUser); - $this->assertEquals('does not have permission', $this->renderView('can', ['permission' => $permission])); + $this->assertEquals('does not have permission', $this->renderView('can', compact('permission'))); + $this->assertEquals('does not have permission', $this->renderView('haspermission', compact('permission', 'elsepermission'))); $this->assertEquals('does not have role', $this->renderView('role', compact('role', 'elserole'))); $this->assertEquals('does not have role', $this->renderView('hasRole', compact('role', 'elserole'))); $this->assertEquals('does not have all of the given roles', $this->renderView('hasAllRoles', compact('roles'))); @@ -61,10 +65,12 @@ public function all_blade_directives_will_evaluate_false_when_somebody_with_anot $role = 'writer'; $roles = 'writer'; $elserole = 'na'; + $elsepermission = 'na'; auth('admin')->setUser($this->testAdmin); $this->assertEquals('does not have permission', $this->renderView('can', compact('permission'))); + $this->assertEquals('does not have permission', $this->renderView('haspermission', compact('permission', 'elsepermission'))); $this->assertEquals('does not have role', $this->renderView('role', compact('role', 'elserole'))); $this->assertEquals('does not have role', $this->renderView('hasRole', compact('role', 'elserole'))); $this->assertEquals('does not have all of the given roles', $this->renderView('hasAllRoles', compact('roles'))); @@ -84,6 +90,30 @@ public function the_can_directive_will_evaluate_true_when_the_logged_in_user_has $this->assertEquals('has permission', $this->renderView('can', ['permission' => 'edit-articles'])); } + /** @test */ + public function the_haspermission_directive_will_evaluate_true_when_the_logged_in_user_has_the_permission() + { + $user = $this->getWriter(); + + $permission = 'edit-articles'; + $user->givePermissionTo('edit-articles'); + + auth()->setUser($user); + + $this->assertEquals('has permission', $this->renderView('haspermission', compact('permission'))); + + $guard = 'admin'; + $elsepermission = 'na'; + $this->assertEquals('does not have permission', $this->renderView('haspermission', compact('permission', 'elsepermission' ,'guard'))); + + $this->testAdminRole->givePermissionTo($this->testAdminPermission); + $this->testAdmin->assignRole($this->testAdminRole); + auth('admin')->setUser($this->testAdmin); + $guard = 'admin'; + $permission = 'admin-permission'; + $this->assertEquals('has permission', $this->renderView('haspermission', compact('permission', 'guard', 'elsepermission'))); + } + /** @test */ public function the_role_directive_will_evaluate_true_when_the_logged_in_user_has_the_role() { diff --git a/tests/resources/views/haspermission.blade.php b/tests/resources/views/haspermission.blade.php new file mode 100644 index 000000000..7dd4e7903 --- /dev/null +++ b/tests/resources/views/haspermission.blade.php @@ -0,0 +1,7 @@ +@haspermission($permission, $guard ?? null) +has permission +@elsehaspermission($elsepermission, $guard ?? null) +has else permission +@else +does not have permission +@endhaspermission