Chris@0: siteDirectory . '/settings.php'; Chris@0: chmod($settings_filename, 0777); Chris@0: $settings_php = file_get_contents($settings_filename); Chris@0: $settings_php .= "\ninclude_once 'core/modules/system/src/Tests/Bootstrap/ErrorContainer.php';\n"; Chris@0: $settings_php .= "\ninclude_once 'core/modules/system/src/Tests/Bootstrap/ExceptionContainer.php';\n"; Chris@0: file_put_contents($settings_filename, $settings_php); Chris@0: Chris@0: $settings = []; Chris@0: $settings['config']['system.logging']['error_level'] = (object) [ Chris@0: 'value' => ERROR_REPORTING_DISPLAY_VERBOSE, Chris@0: 'required' => TRUE, Chris@0: ]; Chris@0: $this->writeSettings($settings); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests uncaught exception handling when system is in a bad state. Chris@0: */ Chris@0: public function testUncaughtException() { Chris@0: $this->expectedExceptionMessage = 'Oh oh, bananas in the instruments.'; Chris@0: \Drupal::state()->set('error_service_test.break_bare_html_renderer', TRUE); Chris@0: Chris@0: $this->config('system.logging') Chris@0: ->set('error_level', ERROR_REPORTING_HIDE) Chris@0: ->save(); Chris@0: $settings = []; Chris@0: $settings['config']['system.logging']['error_level'] = (object) [ Chris@0: 'value' => ERROR_REPORTING_HIDE, Chris@0: 'required' => TRUE, Chris@0: ]; Chris@0: $this->writeSettings($settings); Chris@0: Chris@0: $this->drupalGet(''); Chris@0: $this->assertResponse(500); Chris@0: $this->assertText('The website encountered an unexpected error. Please try again later.'); Chris@0: $this->assertNoText($this->expectedExceptionMessage); Chris@0: Chris@0: $this->config('system.logging') Chris@0: ->set('error_level', ERROR_REPORTING_DISPLAY_ALL) Chris@0: ->save(); Chris@0: $settings = []; Chris@0: $settings['config']['system.logging']['error_level'] = (object) [ Chris@0: 'value' => ERROR_REPORTING_DISPLAY_ALL, Chris@0: 'required' => TRUE, Chris@0: ]; Chris@0: $this->writeSettings($settings); Chris@0: Chris@0: $this->drupalGet(''); Chris@0: $this->assertResponse(500); Chris@0: $this->assertText('The website encountered an unexpected error. Please try again later.'); Chris@0: $this->assertText($this->expectedExceptionMessage); Chris@0: $this->assertErrorLogged($this->expectedExceptionMessage); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests uncaught exception handling with custom exception handler. Chris@0: */ Chris@0: public function testUncaughtExceptionCustomExceptionHandler() { Chris@0: $settings_filename = $this->siteDirectory . '/settings.php'; Chris@0: chmod($settings_filename, 0777); Chris@0: $settings_php = file_get_contents($settings_filename); Chris@0: $settings_php .= "\n"; Chris@0: $settings_php .= "set_exception_handler(function() {\n"; Chris@0: $settings_php .= " header('HTTP/1.1 418 I\'m a teapot');\n"; Chris@0: $settings_php .= " print('Oh oh, flying teapots');\n"; Chris@0: $settings_php .= "});\n"; Chris@0: file_put_contents($settings_filename, $settings_php); Chris@0: Chris@0: \Drupal::state()->set('error_service_test.break_bare_html_renderer', TRUE); Chris@0: Chris@0: $this->drupalGet(''); Chris@0: $this->assertResponse(418); Chris@0: $this->assertNoText('The website encountered an unexpected error. Please try again later.'); Chris@0: $this->assertNoText('Oh oh, bananas in the instruments'); Chris@0: $this->assertText('Oh oh, flying teapots'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests a missing dependency on a service. Chris@0: */ Chris@0: public function testMissingDependency() { Chris@0: if (version_compare(PHP_VERSION, '7.1') < 0) { Chris@0: $this->expectedExceptionMessage = 'Argument 1 passed to Drupal\error_service_test\LonelyMonkeyClass::__construct() must be an instance of Drupal\Core\Database\Connection, non'; Chris@0: } Chris@0: else { Chris@0: $this->expectedExceptionMessage = 'Too few arguments to function Drupal\error_service_test\LonelyMonkeyClass::__construct(), 0 passed'; Chris@0: } Chris@0: $this->drupalGet('broken-service-class'); Chris@0: $this->assertResponse(500); Chris@0: Chris@0: $this->assertRaw('The website encountered an unexpected error.'); Chris@0: $this->assertRaw($this->expectedExceptionMessage); Chris@0: $this->assertErrorLogged($this->expectedExceptionMessage); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests a missing dependency on a service with a custom error handler. Chris@0: */ Chris@0: public function testMissingDependencyCustomErrorHandler() { Chris@0: $settings_filename = $this->siteDirectory . '/settings.php'; Chris@0: chmod($settings_filename, 0777); Chris@0: $settings_php = file_get_contents($settings_filename); Chris@0: $settings_php .= "\n"; Chris@0: $settings_php .= "set_error_handler(function() {\n"; Chris@0: $settings_php .= " header('HTTP/1.1 418 I\'m a teapot');\n"; Chris@0: $settings_php .= " print('Oh oh, flying teapots');\n"; Chris@0: $settings_php .= " exit();\n"; Chris@0: $settings_php .= "});\n"; Chris@0: $settings_php .= "\$settings['teapots'] = TRUE;\n"; Chris@0: file_put_contents($settings_filename, $settings_php); Chris@0: Chris@0: $this->drupalGet('broken-service-class'); Chris@0: $this->assertResponse(418); Chris@0: $this->assertRaw('Oh oh, flying teapots'); Chris@0: Chris@0: $message = 'Argument 1 passed to Drupal\error_service_test\LonelyMonkeyClass::__construct() must be an instance of Drupal\Core\Database\Connection, non'; Chris@0: Chris@0: $this->assertNoRaw('The website encountered an unexpected error.'); Chris@0: $this->assertNoRaw($message); Chris@0: Chris@0: $found_exception = FALSE; Chris@0: foreach ($this->assertions as &$assertion) { Chris@0: if (strpos($assertion['message'], $message) !== FALSE) { Chris@0: $found_exception = TRUE; Chris@0: $this->deleteAssert($assertion['message_id']); Chris@0: unset($assertion); Chris@0: } Chris@0: } Chris@0: Chris@0: $this->assertTrue($found_exception, 'Ensure that the exception of a missing constructor argument was triggered.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests a container which has an error. Chris@0: */ Chris@0: public function testErrorContainer() { Chris@0: $settings = []; Chris@0: $settings['settings']['container_base_class'] = (object) [ Chris@0: 'value' => '\Drupal\system\Tests\Bootstrap\ErrorContainer', Chris@0: 'required' => TRUE, Chris@0: ]; Chris@0: $this->writeSettings($settings); Chris@0: \Drupal::service('kernel')->invalidateContainer(); Chris@0: Chris@0: $this->expectedExceptionMessage = 'Argument 1 passed to Drupal\system\Tests\Bootstrap\ErrorContainer::Drupal\system\Tests\Bootstrap\{closur'; Chris@0: $this->drupalGet(''); Chris@0: $this->assertResponse(500); Chris@0: Chris@0: $this->assertRaw($this->expectedExceptionMessage); Chris@0: $this->assertErrorLogged($this->expectedExceptionMessage); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests a container which has an exception really early. Chris@0: */ Chris@0: public function testExceptionContainer() { Chris@0: $settings = []; Chris@0: $settings['settings']['container_base_class'] = (object) [ Chris@0: 'value' => '\Drupal\system\Tests\Bootstrap\ExceptionContainer', Chris@0: 'required' => TRUE, Chris@0: ]; Chris@0: $this->writeSettings($settings); Chris@0: \Drupal::service('kernel')->invalidateContainer(); Chris@0: Chris@0: $this->expectedExceptionMessage = 'Thrown exception during Container::get'; Chris@0: $this->drupalGet(''); Chris@0: $this->assertResponse(500); Chris@0: Chris@0: $this->assertRaw('The website encountered an unexpected error'); Chris@0: $this->assertRaw($this->expectedExceptionMessage); Chris@0: $this->assertErrorLogged($this->expectedExceptionMessage); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the case when the database connection is gone. Chris@0: */ Chris@0: public function testLostDatabaseConnection() { Chris@0: $incorrect_username = $this->randomMachineName(16); Chris@0: switch ($this->container->get('database')->driver()) { Chris@0: case 'pgsql': Chris@0: case 'mysql': Chris@0: $this->expectedExceptionMessage = $incorrect_username; Chris@0: break; Chris@0: default: Chris@0: // We can not carry out this test. Chris@0: $this->pass('Unable to run \Drupal\system\Tests\System\UncaughtExceptionTest::testLostDatabaseConnection for this database type.'); Chris@0: return; Chris@0: } Chris@0: Chris@0: // We simulate a broken database connection by rewrite settings.php to no Chris@0: // longer have the proper data. Chris@0: $settings['databases']['default']['default']['username'] = (object) [ Chris@0: 'value' => $incorrect_username, Chris@0: 'required' => TRUE, Chris@0: ]; Chris@0: $settings['databases']['default']['default']['password'] = (object) [ Chris@0: 'value' => $this->randomMachineName(16), Chris@0: 'required' => TRUE, Chris@0: ]; Chris@0: Chris@0: $this->writeSettings($settings); Chris@0: Chris@0: $this->drupalGet(''); Chris@0: $this->assertResponse(500); Chris@0: $this->assertRaw('DatabaseAccessDeniedException'); Chris@0: $this->assertErrorLogged($this->expectedExceptionMessage); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests fallback to PHP error log when an exception is thrown while logging. Chris@0: */ Chris@0: public function testLoggerException() { Chris@0: // Ensure the test error log is empty before these tests. Chris@0: $this->assertNoErrorsLogged(); Chris@0: Chris@0: $this->expectedExceptionMessage = 'Deforestation'; Chris@0: \Drupal::state()->set('error_service_test.break_logger', TRUE); Chris@0: Chris@0: $this->drupalGet(''); Chris@0: $this->assertResponse(500); Chris@0: $this->assertText('The website encountered an unexpected error. Please try again later.'); Chris@0: $this->assertRaw($this->expectedExceptionMessage); Chris@0: Chris@0: // Find fatal error logged to the simpletest error.log Chris@0: $errors = file(\Drupal::root() . '/' . $this->siteDirectory . '/error.log'); Chris@0: $this->assertIdentical(count($errors), 8, 'The error + the error that the logging service is broken has been written to the error log.'); Chris@0: $this->assertTrue(strpos($errors[0], 'Failed to log error') !== FALSE, 'The error handling logs when an error could not be logged to the logger.'); Chris@0: Chris@0: $expected_path = \Drupal::root() . '/core/modules/system/tests/modules/error_service_test/src/MonkeysInTheControlRoom.php'; Chris@0: $expected_line = 59; Chris@0: $expected_entry = "Failed to log error: Exception: Deforestation in Drupal\\error_service_test\\MonkeysInTheControlRoom->handle() (line ${expected_line} of ${expected_path})"; Chris@0: $this->assert(strpos($errors[0], $expected_entry) !== FALSE, 'Original error logged to the PHP error log when an exception is thrown by a logger'); Chris@0: Chris@0: // The exception is expected. Do not interpret it as a test failure. Not Chris@0: // using File API; a potential error must trigger a PHP warning. Chris@0: unlink(\Drupal::root() . '/' . $this->siteDirectory . '/error.log'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function error($message = '', $group = 'Other', array $caller = NULL) { Chris@0: if (!empty($this->expectedExceptionMessage) && strpos($message, $this->expectedExceptionMessage) !== FALSE) { Chris@0: // We're expecting this error. Chris@0: return FALSE; Chris@0: } Chris@0: return parent::error($message, $group, $caller); Chris@0: } Chris@0: Chris@0: }