function SqlContentEntityStorageSchemaTest::testDedicatedTableSchemaForEntityWithStringIdentifier

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php \Drupal\Tests\Core\Entity\Sql\SqlContentEntityStorageSchemaTest::testDedicatedTableSchemaForEntityWithStringIdentifier()
  2. 8.9.x core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php \Drupal\Tests\Core\Entity\Sql\SqlContentEntityStorageSchemaTest::testDedicatedTableSchemaForEntityWithStringIdentifier()
  3. 10 core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php \Drupal\Tests\Core\Entity\Sql\SqlContentEntityStorageSchemaTest::testDedicatedTableSchemaForEntityWithStringIdentifier()

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 1018

Class

SqlContentEntityStorageSchemaTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21Entity%21Sql%21SqlContentEntityStorageSchema.php/class/SqlContentEntityStorageSchema/11.x" title="Defines a schema handler that supports revisionable, translatable entities." class="local">\Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema</a> @group Entity

Namespace

Drupal\Tests\Core\Entity\Sql

Code

public function testDedicatedTableSchemaForEntityWithStringIdentifier() : void {
    $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')
        ->willReturn('shape');
    $field_storage->expects($this->any())
        ->method('getTargetEntityTypeId')
        ->willReturn($entity_type_id);
    $field_storage->expects($this->any())
        ->method('isMultiple')
        ->willReturn(TRUE);
    $this->storageDefinitions['id']
        ->expects($this->any())
        ->method('getType')
        ->willReturn('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')
        ->willReturn($table_mapping);
    $this->assertNull($this->storageSchema
        ->onFieldStorageDefinitionCreate($field_storage));
}

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