function FieldStorageCrudTest::testDeleteNoData

Same name and namespace in other branches
  1. 9 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()

Test the deletion of a field storage.

File

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

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->assertTrue(!empty($field_storage) && !$field_storage->isDeleted(), 'A new storage is not marked for deletion.');
    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->assertTrue(empty($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->assertTrue(empty($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->assertTrue(!empty($another_field_storage) && !$another_field_storage->isDeleted(), 'A non-deleted storage is not marked for deletion.');
    $another_field = FieldConfig::load('entity_test.' . $another_field_definition['bundle'] . '.' . $another_field_definition['field_name']);
    $this->assertTrue(!empty($another_field) && !$another_field->isDeleted(), 'A field whose storage was not deleted is not marked for deletion.');
    // 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->assertTrue(!empty($field_storage) && !$field_storage->isDeleted(), 'A new storage with a previously used name is created.');
    $field = FieldConfig::load('entity_test.' . $field_definition['bundle'] . '.' . $field_definition['field_name']);
    $this->assertTrue(!empty($field) && !$field->isDeleted(), 'A new field for a previously used field name is created.');
    // 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->assertIdentical(count($entity->{$field_storage->getName()}), count($values), "Data in previously deleted field saves and loads correctly");
    foreach ($values as $delta => $value) {
        $this->assertEqual($entity->{$field_storage->getName()}[$delta]->value, $values[$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.