Skip to content

Commit 72de717

Browse files
committed
Issue #2984072 by vijaycs85, Lendude, ApacheEx, dawehner: System: Convert ErrorHandlerTest to phpunit
(cherry picked from commit bda5967)
1 parent bdacc9e commit 72de717

File tree

2 files changed

+34
-46
lines changed

2 files changed

+34
-46
lines changed

modules/system/src/Tests/System/ErrorHandlerTest.php renamed to modules/system/tests/src/Functional/System/ErrorHandlerTest.php

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<?php
22

3-
namespace Drupal\system\Tests\System;
3+
namespace Drupal\Tests\system\Functional\System;
44

55
use Drupal\Component\Render\FormattableMarkup;
6-
use Drupal\simpletest\WebTestBase;
6+
use Drupal\Tests\BrowserTestBase;
77

88
/**
99
* Performs tests on the Drupal error and exception handler.
1010
*
1111
* @group system
1212
*/
13-
class ErrorHandlerTest extends WebTestBase {
13+
class ErrorHandlerTest extends BrowserTestBase {
1414

1515
/**
1616
* Modules to enable.
@@ -42,17 +42,6 @@ public function testErrorHandler() {
4242
'%function' => 'Drupal\error_test\Controller\ErrorTestController->generateWarnings()',
4343
'%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
4444
];
45-
$fatal_error = [
46-
'%type' => 'Recoverable fatal error',
47-
'%function' => 'Drupal\error_test\Controller\ErrorTestController->Drupal\error_test\Controller\{closure}()',
48-
'@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 62 and defined',
49-
];
50-
if (version_compare(PHP_VERSION, '7.0.0-dev') >= 0) {
51-
// In PHP 7, instead of a recoverable fatal error we get a TypeError.
52-
$fatal_error['%type'] = 'TypeError';
53-
// The error message also changes in PHP 7.
54-
$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 62';
55-
}
5645

5746
// Set error reporting to display verbose notices.
5847
$this->config('system.logging')->set('error_level', ERROR_REPORTING_DISPLAY_VERBOSE)->save();
@@ -68,25 +57,6 @@ public function testErrorHandler() {
6857

6958
// Set error reporting to display verbose notices.
7059
$this->config('system.logging')->set('error_level', ERROR_REPORTING_DISPLAY_VERBOSE)->save();
71-
$this->drupalGet('error-test/generate-fatals');
72-
$this->assertResponse(500, 'Received expected HTTP status code.');
73-
$this->assertErrorMessage($fatal_error);
74-
$this->assertRaw('<pre class="backtrace">', 'Found pre element with backtrace class.');
75-
// Ensure we are escaping but not double escaping.
76-
$this->assertRaw('&#039;');
77-
$this->assertNoRaw('&amp;#039;');
78-
79-
// Remove the recoverable fatal error from the assertions, it's wanted here.
80-
// Ensure that we just remove this one recoverable fatal error (in PHP 7 this
81-
// is a TypeError).
82-
foreach ($this->assertions as $key => $assertion) {
83-
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) {
84-
unset($this->assertions[$key]);
85-
$this->deleteAssert($assertion['message_id']);
86-
}
87-
}
88-
// Drop the single exception.
89-
$this->results['#exception']--;
9060

9161
// Set error reporting to collect notices.
9262
$config->set('error_level', ERROR_REPORTING_DISPLAY_ALL)->save();
@@ -96,7 +66,6 @@ public function testErrorHandler() {
9666
$this->assertErrorMessage($error_warning);
9767
$this->assertErrorMessage($error_user_notice);
9868
$this->assertNoRaw('<pre class="backtrace">', 'Did not find pre element with backtrace class.');
99-
$this->assertErrorLogged($fatal_error['@message']);
10069

10170
// Set error reporting to not collect notices.
10271
$config->set('error_level', ERROR_REPORTING_DISPLAY_SOME)->save();
@@ -122,9 +91,6 @@ public function testErrorHandler() {
12291
* Test the exception handler.
12392
*/
12493
public function testExceptionHandler() {
125-
// Ensure the test error log is empty before these tests.
126-
$this->assertNoErrorsLogged();
127-
12894
$error_exception = [
12995
'%type' => 'Exception',
13096
'@message' => 'Drupal & awesome',
@@ -148,11 +114,11 @@ public function testExceptionHandler() {
148114
];
149115

150116
$this->drupalGet('error-test/trigger-exception');
151-
$this->assertTrue(strpos($this->drupalGetHeader(':status'), '500 Service unavailable (with message)'), 'Received expected HTTP status line.');
117+
$this->assertSession()->statusCodeEquals(500);
152118
$this->assertErrorMessage($error_exception);
153119

154120
$this->drupalGet('error-test/trigger-pdo-exception');
155-
$this->assertTrue(strpos($this->drupalGetHeader(':status'), '500 Service unavailable (with message)'), 'Received expected HTTP status line.');
121+
$this->assertSession()->statusCodeEquals(500);
156122
// We cannot use assertErrorMessage() since the exact error reported
157123
// varies from database to database. Check that the SQL string is displayed.
158124
$this->assertText($error_pdo_exception['%type'], format_string('Found %type in error page.', $error_pdo_exception));
@@ -161,7 +127,7 @@ public function testExceptionHandler() {
161127
$this->assertRaw($error_details, format_string("Found '@message' in error page.", ['@message' => $error_details]));
162128

163129
$this->drupalGet('error-test/trigger-renderer-exception');
164-
$this->assertTrue(strpos($this->drupalGetHeader(':status'), '500 Service unavailable (with message)'), 'Received expected HTTP status line.');
130+
$this->assertSession()->statusCodeEquals(500);
165131
$this->assertErrorMessage($error_renderer_exception);
166132

167133
// Disable error reporting, ensure that 5xx responses are not cached.
@@ -172,12 +138,8 @@ public function testExceptionHandler() {
172138
$this->drupalGet('error-test/trigger-exception');
173139
$this->assertFalse($this->drupalGetHeader('X-Drupal-Cache'));
174140
$this->assertIdentical(strpos($this->drupalGetHeader('Cache-Control'), 'public'), FALSE, 'Received expected HTTP status line.');
175-
$this->assertTrue(strpos($this->drupalGetHeader(':status'), '500 Service unavailable (with message)'), 'Received expected HTTP status line.');
141+
$this->assertSession()->statusCodeEquals(500);
176142
$this->assertNoErrorMessage($error_exception);
177-
178-
// The exceptions are expected. Do not interpret them as a test failure.
179-
// Not using File API; a potential error must trigger a PHP warning.
180-
unlink(\Drupal::root() . '/' . $this->siteDirectory . '/error.log');
181143
}
182144

183145
/**

tests/Drupal/FunctionalTests/Bootstrap/UncaughtExceptionTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Drupal\FunctionalTests\Bootstrap;
44

5+
use Drupal\Component\Render\FormattableMarkup;
56
use Drupal\Tests\BrowserTestBase;
67

78
/**
@@ -37,7 +38,7 @@ class UncaughtExceptionTest extends BrowserTestBase {
3738
*
3839
* @var array
3940
*/
40-
public static $modules = ['error_service_test'];
41+
public static $modules = ['error_service_test', 'error_test'];
4142

4243
/**
4344
* {@inheritdoc}
@@ -99,6 +100,31 @@ public function testUncaughtException() {
99100
$this->assertErrorLogged($this->expectedExceptionMessage);
100101
}
101102

103+
/**
104+
* Tests displaying an uncaught fatal error.
105+
*/
106+
public function testUncaughtFatalError() {
107+
$fatal_error = [
108+
'%type' => 'Recoverable fatal error',
109+
'@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 62 and defined',
110+
'%function' => 'Drupal\error_test\Controller\ErrorTestController->Drupal\error_test\Controller\{closure}()',
111+
];
112+
if (version_compare(PHP_VERSION, '7.0.0-dev') >= 0) {
113+
// In PHP 7, instead of a recoverable fatal error we get a TypeError.
114+
$fatal_error['%type'] = 'TypeError';
115+
// The error message also changes in PHP 7.
116+
$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 62';
117+
}
118+
$this->drupalGet('error-test/generate-fatals');
119+
$this->assertResponse(500, 'Received expected HTTP status code.');
120+
$message = new FormattableMarkup('%type: @message in %function (line ', $fatal_error);
121+
$this->assertRaw((string) $message);
122+
$this->assertRaw('<pre class="backtrace">');
123+
// Ensure we are escaping but not double escaping.
124+
$this->assertRaw('&#039;');
125+
$this->assertNoRaw('&amp;#039;');
126+
}
127+
102128
/**
103129
* Tests uncaught exception handling with custom exception handler.
104130
*/

0 commit comments

Comments
 (0)