public function FieldSqlStorageTest::testFieldSqlStorageForeignKeys in Drupal 10
Same name and namespace in other branches
- 8 core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldSqlStorageTest::testFieldSqlStorageForeignKeys()
- 9 core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldSqlStorageTest::testFieldSqlStorageForeignKeys()
Tests foreign key support.
File
- core/
tests/ Drupal/ KernelTests/ Core/ Entity/ FieldSqlStorageTest.php, line 446
Class
- FieldSqlStorageTest
- Tests Field SQL Storage .
Namespace
Drupal\KernelTests\Core\EntityCode
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
->assertEquals($foreign_key_name, $schema['foreign keys'][$foreign_key_name]['table'], 'Foreign key table name preserved through CRUD');
$this
->assertEquals('id', $schema['foreign keys'][$foreign_key_name]['columns'][$foreign_key_name], '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
->assertEquals($foreign_key_name, $schema['foreign keys'][$foreign_key_name]['table'], 'Foreign key table name modified after update');
$this
->assertEquals('id', $schema['foreign keys'][$foreign_key_name]['columns'][$foreign_key_name], 'Foreign key column name modified after update');
}