function EntityTranslationTest::doTestEntityLanguageMethods

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

Executes the entity language method tests for the given entity type.

Parameters

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

1 call to EntityTranslationTest::doTestEntityLanguageMethods()
EntityTranslationTest::testEntityLanguageMethods in core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php
Tests language related methods of the Entity class.

File

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

Class

EntityTranslationTest
Tests entity translation functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

protected function doTestEntityLanguageMethods($entity_type) {
  $langcode_key = $this->entityTypeManager
    ->getDefinition($entity_type)
    ->getKey('langcode');
  $entity = $this->container
    ->get('entity_type.manager')
    ->getStorage($entity_type)
    ->create([
    'name' => 'test',
    'user_id' => $this->container
      ->get('current_user')
      ->id(),
  ]);
  $this->assertEquals($this->languageManager
    ->getDefaultLanguage()
    ->getId(), $entity->language()
    ->getId(), "{$entity_type}: Entity created with API has default language.");
  $entity = $this->container
    ->get('entity_type.manager')
    ->getStorage($entity_type)
    ->create([
    'name' => 'test',
    'user_id' => \Drupal::currentUser()->id(),
    $langcode_key => LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);
  $this->assertEquals(LanguageInterface::LANGCODE_NOT_SPECIFIED, $entity->language()
    ->getId(), "{$entity_type}: Entity language not specified.");
  $this->assertEmpty($entity->getTranslationLanguages(FALSE), "{$entity_type}: No translations are available");
  // Set the value in default language.
  $entity->set($this->fieldName, [
    0 => [
      'value' => 'default value',
    ],
  ]);
  // Get the value.
  $field = $entity->getTranslation(LanguageInterface::LANGCODE_DEFAULT)
    ->get($this->fieldName);
  $this->assertEquals('default value', $field->value, "{$entity_type}: Untranslated value retrieved.");
  $this->assertEquals(LanguageInterface::LANGCODE_NOT_SPECIFIED, $field->getLangcode(), "{$entity_type}: Field object has the expected langcode.");
  // Try to get add a translation to language neutral entity.
  try {
    $entity->addTranslation($this->langcodes[1]);
    $this->fail('Adding a translation to a language-neutral entity results in an error.');
  } catch (\InvalidArgumentException $e) {
    // Expected exception; just continue testing.
  }
  // Now, make the entity language-specific by assigning a language and test
  // translating it.
  $default_langcode = $this->langcodes[0];
  $entity->{$langcode_key}->value = $default_langcode;
  $entity->{$this->fieldName} = [];
  $this->assertEquals(\Drupal::languageManager()->getLanguage($this->langcodes[0]), $entity->language(), "{$entity_type}: Entity language retrieved.");
  $this->assertEmpty($entity->getTranslationLanguages(FALSE), "{$entity_type}: No translations are available");
  // Set the value in default language.
  $entity->set($this->fieldName, [
    0 => [
      'value' => 'default value',
    ],
  ]);
  // Get the value.
  $field = $entity->get($this->fieldName);
  $this->assertEquals('default value', $field->value, "{$entity_type}: Untranslated value retrieved.");
  $this->assertEquals($default_langcode, $field->getLangcode(), "{$entity_type}: Field object has the expected langcode.");
  // Set a translation.
  $entity->addTranslation($this->langcodes[1])
    ->set($this->fieldName, [
    0 => [
      'value' => 'translation 1',
    ],
  ]);
  $field = $entity->getTranslation($this->langcodes[1])->{$this->fieldName};
  $this->assertEquals('translation 1', $field->value, "{$entity_type}: Translated value set.");
  $this->assertEquals($this->langcodes[1], $field->getLangcode(), "{$entity_type}: Field object has the expected langcode.");
  // Make sure the untranslated value stays.
  $field = $entity->get($this->fieldName);
  $this->assertEquals('default value', $field->value, 'Untranslated value stays.');
  $this->assertEquals($default_langcode, $field->getLangcode(), 'Untranslated value has the expected langcode.');
  $translations[$this->langcodes[1]] = \Drupal::languageManager()->getLanguage($this->langcodes[1]);
  $this->assertEquals($translations, $entity->getTranslationLanguages(FALSE), 'Translations retrieved.');
  // Try to get a value using a language code for a non-existing translation.
  try {
    $entity->getTranslation($this->langcodes[2])
      ->get($this->fieldName)->value;
    $this->fail('Getting a non existing translation results in an error.');
  } catch (\InvalidArgumentException $e) {
    // Expected exception; just continue testing.
  }
  // Try to get a not available translation.
  $this->assertNull($entity->addTranslation($this->langcodes[2])
    ->get($this->fieldName)->value, "{$entity_type}: A translation that is not available is NULL.");
  // Try to get a value using an invalid language code.
  try {
    $entity->getTranslation('invalid')
      ->get($this->fieldName)->value;
    $this->fail('Getting an invalid translation results in an error.');
  } catch (\InvalidArgumentException $e) {
    // Expected exception; just continue testing.
  }
  // Try to set a value using an invalid language code.
  try {
    $entity->getTranslation('invalid')
      ->set($this->fieldName, NULL);
    $this->fail("{$entity_type}: Setting a translation for an invalid language throws an exception.");
  } catch (\InvalidArgumentException $e) {
    // Expected exception; just continue testing.
  }
  // Set the value in default language.
  $field_name = 'field_test_text';
  $entity->getTranslation($this->langcodes[1])
    ->set($field_name, [
    0 => [
      'value' => 'default value2',
    ],
  ]);
  // Get the value.
  $field = $entity->get($field_name);
  $this->assertEquals('default value2', $field->value, "{$entity_type}: Untranslated value set into a translation in non-strict mode.");
  $this->assertEquals($default_langcode, $field->getLangcode(), "{$entity_type}: Field object has the expected langcode.");
}

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