<?php
/**
 * @file
 * Tests for the pathauto_i18n taxonomy module.
 */

module_load_include('inc', 'pathauto_i18n', 'tests/pathauto_i18n.test');
define('PATHAUTO_I18N_TAG_VOCABULARU_MACHINE_NAME', 'tags');

/**
 * Test functionality for taxonomy.
 */
class Pathautoi18nTaxonomyTest extends Pathautoi18nTest {

  /**
   * GetInfo method.
   */
  public static function getInfo() {
    return array(
      'name' => 'Pathauto i18n taxonomy',
      'description' => 'Ensure that the Pathauto i18n taxonomy works.',
      'group' => 'Pathauto i18n',
    );
  }

  /**
   * SetUp method.
   */
  public function setUp() {
    $modules[] = 'pathauto_i18n_taxonomy';
    $this->prepareTest($modules);

    // Configure patterns for all language for easy testing.
    $edit = array(
      'pathauto_taxonomy_term_pattern' => 'all/[term:name]',
      'pathauto_taxonomy_term_tags_pattern' => 'neutral/[term:name]',
    );

    foreach ($this->availableLanguages as $language) {
      $edit['pathauto_taxonomy_term_tags_' . $language . '_pattern'] = $language . '/[term:name]';
    }
    $this->drupalPost('admin/config/search/path/patterns', $edit, t('Save configuration'));
  }

  /**
   * Test taxonomy terms with automatic alias.
   */
  public function testAutomaticAliasTaxonomy() {
    $this->createTaxonomyTerm(TRUE, TRUE);
    // Check aliases.
    $this->drupalGet('admin/config/search/path');
    foreach ($this->availableLanguages as $language) {
      $alias = $language . '/' . $this->title;
      $this->assertText($alias, 0, "Exist alias '$alias' for language '$language'.");
    }
  }

  /**
   * Test taxonomy terms with custom alias for certain language.
   */
  public function testCustomAliasTaxonomy() {
    $custom_alias = 'custom/' . $this->title;
    $neutral_alias = 'neutral/' . $this->title;
    $this->createTaxonomyTerm(TRUE, TRUE, $custom_alias);
    // Check aliases and custom alias.
    $this->drupalGet('admin/config/search/path');
    foreach ($this->availableLanguages as $language) {
      $alias = $language . '/' . $this->title;
      $this->assertText($alias, 0, "Exist alias '$alias' for language '$language'.");
    }

    $this->assertText($custom_alias, 0, "Exist custom alias '$custom_alias'.");
    $this->assertNoText($neutral_alias, 0, "Alias '$neutral_alias' for undefined language not exist.");
  }

  /**
   * Test clearing of string.
   */
  public function testCleanString() {
    // Set appropriate title which will allow us remove parts of path.
    $initial_title = $this->title;
    $this->title .= ' ' . implode(' ', $this->availableLanguages);

    $this->setCleanStringSettings();
    $this->createTaxonomyTerm(TRUE, TRUE);

    // Check aliases.
    $this->drupalGet('admin/config/search/path');
    foreach ($this->availableLanguages as $language) {
      $suffix = $this->getCleanStringSuffix($language);
      $alias = $language . '/' . $initial_title . '/' . $suffix;
      $this->assertNoText($alias, 0, "Exist alias '$alias' for language '$language' with excluded string '$language'.");
    }
  }

  /**
   * Helper to create taxonomy term.
   */
  public function createTaxonomyTerm($pathauto_i18n_status, $pathauto, $alias = FALSE) {
    $vocabulary = taxonomy_vocabulary_machine_name_load(PATHAUTO_I18N_TAG_VOCABULARU_MACHINE_NAME);
    $term = new stdClass();
    $term->name = $this->title;
    $term->description = $this->title;
    // Use the first available text format.
    $term->format = db_query_range('SELECT format FROM {filter_format}', 0, 1)->fetchField();
    $term->vid = $vocabulary->vid;
    $term->path = array(
      'pathauto_i18n_status' => $pathauto_i18n_status,
      'pathauto' => $pathauto,
    );
    if (!empty($alias)) {
      $term->path['alias'] = $alias;
    }
    taxonomy_term_save($term);
  }
}
