You are here

public function EntityReferenceAdminTest::testMultipleTargetBundles in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/field/tests/src/Functional/EntityReference/EntityReferenceAdminTest.php \Drupal\Tests\field\Functional\EntityReference\EntityReferenceAdminTest::testMultipleTargetBundles()

Tests field settings for an entity reference field when the 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 296

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
    ->assertNoFieldByName('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
    ->drupalPostForm($path, $edit, 'Save settings');
  $this
    ->drupalGet($path);

  // Expect a select element with the two vocabularies as options.
  $this
    ->assertFieldByXPath("//select[@name='settings[handler_settings][auto_create_bundle]']/option[@value='" . $vocabularies[0]
    ->id() . "']");
  $this
    ->assertFieldByXPath("//select[@name='settings[handler_settings][auto_create_bundle]']/option[@value='" . $vocabularies[1]
    ->id() . "']");
  $edit = [
    'settings[handler_settings][auto_create]' => TRUE,
    'settings[handler_settings][auto_create_bundle]' => $vocabularies[1]
      ->id(),
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('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
    ->assertEqual($field_config
    ->getSetting('handler_settings')['auto_create_bundle'], $vocabularies[1]
    ->id());

  // 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
    ->assertIdentical($field_config
    ->getSetting('handler_settings')['auto_create_bundle'], $vocabularies[1]
    ->id());

  // 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
    ->assertSame(FALSE, $field_config
    ->getSetting('handler_settings')['auto_create']);
  $this
    ->assertFalse(isset($field_config
    ->getSetting('handler_settings')['auto_create_bundle']));
}