PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/simpletest/tests/error.test

https://github.com/mikl/drupal
Unknown | 120 lines | 108 code | 12 blank | 0 comment | 0 complexity | 034907b87f7887fc6a40ecf1146f782b MD5 | raw file
  1. <?php
  2. // $Id: error.test,v 1.8 2010-08-05 23:53:38 webchick Exp $
  3. /**
  4. * Tests Drupal error and exception handlers.
  5. */
  6. class DrupalErrorHandlerUnitTest 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. function setUp() {
  15. parent::setUp('error_test');
  16. }
  17. /**
  18. * Test the error handler.
  19. */
  20. function testErrorHandler() {
  21. $error_notice = array(
  22. '%type' => 'Notice',
  23. '%message' => 'Undefined variable: bananas',
  24. '%function' => 'error_test_generate_warnings()',
  25. '%line' => 44,
  26. '%file' => drupal_realpath('modules/simpletest/tests/error_test.module'),
  27. );
  28. $error_warning = array(
  29. '%type' => 'Warning',
  30. '%message' => 'Division by zero',
  31. '%function' => 'error_test_generate_warnings()',
  32. '%line' => 46,
  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. '%line' => 48,
  40. '%file' => drupal_realpath('modules/simpletest/tests/error_test.module'),
  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, t('Received expected HTTP status code.'));
  46. $this->assertErrorMessage($error_notice);
  47. $this->assertErrorMessage($error_warning);
  48. $this->assertErrorMessage($error_user_notice);
  49. // Set error reporting to not collect notices.
  50. variable_set('error_level', ERROR_REPORTING_DISPLAY_SOME);
  51. $this->drupalGet('error-test/generate-warnings');
  52. $this->assertResponse(200, t('Received expected HTTP status code.'));
  53. $this->assertNoErrorMessage($error_notice);
  54. $this->assertErrorMessage($error_warning);
  55. $this->assertErrorMessage($error_user_notice);
  56. // Set error reporting to not show any errors.
  57. variable_set('error_level', ERROR_REPORTING_HIDE);
  58. $this->drupalGet('error-test/generate-warnings');
  59. $this->assertResponse(200, t('Received expected HTTP status code.'));
  60. $this->assertNoErrorMessage($error_notice);
  61. $this->assertNoErrorMessage($error_warning);
  62. $this->assertNoErrorMessage($error_user_notice);
  63. }
  64. /**
  65. * Test the exception handler.
  66. */
  67. function testExceptionHandler() {
  68. $error_exception = array(
  69. '%type' => 'Exception',
  70. '%message' => 'Drupal is awesome',
  71. '%function' => 'error_test_trigger_exception()',
  72. '%line' => 57,
  73. '%file' => drupal_realpath('modules/simpletest/tests/error_test.module'),
  74. );
  75. $error_pdo_exception = array(
  76. '%type' => 'PDOException',
  77. '%message' => 'SELECT * FROM bananas_are_awesome',
  78. '%function' => 'error_test_trigger_pdo_exception()',
  79. '%line' => 65,
  80. '%file' => drupal_realpath('modules/simpletest/tests/error_test.module'),
  81. );
  82. $this->drupalGet('error-test/trigger-exception');
  83. $this->assertTrue(strpos($this->drupalGetHeader(':status'), '500 Service unavailable (with message)'), t('Received expected HTTP status line.'));
  84. $this->assertErrorMessage($error_exception);
  85. $this->drupalGet('error-test/trigger-pdo-exception');
  86. $this->assertTrue(strpos($this->drupalGetHeader(':status'), '500 Service unavailable (with message)'), t('Received expected HTTP status line.'));
  87. // We cannot use assertErrorMessage() since the extact error reported
  88. // varies from database to database. Check that the SQL string is displayed.
  89. $this->assertText($error_pdo_exception['%type'], t('Found %type in error page.', $error_pdo_exception));
  90. $this->assertText($error_pdo_exception['%message'], t('Found %message in error page.', $error_pdo_exception));
  91. $error_details = t('in %function (line %line of %file)', $error_pdo_exception);
  92. $this->assertRaw($error_details, t("Found '!message' in error page.", array('!message' => $error_details)));
  93. }
  94. /**
  95. * Helper function: assert that the error message is found.
  96. */
  97. function assertErrorMessage(array $error) {
  98. $message = t('%type: %message in %function (line %line of %file).', $error);
  99. $this->assertRaw($message, t('Error !message found.', array('!message' => $message)));
  100. }
  101. /**
  102. * Helper function: assert that the error message is not found.
  103. */
  104. function assertNoErrorMessage(array $error) {
  105. $message = t('%type: %message in %function (line %line of %file).', $error);
  106. $this->assertNoRaw($message, t('Error !message not found.', array('!message' => $message)));
  107. }
  108. }