diff --git a/core/modules/field/tests/modules/field_test/field_test.field.inc b/core/modules/field/tests/modules/field_test/field_test.field.inc index 7166f4b..d2f4a94 100644 --- a/core/modules/field/tests/modules/field_test/field_test.field.inc +++ b/core/modules/field/tests/modules/field_test/field_test.field.inc @@ -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', diff --git a/core/modules/field/tests/modules/field_test/field_test.install b/core/modules/field/tests/modules/field_test/field_test.install index 5355e9b..1f8d848 100644 --- a/core/modules/field/tests/modules/field_test/field_test.install +++ b/core/modules/field/tests/modules/field_test/field_test.install @@ -142,6 +142,14 @@ function field_test_field_schema($field) { 'not null' => FALSE, ), ), + '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'), + ), + ), ); } } diff --git a/core/modules/field_sql_storage/field_sql_storage.module b/core/modules/field_sql_storage/field_sql_storage.module index e8afd3c..641945e 100644 --- a/core/modules/field_sql_storage/field_sql_storage.module +++ b/core/modules/field_sql_storage/field_sql_storage.module @@ -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; } diff --git a/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Tests/FieldSqlStorageTest.php b/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Tests/FieldSqlStorageTest.php index f31c63e..889d026 100644 --- a/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Tests/FieldSqlStorageTest.php +++ b/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Tests/FieldSqlStorageTest.php @@ -428,19 +428,33 @@ function testFieldStorageDetails() { function testFieldSqlStorageForeignKeys() { // Create a decimal field. $field_name = 'testfield'; - $field = array('field_name' => $field_name, 'type' => 'text'); + $foreign_key_name = 'shape'; + $field = array('field_name' => $field_name, 'type' => 'shape', 'settings' => array('foreign_key_name' => $foreign_key_name)); $field = field_create_field($field); + // Retrieve the field and instance with field_info and verify the foreign // keys are 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 preserved through CRUD')); + $this->assertEqual($field['foreign keys'][$foreign_key_name]['columns'][$foreign_key_name], 'id', t('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 and instance with field_info and verify the foreign + // keys are in place. + $field = field_info_field($field_name); + $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)); + $schema = drupal_get_schema(_field_sql_storage_tablename($field), TRUE); $this->assertEqual(count($schema['foreign keys']), 1, t("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, t('Foreign key table name preserved in the schema')); + $this->assertEqual($foreign_key['columns'][$foreign_key_column], 'id', t('Foreign key column name preserved in the schema')); } }