Skip to content

Commit

Permalink
Address review feedback.
Browse files Browse the repository at this point in the history
- Update paragraph assert method description.
- Replace switch statement with generalized `$entity->label()`.
- Add comment around node loading logic.
  • Loading branch information
damontgomery committed Jun 27, 2018
1 parent 90669a2 commit 9d3bbe5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ public function assertNotEntityFieldValue($field, $value)
}//end assertNotEntityFieldValue()

/**
* Verify that a field contains a value.
* Verify that a paragraph field contains a paragraph of a certain type.
*
* @Then paragraph field :field should be of type :type
*
Expand Down Expand Up @@ -552,31 +552,21 @@ public function assertEntityFieldValueEntityReference($field, $value)
*/
foreach ($entities as $entity) {

switch ($entity->getEntityTypeId()) {
case 'node':
$title = $entity->title->value;
break;

case 'paragraph':
throw new \Exception('Paragraphs do not have titles, so they must be tested by a different method.');
break;

case 'taxonomy_term':
case 'user':
case 'media':
default:
$title = $entity->name->value;
break;
if ($entity->getEntityTypeId() === 'paragraph') {
throw new \Exception('Paragraphs do not have meaningful labels, so they must be tested by a different method.');
// If we get a single paragraph reference, we will assume
// that the rest are also paragraphs and exit the method.
return;
}

$titles[] = $title;
$labels[] = $entity->label();

if ($title === $value) {
if ($entity->label() === $value) {
return;
}
}

throw new \Exception(sprintf('Field does not contain entity with title "%s" (has "%s" titles instead).', $value, json_encode($titles)));
throw new \Exception(sprintf('Field does not contain entity with label "%s" (has "%s" labels instead).', $value, json_encode($labels)));
}

throw new \Exception('Field is empty.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public function findNodeByTitle($contentType, $title, $language = NULL)
if (count($entities) === 1) {
$node_storage = \Drupal::entityManager()->getStorage('node');

// `entityQuery` will return an array of node IDs with key and
// value equal to the nids.
// Example: `[123 => '123', 456 => '456']`. For this reason, even
// though there is only a single element, we cannot access the
// first element using `$entities[0]`.
$nid = array_shift($entities);

$node = $node_storage->load($nid);
Expand Down

0 comments on commit 9d3bbe5

Please sign in to comment.