Skip to content

Commit

Permalink
Merge pull request #172 from Metadrop/feature/standardize-entity-path…
Browse files Browse the repository at this point in the history
…-browsing

Standardize and refactor the way entity urls are being visited in contexts
  • Loading branch information
omarlopesino authored Jan 29, 2024
2 parents ae1dd7e + 14d1e11 commit 833e620
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/Behat/Context/EntityContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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 {
Expand Down
18 changes: 18 additions & 0 deletions src/Behat/Cores/CoreInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
11 changes: 11 additions & 0 deletions src/Behat/Cores/Drupal7.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
18 changes: 18 additions & 0 deletions src/Behat/Cores/Drupal8.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}

0 comments on commit 833e620

Please sign in to comment.