diff --git a/modules/chatbot_api_entities/chatbot_api_entities.links.menu.yml b/modules/chatbot_api_entities/chatbot_api_entities.links.menu.yml index 7ed69f9..2ba4ddf 100644 --- a/modules/chatbot_api_entities/chatbot_api_entities.links.menu.yml +++ b/modules/chatbot_api_entities/chatbot_api_entities.links.menu.yml @@ -1,5 +1,5 @@ entity.chatbot_api_entities_collection.collection: - title: 'Entity collection' + title: 'Entity collections' route_name: entity.chatbot_api_entities_collection.collection description: 'Chatbot API entity collections' parent: system.admin_config_services diff --git a/modules/chatbot_api_entities/chatbot_api_entities.permissions.yml b/modules/chatbot_api_entities/chatbot_api_entities.permissions.yml index e69de29..a1f9fcb 100644 --- a/modules/chatbot_api_entities/chatbot_api_entities.permissions.yml +++ b/modules/chatbot_api_entities/chatbot_api_entities.permissions.yml @@ -0,0 +1,4 @@ +administer chatbot api entities: + title: 'Administer Chatbot API entities' + description: 'Configure sending of entity information to remote Chatbot APIs' + restrict access: true diff --git a/modules/chatbot_api_entities/src/Entity/EntityCollection.php b/modules/chatbot_api_entities/src/Entity/EntityCollection.php index fdb15f0..9fdd4e2 100644 --- a/modules/chatbot_api_entities/src/Entity/EntityCollection.php +++ b/modules/chatbot_api_entities/src/Entity/EntityCollection.php @@ -20,30 +20,25 @@ use Drupal\Core\Plugin\DefaultLazyPluginCollection; * "form" = { * "add" = "Drupal\chatbot_api_entities\Form\EntityCollectionForm", * "edit" = "Drupal\chatbot_api_entities\Form\EntityCollectionForm", - * "delete" = - * "Drupal\chatbot_api_entities\Form\EntityCollectionDeleteForm" + * "delete" = "Drupal\chatbot_api_entities\Form\EntityCollectionDeleteForm" * }, * "route_provider" = { * "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider", * }, * }, * config_prefix = "chatbot_api_entities_collection", - * admin_permission = "administer site configuration", + * admin_permission = "administer chatbot api entities", * entity_keys = { * "id" = "id", * "label" = "label", * "uuid" = "uuid" * }, * links = { - * "canonical" = - * "/admin/config/service/chatbot_api_entities_collection/{chatbot_api_entities_collection}", - * "add-form" = - * "/admin/config/service/chatbot_api_entities_collection/add", - * "edit-form" = - * "/admin/config/service/chatbot_api_entities_collection/{chatbot_api_entities_collection}/edit", - * "delete-form" = - * "/admin/config/service/chatbot_api_entities_collection/{chatbot_api_entities_collection}/delete", - * "collection" = "/admin/config/service/chatbot_api_entities_collection" + * "canonical" = "/admin/config/service/chatbot-api-entity-collection/{chatbot_api_entities_collection}", + * "add-form" = "/admin/config/service/chatbot-api-entity-collection/add", + * "edit-form" = "/admin/config/service/chatbot-api-entity-collection/{chatbot_api_entities_collection}/edit", + * "delete-form" = "/admin/config/service/chatbot-api-entity-collection/{chatbot_api_entities_collection}/delete", + * "collection" = "/admin/config/service/chatbot-api-entity-collection" * }, * config_export = { * "label", @@ -98,14 +93,14 @@ class EntityCollection extends ConfigEntityBase implements EntityCollectionInter * * @var [] */ - protected $query_handlers; + protected $query_handlers = []; /** * Push handler configurations. * * @var [] */ - protected $push_handlers; + protected $push_handlers = []; /** * Query plugin collection. @@ -166,7 +161,7 @@ class EntityCollection extends ConfigEntityBase implements EntityCollectionInter $entities = []; /** @var \Drupal\chatbot_api_entities\Plugin\QueryHandlerInterface $plugin */ foreach ($this->queryHandlerCollection as $plugin) { - $plugin->query($entityTypeManager, $entities, $this); + $entities = $plugin->query($entityTypeManager, $entities, $this); } if (!$entities) { // Nothing matched. @@ -208,4 +203,44 @@ class EntityCollection extends ConfigEntityBase implements EntityCollectionInter return $this->bundle; } + /** + * {@inheritdoc} + */ + public function save() { + $configuration = $this->pushHandlerCollection->getConfiguration(); + /** @var \Drupal\chatbot_api_entities\Plugin\PushHandlerInterface $plugin */ + foreach ($this->pushHandlerCollection as $instance_id => $plugin) { + $instance_configuration = []; + if (isset($configuration[$instance_id])) { + $instance_configuration = $configuration[$instance_id]; + } + $instance_configuration = $plugin->saveConfiguration($this, $instance_configuration); + $this->setPushHandlerConfiguration($instance_id, $instance_configuration); + } + $return = parent::save(); + return $return; + } + + /** + * {@inheritdoc} + */ + public function setQueryHandlerConfiguration($instance_id, array $configuration) { + $this->query_handlers[$instance_id] = $configuration; + if (isset($this->queryHandlerCollection)) { + $this->queryHandlerCollection->setInstanceConfiguration($instance_id, $configuration); + } + return $this; + } + + /** + * {@inheritdoc} + */ + public function setPushHandlerConfiguration($instance_id, array $configuration) { + $this->push_handlers[$instance_id] = $configuration; + if (isset($this->pushHandlerCollection)) { + $this->pushHandlerCollection->setInstanceConfiguration($instance_id, $configuration); + } + return $this; + } + } diff --git a/modules/chatbot_api_entities/src/Entity/EntityCollectionInterface.php b/modules/chatbot_api_entities/src/Entity/EntityCollectionInterface.php index 131cf1b..611b554 100644 --- a/modules/chatbot_api_entities/src/Entity/EntityCollectionInterface.php +++ b/modules/chatbot_api_entities/src/Entity/EntityCollectionInterface.php @@ -49,4 +49,28 @@ interface EntityCollectionInterface extends ConfigEntityInterface { */ public function getCollectionBundle(); + /** + * Sets configuration for handler. + * + * @param string $instance_id + * Handler instance. + * @param array $configuration + * Configuration. + * + * @return $this + */ + public function setQueryHandlerConfiguration($instance_id, array $configuration); + + /** + * Sets configuration for handler. + * + * @param string $instance_id + * Handler instance. + * @param array $configuration + * Configuration. + * + * @return $this + */ + public function setPushHandlerConfiguration($instance_id, array $configuration); + } diff --git a/modules/chatbot_api_entities/src/Form/EntityCollectionForm.php b/modules/chatbot_api_entities/src/Form/EntityCollectionForm.php index b1822b4..8066f14 100644 --- a/modules/chatbot_api_entities/src/Form/EntityCollectionForm.php +++ b/modules/chatbot_api_entities/src/Form/EntityCollectionForm.php @@ -2,8 +2,17 @@ namespace Drupal\chatbot_api_entities\Form; +use Drupal\chatbot_api_entities\Plugin\PushHandlerManager; +use Drupal\chatbot_api_entities\Plugin\QueryHandlerManager; +use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityForm; +use Drupal\Core\Entity\EntityType; +use Drupal\Core\Entity\EntityTypeBundleInfoInterface; +use Drupal\Core\Entity\EntityTypeInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Class EntityCollectionForm. @@ -11,31 +20,95 @@ use Drupal\Core\Form\FormStateInterface; class EntityCollectionForm extends EntityForm { /** + * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface + */ + private $entityTypeBundleInfo; + + /** + * @var \Drupal\Core\Entity\EntityFieldManagerInterface + */ + private $entityFieldManager; + + /** + * @var \Drupal\chatbot_api_entities\Plugin\PushHandlerManager + */ + private $pushHandlerManager; + + /** + * @var \Drupal\chatbot_api_entities\Plugin\QueryHandlerManager + */ + private $queryHandlerManager; + + /** + * Constructs a new EntityCollectionForm object. + * + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager + * Entity type manager. + * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entityTypeBundleInfo + * Entity bundle info. + * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager + * Field manager. + * @param \Drupal\chatbot_api_entities\Plugin\PushHandlerManager $pushHandlerManager + * Push handler. + * @param \Drupal\chatbot_api_entities\Plugin\QueryHandlerManager $queryHandlerManager + * Query handler. + */ + public function __construct(EntityTypeManagerInterface $entityTypeManager, EntityTypeBundleInfoInterface $entityTypeBundleInfo, EntityFieldManagerInterface $entityFieldManager, PushHandlerManager $pushHandlerManager, QueryHandlerManager $queryHandlerManager) { + $this->entityTypeManager = $entityTypeManager; + $this->entityTypeBundleInfo = $entityTypeBundleInfo; + $this->entityFieldManager = $entityFieldManager; + $this->pushHandlerManager = $pushHandlerManager; + $this->queryHandlerManager = $queryHandlerManager; + } + + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity_type.manager'), + $container->get('entity_type.bundle.info'), + $container->get('entity_field.manager'), + $container->get('plugin.manager.chatbot_api_entities_push_handler'), + $container->get('plugin.manager.chatbot_api_entities_query_handler') + ); + } + + /** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); - $chatbot_api_entities_collection = $this->entity; + /** @var \Drupal\chatbot_api_entities\Entity\EntityCollectionInterface $collection */ + $collection = $this->entity; $form['label'] = [ '#type' => 'textfield', '#title' => $this->t('Label'), '#maxlength' => 255, - '#default_value' => $chatbot_api_entities_collection->label(), + '#default_value' => $collection->label(), '#description' => $this->t("Label for the Entity collection."), '#required' => TRUE, ]; $form['id'] = [ '#type' => 'machine_name', - '#default_value' => $chatbot_api_entities_collection->id(), + '#default_value' => $collection->id(), '#machine_name' => [ 'exists' => '\Drupal\chatbot_api_entities\Entity\EntityCollection::load', ], - '#disabled' => !$chatbot_api_entities_collection->isNew(), + '#disabled' => !$collection->isNew(), ]; - /* You will need additional form elements for your custom properties. */ + $entityTypes = $this->entityTypeManager->getDefinitions(); + $form['entity_type'] = [ + '#type' => 'select', + '#options' => array_combine(array_keys($entityTypes), array_map(function (EntityTypeInterface $entityType) { + return array_map(function (array $bundle_info) { + return $bundle_info['label']; + }, $this->entityTypeBundleInfo->getBundleInfo($entityType->id())); + }, $entityTypes)), + '#title' => $this->t('Entity Type'), + '#description' => $this->t('Choose the entity type for the entities to comprise this collection.'), + '#default_value' => $collection->getCollectionEntityTypeId(), + ]; return $form; } diff --git a/modules/chatbot_api_entities/src/Plugin/ChatbotApiEntities/QueryHandler/DefaultEntity.php b/modules/chatbot_api_entities/src/Plugin/ChatbotApiEntities/QueryHandler/DefaultEntity.php index c3f343c..90b2ed5 100644 --- a/modules/chatbot_api_entities/src/Plugin/ChatbotApiEntities/QueryHandler/DefaultEntity.php +++ b/modules/chatbot_api_entities/src/Plugin/ChatbotApiEntities/QueryHandler/DefaultEntity.php @@ -33,7 +33,7 @@ class DefaultEntity extends QueryHandlerBase { $entity_type_id = $collection->getCollectionEntityTypeId(); $entity_type = $entityTypeManager->getDefinition($entity_type_id); $query = $this->getQuery($entityTypeManager, $collection, $entity_type); - return $existing + $entityTypeManager->getStorage($entity_type_id)->loadMultiple($query->execute); + return $existing + $entityTypeManager->getStorage($entity_type_id)->loadMultiple($query->execute()); } /** diff --git a/modules/chatbot_api_entities/src/Plugin/PushHandlerBase.php b/modules/chatbot_api_entities/src/Plugin/PushHandlerBase.php index 5a5f38c..65efe5b 100644 --- a/modules/chatbot_api_entities/src/Plugin/PushHandlerBase.php +++ b/modules/chatbot_api_entities/src/Plugin/PushHandlerBase.php @@ -3,6 +3,7 @@ namespace Drupal\chatbot_api_entities\Plugin; use Drupal\chatbot_api_entities\Entity\EntityCollection; +use Drupal\chatbot_api_entities\Entity\EntityCollectionInterface; use Drupal\Component\Plugin\PluginBase; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use GuzzleHttp\ClientInterface; @@ -71,4 +72,18 @@ abstract class PushHandlerBase extends PluginBase implements PushHandlerInterfac return $formatted; } + /** + * {@inheritdoc} + */ + public function saveConfiguration(EntityCollectionInterface $entityCollection, array $configuration) { + return $configuration; + } + + /** + * {@inheritdoc} + */ + public function isEnabled() { + return !empty($this->configuration['status']); + } + } diff --git a/modules/chatbot_api_entities/src/Plugin/PushHandlerInterface.php b/modules/chatbot_api_entities/src/Plugin/PushHandlerInterface.php index e2856d3..c0202d1 100644 --- a/modules/chatbot_api_entities/src/Plugin/PushHandlerInterface.php +++ b/modules/chatbot_api_entities/src/Plugin/PushHandlerInterface.php @@ -3,6 +3,7 @@ namespace Drupal\chatbot_api_entities\Plugin; use Drupal\chatbot_api_entities\Entity\EntityCollection; +use Drupal\chatbot_api_entities\Entity\EntityCollectionInterface; use Drupal\Component\Plugin\PluginInspectionInterface; /** @@ -22,4 +23,25 @@ interface PushHandlerInterface extends PluginInspectionInterface { */ public function pushEntities(array $entities, EntityCollection $entityCollection); + /** + * Gives the plugin a chance to modify its configuration. + * + * @param \Drupal\chatbot_api_entities\Entity\EntityCollectionInterface $entityCollection + * Collection being saved. + * @param array $configuration + * Existing configuration. + * + * @return array + * Updated configuration. + */ + public function saveConfiguration(EntityCollectionInterface $entityCollection, array $configuration); + + /** + * Check if plugin is enabled. + * + * @return bool + * TRUE if enabled. + */ + public function isEnabled(); + } diff --git a/modules/chatbot_api_entities/src/Plugin/QueryHandlerBase.php b/modules/chatbot_api_entities/src/Plugin/QueryHandlerBase.php index d3da23a..c11df8e 100644 --- a/modules/chatbot_api_entities/src/Plugin/QueryHandlerBase.php +++ b/modules/chatbot_api_entities/src/Plugin/QueryHandlerBase.php @@ -9,4 +9,11 @@ use Drupal\Component\Plugin\PluginBase; */ abstract class QueryHandlerBase extends PluginBase implements QueryHandlerInterface { + /** + * {@inheritdoc} + */ + public function isEnabled() { + return !empty($this->configuration['status']); + } + } diff --git a/modules/chatbot_api_entities/src/Plugin/QueryHandlerInterface.php b/modules/chatbot_api_entities/src/Plugin/QueryHandlerInterface.php index d289e34..b6b80c2 100644 --- a/modules/chatbot_api_entities/src/Plugin/QueryHandlerInterface.php +++ b/modules/chatbot_api_entities/src/Plugin/QueryHandlerInterface.php @@ -37,4 +37,12 @@ interface QueryHandlerInterface extends PluginInspectionInterface { */ public function applies($entity_type_id); + /** + * Check if plugin is enabled. + * + * @return bool + * TRUE if enabled. + */ + public function isEnabled(); + } diff --git a/modules/chatbot_api_entities/tests/modules/chatbot_api_entities_test/config/schema/chatbot_api_entities_test.schema.yml b/modules/chatbot_api_entities/tests/modules/chatbot_api_entities_test/config/schema/chatbot_api_entities_test.schema.yml index a9fa1de..7f0aed3 100644 --- a/modules/chatbot_api_entities/tests/modules/chatbot_api_entities_test/config/schema/chatbot_api_entities_test.schema.yml +++ b/modules/chatbot_api_entities/tests/modules/chatbot_api_entities_test/config/schema/chatbot_api_entities_test.schema.yml @@ -4,3 +4,6 @@ chatbot_api_entities_push_handler_settings.chatbot_api_entities_test: remote_id: type: string label: 'Remote ID' + added_at_save_time: + type: string + label: 'Added at save time' diff --git a/modules/chatbot_api_entities/tests/modules/chatbot_api_entities_test/src/Plugin/ChatbotApiEntities/PushHandler/ChatbotApiEntitiesTestHandler.php b/modules/chatbot_api_entities/tests/modules/chatbot_api_entities_test/src/Plugin/ChatbotApiEntities/PushHandler/ChatbotApiEntitiesTestHandler.php index c8f36eb..64f7f68 100644 --- a/modules/chatbot_api_entities/tests/modules/chatbot_api_entities_test/src/Plugin/ChatbotApiEntities/PushHandler/ChatbotApiEntitiesTestHandler.php +++ b/modules/chatbot_api_entities/tests/modules/chatbot_api_entities_test/src/Plugin/ChatbotApiEntities/PushHandler/ChatbotApiEntitiesTestHandler.php @@ -3,6 +3,7 @@ namespace Drupal\chatbot_api_entities_test\Plugin\ChatbotApiEntities\PushHandler; use Drupal\chatbot_api_entities\Entity\EntityCollection; +use Drupal\chatbot_api_entities\Entity\EntityCollectionInterface; use Drupal\chatbot_api_entities\Plugin\PushHandlerBase; use Drupal\Core\State\StateInterface; use GuzzleHttp\ClientInterface; @@ -60,11 +61,19 @@ class ChatbotApiEntitiesTestHandler extends PushHandlerBase { * {@inheritdoc} */ public function pushEntities(array $entities, EntityCollection $entityCollection) { - $remote_id = $this->configuration['remote_id']; + $remote_id = $this->configuration['settings']['remote_id']; $stored = $this->state->get(self::STATE_KEY, []); $stored[$remote_id] = $this->formatEntries($entities, $entityCollection); $this->state->set(self::STATE_KEY, $stored); return $this; } + /** + * {@inheritdoc} + */ + public function saveConfiguration(EntityCollectionInterface $entityCollection, array $configuration) { + $configuration['settings']['added_at_save_time'] = $entityCollection->id(); + return parent::saveConfiguration($entityCollection, $configuration); + } + } diff --git a/modules/chatbot_api_entities/tests/src/Functional/ChatbotApiEntitiesFunctionalTest.php b/modules/chatbot_api_entities/tests/src/Functional/ChatbotApiEntitiesFunctionalTest.php index 9cbeca9..192d107 100644 --- a/modules/chatbot_api_entities/tests/src/Functional/ChatbotApiEntitiesFunctionalTest.php +++ b/modules/chatbot_api_entities/tests/src/Functional/ChatbotApiEntitiesFunctionalTest.php @@ -1,5 +1,90 @@ setupEntityTestBundle(); + $this->placeBlock('local_actions_block'); + } + + /** + * Tests admin UI. + */ + public function testAdminUI() { + $assert = $this->assertSession(); + $collectionUrl = Url::fromRoute('entity.chatbot_api_entities_collection.collection'); + $this->drupalGet($collectionUrl); + $assert->statusCodeEquals(403); + $admin = $this->createUser(['administer chatbot api entities']); + $this->drupalLogin($admin); + $this->drupalGet($collectionUrl); + $assert->statusCodeEquals(200); + $assert->pageTextContains('Entity collection'); + $this->clickLink('Add collection'); + $assert->statusCodeEquals(200); + $label = 'Send entity test some bundle'; + $id = 'entity_test_some_bundle'; + $this->submitForm([ + 'id' => $id, + 'label' => $label, + 'entity_type' => 'entity_test:some_bundle', + 'synonyms' => 'field_synonyms', + 'enabled_query_handlers[default:entity_test]' => 1, + 'enabled_push_handlers[chatbot_api_entities_test]' => 1, + 'push_handler_configuration[chatbot_api_entities_test][remote_id]' => 'Foobar', + ], 'Save'); + $assert->pageTextContains(t('Created the %label collection.', [ + '%label' => $label, + ])); + $this->cronRun(); + $config = EntityCollection::load($id); + $this->assertNotEmpty($config); + $asArray = $config->toArray(); + $this->assertEquals([ + 'chatbot_api_entities_test' => [ + 'settings' => ['remote_id' => 'Foobar'], + 'id' => 'chatbot_api_entities_test', + ], + ], $asArray['push_handlers']); + $this->assertEquals([ + 'default:entity_test' => [ + 'id' => 'default:entity_test', + ], + ], $asArray['query_handlers']); + } } diff --git a/modules/chatbot_api_entities/tests/src/Kernel/ChatbotApiEntityIntegrationTest.php b/modules/chatbot_api_entities/tests/src/Kernel/ChatbotApiEntityIntegrationTest.php index 50dce18..2e23de1 100644 --- a/modules/chatbot_api_entities/tests/src/Kernel/ChatbotApiEntityIntegrationTest.php +++ b/modules/chatbot_api_entities/tests/src/Kernel/ChatbotApiEntityIntegrationTest.php @@ -9,6 +9,7 @@ use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; use Drupal\KernelTests\KernelTestBase; use Drupal\chatbot_api_entities\Entity\EntityCollection; +use Drupal\Tests\chatbot_api_entities\Traits\ChatbotApiEntitiesTestTrait; /** * Tests chatbot api integration with entity hooks. @@ -17,6 +18,8 @@ use Drupal\chatbot_api_entities\Entity\EntityCollection; */ class ChatbotApiEntityIntegrationTest extends KernelTestBase { + use ChatbotApiEntitiesTestTrait; + /** * {@inheritdoc} */ @@ -24,7 +27,7 @@ class ChatbotApiEntityIntegrationTest extends KernelTestBase { 'entity_test', 'chatbot_api', 'chatbot_api_entities', - ChatbotApiEntitiesTestHandler::STATE_KEY, + 'chatbot_api_entities_test', 'field', 'text', 'system', @@ -38,6 +41,7 @@ class ChatbotApiEntityIntegrationTest extends KernelTestBase { parent::setUp(); $this->installEntitySchema('user'); $this->installSchema('system', ['sequences']); + $this->installEntitySchema('entity_test'); $this->setupEntityTestBundle(); } @@ -52,18 +56,20 @@ class ChatbotApiEntityIntegrationTest extends KernelTestBase { 'bundle' => 'some_bundle', 'synonyms' => 'field_synonyms', 'query_handlers' => [ - ['id' => 'default:entity_test'], + 'default:entity_test' => [ + 'id' => 'default:entity_test', + ], ], 'push_handlers' => [ - [ + ChatbotApiEntitiesTestHandler::STATE_KEY => [ + 'settings' => ['remote_id' => 'Foobar'], 'id' => ChatbotApiEntitiesTestHandler::STATE_KEY, - 'settings' => [ - 'remote_id' => 'Foobar', - ], ], ], ]); $config->save(); + $asArray = $config->toArray(); + $this->assertEquals('entity_test_some_bundle', $asArray['push_handlers'][ChatbotApiEntitiesTestHandler::STATE_KEY]['settings']['added_at_save_time']); // Create first entity. $entity_test = EntityTest::create([ @@ -144,37 +150,5 @@ class ChatbotApiEntityIntegrationTest extends KernelTestBase { ], $sent); } - /** - * Creates a new entity test bundle and adds synonyms field. - */ - protected function setupEntityTestBundle() { - $this->installEntitySchema('entity_test'); - $field_name = 'field_synonyms'; - $entity_type = 'entity_test'; - $bundle = 'some_bundle'; - entity_test_create_bundle($bundle); - FieldStorageConfig::create([ - 'field_name' => $field_name, - 'entity_type' => $entity_type, - 'type' => 'text', - 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, - ])->save(); - - $field_config = FieldConfig::create([ - 'field_name' => $field_name, - 'label' => $field_name, - 'entity_type' => $entity_type, - 'bundle' => $bundle, - 'required' => FALSE, - ]); - $field_config->save(); - } - - /** - * Runs cron. - */ - protected function cronRun() { - $this->container->get('cron')->run(); - } } diff --git a/modules/chatbot_api_entities/tests/src/Traits/ChatbotApiEntitiesTestTrait.php b/modules/chatbot_api_entities/tests/src/Traits/ChatbotApiEntitiesTestTrait.php index c75432a..81d2b26 100644 --- a/modules/chatbot_api_entities/tests/src/Traits/ChatbotApiEntitiesTestTrait.php +++ b/modules/chatbot_api_entities/tests/src/Traits/ChatbotApiEntitiesTestTrait.php @@ -2,7 +2,45 @@ namespace Drupal\Tests\chatbot_api_entities\Traits; +use Drupal\Core\Field\FieldStorageDefinitionInterface; +use Drupal\field\Entity\FieldConfig; +use Drupal\field\Entity\FieldStorageConfig; -class ChatbotApiEntitiesTestTrait { +/** + * Defines a trait for common test functionality. + */ +trait ChatbotApiEntitiesTestTrait { + + /** + * Creates a new entity test bundle and adds synonyms field. + */ + protected function setupEntityTestBundle() { + $field_name = 'field_synonyms'; + $entity_type = 'entity_test'; + $bundle = 'some_bundle'; + entity_test_create_bundle($bundle); + FieldStorageConfig::create([ + 'field_name' => $field_name, + 'entity_type' => $entity_type, + 'type' => 'text', + 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, + ])->save(); + + $field_config = FieldConfig::create([ + 'field_name' => $field_name, + 'label' => $field_name, + 'entity_type' => $entity_type, + 'bundle' => $bundle, + 'required' => FALSE, + ]); + $field_config->save(); + } + + /** + * Runs cron. + */ + protected function cronRun() { + $this->container->get('cron')->run(); + } } diff --git a/modules/chatbot_api_entities/tests/todo.txt b/modules/chatbot_api_entities/tests/todo.txt index 40a85ff..1f9dbfe 100644 --- a/modules/chatbot_api_entities/tests/todo.txt +++ b/modules/chatbot_api_entities/tests/todo.txt @@ -1,4 +1,2 @@ - config dependencies - config form -- api so that save notifies each plugin before saving -- api ai handler needs to post on new entity so that id can be set