You are here

public function DynamicEntityReferenceSettingsTest::testConfigTargetBundleDeletion in Dynamic Entity Reference 8.2

Same name and namespace in other branches
  1. 8 tests/src/Kernel/DynamicEntityReferenceSettingsTest.php \Drupal\Tests\dynamic_entity_reference\Kernel\DynamicEntityReferenceSettingsTest::testConfigTargetBundleDeletion()

Tests that config bundle deletions are mirrored in field config settings.

File

tests/src/Kernel/DynamicEntityReferenceSettingsTest.php, line 83

Class

DynamicEntityReferenceSettingsTest
Tests dynamic entity reference field settings.

Namespace

Drupal\Tests\dynamic_entity_reference\Kernel

Code

public function testConfigTargetBundleDeletion() {

  // Attach an entity reference field to $this->nodeType.
  $name = mb_strtolower($this
    ->randomMachineName());
  $label = $this
    ->randomString();
  $vid = $this->vocabulary
    ->id();
  $handler_settings = [
    'target_bundles' => [
      $vid => $vid,
    ],
  ];

  // Create a field.
  FieldStorageConfig::create([
    'field_name' => $name,
    'type' => 'dynamic_entity_reference',
    'entity_type' => 'node',
    'cardinality' => 1,
    'settings' => [
      'exclude_entity_types' => FALSE,
      'entity_type_ids' => [
        'taxonomy_term',
      ],
    ],
  ])
    ->save();
  FieldConfig::create([
    'field_name' => $name,
    'entity_type' => 'node',
    'bundle' => $this->nodeType
      ->id(),
    'label' => $label,
    'settings' => [
      'taxonomy_term' => [
        'handler' => 'default:taxonomy_term',
        'handler_settings' => $handler_settings,
      ],
    ],
  ])
    ->save();

  // Check that the 'target_bundle' setting contains the vocabulary.
  $field_config = FieldConfig::loadByName('node', $this->nodeType
    ->id(), $name);
  $settings = $field_config
    ->getSettings();
  $actual_handler_settings = $settings['taxonomy_term']['handler_settings'];
  $this
    ->assertEquals($handler_settings, $actual_handler_settings);

  // Delete the vocabulary.
  $this->vocabulary
    ->delete();

  // Check that the deleted vocabulary is no longer present in the
  // 'target_bundles' field setting.
  $field_config = FieldConfig::loadByName('node', $this->nodeType
    ->id(), $name);
  $settings = $field_config
    ->getSettings();
  $handler_settings = $settings['taxonomy_term']['handler_settings'];
  $this
    ->assertTrue(empty($handler_settings['target_bundles']));
}