<?php
/**
 * @file
 * Test case for multilingual taxonomy
 */


class i18nTaxonomyTestCase extends Drupali18nTestCase {

  public static function getInfo() {
    return array(
      'name' => 'Taxonomy translation',
      'group' => 'Internationalization',
      'description' => 'Taxonomy translation functions'
    );
  }

  function setUp() {
    parent::setUp(array('i18n_taxonomy', 'field_test'));
    parent::setUpLanguages();

    // Create users.
    $filtered_html_format = filter_format_load('filtered_html');
    $full_html_format = filter_format_load('full_html');
    $this->admin_user = $this->drupalCreateUser(array(
      'access field_test content',
      'administer field_test content',
      'administer taxonomy',
      'administer languages',
      'administer site configuration',
      filter_permission_name($filtered_html_format),
      filter_permission_name($full_html_format),
    ));
    $this->translator = $this->drupalCreateUser(array('translate interface', 'translate user-defined strings'));
  }

function testTaxonomyTermLocalize() {
    $this->drupalLogin($this->admin_user);
    // Make Input Format "Filter Text" translatable
    $edit = array(
      'i18n_string_allowed_formats[filtered_html]' => 'filtered_html',
      'i18n_string_allowed_formats[plain_text]' => 'plain_text',
    );
    $this->drupalPost('admin/config/regional/i18n/strings', $edit, t('Save configuration'));

    // Create a localizable vocabulary.
    $vocab = $this->createVocabulary(array('i18n_mode' => I18N_MODE_LOCALIZE));
    $this->assertEqual(i18n_taxonomy_vocabulary_mode($vocab->vid), I18N_MODE_LOCALIZE, 'A vocabulary has been created and it is localizable.');

    $this->field_name = $this->createTermField($vocab->machine_name);

    // Create a term to be localized. We use a common prefix to facilitate the testing of autocomplete suggestions.
    $prefix = $this->randomName() . '_';
    $term = $this->createTerm(array('vid' => $vocab->vid, 'name' => $prefix . $this->randomName()));

    $this->drupalLogin($this->translator);

    // Create and Save Spanish translation, again using the same prefix.
    $term_translation = array(
      'name' => $this->createStringTranslation('taxonomy', $term->name, array($this->secondary_language => $prefix . $this->randomName())),
      'description' => $this->createStringTranslation('taxonomy', $term->description, array($this->secondary_language => $prefix . $this->randomName())),
    );

    $this->drupalLogin($this->admin_user);

    $langcode = LANGUAGE_NONE;
    $edit = array(
      "{$this->field_name}[$langcode]" => array($term->tid),
    );

    // Test the widgets in the original language.
    $this->drupalGet('test-entity/add/test-bundle');
    $this->assertText($term->name, 'Widget values are displayed correctly in default language.');

    $this->drupalPost(NULL, $edit, t('Save'));
    $this->assertText($term->name, 'Field values are displayed correctly in default language.');

    // Terms should be localized in the field widget.
    $this->drupalGet($this->secondary_language . '/test-entity/add/test-bundle');
    $this->assertText($term_translation['name'][$this->secondary_language], 'Widget values are displayed correctly in non-default languages.');

    $this->drupalPost(NULL, $edit, t('Save'));
    $this->assertText($term_translation['name'][$this->secondary_language], 'Field values are displayed correctly in non-default languages.');

    // Term name and term description should be localized
    $this->drupalGet('taxonomy/term/' . $term->tid, array('language' => i18n_language_object($this->default_language)));
    $this->assertText($term->name, 'Term title is displayed correctly in default language.');
    $this->assertText($term->description, 'Term description is displayed correctly in default language.');

    // Term name and term description should be localized
    $this->drupalGet('taxonomy/term/' . $term->tid, array('language' => i18n_language_object($this->secondary_language)));
    $this->assertText($term_translation['name'][$this->secondary_language], 'Term title is displayed correctly in non-default language.');
    $this->assertText($term_translation['description'][$this->secondary_language], 'Term description is displayed correctly in non-default language.');

    // Autocomplete should respect localization.
    $autocomplete_path = 'taxonomy/autocomplete/' . $this->field_name . '/' . $prefix;
    $autocomplete_values = $this->drupalGetAJAX($autocomplete_path);
    $this->assertTrue(isset($autocomplete_values[$term->name]), 'Correct autocomplete suggestions in default language.');
    $this->assertFalse(isset($autocomplete_values[$term_translation['name'][$this->secondary_language]]), 'No incorrect autocomplete suggestions in non-default languages');

 // Autocomplete should respect localization, but doesn't.
 //   $autocomplete_path = $this->secondary_language . '/taxonomy/autocomplete/' . $this->field_name . '/' . $prefix;
 //   $autocomplete_values = $this->drupalGetAJAX($autocomplete_path);
 //   $this->assertFalse(isset($autocomplete_values[$term->name]), 'Correct autocomplete suggestions in non-default languages.');
 //   $this->assertTrue(isset($autocomplete_values[$term_translation[$this->secondary_language]]), 'No incorrect autocomplete suggestions in non-default languages.');
  }

