You are here

function EntityTranslationTest::testEntityAdapter in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/EntityTranslationTest.php \Drupal\system\Tests\Entity\EntityTranslationTest::testEntityAdapter()

Tests how entity adapters work with translations.

File

core/modules/system/src/Tests/Entity/EntityTranslationTest.php, line 763
Contains \Drupal\system\Tests\Entity\EntityTranslationTest.

Class

EntityTranslationTest
Tests entity translation functionality.

Namespace

Drupal\system\Tests\Entity

Code

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

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