Skip to content

Commit

Permalink
Add tests for @hasPermission() directives
Browse files Browse the repository at this point in the history
Ref #2515
  • Loading branch information
drbyte committed Oct 24, 2023
1 parent f8af645 commit 21e55ac
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
32 changes: 31 additions & 1 deletion tests/BladeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')));
Expand All @@ -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')));
Expand All @@ -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')));
Expand All @@ -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()
{
Expand Down
7 changes: 7 additions & 0 deletions tests/resources/views/haspermission.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@haspermission($permission, $guard ?? null)
has permission
@elsehaspermission($elsepermission, $guard ?? null)
has else permission
@else
does not have permission
@endhaspermission

0 comments on commit 21e55ac

Please sign in to comment.