function FieldAttachStorageTest::testEntityDeleteBundle
Same name in other branches
- 9 core/modules/field/tests/src/Kernel/FieldAttachStorageTest.php \Drupal\Tests\field\Kernel\FieldAttachStorageTest::testEntityDeleteBundle()
- 8.9.x core/modules/field/tests/src/Kernel/FieldAttachStorageTest.php \Drupal\Tests\field\Kernel\FieldAttachStorageTest::testEntityDeleteBundle()
- 11.x 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 322
Class
- FieldAttachStorageTest
- Tests storage-related Field Attach API functions.
Namespace
Drupal\Tests\field\KernelCode
public function testEntityDeleteBundle() : void {
$entity_type = 'entity_test_rev';
$this->createFieldWithStorage('', $entity_type);
// Create a new bundle.
$new_bundle = 'test_bundle_' . $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 = $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. The form display has to be deleted first to prevent
// schema errors when fields attached to the deleted bundle are themselves
// deleted, which triggers an update of the form display.
$this->container
->get('entity_display.repository')
->getFormDisplay($entity_type, $this->fieldTestData->field
->getTargetBundle())
->delete();
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");
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.