Commit 2f303cc8 authored by webchick's avatar webchick

Issue #1416506 by Damien Tournoud, swentel, yched: Fixed Field schema foreign...

Issue #1416506 by Damien Tournoud, swentel, yched: Fixed Field schema foreign keys support is broken.
parent 6c6d2335
......@@ -32,7 +32,9 @@ function field_test_field_info() {
'shape' => array(
'label' => t('Shape'),
'description' => t('Another dummy field type.'),
'settings' => array(),
'settings' => array(
'foreign_key_name' => 'shape',
),
'instance_settings' => array(),
'default_widget' => 'test_field_widget',
'default_formatter' => 'field_test_default',
......
......@@ -129,6 +129,18 @@ function field_test_field_schema($field) {
);
}
else {
$foreign_keys = array();
// The 'foreign keys' key is not always used in tests.
if (!empty($field['settings']['foreign_key_name'])) {
$foreign_keys['foreign keys'] = array(
// This is a dummy foreign key definition, references a table that
// doesn't exist, but that's not a problem.
$field['settings']['foreign_key_name'] => array(
'table' => $field['settings']['foreign_key_name'],
'columns' => array($field['settings']['foreign_key_name'] => 'id'),
),
);
}
return array(
'columns' => array(
'shape' => array(
......@@ -142,6 +154,6 @@ function field_test_field_schema($field) {
'not null' => FALSE,
),
),
);
) + $foreign_keys;
}
}
......@@ -218,7 +218,7 @@ function _field_sql_storage_schema($field) {
foreach ($schema['foreign keys'] as $specifier => $specification) {
$real_name = _field_sql_storage_indexname($field['field_name'], $specifier);
$current['foreign keys'][$real_name]['table'] = $specification['table'];
foreach ($specification['columns'] as $column => $referenced) {
foreach ($specification['columns'] as $column_name => $referenced) {
$sql_storage_column = _field_sql_storage_columnname($field['field_name'], $column_name);
$current['foreign keys'][$real_name]['columns'][$sql_storage_column] = $referenced;
}
......
......@@ -426,21 +426,35 @@ function testFieldStorageDetails() {
* Test foreign key support.
*/
function testFieldSqlStorageForeignKeys() {
// Create a decimal field.
// Create a 'shape' field, with a configurable foreign key (see
// field_test_field_schema()).
$field_name = 'testfield';
$field = array('field_name' => $field_name, 'type' => 'text');
$field = field_create_field($field);
// Retrieve the field and instance with field_info and verify the foreign
// keys are in place.
$foreign_key_name = 'shape';
$field = array('field_name' => $field_name, 'type' => 'shape', 'settings' => array('foreign_key_name' => $foreign_key_name));
field_create_field($field);
// Retrieve the field definition and check that the foreign key is in place.
$field = field_info_field($field_name);
$this->assertEqual($field['foreign keys'][$foreign_key_name]['table'], $foreign_key_name, 'Foreign key table name preserved through CRUD');
$this->assertEqual($field['foreign keys'][$foreign_key_name]['columns'][$foreign_key_name], 'id', 'Foreign key column name preserved through CRUD');
// Update the field settings, it should update the foreign key definition too.
$foreign_key_name = 'color';
$field['settings']['foreign_key_name'] = $foreign_key_name;
field_update_field($field);
// Retrieve the field definition and check that the foreign key is in place.
$field = field_info_field($field_name);
$this->assertEqual($field['foreign keys']['format']['table'], 'filter_format', 'Foreign key table name preserved through CRUD');
$this->assertEqual($field['foreign keys']['format']['columns']['format'], 'format', 'Foreign key column name preserved through CRUD');
$this->assertEqual($field['foreign keys'][$foreign_key_name]['table'], $foreign_key_name, t('Foreign key table name modified after update'));
$this->assertEqual($field['foreign keys'][$foreign_key_name]['columns'][$foreign_key_name], 'id', t('Foreign key column name modified after update'));
// Now grab the SQL schema and verify that too.
$schema = drupal_get_schema(_field_sql_storage_tablename($field));
$this->assertEqual(count($schema['foreign keys']), 1, t("There is 1 foreign key in the schema"));
$schema = drupal_get_schema(_field_sql_storage_tablename($field), TRUE);
$this->assertEqual(count($schema['foreign keys']), 1, 'There is 1 foreign key in the schema');
$foreign_key = reset($schema['foreign keys']);
$filter_column = _field_sql_storage_columnname($field['field_name'], 'format');
$this->assertEqual($foreign_key['table'], 'filter_format', 'Foreign key table name preserved in the schema');
$this->assertEqual($foreign_key['columns'][$filter_column], 'format', 'Foreign key column name preserved in the schema');
$foreign_key_column = _field_sql_storage_columnname($field['field_name'], $foreign_key_name);
$this->assertEqual($foreign_key['table'], $foreign_key_name, 'Foreign key table name preserved in the schema');
$this->assertEqual($foreign_key['columns'][$foreign_key_column], 'id', 'Foreign key column name preserved in the schema');
}
}
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