-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andrea R Soper
committed
Dec 5, 2016
1 parent
3de8eea
commit 86c2e6f
Showing
1 changed file
with
43 additions
and
29 deletions.
There are no files selected for viewing
72 changes: 43 additions & 29 deletions
72
src/Palantirnet/PalantirBehatExtension/Context/DrupalUserContext.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,52 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains Palantirnet\PalantirBehatExtension\Context\DrupalUserContext | ||
* | ||
* @copyright Copyright (c) 2016 Palantir.net | ||
* @copyright 2016 Palantir.net | ||
*/ | ||
|
||
use Behat\Gherkin\Node\TableNode; | ||
use Palantirnet\PalantirBehatExtension\Context\SharedDrupalContext; | ||
|
||
class DrupalUserContext extends SharedDrupalContext { | ||
/** | ||
* Asserts a role has a list of permissions | ||
* | ||
* @Then the :role role should have the permission(s): | ||
* | ||
* @param String $role | ||
* @param TableNode $perms | ||
*/ | ||
public function assertRoleHasPermission($role, TableNode $perms) { | ||
// get the role storage object so we can query it for permissions | ||
$roleStorage = \Drupal::entityManager()->getStorage('user_role'); | ||
|
||
// convert the single role given to an array for the isPermissionInRoles() function | ||
$rids = array($role); | ||
|
||
foreach($perms->getHash() as $row){ | ||
// grab the value out of the row. it will always be the first value | ||
$perm = reset($row); | ||
|
||
// check the permission against the role | ||
if(!$roleStorage->isPermissionInRoles($perm, $rids)){ | ||
throw new Exception('Role '.$role.' does not have permission '.$perm); | ||
} | ||
} | ||
} | ||
} | ||
/** | ||
* Class DrupalUserContext | ||
*/ | ||
class DrupalUserContext extends SharedDrupalContext | ||
{ | ||
|
||
|
||
/** | ||
* Asserts a role has a list of permissions | ||
* | ||
* @Then the :role role should have the permission(s): | ||
* | ||
* @param String $role The role to check for the list of permissions. | ||
* @param TableNode $perms The permissions this role should have. | ||
* | ||
* @return void | ||
* | ||
* @throws \Exception | ||
*/ | ||
public function assertRoleHasPermission($role, TableNode $perms) | ||
{ | ||
// Get the role storage object so we can query it for permissions. | ||
$roleStorage = \Drupal::entityManager()->getStorage('user_role'); | ||
|
||
// Convert the single role given to an array for the isPermissionInRoles() function. | ||
$rids = array($role); | ||
|
||
foreach ($perms->getHash() as $row) { | ||
// Grab the value out of the row. It will always be the first value. | ||
$perm = reset($row); | ||
|
||
// Check the permission against the role. | ||
if (false === $roleStorage->isPermissionInRoles($perm, $rids)) { | ||
throw new Exception('Role '.$role.' does not have permission '.$perm); | ||
} | ||
} | ||
|
||
}//end assertRoleHasPermission() | ||
|
||
|
||
}//end class |