Verify that fields are preserved and purged correctly as multiple instances are deleted and purged.
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);
// Purge again to purge the instance.
field_purge_batch(0);
// The field still exists, not deleted.
$fields = field_read_fields(array(
'uuid' => $field['uuid'],
), array(
'include_deleted' => TRUE,
));
$this
->assertTrue(isset($fields[$field['uuid']]) && !$fields[$field['uuid']]->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();
$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);
// The field still exists, deleted.
$fields = field_read_fields(array(
'uuid' => $field['uuid'],
), array(
'include_deleted' => TRUE,
));
$this
->assertTrue(isset($fields[$field['uuid']]) && $fields[$field['uuid']]->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(
'uuid' => $field['uuid'],
), array(
'include_deleted' => TRUE,
'include_inactive' => TRUE,
));
$this
->assertEqual(count($fields), 0, 'The field is purged.');
}