ErrorHandlerTest.php 4.64 KB
Newer Older
1
2
3
<?php

/**
4
5
 * @file
 * Definition of Drupal\system\Tests\ErrorHandlerTest.
6
 */
7

8
9
namespace Drupal\system\Tests;

10
11
use Drupal\simpletest\WebTestBase;

12
13
14
15
/**
 * Tests error and exception handlers.
 */
class ErrorHandlerTest extends WebTestBase {
16
17
  public static function getInfo() {
    return array(
18
      'name' => 'Error handlers',
19
20
      'description' => 'Performs tests on the Drupal error and exception handler.',
      'group' => 'System',
21
22
23
24
25
26
27
28
29
30
31
32
33
    );
  }

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

  /**
   * Test the error handler.
   */
  function testErrorHandler() {
    $error_notice = array(
      '%type' => 'Notice',
34
      '!message' => 'Undefined variable: bananas',
35
      '%function' => 'error_test_generate_warnings()',
36
      '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
37
38
39
    );
    $error_warning = array(
      '%type' => 'Warning',
40
      '!message' => 'Division by zero',
41
      '%function' => 'error_test_generate_warnings()',
42
      '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
43
44
    );
    $error_user_notice = array(
45
      '%type' => 'User warning',
46
      '!message' => 'Drupal is awesome',
47
      '%function' => 'error_test_generate_warnings()',
48
      '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
49
50
51
52
53
    );

    // Set error reporting to collect notices.
    variable_set('error_level', ERROR_REPORTING_DISPLAY_ALL);
    $this->drupalGet('error-test/generate-warnings');
54
    $this->assertResponse(200, t('Received expected HTTP status code.'));
55
56
57
58
59
60
61
    $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');
62
    $this->assertResponse(200, t('Received expected HTTP status code.'));
63
64
65
66
67
68
69
    $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');
70
    $this->assertResponse(200, t('Received expected HTTP status code.'));
71
72
73
74
75
76
77
78
79
80
81
    $this->assertNoErrorMessage($error_notice);
    $this->assertNoErrorMessage($error_warning);
    $this->assertNoErrorMessage($error_user_notice);
  }

  /**
   * Test the exception handler.
   */
  function testExceptionHandler() {
    $error_exception = array(
      '%type' => 'Exception',
82
      '!message' => 'Drupal is awesome',
83
      '%function' => 'error_test_trigger_exception()',
84
      '%line' => 56,
85
      '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
86
87
    );
    $error_pdo_exception = array(
88
      '%type' => 'DatabaseExceptionWrapper',
89
      '!message' => 'SELECT * FROM bananas_are_awesome',
90
      '%function' => 'error_test_trigger_pdo_exception()',
91
      '%line' => 64,
92
      '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
93
94
95
    );

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

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

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

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