function FieldCrudTest::testDeleteFieldNoData

Same name in other branches
  1. 8.9.x core/modules/field/tests/src/Kernel/FieldCrudTest.php \Drupal\Tests\field\Kernel\FieldCrudTest::testDeleteFieldNoData()
  2. 10 core/modules/field/tests/src/Kernel/FieldCrudTest.php \Drupal\Tests\field\Kernel\FieldCrudTest::testDeleteFieldNoData()
  3. 11.x core/modules/field/tests/src/Kernel/FieldCrudTest.php \Drupal\Tests\field\Kernel\FieldCrudTest::testDeleteFieldNoData()

Tests the deletion of a field with no data.

File

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

Class

FieldCrudTest
Create field entities by attaching fields to entities.

Namespace

Drupal\Tests\field\Kernel

Code

public function testDeleteFieldNoData() {
    // Deleting and purging fields with data is tested in
    // \Drupal\Tests\field\Kernel\BulkDeleteTest.
    // Create two fields for the same field storage so we can test that only one
    // is deleted.
    FieldConfig::create($this->fieldDefinition)
        ->save();
    $another_field_definition = $this->fieldDefinition;
    $another_field_definition['bundle'] .= '_another_bundle';
    entity_test_create_bundle($another_field_definition['bundle']);
    FieldConfig::create($another_field_definition)->save();
    // Test that the first field is not deleted, and then delete it.
    $field = current(\Drupal::entityTypeManager()->getStorage('field_config')
        ->loadByProperties([
        'entity_type' => 'entity_test',
        'field_name' => $this->fieldDefinition['field_name'],
        'bundle' => $this->fieldDefinition['bundle'],
        'include_deleted' => TRUE,
    ]));
    $this->assertFalse($field->isDeleted());
    $field->delete();
    // Make sure the field was deleted without being marked for purging as there
    // was no data.
    $fields = \Drupal::entityTypeManager()->getStorage('field_config')
        ->loadByProperties([
        'entity_type' => 'entity_test',
        'field_name' => $this->fieldDefinition['field_name'],
        'bundle' => $this->fieldDefinition['bundle'],
        'include_deleted' => TRUE,
    ]);
    $this->assertCount(0, $fields, 'A deleted field is marked for deletion.');
    // Try to load the field normally and make sure it does not show up.
    $field = FieldConfig::load('entity_test.' . '.' . $this->fieldDefinition['bundle'] . '.' . $this->fieldDefinition['field_name']);
    $this->assertTrue(empty($field), 'Field was deleted');
    // Make sure the other field is not deleted.
    $another_field = FieldConfig::load('entity_test.' . $another_field_definition['bundle'] . '.' . $another_field_definition['field_name']);
    $this->assertFalse($another_field->isDeleted());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.