You are here

protected function FieldTranslationSqlStorageTest::assertFieldStorageLangcode in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Entity/FieldTranslationSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldTranslationSqlStorageTest::assertFieldStorageLangcode()
  2. 9 core/tests/Drupal/KernelTests/Core/Entity/FieldTranslationSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldTranslationSqlStorageTest::assertFieldStorageLangcode()

Checks whether field languages are correctly stored for the given entity.

@internal

Parameters

\Drupal\Core\Entity\FieldableEntityInterface $entity: The entity fields are attached to.

string $message: (optional) A message to display with the assertion.

File

core/tests/Drupal/KernelTests/Core/Entity/FieldTranslationSqlStorageTest.php, line 76

Class

FieldTranslationSqlStorageTest
Tests Field translation SQL Storage.

Namespace

Drupal\KernelTests\Core\Entity

Code

protected function assertFieldStorageLangcode(FieldableEntityInterface $entity, string $message = '') : void {
  $status = TRUE;
  $entity_type = $entity
    ->getEntityTypeId();
  $id = $entity
    ->id();
  $langcode = $entity
    ->getUntranslated()
    ->language()
    ->getId();
  $fields = [
    $this->fieldName,
    $this->untranslatableFieldName,
  ];

  /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
  $table_mapping = \Drupal::entityTypeManager()
    ->getStorage($entity_type)
    ->getTableMapping();
  foreach ($fields as $field_name) {
    $field_storage = FieldStorageConfig::loadByName($entity_type, $field_name);
    $table = $table_mapping
      ->getDedicatedDataTableName($field_storage);
    $record = \Drupal::database()
      ->select($table, 'f')
      ->fields('f')
      ->condition('f.entity_id', $id)
      ->condition('f.revision_id', $id)
      ->execute()
      ->fetchObject();
    if ($record->langcode != $langcode) {
      $status = FALSE;
      break;
    }
  }
  $this
    ->assertTrue($status, $message);
}