From 502d987759675ce3610014890af74233708cd9dd Mon Sep 17 00:00:00 2001 From: Andrew Rosborough Date: Sun, 27 May 2018 13:17:48 -0400 Subject: [PATCH 1/4] Add generic support for entities. --- behat.d8.drush.inc | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/behat.d8.drush.inc b/behat.d8.drush.inc index 697fd2a..d1aad88 100644 --- a/behat.d8.drush.inc +++ b/behat.d8.drush.inc @@ -10,6 +10,7 @@ use Drupal\node\Entity\Node; use Drupal\node\NodeInterface; use Drupal\taxonomy\Entity\Term; use Drupal\taxonomy\TermInterface; +use Drupal\Core\Entity\ContentEntityInterface; include __DIR__ . '/behat-drush-common.inc'; @@ -70,6 +71,52 @@ function drush_behat($operation, $json_data) { } } +/** + * Create an entity. + */ +function drush_behat_op_create_entity($settings) { + $entity_type = $settings['entity_type']; + $entity = $settings['entity']; + + // If the bundle field is empty, put the inferred bundle value in it + $bundle_key = \Drupal::entityManager()->getDefinition($entity_type)->getKey('bundle'); + if (!isset($entity->$bundle_key) && isset($entity->step_bundle)) $entity->$bundle_key = $entity->step_bundle; + + // Throw an exception if a bundle is specified but does not exist. + if (isset($entity->$bundle_key) && ($entity->$bundle_key !== NULL)) { + $bundles = \Drupal::entityManager()->getBundleInfo($entity_type); + + if (!in_array($entity->$bundle_key, array_keys($bundles))) { + throw new \Exception("Cannot create entity because provided bundle '$entity->$bundle_key' does not exist."); + } + } + if (empty($entity_type)) { + throw new \Exception("You must specify an entity type to create an entity."); + } + + // Attempt to decipher any fields that may be specified. + _drush_behat_expand_entity_fields('node', $node); + $createdEntity = entity_create($entity_type, (array) $entity); + $createdEntity->save(); + + $entity->id = $createdEntity->id(); + + return (array) $entity; +} + +/** + * Delete an entity. + */ +public function drush_behat_op_delete_entity($settings) { + $entity_type = $settings['entity_type']; + $entity = $settings['entity']; + + $entity = $entity instanceof ContentEntityInterface ? $entity : entity_load($entity_type, $entity->id); + if ($entity instanceof ContentEntityInterface) { + $entity->delete(); + } +} + /** * Create a node. */ From 8da0a55c7bc6bf53bcfbc21efc002f41aede78fd Mon Sep 17 00:00:00 2001 From: Andrew Rosborough Date: Sun, 27 May 2018 14:48:08 -0400 Subject: [PATCH 2/4] Add Drupal 7 exceptions. --- behat.d7.drush.inc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/behat.d7.drush.inc b/behat.d7.drush.inc index 374c409..036d171 100644 --- a/behat.d7.drush.inc +++ b/behat.d7.drush.inc @@ -71,6 +71,22 @@ function drush_behat($operation, $json_data) { } } +/** + * Create an entity. + */ +function drush_behat_op_create_entity($settings) { + // @todo: create a D7 version of this function + throw new \Exception('Creation of entities via the generic Entity API is not yet implemented for Drupal 7.'); +} + +/** + * Delete an entity. + */ +public function drush_behat_op_delete_entity($settings) { + // @todo: create a D7 version of this function + throw new \Exception('Deletion of entities via the generic Entity API is not yet implemented for Drupal 7.'); +} + /** * Create a node. */ From a6dcddde07ef439e1d0a713a8cc6c8b9caca32f2 Mon Sep 17 00:00:00 2001 From: Andrew Rosborough Date: Sun, 27 May 2018 14:54:24 -0400 Subject: [PATCH 3/4] Fix syntax errors in D7 an D8 includes. --- behat.d7.drush.inc | 2 +- behat.d8.drush.inc | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/behat.d7.drush.inc b/behat.d7.drush.inc index 036d171..ad7c374 100644 --- a/behat.d7.drush.inc +++ b/behat.d7.drush.inc @@ -82,7 +82,7 @@ function drush_behat_op_create_entity($settings) { /** * Delete an entity. */ -public function drush_behat_op_delete_entity($settings) { +function drush_behat_op_delete_entity($settings) { // @todo: create a D7 version of this function throw new \Exception('Deletion of entities via the generic Entity API is not yet implemented for Drupal 7.'); } diff --git a/behat.d8.drush.inc b/behat.d8.drush.inc index d1aad88..43dc00f 100644 --- a/behat.d8.drush.inc +++ b/behat.d8.drush.inc @@ -95,7 +95,7 @@ function drush_behat_op_create_entity($settings) { } // Attempt to decipher any fields that may be specified. - _drush_behat_expand_entity_fields('node', $node); + _drush_behat_expand_entity_fields($entity_type, $entity); $createdEntity = entity_create($entity_type, (array) $entity); $createdEntity->save(); @@ -107,7 +107,7 @@ function drush_behat_op_create_entity($settings) { /** * Delete an entity. */ -public function drush_behat_op_delete_entity($settings) { +function drush_behat_op_delete_entity($settings) { $entity_type = $settings['entity_type']; $entity = $settings['entity']; From 753cb500b9999ab17822351eaf32a5907ef685f5 Mon Sep 17 00:00:00 2001 From: Andrew Rosborough Date: Sun, 27 May 2018 16:52:35 -0400 Subject: [PATCH 4/4] Fix settings access syntax. --- behat.d8.drush.inc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/behat.d8.drush.inc b/behat.d8.drush.inc index 43dc00f..95bc9a9 100644 --- a/behat.d8.drush.inc +++ b/behat.d8.drush.inc @@ -75,8 +75,8 @@ function drush_behat($operation, $json_data) { * Create an entity. */ function drush_behat_op_create_entity($settings) { - $entity_type = $settings['entity_type']; - $entity = $settings['entity']; + $entity_type = $settings->entity_type; + $entity = $settings->entity; // If the bundle field is empty, put the inferred bundle value in it $bundle_key = \Drupal::entityManager()->getDefinition($entity_type)->getKey('bundle'); @@ -108,8 +108,8 @@ function drush_behat_op_create_entity($settings) { * Delete an entity. */ function drush_behat_op_delete_entity($settings) { - $entity_type = $settings['entity_type']; - $entity = $settings['entity']; + $entity_type = $settings->entity_type; + $entity = $settings->entity; $entity = $entity instanceof ContentEntityInterface ? $entity : entity_load($entity_type, $entity->id); if ($entity instanceof ContentEntityInterface) {