function EntityReferenceAdminTest::testMultipleTargetBundles

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

Tests field settings for an entity reference field.

The tested entity reference field has multiple target bundles and is set to auto-create the target entity.

File

core/modules/field/tests/src/Functional/EntityReference/EntityReferenceAdminTest.php, line 307

Class

EntityReferenceAdminTest
Tests for the administrative UI.

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->randomString(),
            'vid' => $vid,
        ]);
        $vocabularies[$i]->save();
    }
    // Create a new field pointing to the first vocabulary.
    $field_name = $this->createEntityReferenceField('taxonomy_term', [
        $vocabularies[0]->id(),
    ]);
    $field_name = "field_{$field_name}";
    $field_id = 'node.' . $this->type . '.' . $field_name;
    $path = 'admin/structure/types/manage/' . $this->type . '/fields/' . $field_id;
    $this->drupalGet($path);
    // Expect that there's no 'auto_create_bundle' selected.
    $this->assertSession()
        ->fieldNotExists('settings[handler_settings][auto_create_bundle]');
    $edit = [
        'settings[handler_settings][target_bundles][' . $vocabularies[1]->id() . ']' => TRUE,
    ];
    // Enable the second vocabulary as a target bundle.
    $this->drupalGet($path);
    $this->submitForm($edit, 'Save settings');
    $this->drupalGet($path);
    // Expect a select element with the two vocabularies as options.
    $this->assertSession()
        ->optionExists('settings[handler_settings][auto_create_bundle]', $vocabularies[0]->id());
    $this->assertSession()
        ->optionExists('settings[handler_settings][auto_create_bundle]', $vocabularies[1]->id());
    $edit = [
        'settings[handler_settings][auto_create]' => TRUE,
        'settings[handler_settings][auto_create_bundle]' => $vocabularies[1]->id(),
    ];
    $this->submitForm($edit, 'Save settings');
    
    /** @var \Drupal\field\Entity\FieldConfig $field_config */
    $field_config = FieldConfig::load($field_id);
    // Expect that the target bundle has been saved in the backend.
    $this->assertEquals($vocabularies[1]->id(), $field_config->getSetting('handler_settings')['auto_create_bundle']);
    // Delete the other bundle. Field config should not be affected.
    $vocabularies[0]->delete();
    $field_config = FieldConfig::load($field_id);
    $this->assertTrue($field_config->getSetting('handler_settings')['auto_create']);
    $this->assertSame($vocabularies[1]->id(), $field_config->getSetting('handler_settings')['auto_create_bundle']);
    // Delete the bundle set for entity auto-creation. Auto-created settings
    // should be reset (no auto-creation).
    $vocabularies[1]->delete();
    $field_config = FieldConfig::load($field_id);
    $this->assertFalse($field_config->getSetting('handler_settings')['auto_create']);
    $this->assertFalse(isset($field_config->getSetting('handler_settings')['auto_create_bundle']));
}

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