You are here

protected function ContentTranslationUITestBase::doTestBasicTranslation in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/content_translation/src/Tests/ContentTranslationUITestBase.php \Drupal\content_translation\Tests\ContentTranslationUITestBase::doTestBasicTranslation()

Tests the basic translation workflow.

3 calls to ContentTranslationUITestBase::doTestBasicTranslation()
BlockContentTranslationUITest::doTestBasicTranslation in core/modules/block_content/src/Tests/BlockContentTranslationUITest.php
Tests the basic translation workflow.
ContentTranslationUITestBase::testTranslationUI in core/modules/content_translation/src/Tests/ContentTranslationUITestBase.php
Tests the basic translation UI.
ShortcutTranslationUITest::doTestBasicTranslation in core/modules/shortcut/src/Tests/ShortcutTranslationUITest.php
Tests the basic translation workflow.
2 methods override ContentTranslationUITestBase::doTestBasicTranslation()
BlockContentTranslationUITest::doTestBasicTranslation in core/modules/block_content/src/Tests/BlockContentTranslationUITest.php
Tests the basic translation workflow.
ShortcutTranslationUITest::doTestBasicTranslation in core/modules/shortcut/src/Tests/ShortcutTranslationUITest.php
Tests the basic translation workflow.

File

core/modules/content_translation/src/Tests/ContentTranslationUITestBase.php, line 75
Contains \Drupal\content_translation\Tests\ContentTranslationUITestBase.

Class

ContentTranslationUITestBase
Tests the Content Translation UI.

Namespace

Drupal\content_translation\Tests

Code

