You are here

function FieldStorageCrudTest::testDelete in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field/src/Tests/FieldStorageCrudTest.php \Drupal\field\Tests\FieldStorageCrudTest::testDelete()

Test the deletion of a field storage.

File

core/modules/field/src/Tests/FieldStorageCrudTest.php, line 291
Contains \Drupal\field\Tests\FieldStorageCrudTest.

Class

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

Namespace

Drupal\field\Tests

Code

function testDelete() {

  // TODO: Also test deletion of the data stored in the field ?
  // Create two fields (so we can test that only one is deleted).
  $field_storage_definition = array(
    'field_name' => 'field_1',
    'type' => 'test_field',
    'entity_type' => 'entity_test',
  );
  entity_create('field_storage_config', $field_storage_definition)
    ->save();
  $another_field_storage_definition = array(
    'field_name' => 'field_2',
    'type' => 'test_field',
    'entity_type' => 'entity_test',
  );
  entity_create('field_storage_config', $another_field_storage_definition)
    ->save();

  // Create fields for each.
  $field_definition = array(
    'field_name' => $field_storage_definition['field_name'],
    'entity_type' => 'entity_test',
    'bundle' => 'entity_test',
  );
  entity_create('field_config', $field_definition)
    ->save();
  $another_field_definition = $field_definition;
  $another_field_definition['field_name'] = $another_field_storage_definition['field_name'];
  entity_create('field_config', $another_field_definition)
    ->save();

  // Test that the first field is not deleted, and then delete it.
  $field_storage = current(entity_load_multiple_by_properties('field_storage_config', array(
    '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 is marked as deleted when it is specifically
  // loaded.
  $field_storage = current(entity_load_multiple_by_properties('field_storage_config', array(
    'field_name' => $field_storage_definition['field_name'],
    'include_deleted' => TRUE,
  )));
  $this
    ->assertTrue($field_storage
    ->isDeleted(), 'A deleted storage is marked for deletion.');

  // Make sure that this field is marked as deleted when it is
  // specifically loaded.
  $field = current(entity_load_multiple_by_properties('field_config', array(
    'entity_type' => 'entity_test',
    'field_name' => $field_definition['field_name'],
    'bundle' => $field_definition['bundle'],
    'include_deleted' => TRUE,
  )));
  $this
    ->assertTrue($field
    ->isDeleted(), 'A field whose storage was deleted is marked for deletion.');

  // 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), 'A deleted storage is not loaded by default.');

  // 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), 'A field whose storage was deleted is not loaded by default.');

  // 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.
  entity_create('field_storage_config', $field_storage_definition)
    ->save();
  entity_create('field_config', $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 = entity_create('entity_test');
  $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");
  }
}