You are here

public function FieldSqlStorageTest::testTableNames 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::testTableNames()

Tests table name generation.

File

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

Class

FieldSqlStorageTest
Tests Field SQL Storage .

Namespace

Drupal\system\Tests\Entity

Code

public function testTableNames() {

  // 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 = entity_create('field_storage_config', array(
    'entity_type' => $entity_type,
    'field_name' => $field_name,
    'type' => 'test_field',
  ));
  $expected = 'short_entity_type__short_field_name';
  $this
    ->assertEqual($this->tableMapping
    ->getDedicatedDataTableName($field_storage), $expected);
  $expected = 'short_entity_type_revision__short_field_name';
  $this
    ->assertEqual($this->tableMapping
    ->getDedicatedRevisionTableName($field_storage), $expected);

  // Short entity type, long field name
  $entity_type = 'short_entity_type';
  $field_name = 'long_field_name_abcdefghijklmnopqrstuvwxyz';
  $field_storage = entity_create('field_storage_config', array(
    'entity_type' => $entity_type,
    'field_name' => $field_name,
    'type' => 'test_field',
  ));
  $expected = 'short_entity_type__' . substr(hash('sha256', $field_storage
    ->uuid()), 0, 10);
  $this
    ->assertEqual($this->tableMapping
    ->getDedicatedDataTableName($field_storage), $expected);
  $expected = 'short_entity_type_r__' . substr(hash('sha256', $field_storage
    ->uuid()), 0, 10);
  $this
    ->assertEqual($this->tableMapping
    ->getDedicatedRevisionTableName($field_storage), $expected);

  // Long entity type, short field name
  $entity_type = 'long_entity_type_abcdefghijklmnopqrstuvwxyz';
  $field_name = 'short_field_name';
  $field_storage = entity_create('field_storage_config', array(
    'entity_type' => $entity_type,
    'field_name' => $field_name,
    'type' => 'test_field',
  ));
  $expected = 'long_entity_type_abcdefghijklmnopq__' . substr(hash('sha256', $field_storage
    ->uuid()), 0, 10);
  $this
    ->assertEqual($this->tableMapping
    ->getDedicatedDataTableName($field_storage), $expected);
  $expected = 'long_entity_type_abcdefghijklmnopq_r__' . substr(hash('sha256', $field_storage
    ->uuid()), 0, 10);
  $this
    ->assertEqual($this->tableMapping
    ->getDedicatedRevisionTableName($field_storage), $expected);

  // Long entity type and field name.
  $entity_type = 'long_entity_type_abcdefghijklmnopqrstuvwxyz';
  $field_name = 'long_field_name_abcdefghijklmnopqrstuvwxyz';
  $field_storage = entity_create('field_storage_config', array(
    'entity_type' => $entity_type,
    'field_name' => $field_name,
    'type' => 'test_field',
  ));
  $expected = 'long_entity_type_abcdefghijklmnopq__' . substr(hash('sha256', $field_storage
    ->uuid()), 0, 10);
  $this
    ->assertEqual($this->tableMapping
    ->getDedicatedDataTableName($field_storage), $expected);
  $expected = 'long_entity_type_abcdefghijklmnopq_r__' . substr(hash('sha256', $field_storage
    ->uuid()), 0, 10);
  $this
    ->assertEqual($this->tableMapping
    ->getDedicatedRevisionTableName($field_storage), $expected);

  // Try creating a second field and check there are no clashes.
  $field_storage2 = entity_create('field_storage_config', array(
    'entity_type' => $entity_type,
    'field_name' => $field_name . '2',
    'type' => 'test_field',
  ));
  $this
    ->assertNotEqual($this->tableMapping
    ->getDedicatedDataTableName($field_storage), $this->tableMapping
    ->getDedicatedDataTableName($field_storage2));
  $this
    ->assertNotEqual($this->tableMapping
    ->getDedicatedRevisionTableName($field_storage), $this->tableMapping
    ->getDedicatedRevisionTableName($field_storage2));

  // Deleted field.
  $field_storage = entity_create('field_storage_config', array(
    '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
    ->assertEqual($this->tableMapping
    ->getDedicatedDataTableName($field_storage, TRUE), $expected);
  $expected = 'field_deleted_revision_' . substr(hash('sha256', $field_storage
    ->uuid()), 0, 10);
  $this
    ->assertEqual($this->tableMapping
    ->getDedicatedRevisionTableName($field_storage, TRUE), $expected);
}