You are here

public function EntityContext::visitEntity in Lightning Core 8

Same name and namespace in other branches
  1. 8.5 tests/contexts/EntityContext.behat.inc \Acquia\LightningExtension\Context\EntityContext::visitEntity()
  2. 8.2 tests/contexts/EntityContext.behat.inc \Acquia\LightningExtension\Context\EntityContext::visitEntity()
  3. 8.3 tests/contexts/EntityContext.behat.inc \Acquia\LightningExtension\Context\EntityContext::visitEntity()
  4. 8.4 tests/contexts/EntityContext.behat.inc \Acquia\LightningExtension\Context\EntityContext::visitEntity()

Visits a randomly chosen entity of a specific type and (optional) bundle.

@When I visit (?:a|an) :entity_type @When I visit (?:a|an) :bundle :entity_type

Parameters

string $entity_type: The entity type ID.

string $bundle: (optional) The bundle.

Throws

\Exception if there are no entities to visit.

File

tests/contexts/EntityContext.behat.inc, line 94

Class

EntityContext
Contains miscellaneous step definitions for working with Drupal entities.

Namespace

Acquia\LightningExtension\Context

Code

public function visitEntity($entity_type, $bundle = NULL) {
  $storage = \Drupal::entityTypeManager()
    ->getStorage($entity_type);
  $query = $storage
    ->getQuery();
  if ($bundle) {
    $key = $storage
      ->getEntityType()
      ->getKey('bundle');
    if ($key) {
      $query
        ->condition($key, $bundle);
    }
  }
  $items = $query
    ->execute();
  if ($items) {
    $id = reset($items);
    $url = $storage
      ->load($id)
      ->toUrl()
      ->getInternalPath();
    $this
      ->visitPath($url);
  }
  else {
    $label = $storage
      ->getEntityType()
      ->getPluralLabel();
    throw new \Exception("There are no {$bundle} {$label} available.");
  }
}