error.test 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Tests Drupal error and exception handlers.
  4. */
  5. class DrupalErrorHandlerTestCase extends DrupalWebTestCase {
  6. public static function getInfo() {
  7. return array(
  8. 'name' => 'Drupal error handlers',
  9. 'description' => 'Performs tests on the Drupal error and exception handler.',
  10. 'group' => 'System',
  11. );
  12. }
  13. function setUp() {
  14. parent::setUp('error_test');
  15. }
  16. /**
  17. * Test the error handler.
  18. */
  19. function testErrorHandler() {
  20. $error_notice = array(
  21. '%type' => 'Notice',
  22. '!message' => 'Undefined variable: bananas',
  23. '%function' => 'error_test_generate_warnings()',
  24. '%file' => drupal_realpath('modules/simpletest/tests/error_test.module'),
  25. );
  26. $error_warning = array(
  27. '%type' => 'Warning',
  28. '!message' => 'Division by zero',
  29. '%function' => 'error_test_generate_warnings()',
  30. '%file' => drupal_realpath('modules/simpletest/tests/error_test.module'),
  31. );
  32. $error_user_notice = array(
  33. '%type' => 'User warning',
  34. '!message' => 'Drupal is awesome',
  35. '%function' => 'error_test_generate_warnings()',
  36. '%file' => drupal_realpath('modules/simpletest/tests/error_test.module'),
  37. );
  38. // Set error reporting to collect notices.
  39. variable_set('error_level', ERROR_REPORTING_DISPLAY_ALL);
  40. $this->drupalGet('error-test/generate-warnings');
  41. $this->assertResponse(200, 'Received expected HTTP status code.');
  42. $this->assertErrorMessage($error_notice);
  43. $this->assertErrorMessage($error_warning);
  44. $this->assertErrorMessage($error_user_notice);
  45. // Set error reporting to not collect notices.
  46. variable_set('error_level', ERROR_REPORTING_DISPLAY_SOME);
  47. $this->drupalGet('error-test/generate-warnings');
  48. $this->assertResponse(200, 'Received expected HTTP status code.');
  49. $this->assertNoErrorMessage($error_notice);
  50. $this->assertErrorMessage($error_warning);
  51. $this->assertErrorMessage($error_user_notice);
  52. // Set error reporting to not show any errors.
  53. variable_set('error_level', ERROR_REPORTING_HIDE);
  54. $this->drupalGet('error-test/generate-warnings');
  55. $this->assertResponse(200, 'Received expected HTTP status code.');
  56. $this->assertNoErrorMessage($error_notice);
  57. $this->assertNoErrorMessage($error_warning);
  58. $this->assertNoErrorMessage($error_user_notice);
  59. }
  60. /**
  61. * Test the exception handler.
  62. */
  63. function testExceptionHandler() {
  64. $error_exception = array(
  65. '%type' => 'Exception',
  66. '!message' => 'Drupal is awesome',
  67. '%function' => 'error_test_trigger_exception()',
  68. '%line' => 57,
  69. '%file' => drupal_realpath('modules/simpletest/tests/error_test.module'),
  70. );
  71. $error_pdo_exception = array(
  72. '%type' => 'PDOException',
  73. '!message' => 'SELECT * FROM bananas_are_awesome',
  74. '%function' => 'error_test_trigger_pdo_exception()',
  75. '%line' => 65,
  76. '%file' => drupal_realpath('modules/simpletest/tests/error_test.module'),
  77. );
  78. $this->drupalGet('error-test/trigger-exception');
  79. $this->assertTrue(strpos($this->drupalGetHeader(':status'), '500 Service unavailable (with message)'), 'Received expected HTTP status line.');
  80. $this->assertErrorMessage($error_exception);
  81. $this->drupalGet('error-test/trigger-pdo-exception');
  82. $this->assertTrue(strpos($this->drupalGetHeader(':status'), '500 Service unavailable (with message)'), 'Received expected HTTP status line.');
  83. // We cannot use assertErrorMessage() since the extact error reported
  84. // varies from database to database. Check that the SQL string is displayed.
  85. $this->assertText($error_pdo_exception['%type'], format_string('Found %type in error page.', $error_pdo_exception));
  86. $this->assertText($error_pdo_exception['!message'], format_string('Found !message in error page.', $error_pdo_exception));
  87. $error_details = format_string('in %function (line ', $error_pdo_exception);
  88. $this->assertRaw($error_details, format_string("Found '!message' in error page.", array('!message' => $error_details)));
  89. }
  90. /**
  91. * Helper function: assert that the error message is found.
  92. */
  93. function assertErrorMessage(array $error) {
  94. $message = t('%type: !message in %function (line ', $error);
  95. $this->assertRaw($message, format_string('Found error message: !message.', array('!message' => $message)));
  96. }
  97. /**
  98. * Helper function: assert that the error message is not found.
  99. */
  100. function assertNoErrorMessage(array $error) {
  101. $message = t('%type: !message in %function (line ', $error);
  102. $this->assertNoRaw($message, format_string('Did not find error message: !message.', array('!message' => $message)));
  103. }
  104. }