You are here

public function FieldSqlStorageTest::testFieldSqlStorageForeignKeys in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldSqlStorageTest::testFieldSqlStorageForeignKeys()
  2. 10 core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldSqlStorageTest::testFieldSqlStorageForeignKeys()

Test foreign key support.

File

core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php, line 445

Class

FieldSqlStorageTest
Tests Field SQL Storage .

Namespace

Drupal\KernelTests\Core\Entity

Code

public 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 = FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'entity_test',
    'type' => 'shape',
    'settings' => [
      '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');
}