function FieldSqlStorageTest::testFieldSqlStorageForeignKeys

Same name in other branches
  1. 8.9.x 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()
  3. 11.x 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 449

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->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');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.