1 field.test FieldInstanceCrudTestCase::testDeleteFieldInstance()

Test the deletion of a field instance.

File

core/modules/field/tests/field.test, line 2818
Tests for field.module.

Class

FieldInstanceCrudTestCase

Code

function testDeleteFieldInstance() {
  // TODO: Test deletion of the data stored in the field also.
  // Need to check that data for a 'deleted' field / instance doesn't get loaded
  // Need to check data marked deleted is cleaned on cron (not implemented yet...)

  // Create two instances for the same field so we can test that only one
  // is deleted.
  field_create_instance($this->instance_definition);
  $another_instance_definition = $this->instance_definition;
  $another_instance_definition['bundle'] .= '_another_bundle';
  field_create_instance($another_instance_definition);

  // Test that the first instance is not deleted, and then delete it.
  $instance = field_read_instance('test_entity', $this->instance_definition['field_name'], $this->instance_definition['bundle'], array('include_deleted' => TRUE));
  $this->assertTrue(!empty($instance) && empty($instance['deleted']), 'A new field instance is not marked for deletion.');
  field_delete_instance($instance);

  // Make sure the instance is marked as deleted when the instance is
  // specifically loaded.
  $instance = field_read_instance('test_entity', $this->instance_definition['field_name'], $this->instance_definition['bundle'], array('include_deleted' => TRUE));
  $this->assertTrue(!empty($instance['deleted']), 'A deleted field instance is marked for deletion.');

  // Try to load the instance normally and make sure it does not show up.
  $instance = field_read_instance('test_entity', $this->instance_definition['field_name'], $this->instance_definition['bundle']);
  $this->assertTrue(empty($instance), 'A deleted field instance is not loaded by default.');

  // Make sure the other field instance is not deleted.
  $another_instance = field_read_instance('test_entity', $another_instance_definition['field_name'], $another_instance_definition['bundle']);
  $this->assertTrue(!empty($another_instance) && empty($another_instance['deleted']), 'A non-deleted field instance is not marked for deletion.');

  // Make sure the field is deleted when its last instance is deleted.
  field_delete_instance($another_instance);
  $field = field_read_field($another_instance['field_name'], array('include_deleted' => TRUE));
  $this->assertFalse($field, 'A deleted field is deleted its instances have been deleted.');
}