Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the DrupalUserContext #31

Open
wants to merge 5 commits into
base: drupal8
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/**
* Contains Palantirnet\PalantirBehatExtension\Context\DrupalUserContext
*
* @copyright 2016 Palantir.net
*/
namespace Palantirnet\PalantirBehatExtension\Context;

use Behat\Gherkin\Node\TableNode;

/**
* 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()


/**
* 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