From 0ff4ddcaafceb2873a6362ee5646b0c79aa52fef Mon Sep 17 00:00:00 2001 From: Bec White Date: Tue, 9 Aug 2016 17:03:01 -0500 Subject: [PATCH 1/8] Fix context class names in the README. --- README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index c0f1683..256e881 100644 --- a/README.md +++ b/README.md @@ -49,15 +49,15 @@ default: suites: default: contexts: - - Palantirnet\PalantirBehatExtension\Context\DrupalAutoNodetitleContext.php - - Palantirnet\PalantirBehatExtension\Context\DrupalCommentContext.php - - Palantirnet\PalantirBehatExtension\Context\DrupalOrganicGroupsContext.php - - Palantirnet\PalantirBehatExtension\Context\DrupalSetupContext.php - - Palantirnet\PalantirBehatExtension\Context\DrupalWorkbenchModerationContext.php - - Palantirnet\PalantirBehatExtension\Context\EntityDataContext.php - - Palantirnet\PalantirBehatExtension\Context\MarkupContext.php - - Palantirnet\PalantirBehatExtension\Context\NodeContext.php - - Palantirnet\PalantirBehatExtension\Context\SharedDrupalContext.php + - Palantirnet\PalantirBehatExtension\Context\DrupalAutoNodetitleContext + - Palantirnet\PalantirBehatExtension\Context\DrupalCommentContext + - Palantirnet\PalantirBehatExtension\Context\DrupalOrganicGroupsContext + - Palantirnet\PalantirBehatExtension\Context\DrupalSetupContext + - Palantirnet\PalantirBehatExtension\Context\DrupalWorkbenchModerationContext + - Palantirnet\PalantirBehatExtension\Context\EntityDataContext + - Palantirnet\PalantirBehatExtension\Context\MarkupContext + - Palantirnet\PalantirBehatExtension\Context\NodeContext + - Palantirnet\PalantirBehatExtension\Context\SharedDrupalContext ``` ---- From 5e5c8dca32264463af8fd4266d9c9bad3d149b14 Mon Sep 17 00:00:00 2001 From: Bec White Date: Thu, 11 Aug 2016 16:49:40 -0500 Subject: [PATCH 2/8] Add info about the drupal version support to the README. --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 256e881..de50a6d 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,10 @@ The Palantir Behat Drupal Extension provides additional step definitions for testing Drupal sites using [Behat](http://behat.org), [Mink Extension](https://github.com/Behat/MinkExtension). +## Drupal 7 + +Drupal 7 is supported using the `master` branch. If you're working on a Drupal 8 project, you need to switch to the `drupal8` branch. + ## What can I do with this? From d15f54b6cc5992ab6d738330036bed318c9906b0 Mon Sep 17 00:00:00 2001 From: Daniel Montgomery Date: Wed, 13 Feb 2019 14:52:51 -0600 Subject: [PATCH 3/8] Text with summary support. Also fail Text long if no matches are found. --- .../Context/EntityDataContext.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php b/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php index 600fbde..7ab5a91 100644 --- a/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php +++ b/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php @@ -686,6 +686,23 @@ public function assertEntityFieldValueLink($field, $value) }//end assertEntityFieldValueLink() + /** + * Test a text field for a partial string. + * + * @param \Drupal\Core\Field\FieldItemList $field + * A Drupal field object. + * @param mixed $value + * The value to look for. + * + * @throws \Exception when value was not found. + * + * @return void + */ + public function assertEntityFieldValueTextWithSummary($field, $value) + { + // Re-use the assertEntityFieldVallueTextLong for now. + return $this->assertEntityFieldValueTextLong($field, $value); + } /** * Test a text field for a partial string. @@ -722,6 +739,8 @@ public function assertEntityFieldValueTextLong($field, $value) return; } } + + throw new \Exception(sprintf('Field value of "%s" does not contain expected value "%s"', $field_value, $value)); }//end assertEntityFieldValueTextLong() From 3ea1d73d07f91c8d2e06232e643f6dbb24209b5d Mon Sep 17 00:00:00 2001 From: Martin Keereman Date: Tue, 10 Mar 2020 13:40:21 -0700 Subject: [PATCH 4/8] Add support for taxonomy terms --- .../Context/EntityDataContext.php | 28 +++++++- .../Context/SharedDrupalContext.php | 65 +++++++++++++++---- 2 files changed, 76 insertions(+), 17 deletions(-) diff --git a/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php b/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php index 7ab5a91..90af806 100644 --- a/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php +++ b/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php @@ -89,15 +89,15 @@ public function assertNodeByTitleAndLanguage($contentType, $title, $language) * * @When I examine the :termName term in the :vocabulary( vocabulary) * - * @param string $termName A Drupal taxonomy term name. + * @param string $termName A Drupal taxonomy term name. * @param string $vocabulary The machine name of a Drupal taxonomy vocabulary. * + * @throws \Exception + * * @return void */ public function assertTermByName($termName, $vocabulary) { - throw new NotUpdatedException('Method not yet updated for Drupal 8.'); - $term = $this->findTermByName($termName, $vocabulary); $this->currentEntity = $term; @@ -106,6 +106,28 @@ public function assertTermByName($termName, $vocabulary) }//end assertTermByName() + /** + * Verify field and property values of a taxonomy term entity. + * + * @When I examine the term with machine name :machineName in the :vocabulary( vocabulary) + * + * @param string $machineName A Drupal taxonomy term machine name. + * @param string $vocabulary The machine name of a Drupal taxonomy vocabulary. + * + * @throws \Exception + * + * @return void + */ + public function assertTermByMachineName($machineName, $vocabulary) + { + $term = $this->findTermByMachineName($machineName, $vocabulary); + + $this->currentEntity = $term; + $this->currentEntityType = 'taxonomy_term'; + + }//end assertTermByName() + + /** * Verify field and property values of a block entity. * diff --git a/src/Palantirnet/PalantirBehatExtension/Context/SharedDrupalContext.php b/src/Palantirnet/PalantirBehatExtension/Context/SharedDrupalContext.php index c88ab9a..7d715d3 100644 --- a/src/Palantirnet/PalantirBehatExtension/Context/SharedDrupalContext.php +++ b/src/Palantirnet/PalantirBehatExtension/Context/SharedDrupalContext.php @@ -124,31 +124,68 @@ protected function getNodeByTitle($contentType, $title) * @param string $termName A Drupal taxonomy term name. * @param string $vocabulary The machine name of a Drupal taxonomy vocabulary. * - * @return stdclass + * @return Drupal\taxonomy\TermInterface * The Drupal term object, if it exists. */ public function findTermByName($termName, $vocabulary) { - throw new NotUpdatedException('Method not yet updated for Drupal 8.'); - - $query = new \EntityFieldQuery(); + /** + * @var $query \Drupal\Core\Entity\Query\QueryInterface + */ + $query = \Drupal::entityQuery('taxonomy_term'); - $entities = $query->entityCondition('entity_type', 'taxonomy_term') - ->entityCondition('bundle', $vocabulary) - ->propertyCondition('name', $termName) + $entities = $query + ->condition('name', $termName) ->execute(); - if (empty($entities['taxonomy_term']) === false && count($entities['taxonomy_term']) === 1) { - $id = key($entities['taxonomy_term']); - return taxonomy_term_load($id); - } else if (empty($entities['taxonomy_term']) === false && count($entities['taxonomy_term']) > 1) { - throw new \Exception(sprintf('Found more than one "%s" term entitled "%s"', $vocabulary, $termName)); + if (count($entities) === 1) { + $term_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term'); + $tid = array_shift($entities); + + $term = $term_storage->load($tid); + + return $term; + } else if (count($entities) > 1) { + throw new \Exception(sprintf('Found more than one term with name "%s"', $termName)); } else { - throw new \Exception(sprintf('No "%s" term entitled "%s" exists', $vocabulary, $termName)); + throw new \Exception(sprintf('No term with name "%s" exists', $termName)); } - }//end findTermByName() + /** + * Get a term object by machine name and vocabulary. + * + * @param string $machineName A Drupal taxonomy term machine name. + * @param string $vocabulary The machine name of a Drupal taxonomy vocabulary. + * + * @return Drupal\taxonomy\TermInterface + * The Drupal term object, if it exists. + */ + public function findTermByMachineName($machineName, $vocabulary) + { + /** + * @var $query \Drupal\Core\Entity\Query\QueryInterface + */ + $query = \Drupal::entityQuery('taxonomy_term'); + + $entities = $query + ->condition('machine_name', $machineName) + ->execute(); + + if (count($entities) === 1) { + $term_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term'); + $tid = array_shift($entities); + + $term = $term_storage->load($tid); + + return $term; + } else if (count($entities) > 1) { + throw new \Exception(sprintf('Found more than one term with machine name "%s"', $machineName)); + } else { + throw new \Exception(sprintf('No term with machine name "%s" exists', $machineName)); + } + }//end findTermByMachineName() + /** * Get block object by its description. From a8585ff124f86b474d23d1c7929580b909017089 Mon Sep 17 00:00:00 2001 From: Martin Keereman Date: Tue, 10 Mar 2020 14:05:04 -0700 Subject: [PATCH 5/8] Filter by vocabulary --- .../PalantirBehatExtension/Context/SharedDrupalContext.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Palantirnet/PalantirBehatExtension/Context/SharedDrupalContext.php b/src/Palantirnet/PalantirBehatExtension/Context/SharedDrupalContext.php index 7d715d3..e9489e5 100644 --- a/src/Palantirnet/PalantirBehatExtension/Context/SharedDrupalContext.php +++ b/src/Palantirnet/PalantirBehatExtension/Context/SharedDrupalContext.php @@ -136,6 +136,7 @@ public function findTermByName($termName, $vocabulary) $entities = $query ->condition('name', $termName) + ->condition('vid', $vocabulary) ->execute(); if (count($entities) === 1) { @@ -170,6 +171,7 @@ public function findTermByMachineName($machineName, $vocabulary) $entities = $query ->condition('machine_name', $machineName) + ->condition('vid', $vocabulary) ->execute(); if (count($entities) === 1) { From d4c0a3dcd38e3af7801992b8469e90144daacd93 Mon Sep 17 00:00:00 2001 From: Jes Constantine Date: Fri, 13 Mar 2020 10:24:57 -0400 Subject: [PATCH 6/8] Update method for asserting Image / File field value by filename. Code cleanup --- .../Context/EntityDataContext.php | 48 +++++++++++++------ 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php b/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php index 90af806..5fbf26c 100644 --- a/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php +++ b/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php @@ -10,6 +10,7 @@ use Behat\Behat\Tester\Exception\PendingException; use Drupal\DrupalExtension\Context\RawDrupalContext; use Drupal\DrupalDriverManager; +use Drupal\file\FileInterface; use Palantirnet\PalantirBehatExtension\NotUpdatedException; /** @@ -767,9 +768,9 @@ public function assertEntityFieldValueTextLong($field, $value) /** - * Test a file field for a Drupal stream wrapper URI. + * Test a file field for a given filename. * - * @param \Drupal\Core\Field\FieldItemList $field A Drupal field name. + * @param \Drupal\Core\Field\FieldItemList $field A Drupal field item list. * @param mixed $value The value to look for. * * @throws \Exception @@ -778,25 +779,45 @@ public function assertEntityFieldValueTextLong($field, $value) */ public function assertEntityFieldValueFile($field, $value) { - throw new NotUpdatedException('Method not yet updated for Drupal 8.'); + // Initialize array to collect field referenced filename(s). + $filenames = []; - $wrapper = entity_metadata_wrapper($this->currentEntityType, $this->currentEntity); + // Get the referenced file id(s) so we can load file(s). + $property = 'target_id'; + $field_values = array_map(function ($field_value) use ($property) { + return $field_value[$property]; + }, $field->getValue()); - $field_value = $wrapper->$field->value(); + if (!empty($field_values)) { + foreach ($field_values as $field_value) { + /** + * @var $file \Drupal\file\FileInterface|NULL + */ + $file = file_load($field_value); + if ($file instanceof FileInterface) { + $filename = $file->getFilename(); - // Note that file field values are array('fid' => '...', ... ), - // which makes it somewhat hard to tell single values from multiple values. - if (isset($field_value['fid']) === true) { - $field_value = array($field_value); - } + if (strpos($filename, $value) !== false) { + return; + } + + $filenames[] = $filename; + } + } + + // Special case for expecting nothing. + if ($value === 'nothing') { + if (!empty($field_values)) { + throw new \Exception(sprintf('Field "%s" has file(s) "%s" and is not empty.', $field->getName(), json_encode($filenames))); + } - foreach ($field_value as $f) { - if (strpos($f['uri'], $value) !== false) { return; } + + throw new \Exception(sprintf('Field "%s" does not contain file with name "%s", has "%s" instead.', $field->getName(), $value, json_encode($filenames))); } - throw new \Exception(sprintf('Field "%s" does not contain file with URI "%s"', $field, $value)); + throw new \Exception('Field is empty.'); }//end assertEntityFieldValueFile() @@ -813,7 +834,6 @@ public function assertEntityFieldValueFile($field, $value) */ public function assertEntityFieldValueImage($field, $value) { - throw new NotUpdatedException('Method not yet updated for Drupal 8.'); $this->assertEntityFieldValueFile($field, $value); From e4482f04afc2e985059f8ffe39d1126d798eed4d Mon Sep 17 00:00:00 2001 From: Jes Constantine Date: Mon, 30 Mar 2020 15:47:46 -0400 Subject: [PATCH 7/8] #1452: Update test on link field to assert title of internal links --- .../Context/EntityDataContext.php | 63 ++++++++++++++++++- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php b/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php index 5fbf26c..2bb31bb 100644 --- a/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php +++ b/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php @@ -8,6 +8,8 @@ namespace Palantirnet\PalantirBehatExtension\Context; use Behat\Behat\Tester\Exception\PendingException; +use Drupal\Component\Utility\UrlHelper; +use Drupal\Core\Url; use Drupal\DrupalExtension\Context\RawDrupalContext; use Drupal\DrupalDriverManager; use Drupal\file\FileInterface; @@ -691,7 +693,11 @@ public function assertEntityFieldValueParagraph($field_name, $type) /** - * Test a link field for its URL value. + * Test a link field for its uri value. + * + * We first check for an external url test value to test against the the + * field value uri. If the test value is not an external url, we assume + * it is the label of an entity referenced by an internal uri. * * @param \Drupal\Core\Field\FieldItemList $field * A Drupal field object. @@ -704,11 +710,62 @@ public function assertEntityFieldValueParagraph($field_name, $type) */ public function assertEntityFieldValueLink($field, $value) { - // Check if the provided value matches any of the field values. - $this->assertEntityFieldHasPropertyValue($field, $value, 'uri'); + // Special case for expecting nothing. + if ($value === 'nothing') { + if (!empty($titles_from_field_values)) { + throw new \Exception(sprintf('Field "%s" has value of "%s" and does not contain "%s"', $field->getName(), json_encode($field->getValue()), $value)); + } + + return; + } + + // Determine if the value being tested is external or internal. + $is_value_external = UrlHelper::isExternal($value); + + if ($is_value_external) { + // Check if the provided uri matches any of the field values. + $this->assertEntityFieldHasPropertyValue($field, $value, 'uri'); + } + else { + // We assume that the value is a label from a referenced entity. + // Check if the provided test value matches any of the field value + // referenced entity labels. + $this->assertEntityLinkFieldInternalReferenceByTitle($field, $value); + } }//end assertEntityFieldValueLink() + /** + * Test a link field for its internal referenced entity label. + * + * @param \Drupal\Core\Field\FieldItemList $field + * A Drupal field object. + * @param mixed $value + * The value to look for. + * + * @throws \Exception when url was not found + * + * @return void + */ + public function assertEntityLinkFieldInternalReferenceByTitle($field, $value) { + // Determine if we have an internal link uri present as a field value. + $internal_uri_field_values = array_filter($field->getValue(), function ($field_value) { + return !UrlHelper::isExternal($field_value['uri']); + }); + + // Get titles of the referenced entities from internal uri field values. + $titles_from_field_values = array_map(function ($field_value) { + $params = Url::fromUri($field_value['uri'])->getRouteParameters(); + $entity_type = key($params); + $entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load($params[$entity_type]); + return $entity->label(); + }, $internal_uri_field_values); + + if (in_array($value, $titles_from_field_values) === false) { + throw new \Exception(sprintf('Field "%s" has a uri referencing entity(ies) with title(s) "%s" and does not contain "%s"', $field->getName(), json_encode($titles_from_field_values), $value)); + } + } // end assertEntityLinkFieldInternalReferenceByTitle() + /** * Test a text field for a partial string. * From 4197264775c063c500a586d9327d6ddf574aed4f Mon Sep 17 00:00:00 2001 From: Jes Constantine Date: Mon, 4 May 2020 13:15:39 -0400 Subject: [PATCH 8/8] #1499: Enable link options to be asserted via json_encoded strings --- .../Context/EntityDataContext.php | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php b/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php index 2bb31bb..b41ddef 100644 --- a/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php +++ b/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php @@ -531,6 +531,52 @@ public function assertEntityFieldPropertyValue($field_name, $value, $property) }//end assertEntityFieldPropertyValue() + /** + * Test a link field for a given property value. + * + * @param \Drupal\Core\Field\FieldItemList $field + * A Drupal field object. + * @param mixed $value + * The value to look for. + * + * @throws \Exception when url was not found + * + * @return void + */ + public function assertEntityFieldPropertyValueLink($field, $value, $property) + { + // Check if the provided value matches any of the field values - if no + // property is defined, use the default `value`. + $field_values = array_map(function ($field_value) use ($property) { + return $field_value[$property]; + }, $field->getValue()); + + // Special case for expecting nothing. + if ($value === 'nothing') { + if (!empty($field_values)) { + throw new \Exception(sprintf('Field "%s" has a "%s" of "%s" and does not contain "%s"', $field->getName(), $property, json_encode($field_values), $value)); + } + + return; + } + + // Special case for options. + if ($property == 'options') { + // Options should be passed in as json_encoded string. + $options_array = json_decode($value, TRUE); + if (in_array($options_array, $field_values) === false) { + throw new \Exception(sprintf('Field "%s" has "options" of "%s" and does not contain "%s"', $field->getName(), json_encode($field_values), $value)); + } + + return; + } + + if (in_array($value, $field_values) === false) { + throw new \Exception(sprintf('Field "%s" has a "%s" of "%s" and does not contain "%s"', $field->getName(), $property, json_encode($field_values), $value)); + } + + }//end assertEntityFieldPropertyValueLink() + /** * For a given field - and optional field property - check if a value is * present.