protected function doTestBasicTranslation() {

  // Create a new test entity with original values in the default language.
  $default_langcode = $this->langcodes[0];
  $values[$default_langcode] = $this
    ->getNewEntityValues($default_langcode);

  // Create the entity with the editor as owner, so that afterwards a new
  // translation is created by the translator and the translation author is
  // tested.
  $this
    ->drupalLogin($this->editor);
  $this->entityId = $this
    ->createEntity($values[$default_langcode], $default_langcode);
  $this
    ->drupalLogin($this->translator);
  $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
  $this
    ->assertTrue($entity, 'Entity found in the database.');
  $this
    ->drupalGet($entity
    ->urlInfo());
  $this
    ->assertResponse(200, 'Entity URL is valid.');

  // Ensure that the content language cache context is not yet added to the
  // page.
  $this
    ->assertCacheContexts($this->defaultCacheContexts);
  $this
    ->drupalGet($entity
    ->urlInfo('drupal:content-translation-overview'));
  $this
    ->assertNoText('Source language', 'Source language column correctly hidden.');
  $translation = $this
    ->getTranslation($entity, $default_langcode);
  foreach ($values[$default_langcode] as $property => $value) {
    $stored_value = $this
      ->getValue($translation, $property, $default_langcode);
    $value = is_array($value) ? $value[0]['value'] : $value;
    $message = format_string('@property correctly stored in the default language.', array(
      '@property' => $property,
    ));
    $this
      ->assertEqual($stored_value, $value, $message);
  }

  // Add a content translation.
  $langcode = 'it';
  $language = ConfigurableLanguage::load($langcode);
  $values[$langcode] = $this
    ->getNewEntityValues($langcode);
  $entity_type_id = $entity
    ->getEntityTypeId();
  $add_url = Url::fromRoute("entity.{$entity_type_id}.content_translation_add", [
    $entity
      ->getEntityTypeId() => $entity
      ->id(),
    'source' => $default_langcode,
    'target' => $langcode,
  ], array(
    'language' => $language,
  ));
  $this
    ->drupalPostForm($add_url, $this
    ->getEditValues($values, $langcode), $this
    ->getFormSubmitActionForNewTranslation($entity, $langcode));

  // Assert that HTML is escaped in "all languages" in UI after SafeMarkup
  // change.
  if ($this->testHTMLEscapeForAllLanguages) {
    $this
      ->assertNoRaw('<span class="translation-entity-all-languages">(all languages)</span>');
    $this
      ->assertRaw('<span class="translation-entity-all-languages">(all languages)</span>');
  }

  // Ensure that the content language cache context is not yet added to the
  // page.
  $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
  $this
    ->drupalGet($entity
    ->urlInfo());
  $this
    ->assertCacheContexts(Cache::mergeContexts([
    'languages:language_content',
  ], $this->defaultCacheContexts));

  // Reset the cache of the entity, so that the new translation gets the
  // updated values.
  $metadata_source_translation = $this->manager
    ->getTranslationMetadata($entity
    ->getTranslation($default_langcode));
  $metadata_target_translation = $this->manager
    ->getTranslationMetadata($entity
    ->getTranslation($langcode));
  $author_field_name = $entity
    ->hasField('content_translation_uid') ? 'content_translation_uid' : 'uid';
  if ($entity
    ->getFieldDefinition($author_field_name)
    ->isTranslatable()) {
    $this
      ->assertEqual($metadata_target_translation
      ->getAuthor()
      ->id(), $this->translator
      ->id(), SafeMarkup::format('Author of the target translation @langcode correctly stored for translatable owner field.', array(
      '@langcode' => $langcode,
    )));
    $this
      ->assertNotEqual($metadata_target_translation
      ->getAuthor()
      ->id(), $metadata_source_translation
      ->getAuthor()
      ->id(), SafeMarkup::format('Author of the target translation @target different from the author of the source translation @source for translatable owner field.', array(
      '@target' => $langcode,
      '@source' => $default_langcode,
    )));
  }
  else {
    $this
      ->assertEqual($metadata_target_translation
      ->getAuthor()
      ->id(), $this->editor
      ->id(), 'Author of the entity remained untouched after translation for non translatable owner field.');
  }
  $created_field_name = $entity
    ->hasField('content_translation_created') ? 'content_translation_created' : 'created';
  if ($entity
    ->getFieldDefinition($created_field_name)
    ->isTranslatable()) {
    $this
      ->assertTrue($metadata_target_translation
      ->getCreatedTime() > $metadata_source_translation
      ->getCreatedTime(), SafeMarkup::format('Translation creation timestamp of the target translation @target is newer than the creation timestamp of the source translation @source for translatable created field.', array(
      '@target' => $langcode,
      '@source' => $default_langcode,
    )));
  }
  else {
    $this
      ->assertEqual($metadata_target_translation
      ->getCreatedTime(), $metadata_source_translation
      ->getCreatedTime(), 'Creation timestamp of the entity remained untouched after translation for non translatable created field.');
  }
  if ($this->testLanguageSelector) {
    $this
      ->assertNoFieldByXPath('//select[@id="edit-langcode-0-value"]', NULL, 'Language selector correctly disabled on translations.');
  }
  $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
  $this
    ->drupalGet($entity
    ->urlInfo('drupal:content-translation-overview'));
  $this
    ->assertNoText('Source language', 'Source language column correctly hidden.');

  // Switch the source language.
  $langcode = 'fr';
  $language = ConfigurableLanguage::load($langcode);
  $source_langcode = 'it';
  $edit = array(
    'source_langcode[source]' => $source_langcode,
  );
  $entity_type_id = $entity
    ->getEntityTypeId();
  $add_url = Url::fromRoute("entity.{$entity_type_id}.content_translation_add", [
    $entity
      ->getEntityTypeId() => $entity
      ->id(),
    'source' => $default_langcode,
    'target' => $langcode,
  ], array(
    'language' => $language,
  ));

  // This does not save anything, it merely reloads the form and fills in the
  // fields with the values from the different source language.
  $this
    ->drupalPostForm($add_url, $edit, t('Change'));
  $this
    ->assertFieldByXPath("//input[@name=\"{$this->fieldName}[0][value]\"]", $values[$source_langcode][$this->fieldName][0]['value'], 'Source language correctly switched.');

  // Add another translation and mark the other ones as outdated.
  $values[$langcode] = $this
    ->getNewEntityValues($langcode);
  $edit = $this
    ->getEditValues($values, $langcode) + array(
    'content_translation[retranslate]' => TRUE,
  );
  $entity_type_id = $entity
    ->getEntityTypeId();
  $add_url = Url::fromRoute("entity.{$entity_type_id}.content_translation_add", [
    $entity
      ->getEntityTypeId() => $entity
      ->id(),
    'source' => $source_langcode,
    'target' => $langcode,
  ], array(
    'language' => $language,
  ));
  $this
    ->drupalPostForm($add_url, $edit, $this
    ->getFormSubmitActionForNewTranslation($entity, $langcode));
  $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
  $this
    ->drupalGet($entity
    ->urlInfo('drupal:content-translation-overview'));
  $this
    ->assertText('Source language', 'Source language column correctly shown.');

  // Check that the entered values have been correctly stored.
  foreach ($values as $langcode => $property_values) {
    $translation = $this
      ->getTranslation($entity, $langcode);
    foreach ($property_values as $property => $value) {
      $stored_value = $this
        ->getValue($translation, $property, $langcode);
      $value = is_array($value) ? $value[0]['value'] : $value;
      $message = format_string('%property correctly stored with language %language.', array(
        '%property' => $property,
        '%language' => $langcode,
      ));
      $this
        ->assertEqual($stored_value, $value, $message);
    }
  }
}