-
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.
Add the DrupalUserContext. Thank you @mistermashu. Great job!
- Loading branch information
Andrea R Soper
committed
Dec 5, 2016
1 parent
8893b7f
commit 3de8eea
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* | ||
* @copyright Copyright (c) 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); | ||
} | ||
} | ||
} | ||
} |