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

module_load_include('inc', 'pathauto_i18n', 'tests/pathauto_i18n.test');
/**
 * Test functionality for nodes.
 */
class Pathautoi18nNodeTest extends Pathautoi18nTest {

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

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

    // Configure patterns for all language for easy testing.
    $edit = array(
      'pathauto_node_article_pattern' => 'all/[node:title]',
      'pathauto_node_article_und_pattern' => 'neutral/[node:title]',
    );

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

  /**
   * Test nodes with automatic alias for certain language.
   */
  public function testAutomaticAliasLanguageSelected() {
    $this->createNode($this->contentLanguage, 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 nodes with custom alias for certain language.
   */
  public function testCustomAliasLanguageSelected() {
    $custom_alias = 'custom/' . $this->title;
    $this->createNode($this->contentLanguage, TRUE, FALSE, $custom_alias);
    // Check aliases and custom alias.
    $this->drupalGet('admin/config/search/path');
    foreach ($this->availableLanguages as $language) {
      if ($language == 'en') {
        $alias = $language . '/' . $this->title;
        $this->assertText($custom_alias, 0, "Exist custom alias '$custom_alias'.");
        $this->assertNoText($alias, 0, "Alias '$alias' for language '$language' not exist.");
      }
      else {
        $alias = $language . '/' . $this->title;
        $this->assertText($alias, 0, "Exist alias '$alias' for language '$language'.");
      }
    }
  }

  /**
   * Test nodes with automatic alias for undefined language.
   */
  public function testAutomaticAliasUndefinedLanguage() {
    $this->contentLanguage = LANGUAGE_NONE;
    $this->testAutomaticAliasLanguageSelected();
    $neutral_alias = 'neutral/' . $this->title;
    $this->assertNoText($neutral_alias, 0, "Alias '$neutral_alias' for undefined language not exist.");
  }

  /**
   * Test nodes with custom alias for undefined language.
   */
  public function testCustomAliasUndefinedLanguage() {
    $custom_alias = 'custom/' . $this->title;
    $neutral_alias = 'neutral/' . $this->title;
    $this->createNode(LANGUAGE_NONE, TRUE, FALSE, $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->createNode($this->contentLanguage, 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 node.
   */
  public function createNode($language, $pathauto_i18n_status, $pathauto, $alias = FALSE) {
    $settings = array(
      'type' => 'article',
      'title' => $this->title,
      'body' => array(
        $language => array(
          array(
            $this->randomName(64),
          ),
        ),
      ),
      'language' => $language,
      'path' => array(
        'pathauto_i18n_status' => $pathauto_i18n_status,
        'pathauto' => $pathauto,
      ),
    );
    if (!empty($alias)) {
      $settings['path']['alias'] = $alias;
    }
    $node = $this->drupalCreateNode($settings);
  }
}
