function EntityTranslationTest::testEntityAdapter

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php \Drupal\KernelTests\Core\Entity\EntityTranslationTest::testEntityAdapter()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php \Drupal\KernelTests\Core\Entity\EntityTranslationTest::testEntityAdapter()
  3. 10 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 778

Class

EntityTranslationTest
Tests entity translation functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testEntityAdapter() : void {
    $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, "Name correctly retrieved from '{$langcode}' adapter");
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.