  function testTaxonomyTermTranslate() {
    // Create a translateable vocabulary.
    $vocab = $this->createVocabulary(array('i18n_mode' => I18N_MODE_TRANSLATE));
    $this->assertEqual(i18n_taxonomy_vocabulary_mode($vocab->vid), I18N_MODE_TRANSLATE, 'A vocabulary has been created and it is translateable.');

    $this->field_select = $this->createTermField($vocab->machine_name);
    $this->field_autocomplete = $this->createTermField($vocab->machine_name, 'taxonomy_autocomplete');

    // Create a term to be translated.
    $en_term = $this->createTerm(array('vid' => $vocab->vid, 'language' => $this->default_language));
    $es_term = $this->createTerm(array('vid' => $vocab->vid, 'language' => $this->secondary_language));

    $this->drupalLogin($this->admin_user);

    // Set terms as translations of each other.
    $edit = array(
      'translations[' . $this->default_language . ']' => $en_term->name,
      'translations[' . $this->secondary_language . ']' => $es_term->name,
    );
    $this->drupalPost('admin/structure/taxonomy/' . $vocab->machine_name . '/list/sets/add', $edit, t('Save'));
    $this->drupalGet('admin/structure/taxonomy/' . $vocab->machine_name . '/list/sets');

    // Freetagging creates terms with the correct language.
    $new_term_name = $this->randomName();
    $langcode = LANGUAGE_NONE;
    $edit = array(
      "{$this->field_autocomplete}[$langcode]" => $new_term_name,
    );
    $this->drupalPost($this->secondary_language . '/test-entity/add/test-bundle', $edit, t('Save'));
    $new_term = current(taxonomy_get_term_by_name($new_term_name));
    $this->assertEqual($new_term->language,  $this->secondary_language, 'Freetagging creates terms with the correct language.');

    // Term translations are used for language switching.
    $language_switcher = language_negotiation_get_switch_links(LANGUAGE_TYPE_INTERFACE, 'taxonomy/term/' . $en_term->tid);
    $this->assertEqual($language_switcher->links[$this->secondary_language]['href'], 'taxonomy/term/' . $es_term->tid, 'Term translations are used for language switching.');
  }

  /**
   * Tests the implementation of 'options_list_callback' for term reference fields.
   * Enable and disable the callback properly. Avoid WSOD!
   */
  function testTaxonomyFieldCallback() {
    $field_name = 'taxonomy_term_test_field';
    $field = field_create_field(array(
      'field_name' => $field_name,
      'type' => 'taxonomy_term_reference',
    ));
    $field = field_info_field($field_name);
    $callback = 'i18n_taxonomy_allowed_values';
    $this->assertTrue(function_exists($callback), "Function $callback exists.");
    $this->assertEqual($field['settings']['options_list_callback'], $callback, "$callback ist option list callback.");
    module_disable(array('i18n_taxonomy'));
    $field = field_info_field($field_name);
    $this->assertNotEqual($field['settings']['options_list_callback'], $callback, "$callback ist option list callback.");
  }

  // Create vocabulary with given fields
  function drupalCreateVocabulary($vocab = array()) {
    $vocab += array('name' => $this->randomName(10), 'description' => $this->randomName(20));
    taxonomy_vocabulary_save($vocab);
    return (object)$vocab;
  }
  // Create term with given fields
  function drupalCreateTerms($number = 1, $data = array()) {
    $list = array();
    for ($i = 1; $i <= $number ; $i++ ) {
      $term = $this->createTerm($data);
      $list[$term->tid] = $term;
    }
    return $list;
  }

  /**
   * Returns a new vocabulary with random properties.
   */
  function createVocabulary($data = array()) {
    // Create a vocabulary.
    $data += array(
      'i18n_mode' => I18N_MODE_LOCALIZE,
      'name' => $this->randomName(),
      'description' => $this->randomName(),
      'machine_name' => drupal_strtolower($this->randomName()),
      'help' => '',
      'nodes' => array('article' => 'article'),
      'weight' => mt_rand(0, 10),
    );
    $vocabulary = (object)$data;
    taxonomy_vocabulary_save($vocabulary);
    return $vocabulary;
  }

  /**
   * Returns a new term with random properties in vocabulary $vid.
   */
  function createTerm($data = array()) {
    $data += array(
      'name' => $this->randomName(),
      'description' => $this->randomName(),
      // Use the first available text format and vocabulary.
      'format' => filter_default_format(),
      'vid' => 1,
    );
    $term = (object)$data;
    taxonomy_term_save($term);
    return $term;
  }

  /**
   * Setup a field and instance.
   */
  function createTermField($machine_name, $widget = 'options_select') {
    $field_name = drupal_strtolower($this->randomName());

    $this->field = array(
      'field_name' => $field_name,
      'type' => 'taxonomy_term_reference',
      'settings' => array(
        'allowed_values' => array(
          array(
            'vocabulary' => $machine_name,
            'parent' => '0',
          ),
        ),
      )
    );
    field_create_field($this->field);
    $this->instance = array(
      'field_name' => $field_name,
      'entity_type' => 'test_entity',
      'bundle' => 'test_bundle',
      'widget' => array(
        'type' => $widget,
      ),
      'display' => array(
        'full' => array(
          'type' => 'taxonomy_term_reference_link',
        ),
      ),
    );
    field_create_instance($this->instance);

    return $field_name;
  }
}
