FieldInstanceCrudTestCase::testDeleteFieldInstance

  1. drupal
    1. 8
    2. 7
Versions
7 FieldInstanceCrudTestCase::testDeleteFieldInstance()

Test the deletion of a field instance.

Code

modules/field/tests/field.test, line 2569

<?php
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);
  $this->another_instance_definition = $this->instance_definition;
  $this->another_instance_definition['bundle'] .= '_another_bundle';
  $instance = field_create_instance($this->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']), t('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']), t('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), t('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', $this->another_instance_definition['field_name'], $this->another_instance_definition['bundle']);
  $this->assertTrue(!empty($another_instance) && empty($another_instance['deleted']), t('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->assertTrue(!empty($field['deleted']), t('A deleted field is marked for deletion after all its instances have been marked for deletion.'));
}
?>