From 9d3bbe52d5c4a82d96be5da16abc07611c339339 Mon Sep 17 00:00:00 2001 From: Daniel Montgomery Date: Wed, 27 Jun 2018 09:22:19 -0500 Subject: [PATCH] Address review feedback. - Update paragraph assert method description. - Replace switch statement with generalized `$entity->label()`. - Add comment around node loading logic. --- .../Context/EntityDataContext.php | 28 ++++++------------- .../Context/SharedDrupalContext.php | 5 ++++ 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php b/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php index 8e247a8..53deb26 100644 --- a/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php +++ b/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php @@ -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 * @@ -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.'); diff --git a/src/Palantirnet/PalantirBehatExtension/Context/SharedDrupalContext.php b/src/Palantirnet/PalantirBehatExtension/Context/SharedDrupalContext.php index 4c6a26f..2e79a18 100644 --- a/src/Palantirnet/PalantirBehatExtension/Context/SharedDrupalContext.php +++ b/src/Palantirnet/PalantirBehatExtension/Context/SharedDrupalContext.php @@ -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);