function FieldStorageCrudTest::testDeleteNoData

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

Tests the deletion of a field storage.

File

core/modules/field/tests/src/Kernel/FieldStorageCrudTest.php, line 294

Class

FieldStorageCrudTest
Tests field storage create, read, update, and delete.

Namespace

Drupal\Tests\field\Kernel

Code

public function testDeleteNoData() {
    // Deleting and purging field storages with data is tested in
    // \Drupal\Tests\field\Kernel\BulkDeleteTest.
    // Create two fields (so we can test that only one is deleted).
    $field_storage_definition = [
        'field_name' => 'field_1',
        'type' => 'test_field',
        'entity_type' => 'entity_test',
    ];
    FieldStorageConfig::create($field_storage_definition)->save();
    $another_field_storage_definition = [
        'field_name' => 'field_2',
        'type' => 'test_field',
        'entity_type' => 'entity_test',
    ];
    FieldStorageConfig::create($another_field_storage_definition)->save();
    // Create fields for each.
    $field_definition = [
        'field_name' => $field_storage_definition['field_name'],
        'entity_type' => 'entity_test',
        'bundle' => 'entity_test',
    ];
    FieldConfig::create($field_definition)->save();
    $another_field_definition = $field_definition;
    $another_field_definition['field_name'] = $another_field_storage_definition['field_name'];
    FieldConfig::create($another_field_definition)->save();
    // Test that the first field is not deleted, and then delete it.
    $field_storage_config_storage = \Drupal::entityTypeManager()->getStorage('field_storage_config');
    $field_storage = current($field_storage_config_storage->loadByProperties([
        'field_name' => $field_storage_definition['field_name'],
        'include_deleted' => TRUE,
    ]));
    $this->assertFalse($field_storage->isDeleted());
    FieldStorageConfig::loadByName('entity_test', $field_storage_definition['field_name'])->delete();
    // Make sure that the field storage is deleted as it had no data.
    $field_storages = $field_storage_config_storage->loadByProperties([
        'field_name' => $field_storage_definition['field_name'],
        'include_deleted' => TRUE,
    ]);
    $this->assertCount(0, $field_storages, 'Field storage was deleted');
    // Make sure that this field is marked as deleted when it is
    // specifically loaded.
    $fields = \Drupal::entityTypeManager()->getStorage('field_config')
        ->loadByProperties([
        'entity_type' => 'entity_test',
        'field_name' => $field_definition['field_name'],
        'bundle' => $field_definition['bundle'],
        'include_deleted' => TRUE,
    ]);
    $this->assertCount(0, $fields, 'Field storage was deleted');
    // Try to load the storage normally and make sure it does not show up.
    $field_storage = FieldStorageConfig::load('entity_test.' . $field_storage_definition['field_name']);
    $this->assertEmpty($field_storage, 'Field storage was deleted');
    // Try to load the field normally and make sure it does not show up.
    $field = FieldConfig::load('entity_test.' . '.' . $field_definition['bundle'] . '.' . $field_definition['field_name']);
    $this->assertEmpty($field, 'Field was deleted');
    // Make sure the other field and its storage are not deleted.
    $another_field_storage = FieldStorageConfig::load('entity_test.' . $another_field_storage_definition['field_name']);
    $this->assertFalse($another_field_storage->isDeleted());
    $another_field = FieldConfig::load('entity_test.' . $another_field_definition['bundle'] . '.' . $another_field_definition['field_name']);
    $this->assertFalse($another_field->isDeleted());
    // Try to create a new field the same name as a deleted field and
    // write data into it.
    FieldStorageConfig::create($field_storage_definition)->save();
    FieldConfig::create($field_definition)->save();
    $field_storage = FieldStorageConfig::load('entity_test.' . $field_storage_definition['field_name']);
    $this->assertFalse($field_storage->isDeleted());
    $field = FieldConfig::load('entity_test.' . $field_definition['bundle'] . '.' . $field_definition['field_name']);
    $this->assertFalse($field->isDeleted());
    // Save an entity with data for the field
    $entity = EntityTest::create();
    $values[0]['value'] = mt_rand(1, 127);
    $entity->{$field_storage->getName()}->value = $values[0]['value'];
    $entity = $this->entitySaveReload($entity);
    // Verify the field is present on load
    $this->assertCount(1, $entity->{$field_storage->getName()}, "Data in previously deleted field saves and loads correctly");
    foreach ($values as $delta => $value) {
        $this->assertEquals($values[$delta]['value'], $entity->{$field_storage->getName()}[$delta]->value, "Data in previously deleted field saves and loads correctly");
    }
}

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