<?php
// $Id: pathauto.test,v 1.2.2.1 2008/06/22 15:59:21 freso Exp $

/**
 * @file
 * Functionality tests for Pathauto.
 *
 * @ingroup pathauto
 */

/**
 * TODO
 */
class PathautoTestCase extends DrupalTestCase {
  protected $web_user;
  protected $content_type;

  /**
   * Implementation of getInfo().
   */
  function get_info() {
    return array(
      'name' => t('Pathauto basic tests'),
      'desc' => t('Test basic pathauto functionality.'),
      'group' => 'Pathauto',
    );
  }

  /**
   * Implementation of setUp().
   */
  function setUp() {
    // Custom content type for path setting tests.
    $this->content_type = $this->drupalCreateContentType();
    menu_rebuild();

    // User to create paths with pathauto.
    $this->web_user = $this->drupalCreateUserRolePerm(array(
      'administer pathauto',
      'create '. $this->content_type->type .' content',
      'edit own '. $this->content_type->type .' content',
      'administer url aliases',
      'create url aliases',
    ));
  }

  /**
   * testPathAuto
   *
   * TODO: Better description!
   */
  function testPathAuto() {
    $this->drupalGet('logout');
    $this->drupalLoginUser($this->web_user);

    $this->drupalGet('admin/build/path/pathauto');
    $this->assertWantedRaw(t('General settings'), '[testPathAuto]: Settings page displayed');

    // Set pathauto settings we assume to be as-is in this test.
    $this->drupalVariableSet('pathauto_node_'. $this->content_type->type .'_pattern', 'content/[title-raw]');
    $this->drupalVariableSet('pathauto_separator', '-');
    $this->drupalVariableSet('pathauto_case', '1'); // lowercase
    $this->drupalVariableSet('pathauto_max_length', '100');
    $this->drupalVariableSet('pathauto_max_component_length', '100');
    $this->drupalVariableSet('pathauto_punctuation_underscore', '2'); // no action

    // Create node for testing.
    $random_title = $this->randomName(10); // this contains _
    $edit['title'] = 'Simpletest title '. $random_title;
    $expected_alias = 'content/simpletest-title-'. strtolower($random_title);
    $edit['body'] = 'Simpletest body '. $this->randomName(10);
    $this->drupalPost('node/add/'. str_replace('_', '-', $this->content_type->type), $edit, 'Save');
    $this->assertWantedRaw(t('@type %title has been created.', array('@type' => $this->content_type->name, '%title' => $edit['title'])), '[testPathAuto]: Test node submitted');

    // Look for alias generated in the form.
    $node = node_load(array('title' => $edit['title']));
    $this->drupalGet('node/'. $node->nid .'/edit');
    $this->assertWantedPattern('!<input[^>]+name="path"[^>]+value="'. $expected_alias .'"!', $this->drupalGetContent(), '[testPathAuto]: Proper automated alias generated');
    // Look for checkbox.
    $this->assertWantedPattern('!<input[^>]+name="pathauto_perform_alias"[^>]+checked="checked"!', $this->drupalGetContent(), '[testPathAuto]: Automated alias turned on');

    // Check whether the alias actually works.
    $this->drupalGet($expected_alias);
    $this->assertWantedRaw($edit['title'], '[testPathAuto]: Node accessible through alias');
  }
}
