function EntityReferenceAutoCreateTest::testMultipleTargetBundles

Same name in other branches
  1. 9 core/modules/field/tests/src/Functional/EntityReference/EntityReferenceAutoCreateTest.php \Drupal\Tests\field\Functional\EntityReference\EntityReferenceAutoCreateTest::testMultipleTargetBundles()
  2. 10 core/modules/field/tests/src/Functional/EntityReference/EntityReferenceAutoCreateTest.php \Drupal\Tests\field\Functional\EntityReference\EntityReferenceAutoCreateTest::testMultipleTargetBundles()
  3. 11.x core/modules/field/tests/src/Functional/EntityReference/EntityReferenceAutoCreateTest.php \Drupal\Tests\field\Functional\EntityReference\EntityReferenceAutoCreateTest::testMultipleTargetBundles()

Tests if an entity reference field having multiple target bundles is storing the auto-created entity in the right destination.

File

core/modules/field/tests/src/Functional/EntityReference/EntityReferenceAutoCreateTest.php, line 154

Class

EntityReferenceAutoCreateTest
Tests creating new entity (e.g. taxonomy-term) from an autocomplete widget.

Namespace

Drupal\Tests\field\Functional\EntityReference

Code

public function testMultipleTargetBundles() {
    
    /** @var \Drupal\taxonomy\Entity\Vocabulary[] $vocabularies */
    $vocabularies = [];
    for ($i = 0; $i < 2; $i++) {
        $vid = mb_strtolower($this->randomMachineName());
        $vocabularies[$i] = Vocabulary::create([
            'name' => $this->randomMachineName(),
            'vid' => $vid,
        ]);
        $vocabularies[$i]->save();
    }
    // Create a taxonomy term entity reference field that saves the auto-created
    // taxonomy terms in the second vocabulary from the two that were configured
    // as targets.
    $field_name = mb_strtolower($this->randomMachineName());
    $handler_settings = [
        'target_bundles' => [
            $vocabularies[0]->id() => $vocabularies[0]->id(),
            $vocabularies[1]->id() => $vocabularies[1]->id(),
        ],
        'auto_create' => TRUE,
        'auto_create_bundle' => $vocabularies[1]->id(),
    ];
    $this->createEntityReferenceField('node', $this->referencingType, $field_name, $this->randomString(), 'taxonomy_term', 'default', $handler_settings);
    
    /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $fd */
    \Drupal::service('entity_display.repository')->getFormDisplay('node', $this->referencingType)
        ->setComponent($field_name, [
        'type' => 'entity_reference_autocomplete',
    ])
        ->save();
    $term_name = $this->randomString();
    $edit = [
        $field_name . '[0][target_id]' => $term_name,
        'title[0][value]' => $this->randomString(),
    ];
    $this->drupalPostForm('node/add/' . $this->referencingType, $edit, 'Save');
    
    /** @var \Drupal\taxonomy\Entity\Term $term */
    $term = taxonomy_term_load_multiple_by_name($term_name);
    $term = reset($term);
    // The new term is expected to be stored in the second vocabulary.
    $this->assertEqual($vocabularies[1]->id(), $term->bundle());
    
    /** @var \Drupal\field\Entity\FieldConfig $field_config */
    $field_config = FieldConfig::loadByName('node', $this->referencingType, $field_name);
    $handler_settings = $field_config->getSetting('handler_settings');
    // Change the field setting to store the auto-created terms in the first
    // vocabulary and test again.
    $handler_settings['auto_create_bundle'] = $vocabularies[0]->id();
    $field_config->setSetting('handler_settings', $handler_settings);
    $field_config->save();
    $term_name = $this->randomString();
    $edit = [
        $field_name . '[0][target_id]' => $term_name,
        'title[0][value]' => $this->randomString(),
    ];
    $this->drupalPostForm('node/add/' . $this->referencingType, $edit, 'Save');
    
    /** @var \Drupal\taxonomy\Entity\Term $term */
    $term = taxonomy_term_load_multiple_by_name($term_name);
    $term = reset($term);
    // The second term is expected to be stored in the first vocabulary.
    $this->assertEqual($vocabularies[0]->id(), $term->bundle());
    // @todo Re-enable this test when WebTestBase::curlHeaderCallback() provides
    //   a way to catch and assert user-triggered errors.
    // Test the case when the field config settings are inconsistent.
    // unset($handler_settings['auto_create_bundle']);
    // $field_config->setSetting('handler_settings', $handler_settings);
    // $field_config->save();
    //
    // $this->drupalGet('node/add/' . $this->referencingType);
    // $error_message = sprintf(
    //  "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.",
    //  $field_config->getLabel(),
    //  $field_config->getName()
    // );
    // $this->assertErrorLogged($error_message);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.