1 field.test FieldBulkDeleteTestCase::testDeleteFieldInstance()

Verify that deleting an instance leaves the field data items in the database and that the appropriate Field API functions can operate on the deleted data and instance.

This tests how EntityFieldQuery interacts with field_delete_instance() and could be moved to FieldCrudTestCase, but depends on this class's setUp().

File

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

Class

FieldBulkDeleteTestCase
Unit test class for field bulk delete and batch purge functionality.

Code

function testDeleteFieldInstance() {
  $bundle = reset($this->bundles);
  $field = reset($this->fields);

  // There are 10 entities of this bundle.
  $query = new EntityFieldQuery();
  $found = $query
  ->fieldCondition($field)
    ->entityCondition('bundle', $bundle)
    ->execute();
  $this->assertEqual(count($found['test_entity']), 10, 'Correct number of entities found before deleting');

  // Delete the instance.
  $instance = field_info_instance($this->entity_type, $field['field_name'], $bundle);
  field_delete_instance($instance);

  // The instance still exists, deleted.
  $instances = field_read_instances(array('field_name' => $field['field_name'], 'deleted' => 1), array('include_deleted' => 1, 'include_inactive' => 1));
  $this->assertEqual(count($instances), 1, 'There is one deleted instance');
  $this->assertEqual($instances[0]['bundle'], $bundle, 'The deleted instance is for the correct bundle');

  // There are 0 entities of this bundle with non-deleted data.
  $query = new EntityFieldQuery();
  $found = $query
  ->fieldCondition($field)
    ->entityCondition('bundle', $bundle)
    ->execute();
  $this->assertTrue(!isset($found['test_entity']), 'No entities found after deleting');

  // There are 10 entities of this bundle when deleted fields are allowed, and
  // their values are correct.
  $query = new EntityFieldQuery();
  $found = $query
  ->fieldCondition($field)
    ->entityCondition('bundle', $bundle)
    ->deleted(TRUE)
    ->execute();
  $entities = array();
  foreach ($found[$this->entity_type] as $ids) {
    $entities[$ids->entity_id] = _field_create_entity_from_ids($ids);
  }
  field_attach_load($this->entity_type, $entities, FIELD_LOAD_CURRENT, array('field_name' => $field['field_name'], 'deleted' => 1));
  $this->assertEqual(count($found['test_entity']), 10, 'Correct number of entities found after deleting');
  foreach ($entities as $id => $entity) {
    $this->assertEqual($this->entities[$id]->{$field['field_name']}, $entity->{$field['field_name']}, "Entity $id with deleted data loaded correctly");
  }
}