You are here

public function EntityTranslationTest::testFieldEntityReference in Drupal 9

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

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

File

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

Class

EntityTranslationTest
Tests entity translation functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testFieldEntityReference() {
  $entity_type = 'entity_test_mul';
  $controller = $this->entityTypeManager
    ->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
          ->assertEquals($langcode, $field
          ->getEntity()
          ->language()
          ->getId(), new FormattableMarkup('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
          ->assertEquals($default_langcode, $field
          ->getEntity()
          ->language()
          ->getId(), new FormattableMarkup('Non translatable field %field_name on translation %langcode has correct entity reference in the default translation %default_langcode.', $args));
      }
    }
  }
}