<?php
// $Id: xmlsitemap_custom.test,v 1.6 2010/01/24 03:57:14 davereid Exp $

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

class XMLSitemapCustomFunctionalTest extends XMLSitemapTestHelper {
  protected $admin_user;

  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() {
    parent::setUp('xmlsitemap_custom', 'path');
    $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/config/people/accounts';
    $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');
    $link = $this->assertSitemapLink(array('type' => 'custom', 'loc' => 'system/files'));
    $this->assertSitemapLinkValues($link, 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');
    $link = $this->assertSitemapLink(array('type' => 'custom', 'id' => $link['id']));
    $this->assertSitemapLinkValues($link, 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');
    $link = $this->assertSitemapLink(array('type' => 'custom', 'loc' => $edit['loc']));

    // 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');
    $link = $this->assertSitemapLink(array('type' => 'custom', 'loc' => $edit['loc']));
  }
}
