Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic entity support #12

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions behat.d7.drush.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
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.
*/
Expand Down
47 changes: 47 additions & 0 deletions behat.d8.drush.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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($entity_type, $entity);
$createdEntity = entity_create($entity_type, (array) $entity);
$createdEntity->save();

$entity->id = $createdEntity->id();

return (array) $entity;
}

/**
* Delete an entity.
*/
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.
*/
Expand Down