3 namespace Drupal\KernelTests\Core\Routing;
5 use Drupal\Component\Utility\Html;
6 use Drupal\KernelTests\KernelTestBase;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\HttpFoundation\Response;
11 * Tests the exception handling for various cases.
15 class ExceptionHandlingTest extends KernelTestBase {
20 public static $modules = ['system', 'router_test'];
25 protected function setUp() {
28 $this->installEntitySchema('date_format');
32 * Tests on a route with a non-supported HTTP method.
34 public function test405() {
35 $request = Request::create('/router_test/test15', 'PATCH');
37 /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */
38 $kernel = \Drupal::getContainer()->get('http_kernel');
39 $response = $kernel->handle($request);
41 $this->assertEqual(Response::HTTP_METHOD_NOT_ALLOWED, $response->getStatusCode());
45 * Tests the exception handling for json and 403 status code.
47 public function testJson403() {
48 $request = Request::create('/router_test/test15');
49 $request->query->set('_format', 'json');
50 $request->setRequestFormat('json');
52 /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */
53 $kernel = \Drupal::getContainer()->get('http_kernel');
54 $response = $kernel->handle($request);
56 $this->assertEqual($response->getStatusCode(), Response::HTTP_FORBIDDEN);
57 $this->assertEqual($response->headers->get('Content-type'), 'application/json');
58 $this->assertEqual('{"message":""}', $response->getContent());
62 * Tests the exception handling for json and 404 status code.
64 public function testJson404() {
65 $request = Request::create('/not-found');
66 $request->query->set('_format', 'json');
67 $request->setRequestFormat('json');
69 /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */
70 $kernel = \Drupal::getContainer()->get('http_kernel');
71 $response = $kernel->handle($request);
73 $this->assertEqual($response->getStatusCode(), Response::HTTP_NOT_FOUND);
74 $this->assertEqual($response->headers->get('Content-type'), 'application/json');
75 $this->assertEqual('{"message":"No route found for \\u0022GET \\/not-found\\u0022"}', $response->getContent());
79 * Tests the exception handling for HTML and 403 status code.
81 public function testHtml403() {
82 $request = Request::create('/router_test/test15');
83 $request->setFormat('html', ['text/html']);
85 /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */
86 $kernel = \Drupal::getContainer()->get('http_kernel');
87 $response = $kernel->handle($request)->prepare($request);
89 $this->assertEqual($response->getStatusCode(), Response::HTTP_FORBIDDEN);
90 $this->assertEqual($response->headers->get('Content-type'), 'text/html; charset=UTF-8');
94 * Tests the exception handling for HTML and 404 status code.
96 public function testHtml404() {
97 $request = Request::create('/not-found');
98 $request->setFormat('html', ['text/html']);
100 /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */
101 $kernel = \Drupal::getContainer()->get('http_kernel');
102 $response = $kernel->handle($request)->prepare($request);
104 $this->assertEqual($response->getStatusCode(), Response::HTTP_NOT_FOUND);
105 $this->assertEqual($response->headers->get('Content-type'), 'text/html; charset=UTF-8');
109 * Tests that the exception response is executed in the original context.
111 public function testExceptionResponseGeneratedForOriginalRequest() {
112 // Test with 404 path pointing to a route that uses '_controller'.
113 $response = $this->doTest404Route('/router_test/test25');
114 $this->assertTrue(strpos($response->getContent(), '/not-found') !== FALSE);
116 // Test with 404 path pointing to a route that uses '_form'.
117 $response = $this->doTest404Route('/router_test/test26');
118 $this->assertTrue(strpos($response->getContent(), '<form class="system-logging-settings"') !== FALSE);
120 // Test with 404 path pointing to a route that uses '_entity_form'.
121 $response = $this->doTest404Route('/router_test/test27');
122 $this->assertTrue(strpos($response->getContent(), '<form class="date-format-add-form date-format-form"') !== FALSE);
126 * Sets the given path to use as the 404 page and triggers a 404.
128 * @param string $path
129 * @return \Drupal\Core\Render\HtmlResponse
131 * @see \Drupal\system\Tests\Routing\ExceptionHandlingTest::testExceptionResponseGeneratedForOriginalRequest()
133 protected function doTest404Route($path) {
134 $this->config('system.site')->set('page.404', $path)->save();
136 $request = Request::create('/not-found');
137 $request->setFormat('html', ['text/html']);
139 /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */
140 $kernel = \Drupal::getContainer()->get('http_kernel');
141 return $kernel->handle($request)->prepare($request);
145 * Tests if exception backtraces are properly escaped when output to HTML.
147 public function testBacktraceEscaping() {
148 // Enable verbose error logging.
149 $this->config('system.logging')->set('error_level', ERROR_REPORTING_DISPLAY_VERBOSE)->save();
151 $request = Request::create('/router_test/test17');
152 $request->setFormat('html', ['text/html']);
154 /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */
155 $kernel = \Drupal::getContainer()->get('http_kernel');
156 $response = $kernel->handle($request)->prepare($request);
157 $this->assertEqual($response->getStatusCode(), Response::HTTP_INTERNAL_SERVER_ERROR);
158 $this->assertEqual($response->headers->get('Content-type'), 'text/plain; charset=UTF-8');
160 // Test both that the backtrace is properly escaped, and that the unescaped
161 // string is not output at all.
162 $this->assertTrue(strpos($response->getContent(), Html::escape('<script>alert(\'xss\')</script>')) !== FALSE);
163 $this->assertTrue(strpos($response->getContent(), '<script>alert(\'xss\')</script>') === FALSE);
167 * Tests exception message escaping.
169 public function testExceptionEscaping() {
170 // Enable verbose error logging.
171 $this->config('system.logging')->set('error_level', ERROR_REPORTING_DISPLAY_VERBOSE)->save();
173 // Using SafeMarkup::format().
174 $request = Request::create('/router_test/test24');
175 $request->setFormat('html', ['text/html']);
177 /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */
178 $kernel = \Drupal::getContainer()->get('http_kernel');
179 $response = $kernel->handle($request)->prepare($request);
180 $this->assertEqual($response->getStatusCode(), Response::HTTP_INTERNAL_SERVER_ERROR);
181 $this->assertEqual($response->headers->get('Content-type'), 'text/plain; charset=UTF-8');
183 // Test message is properly escaped, and that the unescaped string is not
185 $this->setRawContent($response->getContent());
186 $this->assertRaw(Html::escape('Escaped content: <p> <br> <h3>'));
187 $this->assertNoRaw('<p> <br> <h3>');
189 $string = '<script>alert(123);</script>';
190 $request = Request::create('/router_test/test2?_format=json' . urlencode($string), 'GET');
192 $kernel = \Drupal::getContainer()->get('http_kernel');
193 $response = $kernel->handle($request)->prepare($request);
194 // As the Content-type is text/plain the fact that the raw string is
195 // contained in the output would not matter, but because it is output by the
196 // final exception subscriber, it is printed as partial HTML, and hence
198 $this->assertEqual($response->headers->get('Content-type'), 'text/plain; charset=UTF-8');
199 $this->assertStringStartsWith('The website encountered an unexpected error. Please try again later.</br></br><em class="placeholder">Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException</em>: Not acceptable format: json<script>alert(123);</script> in <em class="placeholder">', $response->getContent());