You are here

protected function EntityTranslationTest::doTestLanguageChange 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::doTestLanguageChange()

Executes the entity language change test for the given entity type.

Parameters

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

1 call to EntityTranslationTest::doTestLanguageChange()
EntityTranslationTest::testLanguageChange in core/modules/system/src/Tests/Entity/EntityTranslationTest.php
Tests that changing entity language does not break field language.

File

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

Class

EntityTranslationTest
Tests entity translation functionality.

Namespace

Drupal\system\Tests\Entity

Code

protected function doTestLanguageChange($entity_type) {
  $langcode_key = $this->entityManager
    ->getDefinition($entity_type)
    ->getKey('langcode');
  $controller = $this->entityManager
    ->getStorage($entity_type);
  $langcode = $this->langcodes[0];

  // check that field languages match entity language regardless of field
  // translatability.
  $values = array(
    $langcode_key => $langcode,
    $this->fieldName => $this
      ->randomMachineName(),
    $this->untranslatableFieldName => $this
      ->randomMachineName(),
  );
  $entity = $controller
    ->create($values);
  foreach (array(
    $this->fieldName,
    $this->untranslatableFieldName,
  ) as $field_name) {
    $this
      ->assertEqual($entity
      ->get($field_name)
      ->getLangcode(), $langcode, 'Field language works as expected.');
  }

  // Check that field languages keep matching entity language even after
  // changing it.
  $langcode = $this->langcodes[1];
  $entity->{$langcode_key}->value = $langcode;
  foreach (array(
    $this->fieldName,
    $this->untranslatableFieldName,
  ) as $field_name) {
    $this
      ->assertEqual($entity
      ->get($field_name)
      ->getLangcode(), $langcode, 'Field language works as expected after changing entity language.');
  }

  // Check that entity translation does not affect the language of original
  // field values and untranslatable ones.
  $langcode = $this->langcodes[0];
  $entity
    ->addTranslation($this->langcodes[2], array(
    $this->fieldName => $this
      ->randomMachineName(),
  ));
  $entity->{$langcode_key}->value = $langcode;
  foreach (array(
    $this->fieldName,
    $this->untranslatableFieldName,
  ) as $field_name) {
    $this
      ->assertEqual($entity
      ->get($field_name)
      ->getLangcode(), $langcode, 'Field language works as expected after translating the entity and changing language.');
  }

  // Check that setting the default language to an existing translation
  // language causes an exception to be thrown.
  $message = 'An exception is thrown when setting the default language to an existing translation language';
  try {
    $entity->{$langcode_key}->value = $this->langcodes[2];
    $this
      ->fail($message);
  } catch (\InvalidArgumentException $e) {
    $this
      ->pass($message);
  }
}