PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/system/src/Tests/System/ErrorHandlerTest.php

https://gitlab.com/geeta7/drupal
PHP | 213 lines | 138 code | 24 blank | 51 comment | 3 complexity | 79a749a109bf272c763bed73015b376f MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\system\Tests\System\ErrorHandlerTest.
  5. */
  6. namespace Drupal\system\Tests\System;
  7. use Drupal\simpletest\WebTestBase;
  8. /**
  9. * Performs tests on the Drupal error and exception handler.
  10. *
  11. * @group system
  12. */
  13. class ErrorHandlerTest extends WebTestBase {
  14. /**
  15. * Modules to enable.
  16. *
  17. * @var array
  18. */
  19. public static $modules = array('error_test');
  20. /**
  21. * Test the error handler.
  22. */
  23. function testErrorHandler() {
  24. $config = $this->config('system.logging');
  25. $error_notice = array(
  26. '%type' => 'Notice',
  27. '@message' => 'Undefined variable: bananas',
  28. '%function' => 'Drupal\error_test\Controller\ErrorTestController->generateWarnings()',
  29. '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
  30. );
  31. $error_warning = array(
  32. '%type' => 'Warning',
  33. '@message' => 'Division by zero',
  34. '%function' => 'Drupal\error_test\Controller\ErrorTestController->generateWarnings()',
  35. '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
  36. );
  37. $error_user_notice = array(
  38. '%type' => 'User warning',
  39. '@message' => 'Drupal & awesome',
  40. '%function' => 'Drupal\error_test\Controller\ErrorTestController->generateWarnings()',
  41. '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
  42. );
  43. $fatal_error = array(
  44. '%type' => 'Recoverable fatal error',
  45. '%function' => 'Drupal\error_test\Controller\ErrorTestController->Drupal\error_test\Controller\{closure}()',
  46. '@message' => 'Argument 1 passed to Drupal\error_test\Controller\ErrorTestController::Drupal\error_test\Controller\{closure}() must be of the type array, string given, called in ' . \Drupal::root() . '/core/modules/system/tests/modules/error_test/src/Controller/ErrorTestController.php on line 66 and defined',
  47. );
  48. if (version_compare(PHP_VERSION, '7.0.0-dev') >= 0) {
  49. // In PHP 7, instead of a recoverable fatal error we get a TypeError.
  50. $fatal_error['%type'] = 'TypeError';
  51. // The error message also changes in PHP 7.
  52. $fatal_error['@message'] = 'Argument 1 passed to Drupal\error_test\Controller\ErrorTestController::Drupal\error_test\Controller\{closure}() must be of the type array, string given, called in ' . \Drupal::root() . '/core/modules/system/tests/modules/error_test/src/Controller/ErrorTestController.php on line 66';
  53. }
  54. // Set error reporting to display verbose notices.
  55. $this->config('system.logging')->set('error_level', ERROR_REPORTING_DISPLAY_VERBOSE)->save();
  56. $this->drupalGet('error-test/generate-warnings');
  57. $this->assertResponse(200, 'Received expected HTTP status code.');
  58. $this->assertErrorMessage($error_notice);
  59. $this->assertErrorMessage($error_warning);
  60. $this->assertErrorMessage($error_user_notice);
  61. $this->assertRaw('<pre class="backtrace">', 'Found pre element with backtrace class.');
  62. // Ensure we are escaping but not double escaping.
  63. $this->assertRaw('&amp;');
  64. $this->assertNoRaw('&amp;amp;');
  65. // Set error reporting to display verbose notices.
  66. $this->config('system.logging')->set('error_level', ERROR_REPORTING_DISPLAY_VERBOSE)->save();
  67. $this->drupalGet('error-test/generate-fatals');
  68. $this->assertResponse(500, 'Received expected HTTP status code.');
  69. $this->assertErrorMessage($fatal_error);
  70. $this->assertRaw('<pre class="backtrace">', 'Found pre element with backtrace class.');
  71. // Ensure we are escaping but not double escaping.
  72. $this->assertRaw('&#039;');
  73. $this->assertNoRaw('&amp;#039;');
  74. // Remove the recoverable fatal error from the assertions, it's wanted here.
  75. // Ensure that we just remove this one recoverable fatal error (in PHP 7 this
  76. // is a TypeError).
  77. foreach ($this->assertions as $key => $assertion) {
  78. if (in_array($assertion['message_group'], ['Recoverable fatal error', 'TypeError']) && strpos($assertion['message'], 'Argument 1 passed to Drupal\error_test\Controller\ErrorTestController::Drupal\error_test\Controller\{closure}() must be of the type array, string given, called in') !== FALSE) {
  79. unset($this->assertions[$key]);
  80. $this->deleteAssert($assertion['message_id']);
  81. }
  82. }
  83. // Drop the single exception.
  84. $this->results['#exception']--;
  85. // Set error reporting to collect notices.
  86. $config->set('error_level', ERROR_REPORTING_DISPLAY_ALL)->save();
  87. $this->drupalGet('error-test/generate-warnings');
  88. $this->assertResponse(200, 'Received expected HTTP status code.');
  89. $this->assertErrorMessage($error_notice);
  90. $this->assertErrorMessage($error_warning);
  91. $this->assertErrorMessage($error_user_notice);
  92. $this->assertNoRaw('<pre class="backtrace">', 'Did not find pre element with backtrace class.');
  93. $this->assertErrorLogged($fatal_error['@message']);
  94. // Set error reporting to not collect notices.
  95. $config->set('error_level', ERROR_REPORTING_DISPLAY_SOME)->save();
  96. $this->drupalGet('error-test/generate-warnings');
  97. $this->assertResponse(200, 'Received expected HTTP status code.');
  98. $this->assertNoErrorMessage($error_notice);
  99. $this->assertErrorMessage($error_warning);
  100. $this->assertErrorMessage($error_user_notice);
  101. $this->assertNoRaw('<pre class="backtrace">', 'Did not find pre element with backtrace class.');
  102. // Set error reporting to not show any errors.
  103. $config->set('error_level', ERROR_REPORTING_HIDE)->save();
  104. $this->drupalGet('error-test/generate-warnings');
  105. $this->assertResponse(200, 'Received expected HTTP status code.');
  106. $this->assertNoErrorMessage($error_notice);
  107. $this->assertNoErrorMessage($error_warning);
  108. $this->assertNoErrorMessage($error_user_notice);
  109. $this->assertNoMessages();
  110. $this->assertNoRaw('<pre class="backtrace">', 'Did not find pre element with backtrace class.');
  111. }
  112. /**
  113. * Test the exception handler.
  114. */
  115. function testExceptionHandler() {
  116. // Ensure the test error log is empty before these tests.
  117. $this->assertNoErrorsLogged();
  118. $error_exception = array(
  119. '%type' => 'Exception',
  120. '@message' => 'Drupal & awesome',
  121. '%function' => 'Drupal\error_test\Controller\ErrorTestController->triggerException()',
  122. '%line' => 56,
  123. '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
  124. );
  125. $error_pdo_exception = array(
  126. '%type' => 'DatabaseExceptionWrapper',
  127. '@message' => 'SELECT * FROM bananas_are_awesome',
  128. '%function' => 'Drupal\error_test\Controller\ErrorTestController->triggerPDOException()',
  129. '%line' => 64,
  130. '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
  131. );
  132. $error_renderer_exception = array(
  133. '%type' => 'Exception',
  134. '@message' => 'This is an exception that occurs during rendering',
  135. '%function' => 'Drupal\error_test\Controller\ErrorTestController->Drupal\error_test\Controller\{closure}()',
  136. '%line' => 82,
  137. '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
  138. );
  139. $this->drupalGet('error-test/trigger-exception');
  140. $this->assertTrue(strpos($this->drupalGetHeader(':status'), '500 Service unavailable (with message)'), 'Received expected HTTP status line.');
  141. $this->assertErrorMessage($error_exception);
  142. $this->drupalGet('error-test/trigger-pdo-exception');
  143. $this->assertTrue(strpos($this->drupalGetHeader(':status'), '500 Service unavailable (with message)'), 'Received expected HTTP status line.');
  144. // We cannot use assertErrorMessage() since the exact error reported
  145. // varies from database to database. Check that the SQL string is displayed.
  146. $this->assertText($error_pdo_exception['%type'], format_string('Found %type in error page.', $error_pdo_exception));
  147. $this->assertText($error_pdo_exception['@message'], format_string('Found @message in error page.', $error_pdo_exception));
  148. $error_details = format_string('in %function (line ', $error_pdo_exception);
  149. $this->assertRaw($error_details, format_string("Found '@message' in error page.", array('@message' => $error_details)));
  150. $this->drupalGet('error-test/trigger-renderer-exception');
  151. $this->assertTrue(strpos($this->drupalGetHeader(':status'), '500 Service unavailable (with message)'), 'Received expected HTTP status line.');
  152. $this->assertErrorMessage($error_renderer_exception);
  153. // Disable error reporting, ensure that 5xx responses are not cached.
  154. $this->config('system.logging')
  155. ->set('error_level', ERROR_REPORTING_HIDE)
  156. ->save();
  157. $this->drupalGet('error-test/trigger-exception');
  158. $this->assertFalse($this->drupalGetHeader('X-Drupal-Cache'));
  159. $this->assertIdentical(strpos($this->drupalGetHeader('Cache-Control'), 'public'), FALSE, 'Received expected HTTP status line.');
  160. $this->assertTrue(strpos($this->drupalGetHeader(':status'), '500 Service unavailable (with message)'), 'Received expected HTTP status line.');
  161. $this->assertNoErrorMessage($error_exception);
  162. // The exceptions are expected. Do not interpret them as a test failure.
  163. // Not using File API; a potential error must trigger a PHP warning.
  164. unlink(\Drupal::root() . '/' . $this->siteDirectory . '/error.log');
  165. }
  166. /**
  167. * Helper function: assert that the error message is found.
  168. */
  169. function assertErrorMessage(array $error) {
  170. $message = t('%type: @message in %function (line ', $error);
  171. $this->assertRaw($message, format_string('Found error message: @message.', array('@message' => $message)));
  172. }
  173. /**
  174. * Helper function: assert that the error message is not found.
  175. */
  176. function assertNoErrorMessage(array $error) {
  177. $message = t('%type: @message in %function (line ', $error);
  178. $this->assertNoRaw($message, format_string('Did not find error message: @message.', array('@message' => $message)));
  179. }
  180. /**
  181. * Asserts that no messages are printed onto the page.
  182. *
  183. * @return bool
  184. * TRUE, if there are no messages.
  185. */
  186. protected function assertNoMessages() {
  187. return $this->assertFalse($this->xpath('//div[contains(@class, "messages")]'), 'Ensures that also no messages div exists, which proves that no messages were generated by the error handler, not even an empty one.');
  188. }
  189. }