Skip to content

Commit

Permalink
Steps that checks current user has entity access of entity operations
Browse files Browse the repository at this point in the history
  • Loading branch information
omarlopesino committed Oct 30, 2023
1 parent b15733e commit 695f1e4
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/Behat/Context/DrupalContextDependencyTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Metadrop\Behat\Context;

use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Drupal\DrupalExtension\Context\DrupalContext;

/**
* Use it to have Drupal context available in your context.
*/
trait DrupalContextDependencyTrait {

/**
* Drupal context.
*
* Used by contexts that depends on its methods.
*
* @var \Drupal\DrupalExtension\Context\DrupalContext
*/
protected $drupalContext;

/**
* Get the Drupal context.
*
* It is done by searching any class that extends from DrupalContext,
* it may be only one.
*
* It is assumed that the Drupal context is always present, as is the main
* extension for behat tests in DRupal.
*
* @BeforeScenario
*
* @param BeforeScenarioScope $scope
* Scope del scenario.
*/
public function gatherDrupalContext(BeforeScenarioScope $scope) {
$environment = $scope->getEnvironment();
$classesArray = $environment->getContextClasses();
foreach ($classesArray as $class_name) {
if (is_subclass_of($class_name, DrupalContext::class) || $class_name == DrupalContext::class) {
$this->drupalContext = $environment->getContext($class_name);
break;
}
}
}

}
49 changes: 49 additions & 0 deletions src/Behat/Context/EntityContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
class EntityContext extends RawDrupalContext {

use DrupalContextDependencyTrait;

/**
* Time before scenario.
*
Expand Down Expand Up @@ -85,6 +87,7 @@ public function goToTheLastEntityCreated($entity_type, $bundle = NULL, $subpath
public function goToTheEntityWithLabel($entity_type, $label, $subpath = NULL, $language = NULL) {
$entity = $this->getCore()->loadEntityByLabel($entity_type, $label);
$path = $this->getCore()->buildEntityUri($entity_type, $entity, $subpath);
print_r([$path]);
if ($language) {
$prefix = $this->getCore()->getLanguagePrefix($language);
$path = $prefix . '/' . $path;
Expand Down Expand Up @@ -546,4 +549,50 @@ public function cleanEntities() {
$this->nodes = [];
}

/**
* Check current user is not able to perform a specific operation in the site.
*
* @Then I am able to :operation the :entity_type entity with label :entity_label
*/
public function iAmAbleToDoOperationAtEntityWithLabel($operation, $entity_type, $entity_label) {
if (!$this->userHasAccessToEntity($operation, $entity_type, $entity_label)) {
throw new \InvalidArgumentException(sprintf('User is not able to "%s" the "%s" entity with label "%s"', $operation, $entity_type, $entity_label));
}
}

/**
* Check current user is not able to perform a specific operation in the site.
*
* @Then I am not able to :operation the :entity_type entity with label :entity_label
*/
public function iAmNotAbleToDoOperationAtEntityWithLabel($operation, $entity_type, $entity_label) {
if ($this->userHasAccessToEntity($operation, $entity_type, $entity_label)) {
throw new \InvalidArgumentException(sprintf('User is able to "%s" the "%s" entity with label "%s"', $operation, $entity_type, $entity_label));
}
}

/**
* Check if current user has access to operation on specific entity.
*
* @param string $operation
* Operation that wants to be checked. Examples: view, update, delete.
* @param string $entity_type
* Entity type.
* @param string $entity_label
* Entity label.
*/
public function userHasAccessToEntity($operation, $entity_type, $entity_label) {
$current_user_raw = $this->drupalContext->getUserManager()->getCurrentUser();
$current_user_uid = !empty($current_user_raw) ? (int) $current_user_raw->uid : 0;
$current_user = $this->getCore()->loadEntityByProperties('user', [
'uid' => $current_user_uid
]);
$entity = $this->getCore()->loadEntityByLabel($entity_type, $entity_label);
if (!$entity instanceof EntityInterface) {
throw new \InvalidArgumentException(sprintf('The "%s" entity with label "%s"', $entity_type, $entity_label));
}

return $entity->access($operation, $current_user);
}

}

0 comments on commit 695f1e4

Please sign in to comment.