diff --git a/src/Palantirnet/PalantirBehatExtension/Context/DrupalUserContext.php b/src/Palantirnet/PalantirBehatExtension/Context/DrupalUserContext.php new file mode 100644 index 0000000..bf0f589 --- /dev/null +++ b/src/Palantirnet/PalantirBehatExtension/Context/DrupalUserContext.php @@ -0,0 +1,86 @@ +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() + + + /** + * Asserts a role does not have a list of permissions. + * + * @Then the :role role should not have the permission(s): + * + * @param String $role The role to check for the list of permissions. + * @param TableNode $perms The permissions this role should not have. + * + * @return void + * + * @throws \Exception + */ + public function assertRoleHasNoPermission($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. + $has_permission = $roleStorage->isPermissionInRoles($perm, $rids); + if (true === $has_permission) { + throw new \Exception('Role "'.$role.'" has permission "'.$perm.'" but it should not.'); + } + } + + }//end assertRoleHasNoPermission() + + +}//end class