You are here

public function SqlContentEntityStorageTest::testGetTableMappingRevisionableTranslatableWithFields in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageTest.php \Drupal\Tests\Core\Entity\Sql\SqlContentEntityStorageTest::testGetTableMappingRevisionableTranslatableWithFields()
  2. 10 core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageTest.php \Drupal\Tests\Core\Entity\Sql\SqlContentEntityStorageTest::testGetTableMappingRevisionableTranslatableWithFields()

Tests getTableMapping() with a complex entity type with fields.

@covers ::__construct @covers ::getTableMapping

@dataProvider providerTestGetTableMappingSimple()

Parameters

string[] $entity_keys: A map of entity keys to use for the mocked entity type.

File

core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageTest.php, line 943
Contains \Drupal\Tests\Core\Entity\Sql\SqlContentEntityStorageTest.

Class

SqlContentEntityStorageTest
@coversDefaultClass \Drupal\Core\Entity\Sql\SqlContentEntityStorage @group Entity

Namespace

Drupal\Tests\Core\Entity\Sql

Code

public function testGetTableMappingRevisionableTranslatableWithFields(array $entity_keys) {

  // This allows to re-use the data provider.
  $entity_keys = [
    'id' => $entity_keys['id'],
    'revision' => 'test_revision',
    'bundle' => $entity_keys['bundle'],
    'uuid' => $entity_keys['uuid'],
    'langcode' => 'langcode',
  ];

  // PHPUnit does not allow for multiple data providers.
  $test_cases = [
    [],
    [
      'revision_created' => 'revision_timestamp',
    ],
    [
      'revision_user' => 'revision_uid',
    ],
    [
      'revision_log_message' => 'revision_log',
    ],
    [
      'revision_created' => 'revision_timestamp',
      'revision_user' => 'revision_uid',
    ],
    [
      'revision_created' => 'revision_timestamp',
      'revision_log_message' => 'revision_log',
    ],
    [
      'revision_user' => 'revision_uid',
      'revision_log_message' => 'revision_log',
    ],
    [
      'revision_created' => 'revision_timestamp',
      'revision_user' => 'revision_uid',
      'revision_log_message' => 'revision_log',
    ],
  ];
  foreach ($test_cases as $revision_metadata_field_names) {
    $this
      ->setUp();
    $base_field_names = [
      'title',
    ];
    $field_names = array_merge(array_values(array_filter($entity_keys)), $base_field_names);
    $this->fieldDefinitions = $this
      ->mockFieldDefinitions($field_names);
    $revisionable_field_names = [
      'description',
      'owner',
    ];
    $this->fieldDefinitions += $this
      ->mockFieldDefinitions(array_merge($revisionable_field_names, array_values($revision_metadata_field_names)), [
      'isRevisionable' => TRUE,
    ]);
    $this->entityType
      ->expects($this
      ->atLeastOnce())
      ->method('isRevisionable')
      ->will($this
      ->returnValue(TRUE));
    $this->entityType
      ->expects($this
      ->atLeastOnce())
      ->method('isTranslatable')
      ->will($this
      ->returnValue(TRUE));
    $this->entityType
      ->expects($this
      ->atLeastOnce())
      ->method('getDataTable')
      ->will($this
      ->returnValue('entity_test_field_data'));
    $this->entityType
      ->expects($this
      ->any())
      ->method('getKey')
      ->will($this
      ->returnValueMap([
      [
        'id',
        $entity_keys['id'],
      ],
      [
        'uuid',
        $entity_keys['uuid'],
      ],
      [
        'bundle',
        $entity_keys['bundle'],
      ],
      [
        'revision',
        $entity_keys['revision'],
      ],
      [
        'langcode',
        $entity_keys['langcode'],
      ],
    ]));
    $this->entityType
      ->expects($this
      ->any())
      ->method('getRevisionMetadataKeys')
      ->will($this
      ->returnValue($revision_metadata_field_names));
    $this
      ->setUpEntityStorage();
    $mapping = $this->entityStorage
      ->getTableMapping();
    $expected = [
      'entity_test',
      'entity_test_field_data',
      'entity_test_revision',
      'entity_test_field_revision',
    ];
    $this
      ->assertEquals($expected, $mapping
      ->getTableNames());
    $expected = [
      'entity_test',
      'entity_test_field_data',
      'entity_test_revision',
      'entity_test_field_revision',
    ];
    $this
      ->assertEquals($expected, $mapping
      ->getTableNames());

    // The default language code is not stored on the base table.
    $expected = array_values(array_filter([
      $entity_keys['id'],
      $entity_keys['revision'],
      $entity_keys['bundle'],
      $entity_keys['uuid'],
      $entity_keys['langcode'],
    ]));
    $actual = $mapping
      ->getFieldNames('entity_test');
    $this
      ->assertEquals($expected, $actual);

    // The revision table on the other hand does not store the bundle and the
    // UUID.
    $expected = array_merge(array_filter([
      $entity_keys['id'],
      $entity_keys['revision'],
      $entity_keys['langcode'],
    ]), array_values($revision_metadata_field_names));
    $actual = $mapping
      ->getFieldNames('entity_test_revision');
    $this
      ->assertEquals($expected, $actual);

    // The UUID is not stored on the data table.
    $expected = array_merge(array_filter([
      $entity_keys['id'],
      $entity_keys['revision'],
      $entity_keys['bundle'],
      $entity_keys['langcode'],
    ]), $base_field_names, $revisionable_field_names);
    $actual = $mapping
      ->getFieldNames('entity_test_field_data');
    $this
      ->assertEquals($expected, $actual);

    // The data revision also does not store the bundle.
    $expected = array_merge(array_filter([
      $entity_keys['id'],
      $entity_keys['revision'],
      $entity_keys['langcode'],
    ]), $revisionable_field_names);
    $actual = $mapping
      ->getFieldNames('entity_test_field_revision');
    $this
      ->assertEquals($expected, $actual);
    $expected = [];
    $actual = $mapping
      ->getExtraColumns('entity_test');
    $this
      ->assertEquals($expected, $actual);
    $actual = $mapping
      ->getExtraColumns('entity_test_revision');
    $this
      ->assertEquals($expected, $actual);
    $actual = $mapping
      ->getExtraColumns('entity_test_field_data');
    $this
      ->assertEquals($expected, $actual);
    $actual = $mapping
      ->getExtraColumns('entity_test_field_revision');
    $this
      ->assertEquals($expected, $actual);
  }
}