You are here

public function SqlContentEntityStorageSchemaTest::testGetSchemaBase in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 ::processBaseTable @covers ::processIdentifierSchema

File

core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php, line 102
Contains \Drupal\Tests\Core\Entity\Sql\SqlContentEntityStorageSchemaTest.

Class

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

Namespace

Drupal\Tests\Core\Entity\Sql

Code

public function testGetSchemaBase() {
  $this->entityType = new ContentEntityType(array(
    'id' => 'entity_test',
    'entity_keys' => array(
      'id' => 'id',
    ),
  ));

  // Add a field with a 'length' constraint.
  $this
    ->setUpStorageDefinition('name', array(
    'columns' => array(
      'value' => array(
        'type' => 'varchar',
        'length' => 255,
      ),
    ),
  ));

  // Add a multi-column field.
  $this
    ->setUpStorageDefinition('description', array(
    'columns' => array(
      'value' => array(
        'type' => 'text',
      ),
      'format' => array(
        'type' => 'varchar',
      ),
    ),
  ));

  // Add a field with a unique key.
  $this
    ->setUpStorageDefinition('uuid', array(
    'columns' => array(
      'value' => array(
        'type' => 'varchar',
        'length' => 128,
      ),
    ),
    'unique keys' => array(
      'value' => array(
        'value',
      ),
    ),
  ));

  // Add a field with a unique key, specified as column name and length.
  $this
    ->setUpStorageDefinition('hash', array(
    'columns' => array(
      'value' => array(
        'type' => 'varchar',
        'length' => 20,
      ),
    ),
    'unique keys' => array(
      'value' => array(
        array(
          'value',
          10,
        ),
      ),
    ),
  ));

  // Add a field with a multi-column unique key.
  $this
    ->setUpStorageDefinition('email', array(
    'columns' => array(
      'username' => array(
        'type' => 'varchar',
      ),
      'hostname' => array(
        'type' => 'varchar',
      ),
      'domain' => array(
        'type' => 'varchar',
      ),
    ),
    'unique keys' => array(
      'email' => array(
        'username',
        'hostname',
        array(
          'domain',
          3,
        ),
      ),
    ),
  ));

  // Add a field with an index.
  $this
    ->setUpStorageDefinition('owner', array(
    'columns' => array(
      'target_id' => array(
        'type' => 'int',
      ),
    ),
    'indexes' => array(
      'target_id' => array(
        'target_id',
      ),
    ),
  ));

  // Add a field with an index, specified as column name and length.
  $this
    ->setUpStorageDefinition('translator', array(
    'columns' => array(
      'target_id' => array(
        'type' => 'int',
      ),
    ),
    'indexes' => array(
      'target_id' => array(
        array(
          'target_id',
          10,
        ),
      ),
    ),
  ));

  // Add a field with a multi-column index.
  $this
    ->setUpStorageDefinition('location', array(
    'columns' => array(
      'country' => array(
        'type' => 'varchar',
      ),
      'state' => array(
        'type' => 'varchar',
      ),
      'city' => array(
        'type' => 'varchar',
      ),
    ),
    'indexes' => array(
      'country_state_city' => array(
        'country',
        'state',
        array(
          'city',
          10,
        ),
      ),
    ),
  ));

  // Add a field with a foreign key.
  $this
    ->setUpStorageDefinition('editor', array(
    'columns' => array(
      'target_id' => array(
        'type' => 'int',
      ),
    ),
    'foreign keys' => array(
      'user_id' => array(
        'table' => 'users',
        'columns' => array(
          'target_id' => 'uid',
        ),
      ),
    ),
  ));

  // Add a multi-column field with a foreign key.
  $this
    ->setUpStorageDefinition('editor_revision', array(
    'columns' => array(
      'target_id' => array(
        'type' => 'int',
      ),
      'target_revision_id' => array(
        'type' => 'int',
      ),
    ),
    'foreign keys' => array(
      'user_id' => array(
        'table' => 'users',
        'columns' => array(
          'target_id' => 'uid',
        ),
      ),
    ),
  ));

  // Add a field with a really long index.
  $this
    ->setUpStorageDefinition('long_index_name', array(
    'columns' => array(
      'long_index_name' => array(
        'type' => 'int',
      ),
    ),
    'indexes' => array(
      'long_index_name_really_long_long_name' => array(
        array(
          'long_index_name',
          10,
        ),
      ),
    ),
  ));
  $expected = array(
    'entity_test' => array(
      'description' => 'The base table for entity_test entities.',
      'fields' => array(
        'id' => array(
          'type' => 'serial',
          'not null' => TRUE,
        ),
        'name' => array(
          'type' => 'varchar',
          'length' => 255,
          'not null' => FALSE,
        ),
        'description__value' => array(
          'type' => 'text',
          'not null' => FALSE,
        ),
        'description__format' => array(
          'type' => 'varchar',
          'not null' => FALSE,
        ),
        'uuid' => array(
          'type' => 'varchar',
          'length' => 128,
          'not null' => FALSE,
        ),
        'hash' => array(
          'type' => 'varchar',
          'length' => 20,
          'not null' => FALSE,
        ),
        'email__username' => array(
          'type' => 'varchar',
          'not null' => FALSE,
        ),
        'email__hostname' => array(
          'type' => 'varchar',
          'not null' => FALSE,
        ),
        'email__domain' => array(
          'type' => 'varchar',
          'not null' => FALSE,
        ),
        'owner' => array(
          'type' => 'int',
          'not null' => FALSE,
        ),
        'translator' => array(
          'type' => 'int',
          'not null' => FALSE,
        ),
        'location__country' => array(
          'type' => 'varchar',
          'not null' => FALSE,
        ),
        'location__state' => array(
          'type' => 'varchar',
          'not null' => FALSE,
        ),
        'location__city' => array(
          'type' => 'varchar',
          'not null' => FALSE,
        ),
        'editor' => array(
          'type' => 'int',
          'not null' => FALSE,
        ),
        'editor_revision__target_id' => array(
          'type' => 'int',
          'not null' => FALSE,
        ),
        'editor_revision__target_revision_id' => array(
          'type' => 'int',
          'not null' => FALSE,
        ),
        'long_index_name' => array(
          'type' => 'int',
          'not null' => FALSE,
        ),
      ),
      'primary key' => array(
        'id',
      ),
      'unique keys' => array(
        'entity_test_field__uuid__value' => array(
          'uuid',
        ),
        'entity_test_field__hash__value' => array(
          array(
            'hash',
            10,
          ),
        ),
        'entity_test_field__email__email' => array(
          'email__username',
          'email__hostname',
          array(
            'email__domain',
            3,
          ),
        ),
      ),
      'indexes' => array(
        'entity_test_field__owner__target_id' => array(
          'owner',
        ),
        'entity_test_field__translator__target_id' => array(
          array(
            'translator',
            10,
          ),
        ),
        'entity_test_field__location__country_state_city' => array(
          'location__country',
          'location__state',
          array(
            'location__city',
            10,
          ),
        ),
        'entity_test__b588603cb9' => array(
          array(
            'long_index_name',
            10,
          ),
        ),
      ),
      'foreign keys' => array(
        'entity_test_field__editor__user_id' => array(
          'table' => 'users',
          'columns' => array(
            'editor' => 'uid',
          ),
        ),
        'entity_test_field__editor_revision__user_id' => array(
          'table' => 'users',
          'columns' => array(
            'editor_revision__target_id' => 'uid',
          ),
        ),
      ),
    ),
  );
  $this
    ->setUpStorageSchema($expected);
  $table_mapping = new DefaultTableMapping($this->entityType, $this->storageDefinitions);
  $table_mapping
    ->setFieldNames('entity_test', array_keys($this->storageDefinitions));
  $table_mapping
    ->setExtraColumns('entity_test', array(
    'default_langcode',
  ));
  $this->storage
    ->expects($this
    ->any())
    ->method('getTableMapping')
    ->will($this
    ->returnValue($table_mapping));
  $this
    ->assertNull($this->storageSchema
    ->onEntityTypeCreate($this->entityType));
}