Mercurial > hg > isophonics-drupal-site
comparison core/tests/Drupal/FunctionalTests/Bootstrap/UncaughtExceptionTest.php @ 17:129ea1e6d783
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:21:36 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
16:c2387f117808 | 17:129ea1e6d783 |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\FunctionalTests\Bootstrap; | |
4 | |
5 use Drupal\Component\Render\FormattableMarkup; | |
6 use Drupal\Tests\BrowserTestBase; | |
7 | |
8 /** | |
9 * Tests kernel panic when things are really messed up. | |
10 * | |
11 * @group system | |
12 */ | |
13 class UncaughtExceptionTest extends BrowserTestBase { | |
14 | |
15 /** | |
16 * Last cURL response. | |
17 * | |
18 * @var string | |
19 */ | |
20 protected $response = ''; | |
21 | |
22 /** | |
23 * Last cURL info. | |
24 * | |
25 * @var array | |
26 */ | |
27 protected $info = []; | |
28 | |
29 /** | |
30 * Exceptions thrown by site under test that contain this text are ignored. | |
31 * | |
32 * @var string | |
33 */ | |
34 protected $expectedExceptionMessage; | |
35 | |
36 /** | |
37 * Modules to enable. | |
38 * | |
39 * @var array | |
40 */ | |
41 public static $modules = ['error_service_test', 'error_test']; | |
42 | |
43 /** | |
44 * {@inheritdoc} | |
45 */ | |
46 protected function setUp() { | |
47 parent::setUp(); | |
48 | |
49 $settings_filename = $this->siteDirectory . '/settings.php'; | |
50 chmod($settings_filename, 0777); | |
51 $settings_php = file_get_contents($settings_filename); | |
52 $settings_php .= "\ninclude_once 'core/tests/Drupal/FunctionalTests/Bootstrap/ErrorContainer.php';\n"; | |
53 $settings_php .= "\ninclude_once 'core/tests/Drupal/FunctionalTests/Bootstrap/ExceptionContainer.php';\n"; | |
54 file_put_contents($settings_filename, $settings_php); | |
55 | |
56 $settings = []; | |
57 $settings['config']['system.logging']['error_level'] = (object) [ | |
58 'value' => ERROR_REPORTING_DISPLAY_VERBOSE, | |
59 'required' => TRUE, | |
60 ]; | |
61 $this->writeSettings($settings); | |
62 } | |
63 | |
64 /** | |
65 * Tests uncaught exception handling when system is in a bad state. | |
66 */ | |
67 public function testUncaughtException() { | |
68 $this->expectedExceptionMessage = 'Oh oh, bananas in the instruments.'; | |
69 \Drupal::state()->set('error_service_test.break_bare_html_renderer', TRUE); | |
70 | |
71 $this->config('system.logging') | |
72 ->set('error_level', ERROR_REPORTING_HIDE) | |
73 ->save(); | |
74 $settings = []; | |
75 $settings['config']['system.logging']['error_level'] = (object) [ | |
76 'value' => ERROR_REPORTING_HIDE, | |
77 'required' => TRUE, | |
78 ]; | |
79 $this->writeSettings($settings); | |
80 | |
81 $this->drupalGet(''); | |
82 $this->assertResponse(500); | |
83 $this->assertText('The website encountered an unexpected error. Please try again later.'); | |
84 $this->assertNoText($this->expectedExceptionMessage); | |
85 | |
86 $this->config('system.logging') | |
87 ->set('error_level', ERROR_REPORTING_DISPLAY_ALL) | |
88 ->save(); | |
89 $settings = []; | |
90 $settings['config']['system.logging']['error_level'] = (object) [ | |
91 'value' => ERROR_REPORTING_DISPLAY_ALL, | |
92 'required' => TRUE, | |
93 ]; | |
94 $this->writeSettings($settings); | |
95 | |
96 $this->drupalGet(''); | |
97 $this->assertResponse(500); | |
98 $this->assertText('The website encountered an unexpected error. Please try again later.'); | |
99 $this->assertText($this->expectedExceptionMessage); | |
100 $this->assertErrorLogged($this->expectedExceptionMessage); | |
101 } | |
102 | |
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('''); | |
125 $this->assertNoRaw('&#039;'); | |
126 } | |
127 | |
128 /** | |
129 * Tests uncaught exception handling with custom exception handler. | |
130 */ | |
131 public function testUncaughtExceptionCustomExceptionHandler() { | |
132 $settings_filename = $this->siteDirectory . '/settings.php'; | |
133 chmod($settings_filename, 0777); | |
134 $settings_php = file_get_contents($settings_filename); | |
135 $settings_php .= "\n"; | |
136 $settings_php .= "set_exception_handler(function() {\n"; | |
137 $settings_php .= " header('HTTP/1.1 418 I\'m a teapot');\n"; | |
138 $settings_php .= " print('Oh oh, flying teapots');\n"; | |
139 $settings_php .= "});\n"; | |
140 file_put_contents($settings_filename, $settings_php); | |
141 | |
142 \Drupal::state()->set('error_service_test.break_bare_html_renderer', TRUE); | |
143 | |
144 $this->drupalGet(''); | |
145 $this->assertResponse(418); | |
146 $this->assertNoText('The website encountered an unexpected error. Please try again later.'); | |
147 $this->assertNoText('Oh oh, bananas in the instruments'); | |
148 $this->assertText('Oh oh, flying teapots'); | |
149 } | |
150 | |
151 /** | |
152 * Tests a missing dependency on a service. | |
153 */ | |
154 public function testMissingDependency() { | |
155 if (version_compare(PHP_VERSION, '7.1') < 0) { | |
156 $this->expectedExceptionMessage = 'Argument 1 passed to Drupal\error_service_test\LonelyMonkeyClass::__construct() must be an instance of Drupal\Core\Database\Connection, non'; | |
157 } | |
158 else { | |
159 $this->expectedExceptionMessage = 'Too few arguments to function Drupal\error_service_test\LonelyMonkeyClass::__construct(), 0 passed'; | |
160 } | |
161 $this->drupalGet('broken-service-class'); | |
162 $this->assertResponse(500); | |
163 | |
164 $this->assertRaw('The website encountered an unexpected error.'); | |
165 $this->assertRaw($this->expectedExceptionMessage); | |
166 $this->assertErrorLogged($this->expectedExceptionMessage); | |
167 } | |
168 | |
169 /** | |
170 * Tests a missing dependency on a service with a custom error handler. | |
171 */ | |
172 public function testMissingDependencyCustomErrorHandler() { | |
173 $settings_filename = $this->siteDirectory . '/settings.php'; | |
174 chmod($settings_filename, 0777); | |
175 $settings_php = file_get_contents($settings_filename); | |
176 $settings_php .= "\n"; | |
177 $settings_php .= "set_error_handler(function() {\n"; | |
178 $settings_php .= " header('HTTP/1.1 418 I\'m a teapot');\n"; | |
179 $settings_php .= " print('Oh oh, flying teapots');\n"; | |
180 $settings_php .= " exit();\n"; | |
181 $settings_php .= "});\n"; | |
182 $settings_php .= "\$settings['teapots'] = TRUE;\n"; | |
183 file_put_contents($settings_filename, $settings_php); | |
184 | |
185 $this->drupalGet('broken-service-class'); | |
186 $this->assertResponse(418); | |
187 $this->assertSame('Oh oh, flying teapots', $this->response); | |
188 } | |
189 | |
190 /** | |
191 * Tests a container which has an error. | |
192 */ | |
193 public function testErrorContainer() { | |
194 $settings = []; | |
195 $settings['settings']['container_base_class'] = (object) [ | |
196 'value' => '\Drupal\FunctionalTests\Bootstrap\ErrorContainer', | |
197 'required' => TRUE, | |
198 ]; | |
199 $this->writeSettings($settings); | |
200 \Drupal::service('kernel')->invalidateContainer(); | |
201 | |
202 $this->expectedExceptionMessage = 'Argument 1 passed to Drupal\FunctionalTests\Bootstrap\ErrorContainer::Drupal\FunctionalTests\Bootstrap\{closur'; | |
203 $this->drupalGet(''); | |
204 $this->assertResponse(500); | |
205 | |
206 $this->assertRaw($this->expectedExceptionMessage); | |
207 $this->assertErrorLogged($this->expectedExceptionMessage); | |
208 } | |
209 | |
210 /** | |
211 * Tests a container which has an exception really early. | |
212 */ | |
213 public function testExceptionContainer() { | |
214 $settings = []; | |
215 $settings['settings']['container_base_class'] = (object) [ | |
216 'value' => '\Drupal\FunctionalTests\Bootstrap\ExceptionContainer', | |
217 'required' => TRUE, | |
218 ]; | |
219 $this->writeSettings($settings); | |
220 \Drupal::service('kernel')->invalidateContainer(); | |
221 | |
222 $this->expectedExceptionMessage = 'Thrown exception during Container::get'; | |
223 $this->drupalGet(''); | |
224 $this->assertResponse(500); | |
225 | |
226 $this->assertRaw('The website encountered an unexpected error'); | |
227 $this->assertRaw($this->expectedExceptionMessage); | |
228 $this->assertErrorLogged($this->expectedExceptionMessage); | |
229 } | |
230 | |
231 /** | |
232 * Tests the case when the database connection is gone. | |
233 */ | |
234 public function testLostDatabaseConnection() { | |
235 $incorrect_username = $this->randomMachineName(16); | |
236 switch ($this->container->get('database')->driver()) { | |
237 case 'pgsql': | |
238 case 'mysql': | |
239 $this->expectedExceptionMessage = $incorrect_username; | |
240 break; | |
241 default: | |
242 // We can not carry out this test. | |
243 $this->pass('Unable to run \Drupal\system\Tests\System\UncaughtExceptionTest::testLostDatabaseConnection for this database type.'); | |
244 return; | |
245 } | |
246 | |
247 // We simulate a broken database connection by rewrite settings.php to no | |
248 // longer have the proper data. | |
249 $settings['databases']['default']['default']['username'] = (object) [ | |
250 'value' => $incorrect_username, | |
251 'required' => TRUE, | |
252 ]; | |
253 $settings['databases']['default']['default']['password'] = (object) [ | |
254 'value' => $this->randomMachineName(16), | |
255 'required' => TRUE, | |
256 ]; | |
257 | |
258 $this->writeSettings($settings); | |
259 | |
260 $this->drupalGet(''); | |
261 $this->assertResponse(500); | |
262 $this->assertRaw('DatabaseAccessDeniedException'); | |
263 $this->assertErrorLogged($this->expectedExceptionMessage); | |
264 } | |
265 | |
266 /** | |
267 * Tests fallback to PHP error log when an exception is thrown while logging. | |
268 */ | |
269 public function testLoggerException() { | |
270 // Ensure the test error log is empty before these tests. | |
271 $this->assertNoErrorsLogged(); | |
272 | |
273 $this->expectedExceptionMessage = 'Deforestation'; | |
274 \Drupal::state()->set('error_service_test.break_logger', TRUE); | |
275 | |
276 $this->drupalGet(''); | |
277 $this->assertResponse(500); | |
278 $this->assertText('The website encountered an unexpected error. Please try again later.'); | |
279 $this->assertRaw($this->expectedExceptionMessage); | |
280 | |
281 // Find fatal error logged to the error.log | |
282 $errors = file(\Drupal::root() . '/' . $this->siteDirectory . '/error.log'); | |
283 $this->assertIdentical(count($errors), 8, 'The error + the error that the logging service is broken has been written to the error log.'); | |
284 $this->assertTrue(strpos($errors[0], 'Failed to log error') !== FALSE, 'The error handling logs when an error could not be logged to the logger.'); | |
285 | |
286 $expected_path = \Drupal::root() . '/core/modules/system/tests/modules/error_service_test/src/MonkeysInTheControlRoom.php'; | |
287 $expected_line = 59; | |
288 $expected_entry = "Failed to log error: Exception: Deforestation in Drupal\\error_service_test\\MonkeysInTheControlRoom->handle() (line ${expected_line} of ${expected_path})"; | |
289 $this->assert(strpos($errors[0], $expected_entry) !== FALSE, 'Original error logged to the PHP error log when an exception is thrown by a logger'); | |
290 | |
291 // The exception is expected. Do not interpret it as a test failure. Not | |
292 // using File API; a potential error must trigger a PHP warning. | |
293 unlink(\Drupal::root() . '/' . $this->siteDirectory . '/error.log'); | |
294 } | |
295 | |
296 /** | |
297 * Asserts that a specific error has been logged to the PHP error log. | |
298 * | |
299 * @param string $error_message | |
300 * The expected error message. | |
301 * | |
302 * @see \Drupal\simpletest\TestBase::prepareEnvironment() | |
303 * @see \Drupal\Core\DrupalKernel::bootConfiguration() | |
304 */ | |
305 protected function assertErrorLogged($error_message) { | |
306 $error_log_filename = DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log'; | |
307 if (!file_exists($error_log_filename)) { | |
308 $this->fail('No error logged yet.'); | |
309 } | |
310 | |
311 $content = file_get_contents($error_log_filename); | |
312 $rows = explode(PHP_EOL, $content); | |
313 | |
314 // We iterate over the rows in order to be able to remove the logged error | |
315 // afterwards. | |
316 $found = FALSE; | |
317 foreach ($rows as $row_index => $row) { | |
318 if (strpos($content, $error_message) !== FALSE) { | |
319 $found = TRUE; | |
320 unset($rows[$row_index]); | |
321 } | |
322 } | |
323 | |
324 file_put_contents($error_log_filename, implode("\n", $rows)); | |
325 | |
326 $this->assertTrue($found, sprintf('The %s error message was logged.', $error_message)); | |
327 } | |
328 | |
329 /** | |
330 * Asserts that no errors have been logged to the PHP error.log thus far. | |
331 * | |
332 * @see \Drupal\simpletest\TestBase::prepareEnvironment() | |
333 * @see \Drupal\Core\DrupalKernel::bootConfiguration() | |
334 */ | |
335 protected function assertNoErrorsLogged() { | |
336 // Since PHP only creates the error.log file when an actual error is | |
337 // triggered, it is sufficient to check whether the file exists. | |
338 $this->assertFalse(file_exists(DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log'), 'PHP error.log is empty.'); | |
339 } | |
340 | |
341 /** | |
342 * Retrieves a Drupal path or an absolute path. | |
343 * | |
344 * Executes a cURL request for processing errors and exceptions. | |
345 * | |
346 * @param string|\Drupal\Core\Url $path | |
347 * Request path. | |
348 * @param array $extra_options | |
349 * (optional) Curl options to pass to curl_setopt() | |
350 * @param array $headers | |
351 * (optional) Not used. | |
352 */ | |
353 protected function drupalGet($path, array $extra_options = [], array $headers = []) { | |
354 $url = $this->buildUrl($path, ['absolute' => TRUE]); | |
355 | |
356 $ch = curl_init(); | |
357 curl_setopt($ch, CURLOPT_URL, $url); | |
358 curl_setopt($ch, CURLOPT_HEADER, FALSE); | |
359 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
360 curl_setopt($ch, CURLOPT_USERAGENT, drupal_generate_test_ua($this->databasePrefix)); | |
361 $this->response = curl_exec($ch); | |
362 $this->info = curl_getinfo($ch); | |
363 curl_close($ch); | |
364 } | |
365 | |
366 /** | |
367 * {@inheritdoc} | |
368 */ | |
369 protected function assertResponse($code) { | |
370 $this->assertSame($code, $this->info['http_code']); | |
371 } | |
372 | |
373 /** | |
374 * {@inheritdoc} | |
375 */ | |
376 protected function assertText($text) { | |
377 $this->assertContains($text, $this->response); | |
378 } | |
379 | |
380 /** | |
381 * {@inheritdoc} | |
382 */ | |
383 protected function assertNoText($text) { | |
384 $this->assertNotContains($text, $this->response); | |
385 } | |
386 | |
387 /** | |
388 * {@inheritdoc} | |
389 */ | |
390 protected function assertRaw($text) { | |
391 $this->assertText($text); | |
392 } | |
393 | |
394 /** | |
395 * {@inheritdoc} | |
396 */ | |
397 protected function assertNoRaw($text) { | |
398 $this->assertNoText($text); | |
399 } | |
400 | |
401 } |