public function DynamicEntityReferenceTest::testDynamicEntityReferenceAutoCreate in Dynamic Entity Reference 8
Same name and namespace in other branches
- 8.2 tests/src/Functional/DynamicEntityReferenceTest.php \Drupal\Tests\dynamic_entity_reference\Functional\DynamicEntityReferenceTest::testDynamicEntityReferenceAutoCreate()
Tests entity auto creation using dynamic entity reference.
File
- tests/
src/ Functional/ DynamicEntityReferenceTest.php, line 446
Class
- DynamicEntityReferenceTest
- Ensures that Dynamic Entity References field works correctly.
Namespace
Drupal\Tests\dynamic_entity_reference\FunctionalCode
public function testDynamicEntityReferenceAutoCreate() {
$assert_session = $this
->assertSession();
\Drupal::service('module_installer')
->install([
'taxonomy',
]);
$vocabulary = Vocabulary::create([
'name' => $this
->randomMachineName(),
'vid' => mb_strtolower($this
->randomMachineName()),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$vocabulary
->save();
$term = Term::create([
'name' => $this
->randomMachineName(),
'vid' => $vocabulary
->id(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$term
->save();
$this
->drupalLogin($this->adminUser);
// Add a new dynamic entity reference field.
$this
->drupalGet('entity_test/structure/entity_test/fields/add-field');
$edit = [
'label' => 'Foobar',
'field_name' => 'foobar',
'new_storage_type' => 'dynamic_entity_reference',
];
$this
->submitForm($edit, t('Save and continue'));
$this
->submitForm([
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
'settings[exclude_entity_types]' => FALSE,
'settings[entity_type_ids][]' => [
'taxonomy_term',
'user',
],
], t('Save field settings'));
$edit = [
'settings[taxonomy_term][handler_settings][target_bundles][' . $vocabulary
->id() . ']' => $vocabulary
->id(),
'settings[taxonomy_term][handler_settings][auto_create]' => TRUE,
];
$this
->submitForm($edit, t('Save settings'));
\Drupal::service('entity_field.manager')
->clearCachedFieldDefinitions();
$this
->drupalGet('entity_test/add');
// Add some extra dynamic entity reference fields.
$this
->getSession()
->getPage()
->findButton('Add another item')
->click();
$this
->getSession()
->getPage()
->findButton('Add another item')
->click();
$edit = [
'field_foobar[0][target_id]' => $this->adminUser
->label() . ' (' . $this->adminUser
->id() . ')',
'field_foobar[0][target_type]' => 'user',
// Add a non-existing term.
'field_foobar[1][target_id]' => 'tag',
'field_foobar[1][target_type]' => 'taxonomy_term',
'field_foobar[2][target_id]' => $term
->label() . ' (' . $term
->id() . ')',
'field_foobar[2][target_type]' => 'taxonomy_term',
'name[0][value]' => 'Barfoo',
'user_id[0][target_id]' => $this->adminUser
->label() . ' (' . $this->adminUser
->id() . ')',
];
$this
->submitForm($edit, t('Save'));
$entities = \Drupal::entityTypeManager()
->getStorage('entity_test')
->loadByProperties([
'name' => 'Barfoo',
]);
$this
->assertCount(1, $entities, 'Entity was saved');
$entity = reset($entities);
$this
->assertCount(3, $entity->field_foobar, 'Three items in field');
$this
->assertEquals($entity->field_foobar[0]->entity
->label(), $this->adminUser
->label());
$this
->assertEquals($entity->field_foobar[1]->entity
->label(), 'tag');
$this
->assertEquals($entity->field_foobar[2]->entity
->label(), $term
->label());
$this
->drupalGet('entity_test/' . $entity
->id());
$assert_session
->pageTextContains('Barfoo');
$assert_session
->pageTextContains($this->adminUser
->label());
$assert_session
->pageTextContains('tag');
$assert_session
->pageTextContains($term
->label());
}