function EntityTranslationTest::testFieldEntityReference

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

Class

EntityTranslationTest
Tests entity translation functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testFieldEntityReference() : void {
    $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()) {
                $this->assertEquals($langcode, $field->getEntity()
                    ->language()
                    ->getId(), "Translatable field {$field_name} on translation {$langcode} has correct entity reference in translation {$langcode}.");
            }
            else {
                $this->assertEquals($default_langcode, $field->getEntity()
                    ->language()
                    ->getId(), "Non translatable field {$field_name} on translation {$langcode} has correct entity reference in the default translation {$default_langcode}.");
            }
        }
    }
}

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