You are here

function EntityTranslationTaxonomyAutocompleteTestCase::testTranslatedAutocomplete in Entity Translation 7

That the autocomplete works with translated terms.

File

tests/entity_translation.test, line 1343
Tests for Entity translation module.

Class

EntityTranslationTaxonomyAutocompleteTestCase
Tests for the taxonomy autocomplete translation modes.

Code

function testTranslatedAutocomplete() {
  $this
    ->login($this
    ->getTranslatorUser(array(
    'administer taxonomy',
  )));
  $vocabulary = taxonomy_vocabulary_machine_name_load('tags');
  $entity_type = 'taxonomy_term';
  $existing_values = array();
  $translated_values = array(
    'en' => array(
      'Red' => 'Rosso',
      'Green' => 'Verde',
    ),
    'it' => array(
      'Blu' => 'Blue',
    ),
  );
  $langcodes = array_keys($translated_values);

  // Create a few existing tags with different original language and translate
  // them accordingly.
  foreach ($translated_values as $langcode => $values) {
    title_active_language($langcode);
    $translation_langcode = current(array_diff($langcodes, array(
      $langcode,
    )));
    foreach ($values as $original => $translation) {
      $term = (object) array(
        'vid' => $vocabulary->vid,
        'vocabulary_machine_name' => $vocabulary->machine_name,
        'name' => $original,
        'name_field' => array(
          $langcode => array(
            array(
              'value' => $original,
            ),
          ),
          $translation_langcode => array(
            array(
              'value' => $translation,
            ),
          ),
        ),
      );
      $translation = array(
        'language' => $translation_langcode,
        'source' => $langcode,
        'status' => TRUE,
      );
      $handler = entity_translation_get_handler($entity_type, $term);
      $handler
        ->setOriginalLanguage($langcode);
      $handler
        ->initTranslations();
      $handler
        ->setTranslation($translation);
      taxonomy_term_save($term);
      $existing_values[$term->name_field['en'][0]['value']] = $term->name_field['it'][0]['value'];
    }
  }

  // Verify that the English autocomplete route returns results for terms
  // originally created in English.
  $this
    ->autocompleteGet('en', 'Re');
  $this
    ->assertRaw('Red');
  $this
    ->assertRaw('Green');

  // Verify that the English autocomplete route returns results for terms
  // translated into English.
  $this
    ->autocompleteGet('en', 'Blu');
  $this
    ->assertRaw('Blue');

  // Verify that the Italian autocomplete route returns results for terms
  // originally created in Italian.
  $this
    ->autocompleteGet('it', 'Blu');
  $this
    ->assertRaw('Blu');
  $this
    ->assertNoRaw('Blue');

  // Verify that the Italian autocomplete route returns results for terms
  // translated into Italian.
  $this
    ->autocompleteGet('it', 'R');
  $this
    ->assertRaw('Rosso');
  $this
    ->assertRaw('Verde');

  // Verify that existing tags are correctly referenced and new tags are
  // correctly created, when saving an English node.
  $new_values = array(
    'Cyan' => 'Ciano',
    'Magenta' => 'Magenta',
    'Yellow' => 'Giallo',
    'Black' => 'Nero',
  );
  $all_values = $existing_values + $new_values;
  $edit = array(
    'title' => 'Test 1',
    'field_test_tags[' . LANGUAGE_NONE . ']' => implode(', ', array_keys($all_values)),
    'language' => 'en',
  );
  $this
    ->drupalPost('node/add/page', $edit, t('Save'));
  foreach ($all_values as $original => $translation) {
    $this
      ->assertRaw($original);
  }

  // Verify that existing translated tags are correctly referenced and new
  // tags are correctly created, when translated the node into Italian.
  $node = $this
    ->drupalGetNodeByTitle($edit['title']);
  $this
    ->drupalGet('node/' . $node->nid . '/translate');
  $this
    ->clickLink('add', 1);
  $edit = array(
    'field_test_tags[' . LANGUAGE_NONE . ']' => implode(', ', $all_values),
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));
  foreach ($all_values as $original => $translation) {
    $this
      ->assertRaw($translation);
  }

  // Verify that existing (translated) tags were preserved, while new Italian
  // tags replaced the corresponding English versions.
  $this
    ->drupalGet('node/' . $node->nid);
  foreach ($existing_values as $original => $translation) {
    $this
      ->assertRaw($original);
  }
  foreach ($new_values as $original => $translation) {
    $this
      ->assertRaw($translation);
  }
}