You are here

protected function EntityTranslationTest::doTestEntityLanguageMethods in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/EntityTranslationTest.php \Drupal\system\Tests\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/modules/system/src/Tests/Entity/EntityTranslationTest.php
Tests language related methods of the Entity class.

File

core/modules/system/src/Tests/Entity/EntityTranslationTest.php, line 38
Contains \Drupal\system\Tests\Entity\EntityTranslationTest.

Class

EntityTranslationTest
Tests entity translation functionality.

Namespace

Drupal\system\Tests\Entity

Code

protected function doTestEntityLanguageMethods($entity_type) {
  $langcode_key = $this->entityManager
    ->getDefinition($entity_type)
    ->getKey('langcode');
  $entity = entity_create($entity_type, array(
    'name' => 'test',
    'user_id' => $this->container
      ->get('current_user')
      ->id(),
  ));
  $this
    ->assertEqual($entity
    ->language()
    ->getId(), $this->languageManager
    ->getDefaultLanguage()
    ->getId(), format_string('%entity_type: Entity created with API has default language.', array(
    '%entity_type' => $entity_type,
  )));
  $entity = entity_create($entity_type, array(
    'name' => 'test',
    'user_id' => \Drupal::currentUser()
      ->id(),
    $langcode_key => LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ));
  $this
    ->assertEqual($entity
    ->language()
    ->getId(), LanguageInterface::LANGCODE_NOT_SPECIFIED, format_string('%entity_type: Entity language not specified.', array(
    '%entity_type' => $entity_type,
  )));
  $this
    ->assertFalse($entity
    ->getTranslationLanguages(FALSE), format_string('%entity_type: No translations are available', array(
    '%entity_type' => $entity_type,
  )));

  // Set the value in default language.
  $entity
    ->set($this->fieldName, array(
    0 => array(
      'value' => 'default value',
    ),
  ));

  // Get the value.
  $field = $entity
    ->getTranslation(LanguageInterface::LANGCODE_DEFAULT)
    ->get($this->fieldName);
  $this
    ->assertEqual($field->value, 'default value', format_string('%entity_type: Untranslated value retrieved.', array(
    '%entity_type' => $entity_type,
  )));
  $this
    ->assertEqual($field
    ->getLangcode(), LanguageInterface::LANGCODE_NOT_SPECIFIED, format_string('%entity_type: Field object has the expected langcode.', array(
    '%entity_type' => $entity_type,
  )));

  // Try to get add a translation to language neutral entity.
  $message = 'Adding a translation to a language-neutral entity results in an error.';
  try {
    $entity
      ->addTranslation($this->langcodes[1]);
    $this
      ->fail($message);
  } catch (\InvalidArgumentException $e) {
    $this
      ->pass($message);
  }

  // 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} = array();
  $this
    ->assertEqual($entity
    ->language(), \Drupal::languageManager()
    ->getLanguage($this->langcodes[0]), format_string('%entity_type: Entity language retrieved.', array(
    '%entity_type' => $entity_type,
  )));
  $this
    ->assertFalse($entity
    ->getTranslationLanguages(FALSE), format_string('%entity_type: No translations are available', array(
    '%entity_type' => $entity_type,
  )));

  // Set the value in default language.
  $entity
    ->set($this->fieldName, array(
    0 => array(
      'value' => 'default value',
    ),
  ));

  // Get the value.
  $field = $entity
    ->get($this->fieldName);
  $this
    ->assertEqual($field->value, 'default value', format_string('%entity_type: Untranslated value retrieved.', array(
    '%entity_type' => $entity_type,
  )));
  $this
    ->assertEqual($field
    ->getLangcode(), $default_langcode, format_string('%entity_type: Field object has the expected langcode.', array(
    '%entity_type' => $entity_type,
  )));

  // Set a translation.
  $entity
    ->addTranslation($this->langcodes[1])
    ->set($this->fieldName, array(
    0 => array(
      'value' => 'translation 1',
    ),
  ));
  $field = $entity
    ->getTranslation($this->langcodes[1])->{$this->fieldName};
  $this
    ->assertEqual($field->value, 'translation 1', format_string('%entity_type: Translated value set.', array(
    '%entity_type' => $entity_type,
  )));
  $this
    ->assertEqual($field
    ->getLangcode(), $this->langcodes[1], format_string('%entity_type: Field object has the expected langcode.', array(
    '%entity_type' => $entity_type,
  )));

  // Make sure the untranslated value stays.
  $field = $entity
    ->get($this->fieldName);
  $this
    ->assertEqual($field->value, 'default value', 'Untranslated value stays.');
  $this
    ->assertEqual($field
    ->getLangcode(), $default_langcode, 'Untranslated value has the expected langcode.');
  $translations[$this->langcodes[1]] = \Drupal::languageManager()
    ->getLanguage($this->langcodes[1]);
  $this
    ->assertEqual($entity
    ->getTranslationLanguages(FALSE), $translations, 'Translations retrieved.');

  // Try to get a value using a language code for a non-existing translation.
  $message = 'Getting a non existing translation results in an error.';
  try {
    $entity
      ->getTranslation($this->langcodes[2])
      ->get($this->fieldName)->value;
    $this
      ->fail($message);
  } catch (\InvalidArgumentException $e) {
    $this
      ->pass($message);
  }

  // Try to get a not available translation.
  $this
    ->assertNull($entity
    ->addTranslation($this->langcodes[2])
    ->get($this->fieldName)->value, format_string('%entity_type: A translation that is not available is NULL.', array(
    '%entity_type' => $entity_type,
  )));

  // Try to get a value using an invalid language code.
  $message = 'Getting an invalid translation results in an error.';
  try {
    $entity
      ->getTranslation('invalid')
      ->get($this->fieldName)->value;
    $this
      ->fail($message);
  } catch (\InvalidArgumentException $e) {
    $this
      ->pass($message);
  }

  // Try to set a value using an invalid language code.
  try {
    $entity
      ->getTranslation('invalid')
      ->set($this->fieldName, NULL);
    $this
      ->fail(format_string('%entity_type: Setting a translation for an invalid language throws an exception.', array(
      '%entity_type' => $entity_type,
    )));
  } catch (\InvalidArgumentException $e) {
    $this
      ->pass(format_string('%entity_type: Setting a translation for an invalid language throws an exception.', array(
      '%entity_type' => $entity_type,
    )));
  }

  // Set the value in default language.
  $field_name = 'field_test_text';
  $entity
    ->getTranslation($this->langcodes[1])
    ->set($field_name, array(
    0 => array(
      'value' => 'default value2',
    ),
  ));

  // Get the value.
  $field = $entity
    ->get($field_name);
  $this
    ->assertEqual($field->value, 'default value2', format_string('%entity_type: Untranslated value set into a translation in non-strict mode.', array(
    '%entity_type' => $entity_type,
  )));
  $this
    ->assertEqual($field
    ->getLangcode(), $default_langcode, format_string('%entity_type: Field object has the expected langcode.', array(
    '%entity_type' => $entity_type,
  )));
}