function FieldSqlStorageTest::testTableNames

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldSqlStorageTest::testTableNames()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldSqlStorageTest::testTableNames()
  3. 11.x core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldSqlStorageTest::testTableNames()

Tests table name generation.

File

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

Class

FieldSqlStorageTest
Tests Field SQL Storage .

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testTableNames() : void {
    // Note: we need to test entity types with long names. We therefore use
    // fields on imaginary entity types (works as long as we don't actually save
    // them), and just check the generated table names.
    // Short entity type and field name.
    $entity_type = 'short_entity_type';
    $field_name = 'short_field_name';
    $field_storage = FieldStorageConfig::create([
        'entity_type' => $entity_type,
        'field_name' => $field_name,
        'type' => 'test_field',
    ]);
    $expected = 'short_entity_type__short_field_name';
    $this->assertEquals($expected, $this->tableMapping
        ->getDedicatedDataTableName($field_storage));
    $expected = 'short_entity_type_revision__short_field_name';
    $this->assertEquals($expected, $this->tableMapping
        ->getDedicatedRevisionTableName($field_storage));
    // Short entity type, long field name
    $entity_type = 'short_entity_type';
    $field_name = 'long_field_name_abcdefghijklmnopqrstuvwxyz';
    $field_storage = FieldStorageConfig::create([
        'entity_type' => $entity_type,
        'field_name' => $field_name,
        'type' => 'test_field',
    ]);
    $expected = 'short_entity_type__' . substr(hash('sha256', $field_storage->uuid()), 0, 10);
    $this->assertEquals($expected, $this->tableMapping
        ->getDedicatedDataTableName($field_storage));
    $expected = 'short_entity_type_r__' . substr(hash('sha256', $field_storage->uuid()), 0, 10);
    $this->assertEquals($expected, $this->tableMapping
        ->getDedicatedRevisionTableName($field_storage));
    // Long entity type, short field name
    $entity_type = 'long_entity_type_all_forty_three_characters';
    $field_name = 'short_field_name';
    $field_storage = FieldStorageConfig::create([
        'entity_type' => $entity_type,
        'field_name' => $field_name,
        'type' => 'test_field',
    ]);
    $expected = 'long_entity_type_all_forty_three__' . substr(hash('sha256', $field_storage->uuid()), 0, 10);
    $this->assertEquals($expected, $this->tableMapping
        ->getDedicatedDataTableName($field_storage));
    $expected = 'long_entity_type_all_forty_three_r__' . substr(hash('sha256', $field_storage->uuid()), 0, 10);
    $this->assertEquals($expected, $this->tableMapping
        ->getDedicatedRevisionTableName($field_storage));
    // Long entity type and field name.
    $entity_type = 'long_entity_type_all_forty_three_characters';
    $field_name = 'long_field_name_using_forty_two_characters';
    $field_storage = FieldStorageConfig::create([
        'entity_type' => $entity_type,
        'field_name' => $field_name,
        'type' => 'test_field',
    ]);
    $expected = 'long_entity_type_all_forty_three__' . substr(hash('sha256', $field_storage->uuid()), 0, 10);
    $this->assertEquals($expected, $this->tableMapping
        ->getDedicatedDataTableName($field_storage));
    $expected = 'long_entity_type_all_forty_three_r__' . substr(hash('sha256', $field_storage->uuid()), 0, 10);
    $this->assertEquals($expected, $this->tableMapping
        ->getDedicatedRevisionTableName($field_storage));
    // Try creating a second field and check there are no clashes.
    $field_storage2 = FieldStorageConfig::create([
        'entity_type' => $entity_type,
        'field_name' => $field_name . '2',
        'type' => 'test_field',
    ]);
    $this->assertNotEquals($this->tableMapping
        ->getDedicatedDataTableName($field_storage), $this->tableMapping
        ->getDedicatedDataTableName($field_storage2));
    $this->assertNotEquals($this->tableMapping
        ->getDedicatedRevisionTableName($field_storage), $this->tableMapping
        ->getDedicatedRevisionTableName($field_storage2));
    // Deleted field.
    $field_storage = FieldStorageConfig::create([
        'entity_type' => 'some_entity_type',
        'field_name' => 'some_field_name',
        'type' => 'test_field',
        'deleted' => TRUE,
    ]);
    $expected = 'field_deleted_data_' . substr(hash('sha256', $field_storage->uuid()), 0, 10);
    $this->assertEquals($expected, $this->tableMapping
        ->getDedicatedDataTableName($field_storage, TRUE));
    $expected = 'field_deleted_revision_' . substr(hash('sha256', $field_storage->uuid()), 0, 10);
    $this->assertEquals($expected, $this->tableMapping
        ->getDedicatedRevisionTableName($field_storage, TRUE));
    // Check that the table mapping is kept up-to-date in a request where a new
    // field storage definition is added. Since the cardinality of the field is
    // greater than 1, the table name retrieved from getFieldTableName() should
    // be the dedicated table.
    $field_storage = FieldStorageConfig::create([
        'entity_type' => 'entity_test_rev',
        'field_name' => 'some_field_name',
        'type' => 'test_field',
        'cardinality' => 2,
    ]);
    $field_storage->save();
    $table_mapping = \Drupal::entityTypeManager()->getStorage('entity_test_rev')
        ->getTableMapping();
    $this->assertEquals($table_mapping->getDedicatedDataTableName($field_storage), $table_mapping->getFieldTableName('some_field_name'));
}

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