You are here

TitleTranslationTestCase.test in Title 7

File

tests/TitleTranslationTestCase.test
View source
<?php

/**
 * Tests for legacy field replacement.
 */
class TitleTranslationTestCase extends DrupalWebTestCase {

  /**
   *
   */
  public static function getInfo() {
    return array(
      'name' => 'Replaced fields translation',
      'description' => 'Test replaced field translation.',
      'group' => 'Title',
      'dependencies' => array(
        'entity_translation',
      ),
    );
  }

  /**
   * Use the barebones "testing" installation profile.
   */
  protected $profile = 'testing';

  /**
   * {@inheritdoc}
   */
  public function setUp(array $modules = array()) {

    // Core.
    $modules[] = 'field_test';
    $modules[] = 'locale';
    $modules[] = 'taxonomy';

    // Other dependencies.
    $modules[] = 'entity_translation';

    // This module.
    $modules[] = 'title';
    $modules[] = 'title_test';
    parent::setUp($modules);

    // Create a power user.
    $permissions = array(
      'administer modules',
      'view the administration theme',
      'administer languages',
      'administer taxonomy',
      'administer entity translation',
      'translate any entity',
    );
    $admin_user = $this
      ->drupalCreateUser($permissions);
    $this
      ->drupalLogin($admin_user);

    // Enable a translation language.
    $edit = array(
      'langcode' => 'it',
    );
    $this
      ->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
    $this
      ->assertTrue(drupal_multilingual(), t('Italian language installed.'));

    // Enable URL language negotiation.
    $name = 'language_content[enabled][locale-url]';
    $edit = array(
      $name => 1,
    );
    $this
      ->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
    $this
      ->assertFieldByName($name, 1, t('URL language negotiation enabled.'));

    // Enable taxonomy translation.
    $name = 'entity_translation_entity_types[taxonomy_term]';
    $edit = array(
      $name => 1,
    );
    $this
      ->drupalPost('admin/config/regional/entity_translation', $edit, t('Save configuration'));
    $this
      ->assertFieldByName($name, 'taxonomy_term', t('Taxonomy translation enabled.'));

    // Create a new vocabulary.
    $name = drupal_strtolower($this
      ->randomName());
    $edit = array(
      'name' => $this
        ->randomString(),
      'machine_name' => $name,
      'entity_translation_taxonomy' => 1,
    );
    $this
      ->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
    $this->vocabulary = taxonomy_vocabulary_machine_name_load($name);
    $this
      ->assertTrue($this->vocabulary, t('Vocabulary created.'));

    // Replace both taxonomy term legacy fields.
    $entity_type = 'taxonomy_term';
    foreach (title_field_replacement_info($entity_type) as $legacy_field => $info) {
      title_field_replacement_toggle($entity_type, $name, $legacy_field);
      $t_args = array(
        '%legacy_field' => $legacy_field,
      );
      $this
        ->assertTrue(field_info_instance($entity_type, $info['field']['field_name'], $name), t('The %legacy_field field has been correctly replaced.', $t_args));
    }

    // Ensure static caches do not interfere with API calls.
    drupal_static_reset();
  }

