From 14d1e114b40fab1f66927c8ace5c8341d0a9587a Mon Sep 17 00:00:00 2001 From: Omar Date: Mon, 29 Jan 2024 16:39:47 +0100 Subject: [PATCH] Standardize and refactor entity path browsing - All methods that visit entities use the same method - Now the relative path is generated using Drupal\Core\Url class so code is not dependent of language detection method --- src/Behat/Context/EntityContext.php | 42 ++++++++++++++++------------- src/Behat/Cores/CoreInterface.php | 18 +++++++++++++ src/Behat/Cores/Drupal7.php | 11 ++++++++ src/Behat/Cores/Drupal8.php | 18 +++++++++++++ 4 files changed, 70 insertions(+), 19 deletions(-) diff --git a/src/Behat/Context/EntityContext.php b/src/Behat/Context/EntityContext.php index 3e41d48..e756c52 100644 --- a/src/Behat/Context/EntityContext.php +++ b/src/Behat/Context/EntityContext.php @@ -70,10 +70,7 @@ public function goToTheLastEntityCreated($entity_type, $bundle = NULL, $subpath } $entity = $this->getCore()->entityLoadSingle($entity_type, $last_entity); - $path = $this->getCore()->buildEntityUri($entity_type, $entity, $subpath); - if (!empty($path)) { - $this->getSession()->visit($this->locatePath($path)); - } + $this->visitEntityPath($entity_type, $entity, $subpath); } /** @@ -86,15 +83,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); - - if ($language) { - $prefix = $this->getCore()->getLanguagePrefix($language); - $path = $prefix . '/' . $path; - } - if (!empty($path)) { - $this->getSession()->visit($this->locatePath($path)); - } + $this->visitEntityPath($entity_type, $entity, $subpath, $language); } /** @@ -110,13 +99,28 @@ public function goToTheEntityWithProperties($entity_type, TableNode $properties, $properties_filter[$property_name] = $property_value; } $entity = $this->getCore()->loadEntityByProperties($entity_type, $properties_filter); - $path = $this->getCore()->buildEntityUri($entity_type, $entity, $subpath); + $this->visitEntityPath($entity_type, $entity, $subpath, $language); + } - if ($language) { - $prefix = $this->getCore()->getLanguagePrefix($language); - $path = $prefix . '/' . $path; - } - if (!empty($path)) { + /** + * Visit a path of a specific entity. + * + * @param string $entity_type + * Entity type. + * @param mixed $entity + * Entity. + * @param string|NULL $subpath + * Subpath. + * @param $language + * Language. + */ + protected function visitEntityPath(string $entity_type, $entity, string $subpath = NULL, $language = NULL) { + $path_found = $this->getCore()->buildEntityUri($entity_type, $entity, $subpath); + if (!empty($path_found)) { + $path = $this->getCore()->buildPath( + '/' . $path_found, + $language, + ); $this->visitPath($path); } else { diff --git a/src/Behat/Cores/CoreInterface.php b/src/Behat/Cores/CoreInterface.php index c5b714d..f0472b8 100644 --- a/src/Behat/Cores/CoreInterface.php +++ b/src/Behat/Cores/CoreInterface.php @@ -382,4 +382,22 @@ public function createFileUrl($file, bool $relative = TRUE); */ public function getLanguagePrefix($language); + /** + * Builds URL string given an internal path and a langcode. + * + * It is needed to build a URL without knowing the specific + * language detection the site has. Typically language detection + * is done by language prefix, but in some cases other specific + * logics are used. + * + * @param string $path + * Drupal internal path. + * @param string|NULL $langcode + * Langcode, if needed. + * + * @return string + * Relative path accesible by browser. + */ + public function buildPath(string $path, string $langcode = NULL); + } diff --git a/src/Behat/Cores/Drupal7.php b/src/Behat/Cores/Drupal7.php index 9c25b53..011388b 100644 --- a/src/Behat/Cores/Drupal7.php +++ b/src/Behat/Cores/Drupal7.php @@ -385,4 +385,15 @@ public function fileSaveData(string $data, $destination = NULL, int $replace = F return file_save_data($data, $destination, $replace); } + /** + * {@inheritdoc} + */ + public function buildPath(string $path, string $langcode = NULL) { + if (!empty($langcode)) { + $prefix = $this->getLanguagePrefix($langcode); + return $prefix . '/' . $path; + } + return $path; + } + } diff --git a/src/Behat/Cores/Drupal8.php b/src/Behat/Cores/Drupal8.php index 4837871..8de85b9 100644 --- a/src/Behat/Cores/Drupal8.php +++ b/src/Behat/Cores/Drupal8.php @@ -579,4 +579,22 @@ public function fileSaveData(string $data, $destination = NULL, int $replace = F return $this->getFileRepository()->writeData($data, $destination, $replace); } + /** + * {@inheritdoc} + */ + public function buildPath(string $path, string $langcode = NULL) { + $url_parameters = []; + if (!empty($langcode)) { + $language_manager = \Drupal::languageManager(); + $language = $language_manager->getLanguage($langcode); + if (!$language instanceof LanguageInterface) { + throw new \InvalidArgumentException(sprintf("Language %s not found", $langcode)); + } + $url_parameters['language'] = $language; + } + + $url = Url::fromUserInput($path, $url_parameters); + return $url->toString(); + } + }