View source
<?php
namespace Drupal\Tests\system\Functional\System;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Tests\BrowserTestBase;
class ErrorHandlerTest extends BrowserTestBase {
protected static $modules = [
'error_test',
];
protected $defaultTheme = 'stark';
public function testErrorHandler() {
$config = $this
->config('system.logging');
$error_notice = [
'%type' => 'Notice',
'@message' => 'Object of class stdClass could not be converted to int',
'%function' => 'Drupal\\error_test\\Controller\\ErrorTestController->generateWarnings()',
'%file' => $this
->getModulePath('error_test') . '/error_test.module',
];
$error_warning = [
'%type' => 'Warning',
'@message' => 'var_export does not handle circular references',
'%function' => 'Drupal\\error_test\\Controller\\ErrorTestController->generateWarnings()',
'%file' => $this
->getModulePath('error_test') . '/error_test.module',
];
$error_user_notice = [
'%type' => 'User warning',
'@message' => 'Drupal & awesome',
'%function' => 'Drupal\\error_test\\Controller\\ErrorTestController->generateWarnings()',
'%file' => $this
->getModulePath('error_test') . '/error_test.module',
];
$this
->config('system.logging')
->set('error_level', ERROR_REPORTING_DISPLAY_VERBOSE)
->save();
$this
->drupalGet('error-test/generate-warnings');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertErrorMessage($error_notice);
$this
->assertErrorMessage($error_warning);
$this
->assertErrorMessage($error_user_notice);
$this
->assertSession()
->responseContains('<pre class="backtrace">');
$this
->assertSession()
->responseContains('&');
$this
->assertSession()
->responseNotContains('&amp;');
$this
->config('system.logging')
->set('error_level', ERROR_REPORTING_DISPLAY_VERBOSE)
->save();
$config
->set('error_level', ERROR_REPORTING_DISPLAY_ALL)
->save();
$this
->drupalGet('error-test/generate-warnings');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertErrorMessage($error_notice);
$this
->assertErrorMessage($error_warning);
$this
->assertErrorMessage($error_user_notice);
$this
->assertSession()
->responseNotContains('<pre class="backtrace">');
$config
->set('error_level', ERROR_REPORTING_DISPLAY_SOME)
->save();
$this
->drupalGet('error-test/generate-warnings');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertNoErrorMessage($error_notice);
$this
->assertErrorMessage($error_warning);
$this
->assertErrorMessage($error_user_notice);
$this
->assertSession()
->responseNotContains('<pre class="backtrace">');
$config
->set('error_level', ERROR_REPORTING_HIDE)
->save();
$this
->drupalGet('error-test/generate-warnings');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertNoErrorMessage($error_notice);
$this
->assertNoErrorMessage($error_warning);
$this
->assertNoErrorMessage($error_user_notice);
$this
->assertNoMessages();
$this
->assertSession()
->responseNotContains('<pre class="backtrace">');
}
public function testExceptionHandler() {
$error_exception = [
'%type' => 'Exception',
'@message' => 'Drupal & awesome',
'%function' => 'Drupal\\error_test\\Controller\\ErrorTestController->triggerException()',
'%line' => 56,
'%file' => $this
->getModulePath('error_test') . '/error_test.module',
];
$error_pdo_exception = [
'%type' => 'DatabaseExceptionWrapper',
'@message' => 'SELECT "b".* FROM {bananas_are_awesome} "b"',
'%function' => 'Drupal\\error_test\\Controller\\ErrorTestController->triggerPDOException()',
'%line' => 64,
'%file' => $this
->getModulePath('error_test') . '/error_test.module',
];
$error_renderer_exception = [
'%type' => 'Exception',
'@message' => 'This is an exception that occurs during rendering',
'%function' => 'Drupal\\error_test\\Controller\\ErrorTestController->Drupal\\error_test\\Controller\\{closure}()',
'%line' => 82,
'%file' => $this
->getModulePath('error_test') . '/error_test.module',
];
$this
->drupalGet('error-test/trigger-exception');
$this
->assertSession()
->statusCodeEquals(500);
$this
->assertErrorMessage($error_exception);
$this
->drupalGet('error-test/trigger-pdo-exception');
$this
->assertSession()
->statusCodeEquals(500);
$this
->assertSession()
->pageTextContains($error_pdo_exception['%type']);
$this
->assertSession()
->pageTextContains($error_pdo_exception['@message']);
$error_details = new FormattableMarkup('in %function (line ', $error_pdo_exception);
$this
->assertSession()
->responseContains($error_details);
$this
->drupalGet('error-test/trigger-renderer-exception');
$this
->assertSession()
->statusCodeEquals(500);
$this
->assertErrorMessage($error_renderer_exception);
$this
->config('system.logging')
->set('error_level', ERROR_REPORTING_HIDE)
->save();
$this
->drupalGet('error-test/trigger-exception');
$this
->assertSession()
->responseHeaderDoesNotExist('X-Drupal-Cache');
$this
->assertSession()
->responseHeaderNotContains('Cache-Control', 'public');
$this
->assertSession()
->statusCodeEquals(500);
$this
->assertNoErrorMessage($error_exception);
}
public function assertErrorMessage(array $error) {
$message = new FormattableMarkup('%type: @message in %function (line ', $error);
$this
->assertSession()
->responseContains($message);
}
public function assertNoErrorMessage(array $error) {
$message = new FormattableMarkup('%type: @message in %function (line ', $error);
$this
->assertSession()
->responseNotContains($message);
}
protected function assertNoMessages() {
$this
->assertSession()
->elementNotExists('xpath', '//div[contains(@class, "messages")]');
}
}