protected function EntityTranslationTest::doTestLanguageFallback in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/system/src/Tests/Entity/EntityTranslationTest.php \Drupal\system\Tests\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/
modules/ system/ src/ Tests/ Entity/ EntityTranslationTest.php - Tests language fallback applied to field and entity translations.
File
- core/
modules/ system/ src/ Tests/ Entity/ EntityTranslationTest.php, line 573 - Contains \Drupal\system\Tests\Entity\EntityTranslationTest.
Class
- EntityTranslationTest
- Tests entity translation functionality.
Namespace
Drupal\system\Tests\EntityCode
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 = array();
foreach ($this->langcodes as $langcode) {
$values[$langcode]['name'] = $this
->randomMachineName();
$values[$langcode]['user_id'] = mt_rand(0, 127);
}
$default_langcode = $this->langcodes[0];
$langcode = $this->langcodes[1];
$langcode2 = $this->langcodes[2];
$langcode_key = $this->entityManager
->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->entityManager
->getStorage($entity_type);
$entity = $controller
->create(array(
$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 = $this->entityManager
->getTranslationFromContext($entity, $langcode2);
$this
->assertEqual($translation
->language()
->getId(), $default_langcode, '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 = $this->entityManager
->getTranslationFromContext($entity, $langcode2);
$this
->assertEqual($translation
->language()
->getId(), $langcode, 'The current translation language matches the expected one.');
// Check that the current translation is properly returned.
$translation = $this->entityManager
->getTranslationFromContext($entity);
$this
->assertEqual($langcode, $translation
->language()
->getId(), 'The current translation language matches the topmost language fallback candidate.');
$entity
->addTranslation($current_langcode, $values[$current_langcode]);
$translation = $this->entityManager
->getTranslationFromContext($entity);
$this
->assertEqual($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(array(
$langcode_key => $default_langcode,
));
// Get an view builder.
$controller = $this->entityManager
->getViewBuilder($entity_type);
$entity2_build = $controller
->view($entity2);
$entity2_output = (string) $renderer
->renderRoot($entity2_build);
$translation = $this->entityManager
->getTranslationFromContext($entity2, $default_langcode);
$translation_build = $controller
->view($translation);
$translation_output = (string) $renderer
->renderRoot($translation_build);
$this
->assertIdentical($entity2_output, $translation_output, 'When the entity has no translation no fallback is applied.');
// Checks that entity translations are rendered properly.
$controller = $this->entityManager
->getViewBuilder($entity_type);
$build = $controller
->view($entity);
$renderer
->renderRoot($build);
$this
->assertEqual($build['label']['#markup'], $values[$current_langcode]['name'], '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
->assertEqual($build['label']['#markup'], $values[$expected]['name'], 'The entity is rendered in the expected language.');
}
}