You are here

public function EntityTranslationTest::testEntityAdapter in Drupal 10

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

Tests how entity adapters work with translations.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php, line 780

Class

EntityTranslationTest
Tests entity translation functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testEntityAdapter() {
  $entity_type = 'entity_test';
  $default_langcode = 'en';
  $values[$default_langcode] = [
    'name' => $this
      ->randomString(),
  ];
  $controller = $this->entityTypeManager
    ->getStorage($entity_type);

  /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  $entity = $controller
    ->create($values[$default_langcode]);
  foreach ($this->langcodes as $langcode) {
    $values[$langcode] = [
      'name' => $this
        ->randomString(),
    ];
    $entity
      ->addTranslation($langcode, $values[$langcode]);
  }
  $langcodes = array_merge([
    $default_langcode,
  ], $this->langcodes);
  foreach ($langcodes as $langcode) {
    $adapter = $entity
      ->getTranslation($langcode)
      ->getTypedData();
    $name = $adapter
      ->get('name')->value;
    $this
      ->assertEquals($values[$langcode]['name'], $name, new FormattableMarkup('Name correctly retrieved from "@langcode" adapter', [
      '@langcode' => $langcode,
    ]));
  }
}