You are here

public function FieldAttachStorageTest::testEntityDeleteBundle in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/field/tests/src/Kernel/FieldAttachStorageTest.php \Drupal\Tests\field\Kernel\FieldAttachStorageTest::testEntityDeleteBundle()
  2. 9 core/modules/field/tests/src/Kernel/FieldAttachStorageTest.php \Drupal\Tests\field\Kernel\FieldAttachStorageTest::testEntityDeleteBundle()

Tests entity_bundle_delete().

File

core/modules/field/tests/src/Kernel/FieldAttachStorageTest.php, line 316

Class

FieldAttachStorageTest
Tests storage-related Field Attach API functions.

Namespace

Drupal\Tests\field\Kernel

Code

public function testEntityDeleteBundle() {
  $entity_type = 'entity_test_rev';
  $this
    ->createFieldWithStorage('', $entity_type);

  // Create a new bundle.
  $new_bundle = 'test_bundle_' . mb_strtolower($this
    ->randomMachineName());
  entity_test_create_bundle($new_bundle, NULL, $entity_type);

  // Add a field to that bundle.
  $this->fieldTestData->field_definition['bundle'] = $new_bundle;
  FieldConfig::create($this->fieldTestData->field_definition)
    ->save();

  // Create a second field for the test bundle
  $field_name = mb_strtolower($this
    ->randomMachineName() . '_field_name');
  $field_storage = [
    'field_name' => $field_name,
    'entity_type' => $entity_type,
    'type' => 'test_field',
    'cardinality' => 1,
  ];
  FieldStorageConfig::create($field_storage)
    ->save();
  $field = [
    'field_name' => $field_name,
    'entity_type' => $entity_type,
    'bundle' => $this->fieldTestData->field
      ->getTargetBundle(),
    'label' => $this
      ->randomMachineName() . '_label',
    'description' => $this
      ->randomMachineName() . '_description',
    'weight' => mt_rand(0, 127),
  ];
  FieldConfig::create($field)
    ->save();

  // Save an entity with data for both fields
  $entity = $this->container
    ->get('entity_type.manager')
    ->getStorage($entity_type)
    ->create([
    'type' => $this->fieldTestData->field
      ->getTargetBundle(),
  ]);
  $values = $this
    ->_generateTestFieldValues($this->fieldTestData->field_storage
    ->getCardinality());
  $entity->{$this->fieldTestData->field_name} = $values;
  $entity->{$field_name} = $this
    ->_generateTestFieldValues(1);
  $entity = $this
    ->entitySaveReload($entity);

  // Verify the fields are present on load
  $this
    ->assertCount(4, $entity->{$this->fieldTestData->field_name}, 'First field got loaded');
  $this
    ->assertCount(1, $entity->{$field_name}, 'Second field got loaded');

  // Delete the bundle.
  entity_test_delete_bundle($this->fieldTestData->field
    ->getTargetBundle(), $entity_type);

  // Verify no data gets loaded
  $controller = $this->container
    ->get('entity_type.manager')
    ->getStorage($entity
    ->getEntityTypeId());
  $controller
    ->resetCache();
  $entity = $controller
    ->load($entity
    ->id());
  $this
    ->assertEmpty($entity->{$this->fieldTestData->field_name}, 'No data for first field');
  $this
    ->assertEmpty($entity->{$field_name}, 'No data for second field');

  // Verify that the fields are gone.
  $this
    ->assertNull(FieldConfig::load('entity_test.' . $this->fieldTestData->field
    ->getTargetBundle() . '.' . $this->fieldTestData->field_name), "First field is deleted");
  $this
    ->assertNull(FieldConfig::load('entity_test.' . $field['bundle'] . '.' . $field_name), "Second field is deleted");
}