Commit cf458f2e authored by plach's avatar plach

Issue #2981220 by amateescu, jibran, tstoeckler: SqlContentEntityStorage...

Issue #2981220 by amateescu, jibran, tstoeckler: SqlContentEntityStorage should use the table names provided by its table mapping
parent 79630716
......@@ -244,6 +244,54 @@ public static function create(ContentEntityTypeInterface $entity_type, array $st
return $table_mapping;
}
/**
* Gets the base table name.
*
* @return string
* The base table name.
*
* @internal
*/
public function getBaseTable() {
return $this->baseTable;
}
/**
* Gets the revision table name.
*
* @return string|null
* The revision table name.
*
* @internal
*/
public function getRevisionTable() {
return $this->revisionTable;
}
/**
* Gets the data table name.
*
* @return string|null
* The data table name.
*
* @internal
*/
public function getDataTable() {
return $this->dataTable;
}
/**
* Gets the revision data table name.
*
* @return string|null
* The revision data table name.
*
* @internal
*/
public function getRevisionDataTable() {
return $this->revisionDataTable;
}
/**
* {@inheritdoc}
*/
......
......@@ -185,22 +185,21 @@ protected function initTableLayout() {
$this->dataTable = NULL;
$this->revisionDataTable = NULL;
// @todo Remove table names from the entity type definition in
// https://www.drupal.org/node/2232465.
$this->baseTable = $this->entityType->getBaseTable() ?: $this->entityTypeId;
$table_mapping = $this->getTableMapping();
$this->baseTable = $table_mapping->getBaseTable();
$revisionable = $this->entityType->isRevisionable();
if ($revisionable) {
$this->revisionKey = $this->entityType->getKey('revision') ?: 'revision_id';
$this->revisionTable = $this->entityType->getRevisionTable() ?: $this->entityTypeId . '_revision';
$this->revisionTable = $table_mapping->getRevisionTable();
}
$translatable = $this->entityType->isTranslatable();
if ($translatable) {
$this->dataTable = $this->entityType->getDataTable() ?: $this->entityTypeId . '_field_data';
$this->dataTable = $table_mapping->getDataTable();
$this->langcodeKey = $this->entityType->getKey('langcode');
$this->defaultLangcodeKey = $this->entityType->getKey('default_langcode');
}
if ($revisionable && $translatable) {
$this->revisionDataTable = $this->entityType->getRevisionDataTable() ?: $this->entityTypeId . '_field_revision';
$this->revisionDataTable = $table_mapping->getRevisionDataTable();
}
}
......@@ -288,6 +287,11 @@ public function setEntityType(EntityTypeInterface $entity_type) {
*/
public function setTableMapping(TableMappingInterface $table_mapping) {
$this->tableMapping = $table_mapping;
$this->baseTable = $table_mapping->getBaseTable();
$this->revisionTable = $table_mapping->getRevisionTable();
$this->dataTable = $table_mapping->getDataTable();
$this->revisionDataTable = $table_mapping->getRevisionDataTable();
}
/**
......@@ -656,7 +660,7 @@ protected function buildPropertyQuery(QueryInterface $entity_query, array $value
* A SelectQuery object for loading the entity.
*/
protected function buildQuery($ids, $revision_ids = FALSE) {
$query = $this->database->select($this->entityType->getBaseTable(), 'base');
$query = $this->database->select($this->baseTable, 'base');
$query->addTag($this->entityTypeId . '_load_multiple');
......@@ -734,7 +738,7 @@ public function delete(array $entities) {
protected function doDeleteFieldItems($entities) {
$ids = array_keys($entities);
$this->database->delete($this->entityType->getBaseTable())
$this->database->delete($this->baseTable)
->condition($this->idKey, $ids, 'IN')
->execute();
......@@ -1055,7 +1059,7 @@ protected function saveRevision(ContentEntityInterface $entity) {
$record->{$this->revisionKey} = $insert_id;
}
if ($entity->isDefaultRevision()) {
$this->database->update($this->entityType->getBaseTable())
$this->database->update($this->baseTable)
->fields([$this->revisionKey => $record->{$this->revisionKey}])
->condition($this->idKey, $record->{$this->idKey})
->execute();
......
......@@ -40,11 +40,6 @@ class EntityTypedDataDefinitionTest extends KernelTestBase {
protected function setUp() {
parent::setup();
NodeType::create([
'type' => 'article',
'name' => 'Article',
])->save();
$this->typedDataManager = $this->container->get('typed_data_manager');
}
......@@ -91,6 +86,11 @@ public function testFields() {
* Tests deriving metadata about entities.
*/
public function testEntities() {
NodeType::create([
'type' => 'article',
'name' => 'Article',
])->save();
$entity_definition = EntityDataDefinition::create('node');
$bundle_definition = EntityDataDefinition::create('node', 'article');
// Entities are complex data.
......
......@@ -15,6 +15,7 @@
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\QueryFactoryInterface;
use Drupal\Core\Entity\Sql\DefaultTableMapping;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\Core\Language\Language;
use Drupal\Tests\UnitTestCase;
......@@ -200,7 +201,7 @@ public function providerTestGetBaseTable() {
* @dataProvider providerTestGetRevisionTable
*/
public function testGetRevisionTable($revision_table, $expected) {
$this->entityType->expects($this->once())
$this->entityType->expects($this->any())
->method('isRevisionable')
->will($this->returnValue(TRUE));
$this->entityType->expects($this->once())
......@@ -241,7 +242,7 @@ public function providerTestGetRevisionTable() {
* @covers ::getDataTable
*/
public function testGetDataTable() {
$this->entityType->expects($this->once())
$this->entityType->expects($this->any())
->method('isTranslatable')
->will($this->returnValue(TRUE));
$this->entityType->expects($this->exactly(1))
......@@ -271,10 +272,10 @@ public function testGetDataTable() {
* @dataProvider providerTestGetRevisionDataTable
*/
public function testGetRevisionDataTable($revision_data_table, $expected) {
$this->entityType->expects($this->once())
$this->entityType->expects($this->any())
->method('isRevisionable')
->will($this->returnValue(TRUE));
$this->entityType->expects($this->once())
$this->entityType->expects($this->any())
->method('isTranslatable')
->will($this->returnValue(TRUE));
$this->entityType->expects($this->exactly(1))
......@@ -312,6 +313,51 @@ public function providerTestGetRevisionDataTable() {
];
}
/**
* Tests that setting a new table mapping also updates the table names.
*
* @covers ::setTableMapping
*/
public function testSetTableMapping() {
$this->entityType->expects($this->any())
->method('isRevisionable')
->will($this->returnValue(FALSE));
$this->entityType->expects($this->any())
->method('isTranslatable')
->will($this->returnValue(FALSE));
$this->entityType->expects($this->any())
->method('getRevisionMetadataKeys')
->willReturn([]);
$this->setUpEntityStorage();
$this->assertSame('entity_test', $this->entityStorage->getBaseTable());
$this->assertNull($this->entityStorage->getRevisionTable());
$this->assertNull($this->entityStorage->getDataTable());
$this->assertNull($this->entityStorage->getRevisionDataTable());
// Change the entity type definition and instantiate a new table mapping
// with it.
$updated_entity_type = $this->createMock('Drupal\Core\Entity\ContentEntityTypeInterface');
$updated_entity_type->expects($this->any())
->method('id')
->will($this->returnValue($this->entityTypeId));
$updated_entity_type->expects($this->any())
->method('isRevisionable')
->will($this->returnValue(TRUE));
$updated_entity_type->expects($this->any())
->method('isTranslatable')
->will($this->returnValue(TRUE));
$table_mapping = new DefaultTableMapping($updated_entity_type, []);
$this->entityStorage->setTableMapping($table_mapping);
$this->assertSame('entity_test', $this->entityStorage->getBaseTable());
$this->assertSame('entity_test_revision', $this->entityStorage->getRevisionTable());
$this->assertSame('entity_test_field_data', $this->entityStorage->getDataTable());
$this->assertSame('entity_test_field_revision', $this->entityStorage->getRevisionDataTable());
}
/**
* Tests ContentEntityDatabaseStorage::onEntityTypeCreate().
*
......@@ -1200,10 +1246,12 @@ public function testLoadMultipleNoPersistentCache() {
$entity_storage = $this->getMockBuilder('Drupal\Core\Entity\Sql\SqlContentEntityStorage')
->setConstructorArgs([$this->entityType, $this->connection, $this->entityManager, $this->cache, $this->languageManager, new MemoryCache()])
->setMethods(['getFromStorage', 'invokeStorageLoadHook'])
->setMethods(['getFromStorage', 'invokeStorageLoadHook', 'initTableLayout'])
->getMock();
$entity_storage->method('invokeStorageLoadHook')
->willReturn(NULL);
$entity_storage->method('initTableLayout')
->willReturn(NULL);
$entity_storage->expects($this->once())
->method('getFromStorage')
->with([$id])
......@@ -1252,10 +1300,12 @@ public function testLoadMultiplePersistentCacheMiss() {
$entity_storage = $this->getMockBuilder('Drupal\Core\Entity\Sql\SqlContentEntityStorage')
->setConstructorArgs([$this->entityType, $this->connection, $this->entityManager, $this->cache, $this->languageManager, new MemoryCache()])
->setMethods(['getFromStorage', 'invokeStorageLoadHook'])
->setMethods(['getFromStorage', 'invokeStorageLoadHook', 'initTableLayout'])
->getMock();
$entity_storage->method('invokeStorageLoadHook')
->willReturn(NULL);
$entity_storage->method('initTableLayout')
->willReturn(NULL);
$entity_storage->expects($this->once())
->method('getFromStorage')
->with([$id])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment