You are here

public function SqlContentEntityStorageSchemaTest::testDedicatedTableSchemaForEntityWithStringIdentifier in Drupal 8

Tests the schema for a field dedicated table for an entity with a string identifier.

@covers ::onFieldStorageDefinitionCreate @covers ::getDedicatedTableSchema @covers ::createDedicatedTableSchema

File

core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php, line 1016

Class

SqlContentEntityStorageSchemaTest
@coversDefaultClass \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema @group Entity

Namespace

Drupal\Tests\Core\Entity\Sql

Code

public function testDedicatedTableSchemaForEntityWithStringIdentifier() {
  $entity_type_id = 'entity_test';
  $this->entityType = new ContentEntityType([
    'id' => 'entity_test',
    'entity_keys' => [
      'id' => 'id',
    ],
  ]);

  // Setup a field having a dedicated schema.
  $field_name = $this
    ->getRandomGenerator()
    ->name();
  $this
    ->setUpStorageDefinition($field_name, [
    'columns' => [
      'shape' => [
        'type' => 'varchar',
        'length' => 32,
        'not null' => FALSE,
      ],
      'color' => [
        'type' => 'varchar',
        'length' => 32,
        'not null' => FALSE,
      ],
    ],
    'foreign keys' => [
      'color' => [
        'table' => 'color',
        'columns' => [
          'color' => 'id',
        ],
      ],
    ],
    'unique keys' => [],
    'indexes' => [],
  ]);
  $field_storage = $this->storageDefinitions[$field_name];
  $field_storage
    ->expects($this
    ->any())
    ->method('getType')
    ->will($this
    ->returnValue('shape'));
  $field_storage
    ->expects($this
    ->any())
    ->method('getTargetEntityTypeId')
    ->will($this
    ->returnValue($entity_type_id));
  $field_storage
    ->expects($this
    ->any())
    ->method('isMultiple')
    ->will($this
    ->returnValue(TRUE));
  $this->storageDefinitions['id']
    ->expects($this
    ->any())
    ->method('getType')
    ->will($this
    ->returnValue('string'));
  $expected = [
    $entity_type_id . '__' . $field_name => [
      'description' => "Data storage for {$entity_type_id} field {$field_name}.",
      'fields' => [
        'bundle' => [
          'type' => 'varchar_ascii',
          'length' => 128,
          'not null' => TRUE,
          'default' => '',
          'description' => 'The field instance bundle to which this row belongs, used when deleting a field instance',
        ],
        'deleted' => [
          'type' => 'int',
          'size' => 'tiny',
          'not null' => TRUE,
          'default' => 0,
          'description' => 'A boolean indicating whether this data item has been deleted',
        ],
        'entity_id' => [
          'type' => 'varchar_ascii',
          'length' => 128,
          'not null' => TRUE,
          'description' => 'The entity id this data is attached to',
        ],
        'revision_id' => [
          'type' => 'varchar_ascii',
          'length' => 128,
          'not null' => TRUE,
          'description' => 'The entity revision id this data is attached to, which for an unversioned entity type is the same as the entity id',
        ],
        'langcode' => [
          'type' => 'varchar_ascii',
          'length' => 32,
          'not null' => TRUE,
          'default' => '',
          'description' => 'The language code for this data item.',
        ],
        'delta' => [
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
          'description' => 'The sequence number for this data item, used for multi-value fields',
        ],
        $field_name . '_shape' => [
          'type' => 'varchar',
          'length' => 32,
          'not null' => FALSE,
        ],
        $field_name . '_color' => [
          'type' => 'varchar',
          'length' => 32,
          'not null' => FALSE,
        ],
      ],
      'primary key' => [
        'entity_id',
        'deleted',
        'delta',
        'langcode',
      ],
      'indexes' => [
        'bundle' => [
          'bundle',
        ],
        'revision_id' => [
          'revision_id',
        ],
      ],
      'foreign keys' => [
        $field_name . '_color' => [
          'table' => 'color',
          'columns' => [
            $field_name . '_color' => 'id',
          ],
        ],
      ],
    ],
  ];
  $this
    ->setUpStorageSchema($expected);
  $table_mapping = new TestSqlContentDefaultTableMapping($this->entityType, $this->storageDefinitions);
  $table_mapping
    ->setFieldNames($entity_type_id, array_keys($this->storageDefinitions));
  $table_mapping
    ->setExtraColumns($entity_type_id, [
    'default_langcode',
  ]);
  $this->storageSchema
    ->expects($this
    ->any())
    ->method('getTableMapping')
    ->will($this
    ->returnValue($table_mapping));
  $this
    ->assertNull($this->storageSchema
    ->onFieldStorageDefinitionCreate($field_storage));
}