You are here

function FieldSqlStorageTest::testFieldSqlStorageForeignKeys in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/FieldSqlStorageTest.php \Drupal\system\Tests\Entity\FieldSqlStorageTest::testFieldSqlStorageForeignKeys()

Test foreign key support.

File

core/modules/system/src/Tests/Entity/FieldSqlStorageTest.php, line 441
Contains \Drupal\system\Tests\Entity\FieldSqlStorageTest.

Class

FieldSqlStorageTest
Tests Field SQL Storage .

Namespace

Drupal\system\Tests\Entity

Code

function testFieldSqlStorageForeignKeys() {

  // Create a 'shape' field, with a configurable foreign key (see
  // field_test_field_schema()).
  $field_name = 'testfield';
  $foreign_key_name = 'shape';
  $field_storage = entity_create('field_storage_config', array(
    'field_name' => $field_name,
    'entity_type' => 'entity_test',
    'type' => 'shape',
    'settings' => array(
      'foreign_key_name' => $foreign_key_name,
    ),
  ));
  $field_storage
    ->save();

  // Get the field schema.
  $schema = $field_storage
    ->getSchema();

  // Retrieve the field definition and check that the foreign key is in place.
  $this
    ->assertEqual($schema['foreign keys'][$foreign_key_name]['table'], $foreign_key_name, 'Foreign key table name preserved through CRUD');
  $this
    ->assertEqual($schema['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_storage
    ->setSetting('foreign_key_name', $foreign_key_name);
  $field_storage
    ->save();

  // Reload the field schema after the update.
  $schema = $field_storage
    ->getSchema();

  // Check that the foreign key is in place.
  $this
    ->assertEqual($schema['foreign keys'][$foreign_key_name]['table'], $foreign_key_name, 'Foreign key table name modified after update');
  $this
    ->assertEqual($schema['foreign keys'][$foreign_key_name]['columns'][$foreign_key_name], 'id', 'Foreign key column name modified after update');
}