summaryrefslogtreecommitdiff
path: root/modules/simpletest/tests/error.test
blob: b1ec4b3f5ca64ab902cc6fe6c83efcc34a01befc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php

/**
 * Tests Drupal error and exception handlers.
 */
class DrupalErrorHandlerUnitTest extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Drupal error handlers',
      'description' => 'Performs tests on the Drupal error and exception handler.',
      'group' => 'System',
    );
  }

  function setUp() {
    parent::setUp('error_test');
  }

  /**
   * Test the error handler.
   */
  function testErrorHandler() {
    $error_notice = array(
      '%type' => 'Notice',
      '!message' => 'Undefined variable: bananas',
      '%function' => 'error_test_generate_warnings()',
      '%file' => drupal_realpath('modules/simpletest/tests/error_test.module'),
    );
    $error_warning = array(
      '%type' => 'Warning',
      '!message' => 'Division by zero',
      '%function' => 'error_test_generate_warnings()',
      '%file' => drupal_realpath('modules/simpletest/tests/error_test.module'),
    );
    $error_user_notice = array(
      '%type' => 'User warning',
      '!message' => 'Drupal is awesome',
      '%function' => 'error_test_generate_warnings()',
      '%file' => drupal_realpath('modules/simpletest/tests/error_test.module'),
    );

    // Set error reporting to collect notices.
    variable_set('error_level', ERROR_REPORTING_DISPLAY_ALL);
    $this->drupalGet('error-test/generate-warnings');
    $this->assertResponse(200, t('Received expected HTTP status code.'));
    $this->assertErrorMessage($error_notice);
    $this->assertErrorMessage($error_warning);
    $this->assertErrorMessage($error_user_notice);

    // Set error reporting to not collect notices.
    variable_set('error_level', ERROR_REPORTING_DISPLAY_SOME);
    $this->drupalGet('error-test/generate-warnings');
    $this->assertResponse(200, t('Received expected HTTP status code.'));
    $this->assertNoErrorMessage($error_notice);
    $this->assertErrorMessage($error_warning);
    $this->assertErrorMessage($error_user_notice);

    // Set error reporting to not show any errors.
    variable_set('error_level', ERROR_REPORTING_HIDE);
    $this->drupalGet('error-test/generate-warnings');
    $this->assertResponse(200, t('Received expected HTTP status code.'));
    $this->assertNoErrorMessage($error_notice);
    $this->assertNoErrorMessage($error_warning);
    $this->assertNoErrorMessage($error_user_notice);
  }

  /**
   * Test the exception handler.
   */
  function testExceptionHandler() {
    $error_exception = array(
      '%type' => 'Exception',
      '!message' => 'Drupal is awesome',
      '%function' => 'error_test_trigger_exception()',
      '%line' => 57,
      '%file' => drupal_realpath('modules/simpletest/tests/error_test.module'),
    );
    $error_pdo_exception = array(
      '%type' => 'PDOException',
      '!message' => 'SELECT * FROM bananas_are_awesome',
      '%function' => 'error_test_trigger_pdo_exception()',
      '%line' => 65,
      '%file' => drupal_realpath('modules/simpletest/tests/error_test.module'),
    );

    $this->drupalGet('error-test/trigger-exception');
    $this->assertTrue(strpos($this->drupalGetHeader(':status'), '500 Service unavailable (with message)'), t('Received expected HTTP status line.'));
    $this->assertErrorMessage($error_exception);

    $this->drupalGet('error-test/trigger-pdo-exception');
    $this->assertTrue(strpos($this->drupalGetHeader(':status'), '500 Service unavailable (with message)'), t('Received expected HTTP status line.'));
    // We cannot use assertErrorMessage() since the extact error reported
    // varies from database to database. Check that the SQL string is displayed.
    $this->assertText($error_pdo_exception['%type'], t('Found %type in error page.', $error_pdo_exception));
    $this->assertText($error_pdo_exception['!message'], t('Found !message in error page.', $error_pdo_exception));
    $error_details = t('in %function (line ', $error_pdo_exception);
    $this->assertRaw($error_details, t("Found '!message' in error page.", array('!message' => $error_details)));
  }

  /**
   * Helper function: assert that the error message is found.
   */
  function assertErrorMessage(array $error) {
    $message = t('%type: !message in %function (line ', $error);
    $this->assertRaw($message, t('Found error message: !message.', array('!message' => $message)));
  }

  /**
   * Helper function: assert that the error message is not found.
   */
  function assertNoErrorMessage(array $error) {
    $message = t('%type: !message in %function (line ', $error);
    $this->assertNoRaw($message, t('Did not find error message: !message.', array('!message' => $message)));
  }
}