From 497a3852271e2ef50f9a5de88c84b68eeb50dac7 Mon Sep 17 00:00:00 2001 From: Eduardo Morales Date: Tue, 4 Jan 2022 13:08:00 +0100 Subject: [PATCH 1/2] Issue #124: Allow declare language prefix on step go to --- src/Behat/Context/EntityContext.php | 7 ++++++- src/Behat/Cores/CoreInterface.php | 11 +++++++++++ src/Behat/Cores/Drupal7.php | 7 +++++++ src/Behat/Cores/Drupal8.php | 19 +++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/Behat/Context/EntityContext.php b/src/Behat/Context/EntityContext.php index c782067..ac1f723 100644 --- a/src/Behat/Context/EntityContext.php +++ b/src/Behat/Context/EntityContext.php @@ -79,11 +79,16 @@ public function goToTheLastEntityCreated($entity_type, $bundle = NULL, $subpath * Go to a specific path of an entity with a specific label. * * @Given I go to the :entity_type entity with label :label + * @Given I go to the :entity_type entity with label :label in :language language * @Given I go to :subpath of the :entity_type entity with label :label */ - public function goToTheEntityWithLabel($entity_type, $label, $subpath = NULL) { + 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)); } diff --git a/src/Behat/Cores/CoreInterface.php b/src/Behat/Cores/CoreInterface.php index 8d54f19..1ece4e1 100644 --- a/src/Behat/Cores/CoreInterface.php +++ b/src/Behat/Cores/CoreInterface.php @@ -338,4 +338,15 @@ public function formatString($string, array $params); */ public function createFileUrl($file, bool $relative = TRUE); + /** + * Obtain the language prefix from label. + * + * @param string $language + * Language. + * + * @return string + * Language prefix or empty if not found. + */ + public function getLanguagePrefix($language); + } diff --git a/src/Behat/Cores/Drupal7.php b/src/Behat/Cores/Drupal7.php index 337ec0d..7b52045 100644 --- a/src/Behat/Cores/Drupal7.php +++ b/src/Behat/Cores/Drupal7.php @@ -351,4 +351,11 @@ public function createFileUrl($file, bool $relative = TRUE) { throw new PendingException('Pending to implement method in Drupal 7'); } + /** + * {@inheritdoc} + */ + public function getLanguagePrefix($language) { + throw new PendingException('Pending to implement method in Drupal 7'); + } + } diff --git a/src/Behat/Cores/Drupal8.php b/src/Behat/Cores/Drupal8.php index 2b4360d..d3f7e81 100644 --- a/src/Behat/Cores/Drupal8.php +++ b/src/Behat/Cores/Drupal8.php @@ -469,4 +469,23 @@ public function createFileUrl($file, bool $relative = TRUE) { throw new InvalidArgumentException('%s method only accept %s objects in Drupal 8 or higher', __METHOD__, FileInterface::class); } + /** + * {@inheritdoc} + */ + public function getLanguagePrefix($language) { + $language_manager = \Drupal::languageManager(); + $language_list = $language_manager->getStandardLanguageList(); + array_walk($language_list, function (&$item1, $key) { + $item1 = current($item1); + }); + $language_list = array_flip($language_list); + $langcode = $language_list[$language] ?? ''; + if ($langcode) { + $prefixes = \Drupal::config('language.negotiation')->get('url.prefixes'); + return $prefixes[$langcode] . '/'; + } + + throw new \InvalidArgumentException(sprintf("Language %s not found", $language)); + } + } From 67289f87c7b5ee27095ddf2d5c08bf0100a8be6f Mon Sep 17 00:00:00 2001 From: Eduardo Morales Date: Tue, 4 Jan 2022 15:02:12 +0100 Subject: [PATCH 2/2] Issue #124: Refactor code to be more clean --- src/Behat/Context/EntityContext.php | 2 +- src/Behat/Cores/Drupal8.php | 20 +++++++++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Behat/Context/EntityContext.php b/src/Behat/Context/EntityContext.php index ac1f723..18091bf 100644 --- a/src/Behat/Context/EntityContext.php +++ b/src/Behat/Context/EntityContext.php @@ -87,7 +87,7 @@ public function goToTheEntityWithLabel($entity_type, $label, $subpath = NULL, $l $path = $this->getCore()->buildEntityUri($entity_type, $entity, $subpath); if ($language) { $prefix = $this->getCore()->getLanguagePrefix($language); - $path = $prefix . $path; + $path = $prefix . '/' . $path; } if (!empty($path)) { $this->getSession()->visit($this->locatePath($path)); diff --git a/src/Behat/Cores/Drupal8.php b/src/Behat/Cores/Drupal8.php index d3f7e81..2f21f3b 100644 --- a/src/Behat/Cores/Drupal8.php +++ b/src/Behat/Cores/Drupal8.php @@ -475,17 +475,19 @@ public function createFileUrl($file, bool $relative = TRUE) { public function getLanguagePrefix($language) { $language_manager = \Drupal::languageManager(); $language_list = $language_manager->getStandardLanguageList(); - array_walk($language_list, function (&$item1, $key) { - $item1 = current($item1); - }); - $language_list = array_flip($language_list); - $langcode = $language_list[$language] ?? ''; - if ($langcode) { - $prefixes = \Drupal::config('language.negotiation')->get('url.prefixes'); - return $prefixes[$langcode] . '/'; + + $filter_func = function ($item) use ($language) { + return in_array($language, $item); + }; + + $found = array_filter($language_list, $filter_func); + + if (empty($found)) { + throw new \InvalidArgumentException(sprintf("Language %s not found", $language)); } - throw new \InvalidArgumentException(sprintf("Language %s not found", $language)); + $prefixes = \Drupal::config('language.negotiation')->get('url.prefixes'); + return $prefixes[array_key_first($found)]; } }