3 namespace Drupal\Tests\field\Functional\EntityReference;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Field\FieldStorageDefinitionInterface;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\Tests\BrowserTestBase;
9 use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
10 use Drupal\taxonomy\Entity\Vocabulary;
11 use Drupal\node\Entity\Node;
12 use Drupal\field\Entity\FieldStorageConfig;
15 * Tests creating new entity (e.g. taxonomy-term) from an autocomplete widget.
17 * @group entity_reference
19 class EntityReferenceAutoCreateTest extends BrowserTestBase {
21 use EntityReferenceTestTrait;
23 public static $modules = ['node', 'taxonomy'];
26 * The name of a content type that will reference $referencedType.
30 protected $referencingType;
33 * The name of a content type that will be referenced by $referencingType.
37 protected $referencedType;
39 protected function setUp() {
42 // Create "referencing" and "referenced" node types.
43 $referencing = $this->drupalCreateContentType();
44 $this->referencingType = $referencing->id();
46 $referenced = $this->drupalCreateContentType();
47 $this->referencedType = $referenced->id();
49 FieldStorageConfig::create([
50 'field_name' => 'test_field',
51 'entity_type' => 'node',
52 'translatable' => FALSE,
55 'target_type' => 'node',
57 'type' => 'entity_reference',
58 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
62 'label' => 'Entity reference field',
63 'field_name' => 'test_field',
64 'entity_type' => 'node',
65 'bundle' => $referencing->id(),
67 'handler' => 'default',
68 'handler_settings' => [
69 // Reference a single vocabulary.
73 // Enable auto-create.
74 'auto_create' => TRUE,
79 entity_get_display('node', $referencing->id(), 'default')
80 ->setComponent('test_field')
82 entity_get_form_display('node', $referencing->id(), 'default')
83 ->setComponent('test_field', [
84 'type' => 'entity_reference_autocomplete',
88 $account = $this->drupalCreateUser(['access content', "create $this->referencingType content"]);
89 $this->drupalLogin($account);
93 * Tests that the autocomplete input element appears and the creation of a new
96 public function testAutoCreate() {
97 $this->drupalGet('node/add/' . $this->referencingType);
98 $this->assertFieldByXPath('//input[@id="edit-test-field-0-target-id" and contains(@class, "form-autocomplete")]', NULL, 'The autocomplete input element appears.');
100 $new_title = $this->randomMachineName();
102 // Assert referenced node does not exist.
103 $base_query = \Drupal::entityQuery('node');
105 ->condition('type', $this->referencedType)
106 ->condition('title', $new_title);
108 $query = clone $base_query;
109 $result = $query->execute();
110 $this->assertFalse($result, 'Referenced node does not exist yet.');
113 'title[0][value]' => $this->randomMachineName(),
114 'test_field[0][target_id]' => $new_title,
116 $this->drupalPostForm("node/add/$this->referencingType", $edit, 'Save');
118 // Assert referenced node was created.
119 $query = clone $base_query;
120 $result = $query->execute();
121 $this->assertTrue($result, 'Referenced node was created.');
122 $referenced_nid = key($result);
123 $referenced_node = Node::load($referenced_nid);
125 // Assert the referenced node is associated with referencing node.
126 $result = \Drupal::entityQuery('node')
127 ->condition('type', $this->referencingType)
130 $referencing_nid = key($result);
131 $referencing_node = Node::load($referencing_nid);
132 $this->assertEqual($referenced_nid, $referencing_node->test_field->target_id, 'Newly created node is referenced from the referencing node.');
134 // Now try to view the node and check that the referenced node is shown.
135 $this->drupalGet('node/' . $referencing_node->id());
136 $this->assertText($referencing_node->label(), 'Referencing node label found.');
137 $this->assertText($referenced_node->label(), 'Referenced node label found.');
141 * Tests if an entity reference field having multiple target bundles is
142 * storing the auto-created entity in the right destination.
144 public function testMultipleTargetBundles() {
145 /** @var \Drupal\taxonomy\Entity\Vocabulary[] $vocabularies */
147 for ($i = 0; $i < 2; $i++) {
148 $vid = Unicode::strtolower($this->randomMachineName());
149 $vocabularies[$i] = Vocabulary::create([
150 'name' => $this->randomMachineName(),
153 $vocabularies[$i]->save();
156 // Create a taxonomy term entity reference field that saves the auto-created
157 // taxonomy terms in the second vocabulary from the two that were configured
159 $field_name = Unicode::strtolower($this->randomMachineName());
160 $handler_settings = [
161 'target_bundles' => [
162 $vocabularies[0]->id() => $vocabularies[0]->id(),
163 $vocabularies[1]->id() => $vocabularies[1]->id(),
165 'auto_create' => TRUE,
166 'auto_create_bundle' => $vocabularies[1]->id(),
168 $this->createEntityReferenceField('node', $this->referencingType, $field_name, $this->randomString(), 'taxonomy_term', 'default', $handler_settings);
169 /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $fd */
170 entity_get_form_display('node', $this->referencingType, 'default')
171 ->setComponent($field_name, ['type' => 'entity_reference_autocomplete'])
174 $term_name = $this->randomString();
176 $field_name . '[0][target_id]' => $term_name,
177 'title[0][value]' => $this->randomString(),
180 $this->drupalPostForm('node/add/' . $this->referencingType, $edit, 'Save');
181 /** @var \Drupal\taxonomy\Entity\Term $term */
182 $term = taxonomy_term_load_multiple_by_name($term_name);
183 $term = reset($term);
185 // The new term is expected to be stored in the second vocabulary.
186 $this->assertEqual($vocabularies[1]->id(), $term->bundle());
188 /** @var \Drupal\field\Entity\FieldConfig $field_config */
189 $field_config = FieldConfig::loadByName('node', $this->referencingType, $field_name);
190 $handler_settings = $field_config->getSetting('handler_settings');
192 // Change the field setting to store the auto-created terms in the first
193 // vocabulary and test again.
194 $handler_settings['auto_create_bundle'] = $vocabularies[0]->id();
195 $field_config->setSetting('handler_settings', $handler_settings);
196 $field_config->save();
198 $term_name = $this->randomString();
200 $field_name . '[0][target_id]' => $term_name,
201 'title[0][value]' => $this->randomString(),
204 $this->drupalPostForm('node/add/' . $this->referencingType, $edit, 'Save');
205 /** @var \Drupal\taxonomy\Entity\Term $term */
206 $term = taxonomy_term_load_multiple_by_name($term_name);
207 $term = reset($term);
209 // The second term is expected to be stored in the first vocabulary.
210 $this->assertEqual($vocabularies[0]->id(), $term->bundle());
212 // @todo Re-enable this test when WebTestBase::curlHeaderCallback() provides
213 // a way to catch and assert user-triggered errors.
215 // Test the case when the field config settings are inconsistent.
216 //unset($handler_settings['auto_create_bundle']);
217 //$field_config->setSetting('handler_settings', $handler_settings);
218 //$field_config->save();
220 //$this->drupalGet('node/add/' . $this->referencingType);
221 //$error_message = sprintf(
222 // "Create referenced entities if they don't already exist option is enabled but a specific destination bundle is not set. You should re-visit and fix the settings of the '%s' (%s) field.",
223 // $field_config->getLabel(),
224 // $field_config->getName()
226 //$this->assertErrorLogged($error_message);