You are here

public function DefaultTableMappingTest::testGetFieldTableName in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/Entity/Sql/DefaultTableMappingTest.php \Drupal\Tests\Core\Entity\Sql\DefaultTableMappingTest::testGetFieldTableName()

Tests DefaultTableMapping::getFieldTableName().

@covers ::getFieldTableName

@dataProvider providerTestGetFieldTableName

Parameters

string[] $table_names: An associative array of table names that should hold the field columns, where keys can be 'base', 'data' and 'revision'.

string $expected: The expected table name.

File

core/tests/Drupal/Tests/Core/Entity/Sql/DefaultTableMappingTest.php, line 352
Contains \Drupal\Tests\Core\Entity\Sql\DefaultTableMappingTest.

Class

DefaultTableMappingTest
@coversDefaultClass \Drupal\Core\Entity\Sql\DefaultTableMapping @group Entity

Namespace

Drupal\Tests\Core\Entity\Sql

Code

public function testGetFieldTableName($table_names, $expected) {
  $field_name = 'test';
  $columns = [
    'test',
  ];
  $definition = $this
    ->setUpDefinition($field_name, $columns);
  $definition
    ->expects($this
    ->any())
    ->method('getColumns')
    ->willReturn($columns);
  $storage = $this
    ->getMockBuilder('\\Drupal\\Core\\Entity\\Sql\\SqlContentEntityStorage')
    ->disableOriginalConstructor()
    ->getMock();
  $storage
    ->expects($this
    ->any())
    ->method('getBaseTable')
    ->willReturn(isset($table_names['base']) ? $table_names['base'] : 'base_table');
  $storage
    ->expects($this
    ->any())
    ->method('getDataTable')
    ->willReturn(isset($table_names['data']) ? $table_names['data'] : NULL);
  $storage
    ->expects($this
    ->any())
    ->method('getRevisionTable')
    ->willReturn(isset($table_names['revision']) ? $table_names['revision'] : NULL);
  $entity_manager = $this
    ->getMock('\\Drupal\\Core\\Entity\\EntityManagerInterface');
  $entity_manager
    ->expects($this
    ->any())
    ->method('getStorage')
    ->willReturn($storage);
  $container = $this
    ->getMock('\\Symfony\\Component\\DependencyInjection\\ContainerInterface');
  $container
    ->expects($this
    ->any())
    ->method('get')
    ->willReturn($entity_manager);
  \Drupal::setContainer($container);
  $table_mapping = new DefaultTableMapping($this->entityType, [
    $field_name => $definition,
  ]);

  // Add the field to all the defined tables to ensure the correct one is
  // picked.
  foreach ($table_names as $table_name) {
    $table_mapping
      ->setFieldNames($table_name, [
      $field_name,
    ]);
  }
  $this
    ->assertEquals($expected, $table_mapping
    ->getFieldTableName('test'));
}