You are here

public function FieldCrudTest::testDeleteFieldCrossDeletion in Drupal 8

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

Tests the cross deletion behavior between field storages and fields.

File

core/modules/field/tests/src/Kernel/FieldCrudTest.php, line 307

Class

FieldCrudTest
Create field entities by attaching fields to entities.

Namespace

Drupal\Tests\field\Kernel

Code

public function testDeleteFieldCrossDeletion() {
  $field_definition_2 = $this->fieldDefinition;
  $field_definition_2['bundle'] .= '_another_bundle';
  entity_test_create_bundle($field_definition_2['bundle']);

  // Check that deletion of a field storage deletes its fields.
  $field_storage = $this->fieldStorage;
  FieldConfig::create($this->fieldDefinition)
    ->save();
  FieldConfig::create($field_definition_2)
    ->save();
  $field_storage
    ->delete();
  $this
    ->assertNull(FieldConfig::loadByName('entity_test', $this->fieldDefinition['bundle'], $field_storage
    ->getName()));
  $this
    ->assertNull(FieldConfig::loadByName('entity_test', $field_definition_2['bundle'], $field_storage
    ->getName()));

  // Check that deletion of the last field deletes the storage.
  $field_storage = FieldStorageConfig::create($this->fieldStorageDefinition);
  $field_storage
    ->save();
  $field = FieldConfig::create($this->fieldDefinition);
  $field
    ->save();
  $field_2 = FieldConfig::create($field_definition_2);
  $field_2
    ->save();
  $field
    ->delete();
  $this
    ->assertNotEmpty(FieldStorageConfig::loadByName('entity_test', $field_storage
    ->getName()));
  $field_2
    ->delete();
  $this
    ->assertNull(FieldStorageConfig::loadByName('entity_test', $field_storage
    ->getName()));

  // Check that deletion of all fields using a storage simultaneously deletes
  // the storage.
  $field_storage = FieldStorageConfig::create($this->fieldStorageDefinition);
  $field_storage
    ->save();
  $field = FieldConfig::create($this->fieldDefinition);
  $field
    ->save();
  $field_2 = FieldConfig::create($field_definition_2);
  $field_2
    ->save();
  $this->container
    ->get('entity_type.manager')
    ->getStorage('field_config')
    ->delete([
    $field,
    $field_2,
  ]);
  $this
    ->assertNull(FieldStorageConfig::loadByName('entity_test', $field_storage
    ->getName()));
}