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

class CustomErrorTestCase extends DrupalWebTestCase {

  /**
   * Implements getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => 'Custom Error',
      'description' => 'Check that the custom error messages are displayed.',
      'group' => 'Custom Error',
    );
  }

  /**
   * Enable any modules required for the test.
   */
  public function setUp() {
    parent::setUp('customerror');
  }

  /**
   * Tests 404 pages.
   */
  public function testPageNotFoundMessage() {
    // Set title and description of error message.
    $title = $this->randomName(10);
    $description = $this->randomName(80);
    variable_set('site_404', 'customerror/404');
    variable_set('customerror_404_title', $title);
    variable_set('customerror_404', $description);

    // Access error page directly, check for response code, title
    // and description of error message.
    $this->drupalGet('customerror/404');
    $this->assertResponse(404, 'Response code on 404 error page set when accessed directly.');
    $this->assertText($title, 'Title on 404 error page set when accessed directly.');
    $this->assertText($description, 'Description on 404 error page set when accessed directly.');

    // Access a non-existing page, check for response code, title
    // and description of error message.
    $this->drupalGet('foo/dontexist');
    $this->assertResponse(404, 'Response code on 404 error page set when accessed at non-existent URL.');
    $this->assertText($title, 'Title on 404 error page set when accessed at non-existent URL.');
    $this->assertText($description, 'Description on 404 error page set when accessed at non-existent URL.');
  }


  /**
   * Tests 403 pages.
   */
  public function testAccessDeniedMessage() {
    // Set title and description of error message.
    $title = $this->randomName(10);
    $description = $this->randomName(80);
    variable_set('site_403', 'customerror/403');
    variable_set('customerror_403_title', $title);
    variable_set('customerror_403', $description);

    // Access error page directly, check for response code, title
    // and description of error message.
    $this->drupalGet('customerror/403');
    $this->assertResponse(403, 'Response code on 403 error page set when accessed at non-existent URL');
    $this->assertText($title, 'Title on 403 error page set when accessed directly');
    $this->assertText($description, 'Description on 403 error page set when accessed directly');

    // Access admin page as an anonymous user, check for response code, title
    // and description of error message.
    $this->drupalGet('admin');
    $this->assertResponse(403, 'Response code on 403 error page set when accessed at non-existent URL');
    $this->assertText($title, 'Title on 403 error page set when accessed at non-existent URL');
    $this->assertText($description, 'Description on 403 error page set when accessed at non-existent URL');
  }
}
