Skip to content

Commit

Permalink
remove convertPipeToArray
Browse files Browse the repository at this point in the history
  • Loading branch information
makladuxbert committed Aug 29, 2017
1 parent f58cbf1 commit e57f9f0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 54 deletions.
26 changes: 2 additions & 24 deletions src/Traits/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function syncRoles(...$roles)
public function hasRole($roles): bool
{
if (is_string($roles) && false !== strpos($roles, '|')) {
$roles = $this->convertPipeToArray($roles);
$roles = explode('|', $roles);
}

if (is_string($roles)) {
Expand Down Expand Up @@ -171,7 +171,7 @@ public function hasAnyRole($roles): bool
public function hasAllRoles($roles): bool
{
if (is_string($roles) && false !== strpos($roles, '|')) {
$roles = $this->convertPipeToArray($roles);
$roles = explode('|', $roles);
}

if (is_string($roles)) {
Expand Down Expand Up @@ -301,26 +301,4 @@ protected function getStoredRole($role): Role

return $role;
}

protected function convertPipeToArray(string $pipeString)
{
$pipeString = trim($pipeString);

if (strlen($pipeString) <= 2) {
return $pipeString;
}

$quoteCharacter = substr($pipeString, 0, 1);
$endCharacter = substr($quoteCharacter, -1, 1);

if ($quoteCharacter !== $endCharacter) {
return explode('|', $pipeString);
}

if (! in_array($quoteCharacter, ["'", '"'])) {
return explode('|', $pipeString);
}

return explode('|', trim($pipeString, $quoteCharacter));
}
}
81 changes: 51 additions & 30 deletions tests/BladeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,26 @@ public function setUp()
public function all_blade_directives_will_evaluate_falsly_when_there_is_nobody_logged_in()
{
$permission = 'edit-articles';
$role = 'writer';
$roles = [$role];
$role = 'writer';
$roles = [$role];

$this->assertEquals('does not have permission', $this->renderView('can', ['permission' => $permission]));
$this->assertEquals('does not have role', $this->renderView('role', [$role]));
$this->assertEquals('does not have role', $this->renderView('hasRole', [$role]));
$this->assertEquals('does not have all of the given roles', $this->renderView('hasAllRoles', $roles));
$this->assertEquals('does not have all of the given roles', $this->renderView('hasAllRoles', ['roles' => implode('|', $roles)]));
$this->assertEquals('does not have all of the given roles',
$this->renderView('hasAllRoles', ['roles' => implode('|', $roles)]));
$this->assertEquals('does not have any of the given roles', $this->renderView('hasAnyRole', $roles));
$this->assertEquals('does not have any of the given roles', $this->renderView('hasAnyRole', ['roles' => implode('|', $roles)]));
$this->assertEquals('does not have any of the given roles',
$this->renderView('hasAnyRole', ['roles' => implode('|', $roles)]));
}

/** @test */
public function all_blade_directives_will_evaluate_falsy_when_somebody_without_roles_or_permissions_is_logged_in()
{
$permission = 'edit-articles';
$role = 'writer';
$roles = 'writer';
$role = 'writer';
$roles = 'writer';

auth()->setUser($this->testUser);

Expand All @@ -56,8 +58,8 @@ public function all_blade_directives_will_evaluate_falsy_when_somebody_without_r
public function all_blade_directives_will_evaluate_falsy_when_somebody_with_another_guard_is_logged_in()
{
$permission = 'edit-articles';
$role = 'writer';
$roles = 'writer';
$role = 'writer';
$roles = 'writer';

auth('admin')->setUser($this->testAdmin);

Expand Down Expand Up @@ -94,7 +96,8 @@ public function the_role_directive_will_evaluate_true_when_the_logged_in_user_ha
{
auth('admin')->setUser($this->getSuperAdmin());

$this->assertEquals('has role for guard', $this->renderView('guardRole', ['role' => 'super-admin', 'guard' => 'admin']));
$this->assertEquals('has role for guard',
$this->renderView('guardRole', ['role' => 'super-admin', 'guard' => 'admin']));
}

/** @test */
Expand All @@ -110,44 +113,51 @@ public function the_hasrole_directive_will_evaluate_true_when_the_logged_in_user
{
auth('admin')->setUser($this->getSuperAdmin());

$this->assertEquals('has role', $this->renderView('guardHasRole', ['role' => 'super-admin', 'guard' => 'admin']));
$this->assertEquals('has role',
$this->renderView('guardHasRole', ['role' => 'super-admin', 'guard' => 'admin']));
}

/** @test */
public function the_hasanyrole_directive_will_evaluate_false_when_the_logged_in_user_does_not_have_any_of_the_required_roles()
public function the_hasanyrole_directive_will_evaluate_false_when_the_logged_in_user_does_not_have_any_of_the_required_roles(
)
{
$roles = ['writer', 'intern'];

auth()->setUser($this->getMember());

$this->assertEquals('does not have any of the given roles', $this->renderView('hasAnyRole', compact('roles')));
$this->assertEquals('does not have any of the given roles', $this->renderView('hasAnyRole', ['roles' => implode('|', $roles)]));
$this->assertEquals('does not have any of the given roles', $this->renderView('hasAnyRole', ['roles' => '"writer"|"intern"']));
}

/** @test */
public function the_hasanyrole_directive_will_evaluate_true_when_the_logged_in_user_does_have_some_of_the_required_roles()
public function the_hasanyrole_directive_will_evaluate_true_when_the_logged_in_user_does_have_some_of_the_required_roles(
)
{
$roles = ['member', 'writer', 'intern'];

auth()->setUser($this->getMember());

$this->assertEquals('does have some of the roles', $this->renderView('hasAnyRole', compact('roles')));
$this->assertEquals('does have some of the roles', $this->renderView('hasAnyRole', ['roles' => implode('|', $roles)]));
$this->assertEquals('does have some of the roles',
$this->renderView('hasAnyRole', ['roles' => implode('|', $roles)]));
}

/** @test */
public function the_hasanyrole_directive_will_evaluate_true_when_the_logged_in_user_does_have_some_of_the_required_roles_for_the_given_guard()
public function the_hasanyrole_directive_will_evaluate_true_when_the_logged_in_user_does_have_some_of_the_required_roles_for_the_given_guard(
)
{
$roles = ['super-admin', 'moderator'];
$guard = 'admin';

auth('admin')->setUser($this->getSuperAdmin());

$this->assertEquals('does have some of the roles', $this->renderView('guardHasAnyRole', compact('roles', 'guard')));
$this->assertEquals('does have some of the roles',
$this->renderView('guardHasAnyRole', compact('roles', 'guard')));
}

/** @test */
public function the_hasanyrole_directive_will_evaluate_true_when_the_logged_in_user_does_have_some_of_the_required_roles_in_pipe()
public function the_hasanyrole_directive_will_evaluate_true_when_the_logged_in_user_does_have_some_of_the_required_roles_in_pipe(
)
{
$guard = 'admin';

Expand All @@ -157,24 +167,28 @@ public function the_hasanyrole_directive_will_evaluate_true_when_the_logged_in_u
}

/** @test */
public function the_hasanyrole_directive_will_evaluate_false_when_the_logged_in_user_doesnt_have_some_of_the_required_roles_in_pipe()
public function the_hasanyrole_directive_will_evaluate_false_when_the_logged_in_user_doesnt_have_some_of_the_required_roles_in_pipe(
)
{
$guard = '';

auth('admin')->setUser($this->getMember());

$this->assertEquals('does not have any of the given roles', $this->renderView('guardHasAnyRolePipe', compact('guard')));
$this->assertEquals('does not have any of the given roles',
$this->renderView('guardHasAnyRolePipe', compact('guard')));
}

/** @test */
public function the_hasallroles_directive_will_evaluate_false_when_the_logged_in_user_does_not_have_all_required_roles()
public function the_hasallroles_directive_will_evaluate_false_when_the_logged_in_user_does_not_have_all_required_roles(
)
{
$roles = ['member', 'writer'];

auth()->setUser($this->getMember());

$this->assertEquals('does not have all of the given roles', $this->renderView('hasAllRoles', compact('roles')));
$this->assertEquals('does not have all of the given roles', $this->renderView('hasAllRoles', ['roles' => implode('|', $roles)]));
$this->assertEquals('does not have all of the given roles',
$this->renderView('hasAllRoles', ['roles' => implode('|', $roles)]));
}

/** @test */
Expand All @@ -191,11 +205,13 @@ public function the_hasallroles_directive_will_evaluate_true_when_the_logged_in_
auth()->setUser($user);

$this->assertEquals('does have all of the given roles', $this->renderView('hasAllRoles', compact('roles')));
$this->assertEquals('does have all of the given roles', $this->renderView('hasAllRoles', ['roles' => implode('|', $roles)]));
$this->assertEquals('does have all of the given roles',
$this->renderView('hasAllRoles', ['roles' => implode('|', $roles)]));
}

/** @test */
public function the_hasallroles_directive_will_evaluate_true_when_the_logged_in_user_does_have_all_required_roles_for_the_given_guard()
public function the_hasallroles_directive_will_evaluate_true_when_the_logged_in_user_does_have_all_required_roles_for_the_given_guard(
)
{
$roles = ['super-admin', 'moderator'];
$guard = 'admin';
Expand All @@ -208,11 +224,13 @@ public function the_hasallroles_directive_will_evaluate_true_when_the_logged_in_

auth('admin')->setUser($admin);

$this->assertEquals('does have all of the given roles', $this->renderView('guardHasAllRoles', compact('roles', 'guard')));
$this->assertEquals('does have all of the given roles',
$this->renderView('guardHasAllRoles', compact('roles', 'guard')));
}

/** @test */
public function the_hasallroles_directive_will_evaluate_true_when_the_logged_in_user_does_have_all_required_roles_in_pipe()
public function the_hasallroles_directive_will_evaluate_true_when_the_logged_in_user_does_have_all_required_roles_in_pipe(
)
{
$guard = 'admin';

Expand All @@ -224,22 +242,25 @@ public function the_hasallroles_directive_will_evaluate_true_when_the_logged_in_

auth('admin')->setUser($admin);

$this->assertEquals('does have all of the given roles', $this->renderView('guardHasAllRolesPipe', compact('guard')));
$this->assertEquals('does have all of the given roles',
$this->renderView('guardHasAllRolesPipe', compact('guard')));
}

/** @test */
public function the_hasallroles_directive_will_evaluate_false_when_the_logged_in_user_doesnt_have_all_required_roles_in_pipe()
public function the_hasallroles_directive_will_evaluate_false_when_the_logged_in_user_doesnt_have_all_required_roles_in_pipe(
)
{
$guard = '';
$user = $this->getMember();
$user = $this->getMember();

$user->assignRole('writer');

$this->refreshTestUser();

auth()->setUser($user);

$this->assertEquals('does not have all of the given roles', $this->renderView('guardHasAllRolesPipe', compact('guard')));
$this->assertEquals('does not have all of the given roles',
$this->renderView('guardHasAllRolesPipe', compact('guard')));
}

protected function getWriter()
Expand Down Expand Up @@ -277,6 +298,6 @@ protected function renderView($view, $parameters)
$view = view($view)->with($parameters);
}

return trim((string) ($view));
return trim((string)($view));
}
}

0 comments on commit e57f9f0

Please sign in to comment.