Projet

Général

Profil

Paste
Télécharger (4,58 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / simpletest / tests / error.test @ 76597ebf

1
<?php
2

    
3
/**
4
 * Tests Drupal error and exception handlers.
5
 */
6
class DrupalErrorHandlerTestCase extends DrupalWebTestCase {
7
  public static function getInfo() {
8
    return array(
9
      'name' => 'Drupal error handlers',
10
      'description' => 'Performs tests on the Drupal error and exception handler.',
11
      'group' => 'System',
12
    );
13
  }
14

    
15
  function setUp() {
16
    parent::setUp('error_test');
17
  }
18

    
19
  /**
20
   * Test the error handler.
21
   */
22
  function testErrorHandler() {
23
    $error_notice = array(
24
      '%type' => 'Notice',
25
      '!message' => 'Undefined variable: bananas',
26
      '%function' => 'error_test_generate_warnings()',
27
      '%file' => drupal_realpath('modules/simpletest/tests/error_test.module'),
28
    );
29
    $error_warning = array(
30
      '%type' => 'Warning',
31
      '!message' => 'Division by zero',
32
      '%function' => 'error_test_generate_warnings()',
33
      '%file' => drupal_realpath('modules/simpletest/tests/error_test.module'),
34
    );
35
    $error_user_notice = array(
36
      '%type' => 'User warning',
37
      '!message' => 'Drupal is awesome',
38
      '%function' => 'error_test_generate_warnings()',
39
      '%file' => drupal_realpath('modules/simpletest/tests/error_test.module'),
40
    );
41

    
42
    // Set error reporting to collect notices.
43
    variable_set('error_level', ERROR_REPORTING_DISPLAY_ALL);
44
    $this->drupalGet('error-test/generate-warnings');
45
    $this->assertResponse(200, 'Received expected HTTP status code.');
46
    $this->assertErrorMessage($error_notice);
47
    $this->assertErrorMessage($error_warning);
48
    $this->assertErrorMessage($error_user_notice);
49

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

    
58
    // Set error reporting to not show any errors.
59
    variable_set('error_level', ERROR_REPORTING_HIDE);
60
    $this->drupalGet('error-test/generate-warnings');
61
    $this->assertResponse(200, 'Received expected HTTP status code.');
62
    $this->assertNoErrorMessage($error_notice);
63
    $this->assertNoErrorMessage($error_warning);
64
    $this->assertNoErrorMessage($error_user_notice);
65
  }
66

    
67
  /**
68
   * Test the exception handler.
69
   */
70
  function testExceptionHandler() {
71
    $error_exception = array(
72
      '%type' => 'Exception',
73
      '!message' => 'Drupal is awesome',
74
      '%function' => 'error_test_trigger_exception()',
75
      '%line' => 57,
76
      '%file' => drupal_realpath('modules/simpletest/tests/error_test.module'),
77
    );
78
    $error_pdo_exception = array(
79
      '%type' => 'PDOException',
80
      '!message' => 'SELECT * FROM bananas_are_awesome',
81
      '%function' => 'error_test_trigger_pdo_exception()',
82
      '%line' => 65,
83
      '%file' => drupal_realpath('modules/simpletest/tests/error_test.module'),
84
    );
85

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

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

    
100
  /**
101
   * Helper function: assert that the error message is found.
102
   */
103
  function assertErrorMessage(array $error) {
104
    $message = t('%type: !message in %function (line ', $error);
105
    $this->assertRaw($message, format_string('Found error message: !message.', array('!message' => $message)));
106
  }
107

    
108
  /**
109
   * Helper function: assert that the error message is not found.
110
   */
111
  function assertNoErrorMessage(array $error) {
112
    $message = t('%type: !message in %function (line ', $error);
113
    $this->assertNoRaw($message, format_string('Did not find error message: !message.', array('!message' => $message)));
114
  }
115
}
116