You are here

function FieldBulkDeleteTestCase::testPurgeField in Drupal 7

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

File

modules/field/tests/field.test, line 3671
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();
  $stubs = $this
    ->_generateStubEntities($this->entity_type, $this->entities_by_bundles[$bundle], $field['field_name']);
  $hooks['field_test_field_load'][] = $stubs;
  foreach ($stubs as $stub) {
    $hooks['field_test_field_delete'][] = $stub;
  }
  $this
    ->checkHooksInvocations($hooks, $actual_hooks);

  // Purge again to purge the instance.
  field_purge_batch(0);

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

  // Delete the second instance.
  $bundle = next($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 (same as above, for the 2nd bundle).
  $actual_hooks = field_test_memorize();
  $hooks = array();
  $stubs = $this
    ->_generateStubEntities($this->entity_type, $this->entities_by_bundles[$bundle], $field['field_name']);
  $hooks['field_test_field_load'][] = $stubs;
  foreach ($stubs as $stub) {
    $hooks['field_test_field_delete'][] = $stub;
  }
  $this
    ->checkHooksInvocations($hooks, $actual_hooks);

  // The field still exists, deleted.
  $fields = field_read_fields(array(
    'id' => $field['id'],
  ), array(
    'include_deleted' => 1,
  ));
  $this
    ->assertTrue(isset($fields[$field['id']]) && $fields[$field['id']]['deleted'], 'The field exists and is deleted');

  // Purge again to purge the instance and the field.
  field_purge_batch(0);

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