From 695f1e49941d652800f9737d4d651e66b9f910d6 Mon Sep 17 00:00:00 2001 From: Omar Date: Mon, 30 Oct 2023 11:45:29 +0100 Subject: [PATCH] Steps that checks current user has entity access of entity operations --- .../Context/DrupalContextDependencyTrait.php | 47 ++++++++++++++++++ src/Behat/Context/EntityContext.php | 49 +++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 src/Behat/Context/DrupalContextDependencyTrait.php diff --git a/src/Behat/Context/DrupalContextDependencyTrait.php b/src/Behat/Context/DrupalContextDependencyTrait.php new file mode 100644 index 0000000..1b07e80 --- /dev/null +++ b/src/Behat/Context/DrupalContextDependencyTrait.php @@ -0,0 +1,47 @@ +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; + } + } + } + +} diff --git a/src/Behat/Context/EntityContext.php b/src/Behat/Context/EntityContext.php index 6cc35fd..cf4a2a4 100644 --- a/src/Behat/Context/EntityContext.php +++ b/src/Behat/Context/EntityContext.php @@ -16,6 +16,8 @@ */ class EntityContext extends RawDrupalContext { + use DrupalContextDependencyTrait; + /** * Time before scenario. * @@ -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; @@ -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); + } + }