  /**
   * Tests taxonomy programmatic translation workflow.
   */
  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.');
  }

  /**
   * Tests taxonomy form translation workflow.
   */
  public function testFormTranslationWorkflow() {

    // Create a taxonomy term and check that legacy fields are properly
    // populated.
    $original_values = array(
      'name' => $this
        ->randomName(),
      'description' => $this
        ->randomName(),
    );
    $langcode = 'en';
    $edit = $this
      ->editValues($original_values, $langcode);
    $this
      ->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/add', $edit, t('Save'));
    $term = current(entity_load('taxonomy_term', FALSE, array(
      'name' => $original_values['name'],
    ), TRUE));
    $this
      ->assertEqual($term->description, $original_values['description'], t('Taxonomy term created.'));

    // Translate the taxonomy term and check that both the original values and
    // the translations were correctly stored.
    $translated_values = array(
      'name' => $this
        ->randomName(),
      'description' => $this
        ->randomName(),
    );
    $translation_langcode = 'it';
    $edit = $this
      ->editValues($translated_values, 'it');
    $this
      ->drupalPost($translation_langcode . '/taxonomy/term/' . $term->tid . '/edit/add/' . $langcode . '/' . $translation_langcode, $edit, t('Save'));
    $term = $this
      ->termLoad($term->tid);
    $this
      ->assertTrue($this
      ->checkFieldValues($term, $translated_values, $translation_langcode, FALSE), t('Taxonomy term translation created.'));
    $this
      ->assertTrue($this
      ->checkFieldValues($term, $original_values, $langcode), t('Taxonomy term original values preserved.'));

    // Check that legacy fields have the correct values.
    $this
      ->assertEqual($term->name, $original_values['name'], t('Taxonomy term name correctly stored.'));
    $this
      ->assertEqual($term->description, $original_values['description'], t('Taxonomy term description correctly stored.'));

    // Updated the taxonomy term translation and check that both the original
    // values and the translations were correctly stored.
    $translated_values = array(
      'name' => $this
        ->randomName(),
      'description' => $this
        ->randomName(),
    );
    $edit = $this
      ->editValues($translated_values, $translation_langcode);
    $this
      ->drupalPost($translation_langcode . '/taxonomy/term/' . $term->tid . '/edit/' . $translation_langcode, $edit, t('Save'));
    $term = $this
      ->termLoad($term->tid);
    $this
      ->assertTrue($this
      ->checkFieldValues($term, $translated_values, $translation_langcode, FALSE), t('Taxonomy term translation updated.'));
    $this
      ->assertTrue($this
      ->checkFieldValues($term, $original_values, $langcode), t('Taxonomy term original values preserved.'));

    // Check that legacy fields have the correct values.
    $this
      ->assertEqual($term->name, $original_values['name'], t('Taxonomy term name correctly stored.'));
    $this
      ->assertEqual($term->description, $original_values['description'], t('Taxonomy term description correctly stored.'));
  }

  /**
   * Loads a term using the given language as active language.
   *
   * @param int $tid
   *   The term identifier.
   * @param string|null $langcode
   *   (optional) The active language to be set. Defaults to none.
   *
   * @return object|bool
   *   A term object.
   */
  protected function termLoad($tid, $langcode = NULL) {
    drupal_static_reset();
    title_active_language($langcode);
    return current(entity_load('taxonomy_term', array(
      $tid,
    ), array(), TRUE));
  }

  /**
   * Returns the drupalPost() $edit array corresponding to the given values.
   */
  protected function editValues($values, $langcode) {
    $edit = array();
    foreach ($values as $name => $value) {
      $edit["{$name}_field[{$langcode}][0][value]"] = $value;
    }
    return $edit;
  }

  /**
   * Checks the field values.
   *
   * Checks that the field values and optionally the legacy ones match the given
   * values.
   */
  protected function checkFieldValues($term, $values, $langcode, $legacy_match = TRUE) {
    foreach ($values as $name => $value) {
      $field_value = $term->{$name . '_field'}[$langcode][0]['value'];
      if ($field_value != $value || $legacy_match !== ($field_value == $term->{$name})) {
        return FALSE;
      }
    }
    return TRUE;
  }

  /**
   * Checks that the legacy field values in the database match the given values.
   */
  protected function checkLegacyValues($term, $values) {
    $record = db_query('SELECT * FROM {taxonomy_term_data} t WHERE t.tid = :tid', array(
      ':tid' => $term->tid,
    ))
      ->fetchAssoc();
    foreach ($values as $name => $value) {
      if ($record[$name] != $value) {
        return FALSE;
      }
    }
    return TRUE;
  }

}

Classes

Namesort descending Description
TitleTranslationTestCase Tests for legacy field replacement.