function SqlContentEntityStorageTest::testOnEntityTypeCreate

Tests ContentEntityDatabaseStorage::onEntityTypeCreate().

@covers ::__construct
@covers ::onEntityTypeCreate
@covers ::getTableMapping

File

core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageTest.php, line 364

Class

SqlContentEntityStorageTest
@coversDefaultClass \Drupal\Core\Entity\Sql\SqlContentEntityStorage[[api-linebreak]] @group Entity

Namespace

Drupal\Tests\Core\Entity\Sql

Code

public function testOnEntityTypeCreate() {
  $columns = [
    'value' => [
      'type' => 'int',
    ],
  ];
  $this->fieldDefinitions = $this->mockFieldDefinitions([
    'id',
  ]);
  $this->fieldDefinitions['id']
    ->expects($this->any())
    ->method('getColumns')
    ->will($this->returnValue($columns));
  $this->fieldDefinitions['id']
    ->expects($this->once())
    ->method('getSchema')
    ->will($this->returnValue([
    'columns' => $columns,
  ]));
  $this->entityType
    ->expects($this->once())
    ->method('getKeys')
    ->will($this->returnValue([
    'id' => 'id',
  ]));
  $this->entityType
    ->expects($this->any())
    ->method('hasKey')
    ->will($this->returnValueMap([
    // SqlContentEntityStorageSchema::initializeBaseTable()
[
      'revision',
      FALSE,
    ],
    // SqlContentEntityStorageSchema::processBaseTable()
[
      'id',
      TRUE,
    ],
  ]));
  $this->entityType
    ->expects($this->any())
    ->method('getKey')
    ->will($this->returnValueMap([
    // EntityStorageBase::__construct()
[
      'id',
      'id',
    ],
    // ContentEntityStorageBase::__construct()
[
      'uuid',
      NULL,
    ],
    [
      'bundle',
      NULL,
    ],
    // SqlContentEntityStorageSchema::initializeBaseTable()
[
      'id' => 'id',
    ],
    // SqlContentEntityStorageSchema::processBaseTable()
[
      'id' => 'id',
    ],
  ]));
  $this->setUpEntityStorage();
  $expected = [
    'description' => 'The base table for entity_test entities.',
    'fields' => [
      'id' => [
        'type' => 'serial',
        'not null' => TRUE,
      ],
    ],
    'primary key' => [
      'id',
    ],
    'unique keys' => [],
    'indexes' => [],
    'foreign keys' => [],
  ];
  $schema_handler = $this->getMockBuilder('Drupal\\Core\\Database\\Schema')
    ->disableOriginalConstructor()
    ->getMock();
  $schema_handler->expects($this->any())
    ->method('createTable')
    ->with($this->equalTo('entity_test'), $this->equalTo($expected));
  $this->connection
    ->expects($this->once())
    ->method('schema')
    ->will($this->returnValue($schema_handler));
  $storage = $this->getMockBuilder('Drupal\\Core\\Entity\\Sql\\SqlContentEntityStorage')
    ->setConstructorArgs([
    $this->entityType,
    $this->connection,
    $this->entityFieldManager,
    $this->cache,
    $this->languageManager,
    new MemoryCache(),
    $this->entityTypeBundleInfo,
    $this->entityTypeManager,
  ])
    ->setMethods([
    'getStorageSchema',
  ])
    ->getMock();
  $key_value = $this->createMock('Drupal\\Core\\KeyValueStore\\KeyValueStoreInterface');
  $schema_handler = $this->getMockBuilder('Drupal\\Core\\Entity\\Sql\\SqlContentEntityStorageSchema')
    ->setConstructorArgs([
    $this->entityTypeManager,
    $this->entityType,
    $storage,
    $this->connection,
    $this->entityFieldManager,
  ])
    ->setMethods([
    'installedStorageSchema',
    'createSharedTableSchema',
  ])
    ->getMock();
  $schema_handler->expects($this->any())
    ->method('installedStorageSchema')
    ->will($this->returnValue($key_value));
  $storage->expects($this->any())
    ->method('getStorageSchema')
    ->will($this->returnValue($schema_handler));
  $storage->onEntityTypeCreate($this->entityType);
}

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