You are here

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

Executes the Entity Translation API tests for the given entity type.

Parameters

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

1 call to EntityTranslationTest::doTestEntityTranslationAPI()
EntityTranslationTest::testEntityTranslationAPI in core/modules/system/src/Tests/Entity/EntityTranslationTest.php
Tests the Entity Translation API behavior.

File

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

Class

EntityTranslationTest
Tests entity translation functionality.

Namespace

Drupal\system\Tests\Entity

Code

protected function doTestEntityTranslationAPI($entity_type) {
  $default_langcode = $this->langcodes[0];
  $langcode = $this->langcodes[1];
  $langcode_key = $this->entityManager
    ->getDefinition($entity_type)
    ->getKey('langcode');
  $default_langcode_key = $this->entityManager
    ->getDefinition($entity_type)
    ->getKey('default_langcode');

  /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  $entity = $this->entityManager
    ->getStorage($entity_type)
    ->create(array(
    'name' => $this
      ->randomMachineName(),
    $langcode_key => LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ));
  $entity
    ->save();
  $hooks = $this
    ->getHooksInfo();
  $this
    ->assertFalse($hooks, 'No entity translation hooks are fired when creating an entity.');

  // Verify that we obtain the entity object itself when we attempt to
  // retrieve a translation referring to it.
  $translation = $entity
    ->getTranslation(LanguageInterface::LANGCODE_NOT_SPECIFIED);
  $this
    ->assertFalse($translation
    ->isNewTranslation(), 'Existing translations are not marked as new.');
  $this
    ->assertIdentical($entity, $translation, 'The translation object corresponding to a non-default language is the entity object itself when the entity is language-neutral.');
  $entity->{$langcode_key}->value = $default_langcode;
  $translation = $entity
    ->getTranslation($default_langcode);
  $this
    ->assertIdentical($entity, $translation, 'The translation object corresponding to the default language (explicit) is the entity object itself.');
  $translation = $entity
    ->getTranslation(LanguageInterface::LANGCODE_DEFAULT);
  $this
    ->assertIdentical($entity, $translation, 'The translation object corresponding to the default language (implicit) is the entity object itself.');
  $this
    ->assertTrue($entity->{$default_langcode_key}->value, 'The translation object is the default one.');

  // Verify that trying to retrieve a translation for a locked language when
  // the entity is language-aware causes an exception to be thrown.
  $message = 'A language-neutral translation cannot be retrieved.';
  try {
    $entity
      ->getTranslation(LanguageInterface::LANGCODE_NOT_SPECIFIED);
    $this
      ->fail($message);
  } catch (\LogicException $e) {
    $this
      ->pass($message);
  }

  // Create a translation and verify that the translation object and the
  // original object behave independently.
  $name = $default_langcode . '_' . $this
    ->randomMachineName();
  $entity->name->value = $name;
  $name_translated = $langcode . '_' . $this
    ->randomMachineName();
  $translation = $entity
    ->addTranslation($langcode);
  $this
    ->assertTrue($translation
    ->isNewTranslation(), 'Newly added translations are marked as new.');
  $this
    ->assertNotIdentical($entity, $translation, 'The entity and the translation object differ from one another.');
  $this
    ->assertTrue($entity
    ->hasTranslation($langcode), 'The new translation exists.');
  $this
    ->assertEqual($translation
    ->language()
    ->getId(), $langcode, 'The translation language matches the specified one.');
  $this
    ->assertEqual($translation->{$langcode_key}->value, $langcode, 'The translation field language value matches the specified one.');
  $this
    ->assertFalse($translation->{$default_langcode_key}->value, 'The translation object is not the default one.');
  $this
    ->assertEqual($translation
    ->getUntranslated()
    ->language()
    ->getId(), $default_langcode, 'The original language can still be retrieved.');
  $translation->name->value = $name_translated;
  $this
    ->assertEqual($entity->name->value, $name, 'The original name is retained after setting a translated value.');
  $entity->name->value = $name;
  $this
    ->assertEqual($translation->name->value, $name_translated, 'The translated name is retained after setting the original value.');

  // Save the translation and check that the expected hooks are fired.
  $translation
    ->save();
  $hooks = $this
    ->getHooksInfo();
  $this
    ->assertEqual($hooks['entity_translation_insert'], $langcode, 'The generic entity translation insertion hook has fired.');
  $this
    ->assertEqual($hooks[$entity_type . '_translation_insert'], $langcode, 'The entity-type-specific entity translation insertion hook has fired.');

  // Verify that changing translation language causes an exception to be
  // thrown.
  $message = 'The translation language cannot be changed.';
  try {
    $translation->{$langcode_key}->value = $this->langcodes[2];
    $this
      ->fail($message);
  } catch (\LogicException $e) {
    $this
      ->pass($message);
  }

  // Verify that reassigning the same translation language is allowed.
  $message = 'The translation language can be reassigned the same value.';
  try {
    $translation->{$langcode_key}->value = $langcode;
    $this
      ->pass($message);
  } catch (\LogicException $e) {
    $this
      ->fail($message);
  }

  // Verify that changing the default translation flag causes an exception to
  // be thrown.
  foreach ($entity
    ->getTranslationLanguages() as $t_langcode => $language) {
    $translation = $entity
      ->getTranslation($t_langcode);
    $default = $translation
      ->isDefaultTranslation();
    $message = 'The default translation flag can be reassigned the same value.';
    try {
      $translation->{$default_langcode_key}->value = $default;
      $this
        ->pass($message);
    } catch (\LogicException $e) {
      $this
        ->fail($message);
    }
    $message = 'The default translation flag cannot be changed.';
    try {
      $translation->{$default_langcode_key}->value = !$default;
      $this
        ->fail($message);
    } catch (\LogicException $e) {
      $this
        ->pass($message);
    }
    $this
      ->assertEqual($translation->{$default_langcode_key}->value, $default);
  }

  // Check that after loading an entity the language is the default one.
  $entity = $this
    ->reloadEntity($entity);
  $this
    ->assertEqual($entity
    ->language()
    ->getId(), $default_langcode, 'The loaded entity is the original one.');

  // Add another translation and check that everything works as expected. A
  // new translation object can be obtained also by just specifying a valid
  // language.
  $langcode2 = $this->langcodes[2];
  $translation = $entity
    ->addTranslation($langcode2);
  $value = $entity !== $translation && $translation
    ->language()
    ->getId() == $langcode2 && $entity
    ->hasTranslation($langcode2);
  $this
    ->assertTrue($value, 'A new translation object can be obtained also by specifying a valid language.');
  $this
    ->assertEqual($entity
    ->language()
    ->getId(), $default_langcode, 'The original language has been preserved.');
  $translation
    ->save();
  $hooks = $this
    ->getHooksInfo();
  $this
    ->assertEqual($hooks['entity_translation_insert'], $langcode2, 'The generic entity translation insertion hook has fired.');
  $this
    ->assertEqual($hooks[$entity_type . '_translation_insert'], $langcode2, 'The entity-type-specific entity translation insertion hook has fired.');

  // Verify that trying to manipulate a translation object referring to a
  // removed translation results in exceptions being thrown.
  $entity = $this
    ->reloadEntity($entity);
  $translation = $entity
    ->getTranslation($langcode2);
  $entity
    ->removeTranslation($langcode2);
  foreach (array(
    'get',
    'set',
    '__get',
    '__set',
    'createDuplicate',
  ) as $method) {
    $message = format_string('The @method method raises an exception when trying to manipulate a removed translation.', array(
      '@method' => $method,
    ));
    try {
      $translation
        ->{$method}('name', $this
        ->randomMachineName());
      $this
        ->fail($message);
    } catch (\Exception $e) {
      $this
        ->pass($message);
    }
  }

  // Verify that deletion hooks are fired when saving an entity with a removed
  // translation.
  $entity
    ->save();
  $hooks = $this
    ->getHooksInfo();
  $this
    ->assertEqual($hooks['entity_translation_delete'], $langcode2, 'The generic entity translation deletion hook has fired.');
  $this
    ->assertEqual($hooks[$entity_type . '_translation_delete'], $langcode2, 'The entity-type-specific entity translation deletion hook has fired.');
  $entity = $this
    ->reloadEntity($entity);
  $this
    ->assertFalse($entity
    ->hasTranslation($langcode2), 'The translation does not appear among available translations after saving the entity.');

  // Check that removing an invalid translation causes an exception to be
  // thrown.
  foreach (array(
    $default_langcode,
    LanguageInterface::LANGCODE_DEFAULT,
    $this
      ->randomMachineName(),
  ) as $invalid_langcode) {
    $message = format_string('Removing an invalid translation (@langcode) causes an exception to be thrown.', array(
      '@langcode' => $invalid_langcode,
    ));
    try {
      $entity
        ->removeTranslation($invalid_langcode);
      $this
        ->fail($message);
    } catch (\Exception $e) {
      $this
        ->pass($message);
    }
  }

  // Check that hooks are fired only when actually storing data.
  $entity = $this
    ->reloadEntity($entity);
  $entity
    ->addTranslation($langcode2);
  $entity
    ->removeTranslation($langcode2);
  $entity
    ->save();
  $hooks = $this
    ->getHooksInfo();
  $this
    ->assertFalse($hooks, 'No hooks are run when adding and removing a translation without storing it.');

  // Check that hooks are fired only when actually storing data.
  $entity = $this
    ->reloadEntity($entity);
  $entity
    ->addTranslation($langcode2);
  $entity
    ->save();
  $entity = $this
    ->reloadEntity($entity);
  $this
    ->assertTrue($entity
    ->hasTranslation($langcode2), 'Entity has translation after adding one and saving.');
  $entity
    ->removeTranslation($langcode2);
  $entity
    ->save();
  $entity = $this
    ->reloadEntity($entity);
  $this
    ->assertFalse($entity
    ->hasTranslation($langcode2), 'Entity does not have translation after removing it and saving.');

  // Reset hook firing information.
  $this
    ->getHooksInfo();

  // Verify that entity serialization does not cause stale references to be
  // left around.
  $entity = $this
    ->reloadEntity($entity);
  $translation = $entity
    ->getTranslation($langcode);
  $entity = unserialize(serialize($entity));
  $entity->name->value = $this
    ->randomMachineName();
  $name = $default_langcode . '_' . $this
    ->randomMachineName();
  $entity
    ->getTranslation($default_langcode)->name->value = $name;
  $this
    ->assertEqual($entity->name->value, $name, 'No stale reference for the translation object corresponding to the original language.');
  $translation2 = $entity
    ->getTranslation($langcode);
  $translation2->name->value .= $this
    ->randomMachineName();
  $this
    ->assertNotEqual($translation->name->value, $translation2->name->value, 'No stale reference for the actual translation object.');
  $this
    ->assertEqual($entity, $translation2
    ->getUntranslated(), 'No stale reference in the actual translation object.');

  // Verify that deep-cloning is still available when we are not instantiating
  // a translation object, which instead relies on shallow cloning.
  $entity = $this
    ->reloadEntity($entity);
  $entity
    ->getTranslation($langcode);
  $cloned = clone $entity;
  $translation = $cloned
    ->getTranslation($langcode);
  $this
    ->assertNotIdentical($entity, $translation
    ->getUntranslated(), 'A cloned entity object has no reference to the original one.');
  $entity
    ->removeTranslation($langcode);
  $this
    ->assertFalse($entity
    ->hasTranslation($langcode));
  $this
    ->assertTrue($cloned
    ->hasTranslation($langcode));

  // Check that untranslatable field references keep working after serializing
  // and cloning the entity.
  $entity = $this
    ->reloadEntity($entity);
  $type = $this
    ->randomMachineName();
  $entity
    ->getTranslation($langcode)->type->value = $type;
  $entity = unserialize(serialize($entity));
  $cloned = clone $entity;
  $translation = $cloned
    ->getTranslation($langcode);
  $translation->type->value = strrev($type);
  $this
    ->assertEqual($cloned->type->value, $translation->type->value, 'Untranslatable field references keep working after serializing and cloning the entity.');

  // Check that per-language defaults are properly populated. The
  // 'entity_test_mul_default_value' entity type is translatable and uses
  // entity_test_field_default_value() as a "default value callback" for its
  // 'description' field.
  $entity = $this->entityManager
    ->getStorage('entity_test_mul_default_value')
    ->create([
    'name' => $this
      ->randomMachineName(),
    'langcode' => $langcode,
  ]);
  $translation = $entity
    ->addTranslation($langcode2);
  $expected = array(
    array(
      'shape' => "shape:0:description_{$langcode2}",
      'color' => "color:0:description_{$langcode2}",
    ),
    array(
      'shape' => "shape:1:description_{$langcode2}",
      'color' => "color:1:description_{$langcode2}",
    ),
  );
  $this
    ->assertEqual($translation->description
    ->getValue(), $expected, 'Language-aware default values correctly populated.');
  $this
    ->assertEqual($translation->description
    ->getLangcode(), $langcode2, 'Field object has the expected langcode.');
}