You are here

public function EntityTranslationTest::testFieldEntityReference 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::testFieldEntityReference()

Tests if entity references are correct after adding a new translation.

File

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

Class

EntityTranslationTest
Tests entity translation functionality.

Namespace

Drupal\system\Tests\Entity

Code

public function testFieldEntityReference() {
  $entity_type = 'entity_test_mul';
  $controller = $this->entityManager
    ->getStorage($entity_type);

  /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  $entity = $controller
    ->create();
  foreach ($this->langcodes as $langcode) {
    $entity
      ->addTranslation($langcode);
  }
  $default_langcode = $entity
    ->getUntranslated()
    ->language()
    ->getId();
  foreach (array_keys($entity
    ->getTranslationLanguages()) as $langcode) {
    $translation = $entity
      ->getTranslation($langcode);
    foreach ($translation
      ->getFields() as $field_name => $field) {
      if ($field
        ->getFieldDefinition()
        ->isTranslatable()) {
        $args = [
          '%field_name' => $field_name,
          '%langcode' => $langcode,
        ];
        $this
          ->assertEqual($langcode, $field
          ->getEntity()
          ->language()
          ->getId(), format_string('Translatable field %field_name on translation %langcode has correct entity reference in translation %langcode.', $args));
      }
      else {
        $args = [
          '%field_name' => $field_name,
          '%langcode' => $langcode,
          '%default_langcode' => $default_langcode,
        ];
        $this
          ->assertEqual($default_langcode, $field
          ->getEntity()
          ->language()
          ->getId(), format_string('Non translatable field %field_name on translation %langcode has correct entity reference in the default translation %default_langcode.', $args));
      }
    }
  }
}