Commit 29addd06 authored by Dries's avatar Dries

- Patch #569364 by bjaspan, yched: handle failures on field storage creation.

parent a4b833b6
......@@ -291,7 +291,17 @@ function field_create_field($field) {
// Invoke hook_field_storage_create_field after the field is
// complete (e.g. it has its id).
module_invoke(variable_get('field_storage_module', 'field_sql_storage'), 'field_storage_create_field', $field);
try {
module_invoke(variable_get('field_storage_module', 'field_sql_storage'), 'field_storage_create_field', $field);
}
catch (Exception $e) {
// If storage creation failed, remove the field_config record before
// rethrowing the exception.
db_delete('field_config')
->condition('id', $field['id'])
->execute();
throw $e;
}
// Clear caches
field_cache_clear(TRUE);
......
......@@ -1559,6 +1559,35 @@ class FieldCrudTestCase extends FieldTestCase {
}
}
/**
* Test failure to create a field.
*/
function testCreateFieldFail() {
$field_name = 'duplicate';
$field_definition = array('field_name' => $field_name, 'type' => 'test_field');
$query = db_select('field_config')->condition('field_name', $field_name)->countQuery();
// The field does not appear in field_config.
$count = $query->execute()->fetchField();
$this->assertEqual($count, 0, 'A field_config row for the field does not exist.');
// Make field creation fail.
variable_set('field_storage_module', 'field_test');
// Try to create the field.
try {
$field = field_create_field($field_definition);
$this->assertTrue(FALSE, 'Field creation (correctly) fails.');
}
catch (Exception $e) {
$this->assertTrue(TRUE, 'Field creation (correctly) fails.');
}
// The field does not appear in field_config.
$count = $query->execute()->fetchField();
$this->assertEqual($count, 0, 'A field_config row for the field does not exist.');
}
/**
* Test reading back a field definition.
*/
......
......@@ -714,3 +714,13 @@ function field_test_field_delete($obj_type, $object, $field, $instance, $items)
$args = func_get_args();
field_test_memorize(__FUNCTION__, $args);
}
/**
*
* 'Field storage' API.
*
*/
function field_test_field_storage_create_field($field) {
throw new Exception('field_test storage module always fails to create fields');
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment