<?php
// $Id: xmlsitemap_i18n.test,v 1.5 2010/01/24 03:56:51 davereid Exp $

/**
 * @file
 * Unit tests for the xmlsitemap_i18n module.
 */

/**
 * Common base test class for XML sitemap internationalization tests.
 */
class XMLSitemapI18nWebTestCase extends XMLSitemapTestHelper {
  protected $admin_user;

  /**
   * Set up an administrative user account and testing keys.
   */
  function setUp() {
    // Call parent::setUp() allowing test cases to pass further modules.
    $modules = func_get_args();
    $modules = array_merge(array('locale', 'translation', 'i18n', 'xmlsitemap_i18n'), $modules);
    call_user_func_array(array('parent', 'setUp'), $modules);

    // Add predefined language and reset the locale cache.
    require_once DRUPAL_ROOT . '/includes/locale.inc';
    locale_add_language('fr', NULL, NULL, LANGUAGE_LTR, '', 'fr');

    variable_set('xmlsitemap_languages', array('en', 'fr'));
    variable_set('language_negotiation', LANGUAGE_NEGOTIATION_PATH);
  }
}

class XMLSitemapI18nTest extends XMLSitemapI18nWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'XML sitemap i18n tests',
      'description' => 'Functional and integration tests for the XML sitemap and internationalization modules.',
      'group' => 'XML sitemap',
      'dependencies' => array('i18n'),
    );
  }

  function testLanguageSelection() {
    // Create our three different language nodes.
    $node = $this->addSitemapLink(array('type' => 'node', 'language' => LANGUAGE_NONE));
    $node_en = $this->addSitemapLink(array('type' => 'node', 'language' => 'en'));
    $node_fr = $this->addSitemapLink(array('type' => 'node', 'language' => 'fr'));

    // Create three non-node language nodes.
    $link = $this->addSitemapLink(array('language' => LANGUAGE_NONE));
    $link_en = $this->addSitemapLink(array('language' => 'en'));
    $link_fr = $this->addSitemapLink(array('language' => 'fr'));

    variable_set('i18n_selection_mode', 'off');
    $this->regenerateSitemap();
    $this->drupalGetSitemap('en');
    $this->assertRawSitemapLinks($node, $node_en, $node_fr, $link, $link_en, $link_fr);
    $this->drupalGetSitemap('fr');
    $this->assertRawSitemapLinks($node, $node_en, $node_fr, $link, $link_en, $link_fr);

    variable_set('i18n_selection_mode', 'simple');
    $this->regenerateSitemap();
    $this->drupalGetSitemap('en');
    $this->assertRawSitemapLinks($node, $node_en, $link, $link_en);
    $this->assertNoRawSitemapLinks($node_fr, $link_fr);
    $this->drupalGetSitemap('fr');
    $this->assertRawSitemapLinks($node, $node_fr, $link, $link_fr);
    $this->assertNoRawSitemapLinks($node_en, $link_en);

    variable_set('i18n_selection_mode', 'mixed');
    $this->regenerateSitemap();
    $this->drupalGetSitemap('en');
    $this->assertRawSitemapLinks($node, $node_en, $link, $link_en);
    $this->assertNoRawSitemapLinks($node_fr, $link_fr);
    $this->drupalGetSitemap('fr');
    $this->assertRawSitemapLinks($node, $node_en, $node_fr, $link, $link_en, $link_fr);

    variable_set('i18n_selection_mode', 'default');
    $this->regenerateSitemap();
    $this->drupalGetSitemap('en');
    $this->assertRawSitemapLinks($node, $node_en, $link, $link_en);
    $this->assertNoRawSitemapLinks($node_fr, $link_fr);
    $this->drupalGetSitemap('fr');
    $this->assertRawSitemapLinks($node, $node_en, $link, $link_en);
    $this->assertNoRawSitemapLinks($node_fr, $link_fr);

    // With strict mode, the language neutral node should not be found, but the
    // language neutral non-node should be.
    variable_set('i18n_selection_mode', 'strict');
    $this->regenerateSitemap();
    $this->drupalGetSitemap('en');
    $this->assertRawSitemapLinks($node_en, $link, $link_en);
    $this->assertNoRawSitemapLinks($node, $node_fr, $link_fr);
    $this->drupalGetSitemap('fr');
    $this->assertRawSitemapLinks($node_fr, $link, $link_fr);
    $this->assertNoRawSitemapLinks($node, $node_en, $link_en);
  }
}

class XMLSitemapI18nNodeTest extends XMLSitemapI18nWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'XML sitemap i18n node tests',
      'description' => 'Functional and integration tests for the XML sitemap node and internationalization modules.',
      'group' => 'XML sitemap',
      'dependencies' => array('i18n'),
    );
  }

  function setUp() {
    parent::setUp('xmlsitemap_node');

    variable_set('language_content_type_page', 1);
    $this->admin_user = $this->drupalCreateUser(array('administer nodes'));
    $this->drupalLogin($this->admin_user);
  }

  function testNodeLanguageData() {
    $node = $this->drupalCreateNode(array());

    $this->drupalPost('node/' . $node->nid . '/edit', array('language' => 'en'), t('Save'));
    $node = node_load($node->nid, NULL, TRUE);
    $this->assertIdentical($node->xmlsitemap['language'], 'en');

    $this->drupalPost('node/' . $node->nid . '/edit', array('language' => 'fr'), t('Save'));
    $node = node_load($node->nid, NULL, TRUE);
    $this->assertIdentical($node->xmlsitemap['language'], 'fr');
  }
}
