function EntityTranslationTest::doTestLanguageFallback

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php \Drupal\KernelTests\Core\Entity\EntityTranslationTest::doTestLanguageFallback()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php \Drupal\KernelTests\Core\Entity\EntityTranslationTest::doTestLanguageFallback()
  3. 10 core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php \Drupal\KernelTests\Core\Entity\EntityTranslationTest::doTestLanguageFallback()

Executes the language fallback test for the given entity type.

Parameters

string $entity_type: The entity type to run the tests with.

1 call to EntityTranslationTest::doTestLanguageFallback()
EntityTranslationTest::testLanguageFallback in core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php
Tests language fallback applied to field and entity translations.

File

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

Class

EntityTranslationTest
Tests entity translation functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

protected function doTestLanguageFallback($entity_type) {
    
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = $this->container
        ->get('renderer');
    $current_langcode = $this->languageManager
        ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
        ->getId();
    $this->langcodes[] = $current_langcode;
    $values = [];
    foreach ($this->langcodes as $langcode) {
        $values[$langcode]['name'] = $this->randomMachineName();
        $values[$langcode]['user_id'] = 2;
    }
    $default_langcode = $this->langcodes[0];
    $langcode = $this->langcodes[1];
    $langcode2 = $this->langcodes[2];
    $langcode_key = $this->entityTypeManager
        ->getDefinition($entity_type)
        ->getKey('langcode');
    $languages = $this->languageManager
        ->getLanguages();
    $language = ConfigurableLanguage::load($languages[$langcode]->getId());
    $language->set('weight', 1);
    $language->save();
    $this->languageManager
        ->reset();
    $controller = $this->entityTypeManager
        ->getStorage($entity_type);
    $entity = $controller->create([
        $langcode_key => $default_langcode,
    ] + $values[$default_langcode]);
    $entity->save();
    $entity->addTranslation($langcode, $values[$langcode]);
    $entity->save();
    // Check that retrieving the current translation works as expected.
    $entity = $this->reloadEntity($entity);
    $translation = \Drupal::service('entity.repository')->getTranslationFromContext($entity, $langcode2);
    $this->assertEquals($default_langcode, $translation->language()
        ->getId(), 'The current translation language matches the expected one.');
    // Check that language fallback respects language weight by default.
    $language = ConfigurableLanguage::load($languages[$langcode]->getId());
    $language->set('weight', -1);
    $language->save();
    $translation = \Drupal::service('entity.repository')->getTranslationFromContext($entity, $langcode2);
    $this->assertEquals($langcode, $translation->language()
        ->getId(), 'The current translation language matches the expected one.');
    // Check that the current translation is properly returned.
    $translation = \Drupal::service('entity.repository')->getTranslationFromContext($entity);
    $this->assertEquals($langcode, $translation->language()
        ->getId(), 'The current translation language matches the topmost language fallback candidate.');
    $entity->addTranslation($current_langcode, $values[$current_langcode]);
    $translation = \Drupal::service('entity.repository')->getTranslationFromContext($entity);
    $this->assertEquals($current_langcode, $translation->language()
        ->getId(), 'The current translation language matches the current language.');
    // Check that if the entity has no translation no fallback is applied.
    $entity2 = $controller->create([
        $langcode_key => $default_langcode,
    ]);
    // Get a view builder.
    $controller = $this->entityTypeManager
        ->getViewBuilder($entity_type);
    $entity2_build = $controller->view($entity2);
    $entity2_output = (string) $renderer->renderRoot($entity2_build);
    $translation = \Drupal::service('entity.repository')->getTranslationFromContext($entity2, $default_langcode);
    $translation_build = $controller->view($translation);
    $translation_output = (string) $renderer->renderRoot($translation_build);
    $this->assertSame($entity2_output, $translation_output, 'When the entity has no translation no fallback is applied.');
    // Checks that entity translations are rendered properly.
    $controller = $this->entityTypeManager
        ->getViewBuilder($entity_type);
    $build = $controller->view($entity);
    $renderer->renderRoot($build);
    $this->assertEquals($values[$current_langcode]['name'], $build['label']['#markup'], 'By default the entity is rendered in the current language.');
    $langcodes = array_combine($this->langcodes, $this->langcodes);
    // We have no translation for the $langcode2 language, hence the expected
    // result is the topmost existing translation, that is $langcode.
    $langcodes[$langcode2] = $langcode;
    foreach ($langcodes as $desired => $expected) {
        $build = $controller->view($entity, 'full', $desired);
        // Unset the #cache key so that a fresh render is produced with each pass,
        // making the renderable array keys available to compare.
        unset($build['#cache']);
        $renderer->renderRoot($build);
        $this->assertEquals($values[$expected]['name'], $build['label']['#markup'], 'The entity is rendered in the expected language.');
    }
}

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