You are here

function FieldCrudTest::testDeleteField in Zircon Profile 8

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

Test the deletion of a field.

File

core/modules/field/src/Tests/FieldCrudTest.php, line 217
Contains \Drupal\field\Tests\FieldCrudTest.

Class

FieldCrudTest
Create field entities by attaching fields to entities.

Namespace

Drupal\field\Tests

Code

function testDeleteField() {

  // TODO: Test deletion of the data stored in the field also.
  // Need to check that data for a 'deleted' field / storage doesn't get loaded
  // Need to check data marked deleted is cleaned on cron (not implemented yet...)
  // Create two fields for the same field storage so we can test that only one
  // is deleted.
  entity_create('field_config', $this->fieldDefinition)
    ->save();
  $another_field_definition = $this->fieldDefinition;
  $another_field_definition['bundle'] .= '_another_bundle';
  entity_test_create_bundle($another_field_definition['bundle']);
  entity_create('field_config', $another_field_definition)
    ->save();

  // Test that the first field is not deleted, and then delete it.
  $field = current(entity_load_multiple_by_properties('field_config', array(
    'entity_type' => 'entity_test',
    'field_name' => $this->fieldDefinition['field_name'],
    'bundle' => $this->fieldDefinition['bundle'],
    'include_deleted' => TRUE,
  )));
  $this
    ->assertTrue(!empty($field) && empty($field->deleted), 'A new field is not marked for deletion.');
  $field
    ->delete();

  // Make sure the 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' => $this->fieldDefinition['field_name'],
    'bundle' => $this->fieldDefinition['bundle'],
    'include_deleted' => TRUE,
  )));
  $this
    ->assertTrue($field
    ->isDeleted(), '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), 'A deleted field is not loaded by default.');

  // Make sure the other field is not deleted.
  $another_field = FieldConfig::load('entity_test.' . $another_field_definition['bundle'] . '.' . $another_field_definition['field_name']);
  $this
    ->assertTrue(!empty($another_field) && empty($another_field->deleted), 'A non-deleted field is not marked for deletion.');
}