1 field_sql_storage.test FieldSqlStorageTestCase::testFieldSqlStorageForeignKeys()

Test foreign key support.


core/modules/field/modules/field_sql_storage/tests/field_sql_storage.test, line 356
Tests for field_sql_storage.module.


Tests field storage.


function testFieldSqlStorageForeignKeys() {
  // Create a decimal field.
  $field_name = 'testfield';
  $field = array(
    'field_name' => $field_name,
    'type' => 'shape',
    'settings' => array(
      'foreign_key_name' => 'foo',

  $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']['foo']['table'], 'foo', 'Foreign key table name preserved through CRUD');
  $this->assertEqual($field['foreign keys']['foo']['columns']['foo'], 'id', 'Foreign key column name preserved through CRUD');
  // Now grab the SQL schema and verify that too.
  $schema = backdrop_get_schema(_field_sql_storage_tablename($field));
  $this->assertEqual(count($schema['foreign keys']), 1, 'There is 1 foreign key in the schema');
  $foreign_key = reset($schema['foreign keys']);
  $id_column = _field_sql_storage_columnname($field['field_name'], 'foo');
  $this->assertEqual($foreign_key['table'], 'foo', 'Foreign key table name preserved in the schema');
  $this->assertEqual($foreign_key['columns'][$id_column], 'id', 'Foreign key column name preserved in the schema');