You are here

protected function EntityTranslationTest::doTestEntityLanguageMethods in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php \Drupal\KernelTests\Core\Entity\EntityTranslationTest::doTestEntityLanguageMethods()
  2. 9 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.

File

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

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(), new FormattableMarkup('%entity_type: Entity created with API has default language.', [
    '%entity_type' => $entity_type,
  ]));
  $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(), new FormattableMarkup('%entity_type: Entity language not specified.', [
    '%entity_type' => $entity_type,
  ]));
  $this
    ->assertEmpty($entity
    ->getTranslationLanguages(FALSE), new FormattableMarkup('%entity_type: No translations are available', [
    '%entity_type' => $entity_type,
  ]));

  // 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, new FormattableMarkup('%entity_type: Untranslated value retrieved.', [
    '%entity_type' => $entity_type,
  ]));
  $this
    ->assertEquals(LanguageInterface::LANGCODE_NOT_SPECIFIED, $field
    ->getLangcode(), new FormattableMarkup('%entity_type: Field object has the expected langcode.', [
    '%entity_type' => $entity_type,
  ]));

  // 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(), new FormattableMarkup('%entity_type: Entity language retrieved.', [
    '%entity_type' => $entity_type,
  ]));
  $this
    ->assertEmpty($entity
    ->getTranslationLanguages(FALSE), new FormattableMarkup('%entity_type: No translations are available', [
    '%entity_type' => $entity_type,
  ]));

  // 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, new FormattableMarkup('%entity_type: Untranslated value retrieved.', [
    '%entity_type' => $entity_type,
  ]));
  $this
    ->assertEquals($default_langcode, $field
    ->getLangcode(), new FormattableMarkup('%entity_type: Field object has the expected langcode.', [
    '%entity_type' => $entity_type,
  ]));

  // 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, new FormattableMarkup('%entity_type: Translated value set.', [
    '%entity_type' => $entity_type,
  ]));
  $this
    ->assertEquals($this->langcodes[1], $field
    ->getLangcode(), new FormattableMarkup('%entity_type: Field object has the expected langcode.', [
    '%entity_type' => $entity_type,
  ]));

  // 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, new FormattableMarkup('%entity_type: A translation that is not available is NULL.', [
    '%entity_type' => $entity_type,
  ]));

  // 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(new FormattableMarkup('%entity_type: Setting a translation for an invalid language throws an exception.', [
      '%entity_type' => $entity_type,
    ]));
  } 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, new FormattableMarkup('%entity_type: Untranslated value set into a translation in non-strict mode.', [
    '%entity_type' => $entity_type,
  ]));
  $this
    ->assertEquals($default_langcode, $field
    ->getLangcode(), new FormattableMarkup('%entity_type: Field object has the expected langcode.', [
    '%entity_type' => $entity_type,
  ]));
}