You are here

public function DynamicEntityReferenceSettingsTest::testCustomTargetBundleDeletion 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::testCustomTargetBundleDeletion()

Tests that deletions of custom bundles are mirrored in field settings.

File

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

Class

DynamicEntityReferenceSettingsTest
Tests dynamic entity reference field settings.

Namespace

Drupal\Tests\dynamic_entity_reference\Kernel

Code

public function testCustomTargetBundleDeletion() {

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

  // 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' => [
        'entity_test',
      ],
    ],
  ])
    ->save();
  FieldConfig::create([
    'field_name' => $name,
    'entity_type' => 'node',
    'bundle' => $this->nodeType
      ->id(),
    'label' => $label,
    'settings' => [
      'entity_test' => [
        'handler' => 'default:entity_test',
        'handler_settings' => $handler_settings,
      ],
    ],
  ])
    ->save();

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

  // Delete the custom bundle.
  entity_test_delete_bundle($this->customBundle, 'entity_test');

  // Check that the deleted bundle 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['entity_test']['handler_settings'];
  $this
    ->assertTrue(empty($handler_settings['target_bundles']));
}