getTableNames example

// 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());
  }

}

  protected function getSchemaFromStorageDefinition(FieldStorageDefinitionInterface $storage_definition) {
    assert(!$storage_definition->hasCustomStorage());
    $table_mapping = $this->getTableMapping($this->entityType, [$storage_definition]);
    $schema = [];
    if ($table_mapping->requiresDedicatedTableStorage($storage_definition)) {
      $schema = $this->getDedicatedTableSchema($storage_definition);
    }
    elseif ($table_mapping->allowsSharedTableStorage($storage_definition)) {
      $field_name = $storage_definition->getName();
      foreach (array_diff($table_mapping->getTableNames()$table_mapping->getDedicatedTableNames()) as $table_name) {
        if (in_array($field_name$table_mapping->getFieldNames($table_name))) {
          $column_names = $table_mapping->getColumnNames($storage_definition->getName());
          $schema[$table_name] = $this->getSharedTableFieldSchema($storage_definition$table_name$column_names);
        }
      }
    }
    return $schema;
  }

  /** * {@inheritdoc} */
/** * Asserts that the backup tables have been kept after a successful update. * * @internal */
  protected function assertBackupTables(): void {
    $backups = \Drupal::keyValue('entity.update_backup')->getAll();
    $backup = reset($backups);

    $schema = $this->database->schema();
    foreach ($backup['table_mapping']->getTableNames() as $table_name) {
      $this->assertTrue($schema->tableExists($table_name));
    }
  }

  /** * Tests that a failed entity schema update preserves the existing data. */
  public function testFieldableEntityTypeUpdatesErrorHandling() {
    $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_tables as $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 */
  public function testGetTableNames() {
    // The storage definitions are only used in getColumnNames() so we do not     // need to provide any here.     $table_mapping = new TestDefaultTableMapping($this->entityType, []);
    $this->assertSame([]$table_mapping->getTableNames());

    $table_mapping->setFieldNames('foo', []);
    $this->assertSame(['foo']$table_mapping->getTableNames());

    $table_mapping->setFieldNames('bar', []);
    $this->assertSame(['foo', 'bar']$table_mapping->getTableNames());

    $table_mapping->setExtraColumns('baz', []);
    $this->assertSame(['foo', 'bar', 'baz']$table_mapping->getTableNames());

    // Test that table names are not duplicated.
// 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');

    // Make sure the field schema can be deleted.     \Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionDelete($storage_definitions['custom_base_field']);
    \Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionDelete($storage_definitions['custom_bundle_field']);
    $this->assertFalse($this->database->schema()->fieldExists($base_table$base_column), 'Table column dropped');
    $this->assertFalse($this->database->schema()->tableExists($table), 'Table dropped');
  }

  
/** @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_keysarray_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 */
  public function testGetTableMappingEmpty() {
    $this->setUpEntityStorage();

    $mapping = $this->entityStorage->getTableMapping();
    $this->assertSame(['entity_test']$mapping->getTableNames());
    $this->assertSame([]$mapping->getFieldNames('entity_test'));
    $this->assertSame([]$mapping->getExtraColumns('entity_test'));
  }

  /** * 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 */

  protected function installEntitySchema($entity_type_id) {
    $entity_type_manager = \Drupal::entityTypeManager();
    $entity_type = $entity_type_manager->getDefinition($entity_type_id);
    \Drupal::service('entity_type.listener')->onEntityTypeCreate($entity_type);

    // 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 ($tables as $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. */
Home | Imprint | This part of the site doesn't use cookies.