1.20.x field.test FieldBulkDeleteTestCase::testPurgeField()

Verify that fields are preserved and purged correctly as multiple instances are deleted and purged.

File

modules/field/tests/field.test, line 3383
Tests for field.module.

Class

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

Code

function testPurgeField() {
  // Start recording hook invocations.
  field_test_memorize();

  $field = reset($this->fields);

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

  // Assert that hook_field_delete() was not called yet.
  $mem = field_test_memorize();
  $this->assertEqual(count($mem), 0, 'No field hooks were called.');

  // Purge the data.
  field_purge_batch(10);

  // Check hooks invocations.
  // - hook_field_load() (multiple hook) should have been called once, for all
  // entities in the bundle.
  // - hook_field_delete() should have been called once for each entity in the
  // bundle.
  $actual_hooks = field_test_memorize();
  $hooks = array();
  $entities = $this->convertToPartialEntities($this->entities_by_bundles[$bundle], $field['field_name']);
  $hooks['field_test_field_load'][] = $entities;
  $hooks['field_test_field_delete'] = $entities;
  $this->checkHooksInvocations($hooks, $actual_hooks);

  // Check that the instance has been deleted.
  $instances = field_read_instances(array('field_name' => $field['field_name'], 'bundle' => $bundle), array('include_deleted' => 1));
  $this->assertEqual(count($instances), 0, 'The first instance has been deleted.');

  // The field still exists, not deleted.
  $fields = field_read_fields(array('field_name' => $field['field_name']), array('include_deleted' => 1));
  $this->assertTrue(isset($fields[$field['field_name']]) && !$fields[$field['field_name']]['deleted'], 'The field exists and is not deleted because one instance still remains.');

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

  // One more purge to delete remaining data.
  field_purge_batch(10);

  // The field is gone.
  $fields = field_read_fields(array('field_name' => $field['field_name']), array('include_deleted' => 1, 'include_inactive' => 1));
  $this->assertEqual(count($fields), 0, 'The field is purged.');
}