function SqlContentEntityStorageSchemaTest::testGetSchemaBase

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

Tests the schema for non-revisionable, non-translatable entities.

@covers ::__construct @covers ::getEntitySchemaTables @covers ::initializeBaseTable @covers ::addTableDefaults @covers ::getEntityIndexName @covers ::getFieldIndexes @covers ::getFieldUniqueKeys @covers ::getFieldForeignKeys @covers ::getFieldSchemaData @covers ::processIdentifierSchema

File

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

Class

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

Namespace

Drupal\Tests\Core\Entity\Sql

Code

public function testGetSchemaBase() : void {
    $this->entityType = new ContentEntityType([
        'id' => 'entity_test',
        'entity_keys' => [
            'id' => 'id',
        ],
    ]);
    // Add a field with a 'length' constraint.
    $this->setUpStorageDefinition('name', [
        'columns' => [
            'value' => [
                'type' => 'varchar',
                'length' => 255,
            ],
        ],
    ]);
    // Add a multi-column field.
    $this->setUpStorageDefinition('description', [
        'columns' => [
            'value' => [
                'type' => 'text',
            ],
            'format' => [
                'type' => 'varchar',
            ],
        ],
    ]);
    // Add a field with a unique key.
    $this->setUpStorageDefinition('uuid', [
        'columns' => [
            'value' => [
                'type' => 'varchar',
                'length' => 128,
            ],
        ],
        'unique keys' => [
            'value' => [
                'value',
            ],
        ],
    ]);
    // Add a field with a unique key, specified as column name and length.
    $this->setUpStorageDefinition('hash', [
        'columns' => [
            'value' => [
                'type' => 'varchar',
                'length' => 20,
            ],
        ],
        'unique keys' => [
            'value' => [
                [
                    'value',
                    10,
                ],
            ],
        ],
    ]);
    // Add a field with a multi-column unique key.
    $this->setUpStorageDefinition('email', [
        'columns' => [
            'username' => [
                'type' => 'varchar',
            ],
            'hostname' => [
                'type' => 'varchar',
            ],
            'domain' => [
                'type' => 'varchar',
            ],
        ],
        'unique keys' => [
            'email' => [
                'username',
                'hostname',
                [
                    'domain',
                    3,
                ],
            ],
        ],
    ]);
    // Add a field with an index.
    $this->setUpStorageDefinition('owner', [
        'columns' => [
            'target_id' => [
                'type' => 'int',
            ],
        ],
        'indexes' => [
            'target_id' => [
                'target_id',
            ],
        ],
    ]);
    // Add a field with an index, specified as column name and length.
    $this->setUpStorageDefinition('translator', [
        'columns' => [
            'target_id' => [
                'type' => 'int',
            ],
        ],
        'indexes' => [
            'target_id' => [
                [
                    'target_id',
                    10,
                ],
            ],
        ],
    ]);
    // Add a field with a multi-column index.
    $this->setUpStorageDefinition('location', [
        'columns' => [
            'country' => [
                'type' => 'varchar',
            ],
            'state' => [
                'type' => 'varchar',
            ],
            'city' => [
                'type' => 'varchar',
            ],
        ],
        'indexes' => [
            'country_state_city' => [
                'country',
                'state',
                [
                    'city',
                    10,
                ],
            ],
        ],
    ]);
    // Add a field with a foreign key.
    $this->setUpStorageDefinition('editor', [
        'columns' => [
            'target_id' => [
                'type' => 'int',
            ],
        ],
        'foreign keys' => [
            'user_id' => [
                'table' => 'users',
                'columns' => [
                    'target_id' => 'uid',
                ],
            ],
        ],
    ]);
    // Add a multi-column field with a foreign key.
    $this->setUpStorageDefinition('editor_revision', [
        'columns' => [
            'target_id' => [
                'type' => 'int',
            ],
            'target_revision_id' => [
                'type' => 'int',
            ],
        ],
        'foreign keys' => [
            'user_id' => [
                'table' => 'users',
                'columns' => [
                    'target_id' => 'uid',
                ],
            ],
        ],
    ]);
    // Add a field with a really long index.
    $this->setUpStorageDefinition('long_index_name', [
        'columns' => [
            'long_index_name' => [
                'type' => 'int',
            ],
        ],
        'indexes' => [
            'long_index_name_really_long_long_name' => [
                [
                    'long_index_name',
                    10,
                ],
            ],
        ],
    ]);
    $expected = [
        'entity_test' => [
            'description' => 'The base table for entity_test entities.',
            'fields' => [
                'id' => [
                    'type' => 'serial',
                    'not null' => TRUE,
                ],
                'name' => [
                    'type' => 'varchar',
                    'length' => 255,
                    'not null' => FALSE,
                ],
                'description__value' => [
                    'type' => 'text',
                    'not null' => FALSE,
                ],
                'description__format' => [
                    'type' => 'varchar',
                    'not null' => FALSE,
                ],
                'uuid' => [
                    'type' => 'varchar',
                    'length' => 128,
                    'not null' => FALSE,
                ],
                'hash' => [
                    'type' => 'varchar',
                    'length' => 20,
                    'not null' => FALSE,
                ],
                'email__username' => [
                    'type' => 'varchar',
                    'not null' => FALSE,
                ],
                'email__hostname' => [
                    'type' => 'varchar',
                    'not null' => FALSE,
                ],
                'email__domain' => [
                    'type' => 'varchar',
                    'not null' => FALSE,
                ],
                'owner' => [
                    'type' => 'int',
                    'not null' => FALSE,
                ],
                'translator' => [
                    'type' => 'int',
                    'not null' => FALSE,
                ],
                'location__country' => [
                    'type' => 'varchar',
                    'not null' => FALSE,
                ],
                'location__state' => [
                    'type' => 'varchar',
                    'not null' => FALSE,
                ],
                'location__city' => [
                    'type' => 'varchar',
                    'not null' => FALSE,
                ],
                'editor' => [
                    'type' => 'int',
                    'not null' => FALSE,
                ],
                'editor_revision__target_id' => [
                    'type' => 'int',
                    'not null' => FALSE,
                ],
                'editor_revision__target_revision_id' => [
                    'type' => 'int',
                    'not null' => FALSE,
                ],
                'long_index_name' => [
                    'type' => 'int',
                    'not null' => FALSE,
                ],
            ],
            'primary key' => [
                'id',
            ],
            'unique keys' => [
                'entity_test_field__uuid__value' => [
                    'uuid',
                ],
                'entity_test_field__hash__value' => [
                    [
                        'hash',
                        10,
                    ],
                ],
                'entity_test_field__email__email' => [
                    'email__username',
                    'email__hostname',
                    [
                        'email__domain',
                        3,
                    ],
                ],
            ],
            'indexes' => [
                'entity_test_field__owner__target_id' => [
                    'owner',
                ],
                'entity_test_field__translator__target_id' => [
                    [
                        'translator',
                        10,
                    ],
                ],
                'entity_test_field__location__country_state_city' => [
                    'location__country',
                    'location__state',
                    [
                        'location__city',
                        10,
                    ],
                ],
                'entity_test__b588603cb9' => [
                    [
                        'long_index_name',
                        10,
                    ],
                ],
            ],
            'foreign keys' => [
                'entity_test_field__editor__user_id' => [
                    'table' => 'users',
                    'columns' => [
                        'editor' => 'uid',
                    ],
                ],
                'entity_test_field__editor_revision__user_id' => [
                    'table' => 'users',
                    'columns' => [
                        'editor_revision__target_id' => 'uid',
                    ],
                ],
            ],
        ],
    ];
    $this->setUpStorageSchema($expected);
    $table_mapping = new TestSqlContentDefaultTableMapping($this->entityType, $this->storageDefinitions);
    $table_mapping->setFieldNames('entity_test', array_keys($this->storageDefinitions));
    $table_mapping->setExtraColumns('entity_test', [
        'default_langcode',
    ]);
    $this->storageSchema
        ->expects($this->any())
        ->method('getTableMapping')
        ->willReturn($table_mapping);
    $this->assertNull($this->storageSchema
        ->onEntityTypeCreate($this->entityType));
}

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