// Check that the table mapping contains both the data and the revision
// tables exist for a multi-valued base field.
$expected = [ 'entity_test_mulrev', 'entity_test_mulrev_property_data', 'entity_test_mulrev_revision', 'entity_test_mulrev_property_revision', $dedicated_data_table, $dedicated_revision_table, ]; $this->assertEquals($expected, $this->tableMapping->getTableNames()); }
/**
* Asserts that the backup tables have been kept after a successful update.
*
* @internal
*/ protectedfunctionassertBackupTables(): void { $backups = \Drupal::keyValue('entity.update_backup')->getAll(); $backup = reset($backups);
/**
* Tests that a failed entity schema update preserves the existing data.
*/ publicfunctiontestFieldableEntityTypeUpdatesErrorHandling(){ $schema = $this->database->schema();
// First, convert the entity type to be translatable for better coverage and
// access control to 'update' and 'delete' operations.
if(!($query instanceof SelectInterface)){ return; }
// The tables in the query. This can include media entity tables and other
// tables. Tables might be joined more than once, with aliases.
$query_tables = $query->getTables();
// The tables belonging to media entity storage.
$table_mapping = \Drupal::entityTypeManager()->getStorage('media')->getTableMapping(); $media_tables = $table_mapping->getTableNames();
// For each table in the query, if it's a media entity storage table, add a
// condition to filter out records belonging to a media entity that we wish
// to hide.
foreach($query_tablesas$alias => $info){ // Skip over subqueries.
if($info['table'] instanceof SelectInterface){ continue; } $real_table_name = $info['table']; if(in_array($real_table_name, $media_tables)){
/**
* Tests DefaultTableMapping::getTableNames().
*
* @covers ::getTableNames
*/ publicfunctiontestGetTableNames(){ // The storage definitions are only used in getColumnNames() so we do not
// need to provide any here.
$table_mapping = newTestDefaultTableMapping($this->entityType, []); $this->assertSame([], $table_mapping->getTableNames());
// Install the module which adds the field.
$this->installModule('entity_schema_test'); $storage_definitions = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions('entity_test_update'); $this->assertNotNull($storage_definitions['custom_base_field'], 'Base field definition found.'); $this->assertNotNull($storage_definitions['custom_bundle_field'], 'Bundle field definition found.');
// Make sure the field schema can be created.
\Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionCreate($storage_definitions['custom_base_field']); \Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionCreate($storage_definitions['custom_bundle_field']); /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ $table_mapping = $this->entityTypeManager->getStorage('entity_test_update')->getTableMapping(); $base_table = current($table_mapping->getTableNames()); $base_column = current($table_mapping->getColumnNames('custom_base_field')); $this->assertTrue($this->database->schema()->fieldExists($base_table, $base_column), 'Table column created'); $table = $table_mapping->getDedicatedDataTableName($storage_definitions['custom_bundle_field']); $this->assertTrue($this->database->schema()->tableExists($table), 'Table created');
/** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ $table_mapping = $this->storage->getTableMapping($field_definitions); // Fetch all fields that can appear in both the base table and the data
// table.
$duplicate_fields = array_intersect_key($entity_keys, array_flip(['id', 'revision', 'bundle'])); // Iterate over each table we have so far and collect field data for each.
// Based on whether the field is in the field_definitions provided by the
// entity field manager.
// @todo We should better just rely on information coming from the entity
// storage.
// @todo https://www.drupal.org/node/2337511
foreach($table_mapping->getTableNames()as$table){ foreach($table_mapping->getFieldNames($table)as$field_name){ // To avoid confusing duplication in the user interface, for fields
// that are on both base and data tables, only add them on the data
// table (same for revision vs. revision data).
if($data_table&&($table === $base_table || $table === $revision_table) && in_array($field_name, $duplicate_fields)){ continue; } $this->mapFieldDefinition($table, $field_name, $field_definitions[$field_name], $table_mapping, $data[$table]); } }
/**
* Tests getTableMapping() with an empty entity type.
*
* @covers ::__construct
* @covers ::getTableMapping
*/ publicfunctiontestGetTableMappingEmpty(){ $this->setUpEntityStorage();
/**
* Tests getTableMapping() with a simple entity type.
*
* @param string[] $entity_keys
* A map of entity keys to use for the mocked entity type.
*
* @covers ::__construct
* @covers ::getTableMapping
*
* @dataProvider providerTestGetTableMappingSimple
*/
// For test runs, the most common storage backend is a SQL database. For
// this case, ensure the tables got created.
$storage = $entity_type_manager->getStorage($entity_type_id); if($storage instanceof SqlEntityStorageInterface){ $tables = $storage->getTableMapping()->getTableNames(); $db_schema = $this->container->get('database')->schema(); foreach($tablesas$table){ $this->assertTrue($db_schema->tableExists($table), "The entity type table '$table' for the entity type '$entity_type_id' should exist."); } } }
/**
* Enables modules for this test.
*
* This method does not install modules fully. Services and hooks for the
* module are available, but the install process is not performed.
*
* To install test modules outside of the testing environment, add
* @code
* $settings['extension_discovery_scan_tests'] = TRUE;
* @endcode
* to your settings.php.
*
* @param string[] $modules
* A list of modules to enable. Dependencies are not resolved; i.e.,
* multiple modules have to be specified individually. The modules are only
* added to the active module list and loaded; i.e., their database schema
* is not installed. hook_install() is not invoked. A custom module weight
* is not applied.
*
* @throws \LogicException
* If any module in $modules is already enabled.
* @throws \RuntimeException
* If a module is not enabled after enabling it.
*/