You are here

public function SqlContentEntityStorageSchemaTest::testDedicatedTableSchema 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::testDedicatedTableSchema()

Tests the schema for a field dedicated table.

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

File

core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php, line 807
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 testDedicatedTableSchema() {
  $entity_type_id = 'entity_test';
  $this->entityType = new ContentEntityType(array(
    'id' => 'entity_test',
    'entity_keys' => array(
      'id' => 'id',
    ),
  ));

  // Setup a field having a dedicated schema.
  $field_name = $this
    ->getRandomGenerator()
    ->name();
  $this
    ->setUpStorageDefinition($field_name, array(
    'columns' => array(
      'shape' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => FALSE,
      ),
      'color' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => FALSE,
      ),
    ),
    'foreign keys' => array(
      'color' => array(
        'table' => 'color',
        'columns' => array(
          'color' => 'id',
        ),
      ),
    ),
    'unique keys' => array(),
    'indexes' => array(),
  ));
  $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('integer'));
  $expected = array(
    $entity_type_id . '__' . $field_name => array(
      'description' => "Data storage for {$entity_type_id} field {$field_name}.",
      'fields' => array(
        'bundle' => array(
          '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' => array(
          'type' => 'int',
          'size' => 'tiny',
          'not null' => true,
          'default' => 0,
          'description' => 'A boolean indicating whether this data item has been deleted',
        ),
        'entity_id' => array(
          'type' => 'int',
          'unsigned' => true,
          'not null' => true,
          'description' => 'The entity id this data is attached to',
        ),
        'revision_id' => array(
          'type' => 'int',
          'unsigned' => true,
          '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' => array(
          'type' => 'varchar_ascii',
          'length' => 32,
          'not null' => true,
          'default' => '',
          'description' => 'The language code for this data item.',
        ),
        'delta' => array(
          'type' => 'int',
          'unsigned' => true,
          'not null' => true,
          'description' => 'The sequence number for this data item, used for multi-value fields',
        ),
        $field_name . '_shape' => array(
          'type' => 'varchar',
          'length' => 32,
          'not null' => false,
        ),
        $field_name . '_color' => array(
          'type' => 'varchar',
          'length' => 32,
          'not null' => false,
        ),
      ),
      'primary key' => array(
        'entity_id',
        'deleted',
        'delta',
        'langcode',
      ),
      'indexes' => array(
        'bundle' => array(
          'bundle',
        ),
        'revision_id' => array(
          'revision_id',
        ),
      ),
      'foreign keys' => array(
        $field_name . '_color' => array(
          'table' => 'color',
          'columns' => array(
            $field_name . '_color' => 'id',
          ),
        ),
      ),
    ),
  );
  $this
    ->setUpStorageSchema($expected);
  $table_mapping = new DefaultTableMapping($this->entityType, $this->storageDefinitions);
  $table_mapping
    ->setFieldNames($entity_type_id, array_keys($this->storageDefinitions));
  $table_mapping
    ->setExtraColumns($entity_type_id, array(
    'default_langcode',
  ));
  $this->storage
    ->expects($this
    ->any())
    ->method('getTableMapping')
    ->will($this
    ->returnValue($table_mapping));
  $this
    ->assertNull($this->storageSchema
    ->onFieldStorageDefinitionCreate($field_storage));
}