You are here

public function TitleTranslationTestCase::testProgrammaticTranslationWorkflow in Title 7

Tests taxonomy programmatic translation workflow.

File

tests/TitleTranslationTestCase.test, line 95

Class

TitleTranslationTestCase
Tests for legacy field replacement.

Code

public function testProgrammaticTranslationWorkflow() {

  // Create a taxonomy term and assign it an original language different from
  // the default language.
  $langcode = 'it';
  $original_values = array(
    'name' => $langcode . '_' . $this
      ->randomName(),
    'description' => $langcode . '_' . $this
      ->randomName(),
  );
  $term = (object) ($original_values + array(
    'format' => 'filtered_html',
    'vocabulary_machine_name' => $this->vocabulary->machine_name,
    'vid' => $this->vocabulary->vid,
  ));
  entity_translation_get_handler('taxonomy_term', $term)
    ->setOriginalLanguage($langcode);
  taxonomy_term_save($term);
  $this
    ->assertTrue($this
    ->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.');
  $term = $this
    ->termLoad($term->tid);
  $this
    ->assertTrue($this
    ->checkFieldValues($term, $original_values, $langcode), 'Replacing field values correctly created from the legacy field values.');

  // Pollute synchronization cache to ensure the expected values are stored
  // anyway.
  title_entity_sync('taxonomy_term', $term, $langcode);

  // Create a translation using the default language.
  $translation_langcode = language_default()->language;
  $translated_values = array(
    'name' => $translation_langcode . '_' . $this
      ->randomName(),
    'description' => $translation_langcode . '_' . $this
      ->randomName(),
  );
  foreach ($translated_values as $name => $value) {
    $term->{$name} = $value;
  }
  $translation = array(
    'language' => $translation_langcode,
    'source' => $langcode,
    'uid' => $this->loggedInUser->uid,
    'status' => 1,
    'translate' => 0,
    'created' => REQUEST_TIME,
    'changed' => REQUEST_TIME,
  );
  entity_translation_get_handler('taxonomy_term', $term)
    ->setTranslation($translation);
  taxonomy_term_save($term);
  $this
    ->assertTrue($this
    ->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.');
  $term = $this
    ->termLoad($term->tid, $translation_langcode);
  $this
    ->assertTrue($this
    ->checkFieldValues($term, $translated_values, $translation_langcode), 'Replacing field translations correctly created.');
  $this
    ->assertTrue($this
    ->checkFieldValues($term, $original_values, $langcode, FALSE), 'Replacing field original values correctly preserved.');

  // Delete the translation.
  entity_translation_get_handler('taxonomy_term', $term)
    ->removeTranslation($translation_langcode);
  taxonomy_term_save($term);
  $this
    ->assertTrue($this
    ->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.');
  $term = $this
    ->termLoad($term->tid, $langcode);
  $this
    ->assertTrue($this
    ->checkFieldValues($term, $original_values, $langcode), 'Replacing field translations correctly deleted.');

  // Make the term language neutral.
  entity_translation_get_handler('taxonomy_term', $term)
    ->setOriginalLanguage(LANGUAGE_NONE);
  foreach ($original_values as $name => $value) {
    $field_name = $name . '_field';
    $term->{$field_name}[LANGUAGE_NONE] = $term->{$field_name}[$langcode];
    $term->{$field_name}[$langcode] = array();
  }
  taxonomy_term_save($term);
  $this
    ->assertTrue($this
    ->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.');
  $term = $this
    ->termLoad($term->tid);
  $this
    ->assertTrue($this
    ->checkFieldValues($term, $original_values, LANGUAGE_NONE), 'Term original language correctly changed to the former translation language.');

  // Change the term language to the former translation language.
  entity_translation_get_handler('taxonomy_term', $term)
    ->setOriginalLanguage($translation_langcode);
  foreach ($original_values as $name => $value) {
    $field_name = $name . '_field';
    $term->{$field_name}[$translation_langcode] = $term->{$field_name}[LANGUAGE_NONE];
    $term->{$field_name}[LANGUAGE_NONE] = array();
  }
  taxonomy_term_save($term);
  $this
    ->assertTrue($this
    ->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.');
  $term = $this
    ->termLoad($term->tid, $translation_langcode);
  $this
    ->assertTrue($this
    ->checkFieldValues($term, $original_values, $translation_langcode), 'Term original language correctly changed to language neutral.');

  // Make a replacing field untranslatable and change its value.
  $field_name = 'name_field';
  $field = field_info_field($field_name);
  $field['translatable'] = FALSE;
  field_update_field($field);
  $original_values['name'] = LANGUAGE_NONE . '_' . $this
    ->randomName();
  $term->name = $original_values['name'];
  taxonomy_term_save($term);
  $this
    ->assertTrue($this
    ->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.');
  $term = $this
    ->termLoad($term->tid);
  $this
    ->assertEqual($term->{$field_name}[LANGUAGE_NONE][0]['value'], $original_values['name'], 'Untranslatable replacing field on translatable entity correctly handled.');
}