1 field.test FieldCrudTestCase::testDeleteField()

Test the deletion of a field.

File

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

Class

FieldCrudTestCase

Code

function testDeleteField() {
  // TODO: Also test deletion of the data stored in the field ?

  // Create two fields (so we can test that only one is deleted).
  $this->field = array('field_name' => 'field_1', 'type' => 'test_field');
  field_create_field($this->field);
  $another_field = array('field_name' => 'field_2', 'type' => 'test_field');
  field_create_field($another_field);

  // Create instances for each.
  $instance_definition = array(
    'field_name' => $this->field['field_name'],
    'entity_type' => 'test_entity',
    'bundle' => 'test_bundle',
    'widget' => array(
      'type' => 'test_field_widget',
    ),
  );
  field_create_instance($instance_definition);
  $another_instance_definition = $instance_definition;
  $another_instance_definition['field_name'] = $another_field['field_name'];
  field_create_instance($another_instance_definition);

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

  // Fields that have no data will be purged entirely.
  $field = field_read_field($this->field['field_name'], array('include_deleted' => TRUE));
  $this->assertFalse($field, 'A deleted field is completely purged when it has no data.');

  // The instances of that field should be purged entirely as well.
  $instance = field_read_instance('test_entity', $instance_definition['field_name'], $instance_definition['bundle'], array('include_deleted' => TRUE));
  $this->assertFalse($instance, 'An instance for a deleted field is completely purged when it has no data.');

  // Re-create the field and attempt again with data in the fields.
  field_create_field($this->field);
  field_create_instance($instance_definition);

  // Add an entry.
  $entity = field_test_create_entity(1, 1, $instance_definition['bundle']);
  $entity->{$this->field['field_name']}[LANGUAGE_NONE][0]['value'] = 1;
  field_attach_insert('test_entity', $entity);

  // Delete the field again.
  field_delete_field($this->field['field_name']);

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

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

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

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

  // Make sure the other field (and its field instance) are not deleted.
  $another_field = field_read_field($another_field['field_name']);
  $this->assertTrue(!empty($another_field) && empty($another_field['deleted']), 'A non-deleted field is not marked for deletion.');
  $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']), 'An instance of a non-deleted field is not marked for deletion.');

  // Run a purge to delete the piece of content and check if the field is
  // deleted entirely.
  field_purge_batch(1);

  $field = field_read_field($this->field['field_name'], array('include_deleted' => TRUE));
  $this->assertFalse($field, 'Field is deleted after a batch run.');
}