<?php

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

class XMLSitemapCustomFunctionalTest extends XMLSitemapTestHelper {
  public static function getInfo() {
    return array(
      'name' => 'XML sitemap custom interface tests',
      'description' => 'Functional tests for the XML sitemap custom module.',
      'group' => 'XML sitemap',
    );
  }

  function setUp($modules = array()) {
    $modules[] = 'xmlsitemap_custom';
    $modules[] = 'path';
    parent::setUp($modules);

    $this->admin_user = $this->drupalCreateUser(array('access content', 'administer xmlsitemap'));
    $this->drupalLogin($this->admin_user);
  }

  function testCustomLinks() {
    // Set a path alias for the node page.
    $alias = array('source' => 'system/files', 'alias' => 'public-files');
    path_save($alias);

    $this->drupalGet('admin/config/search/xmlsitemap/custom');
    $this->clickLink(t('Add custom link'));

    // Test an invalid path.
    $edit['loc'] = 'invalid-testing-path';
    $this->drupalPost(NULL, $edit, t('Save'));
    $this->assertText(t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', array('@link' => $edit['loc'])));
    $this->assertNoSitemapLink(array('type' => 'custom', 'loc' => $edit['loc']));

    // Test a path not accessible to anonymous user.
    $edit['loc'] = 'admin/people/people';
    $this->drupalPost(NULL, $edit, t('Save'));
    $this->assertText(t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', array('@link' => $edit['loc'])));
    $this->assertNoSitemapLink(array('type' => 'custom', 'loc' => $edit['loc']));

    // Test that the current page, which should not give a false positive for
    // $menu_item['access'] since the result has been cached already.
    $edit['loc'] = 'admin/config/search/xmlsitemap/custom/add';
    $this->drupalPost(NULL, $edit, t('Save'));
    $this->assertText(t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', array('@link' => $edit['loc'])));
    $this->assertNoSitemapLink(array('type' => 'custom', 'loc' => $edit['loc']));

    // Add an aliased path with padded spaces.
    $edit['loc'] = ' public-files ';
    $this->drupalPost(NULL, $edit, t('Save'));
    $this->assertText('The custom link for system/files was saved');
    $links = xmlsitemap_link_load_multiple(array('type' => 'custom', 'loc' => 'system/files'));
    $this->assertEqual(count($links), 1, t('Custom link saved in the database.'));
    $link = reset($links);
    $this->assertSitemapLinkValues('custom', $link['id'], array('priority' => 0.5, 'changefreq' => 0, 'access' => 1, 'status' => 1));

    $this->clickLink('Edit');
    $edit = array(
      'priority' => 0.1,
      'changefreq' => XMLSITEMAP_FREQUENCY_ALWAYS,
    );
    $this->drupalPost(NULL, $edit, t('Save'));
    $this->assertText('The custom link for system/files was saved');
    $this->assertSitemapLinkValues('custom', $link['id'], array('priority' => 0.1, 'changefreq' => XMLSITEMAP_FREQUENCY_ALWAYS, 'access' => 1, 'status' => 1));

    $this->clickLink('Delete');
    $this->drupalPost(NULL, array(), t('Delete'));
    $this->assertText('The custom link for system/files has been deleted.');
    $this->assertNoSitemapLink(array('type' => 'custom', 'loc' => 'system/files'));
  }

  /**
   * Test adding files as custom links.
   */
  function testCustomFileLinks() {
    // Test an invalid file.
    $edit['loc'] = $this->randomName();
    $this->drupalPost('admin/config/search/xmlsitemap/custom/add', $edit, t('Save'));
    $this->assertText(t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', array('@link' => $edit['loc'])));
    $this->assertNoSitemapLink(array('type' => 'custom', 'loc' => $edit['loc']));

    // Test an unaccessible file .
    //$edit['loc'] = '.htaccess';
    //$this->drupalPost('admin/config/search/xmlsitemap/custom/add', $edit, t('Save'));
    //$this->assertText(t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', array('@link' => $edit['loc'])));
    //$this->assertNoSitemapLink(array('type' => 'custom', 'loc' => $edit['loc']));

    // Test a valid file.
    $edit['loc'] = 'misc/drupal.js';
    $this->drupalPost('admin/config/search/xmlsitemap/custom/add', $edit, t('Save'));
    $this->assertText('The custom link for ' . $edit['loc'] . ' was saved');
    $links = xmlsitemap_link_load_multiple(array('type' => 'custom', 'loc' => $edit['loc']));
    $this->assertEqual(count($links), 1, t('Custom link saved in the database.'));

    // Test a valid folder.
    $edit['loc'] = 'misc';
    $this->drupalPost('admin/config/search/xmlsitemap/custom/add', $edit, t('Save'));
    $this->assertText('The custom link for ' . $edit['loc'] . ' was saved');
    $links = xmlsitemap_link_load_multiple(array('type' => 'custom', 'loc' => $edit['loc']));
    $this->assertEqual(count($links), 1, t('Custom link saved in the database.'));
  }
}
