3 namespace Drupal\Tests\Core\EventSubscriber;
5 use Drupal\Core\EventSubscriber\FinalExceptionSubscriber;
6 use Drupal\Tests\UnitTestCase;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\HttpFoundation\Response;
9 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
10 use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
11 use Symfony\Component\HttpKernel\HttpKernelInterface;
14 * @coversDefaultClass \Drupal\Core\EventSubscriber\FinalExceptionSubscriber
15 * @group EventSubscriber
17 class FinalExceptionSubscriberTest extends UnitTestCase {
20 * @covers ::onException
22 public function testOnExceptionWithUnknownFormat() {
23 $config_factory = $this->getConfigFactoryStub();
25 $kernel = $this->prophesize(HttpKernelInterface::class);
26 $request = Request::create('/test');
27 // \Drupal\Core\StackMiddleware\NegotiationMiddleware normally takes care
28 // of this so we'll hard code it here.
29 $request->setRequestFormat('bananas');
30 $e = new MethodNotAllowedHttpException(['POST', 'PUT'], 'test message');
31 $event = new GetResponseForExceptionEvent($kernel->reveal(), $request, 'GET', $e);
32 $subscriber = new TestDefaultExceptionSubscriber($config_factory);
33 $subscriber->setStringTranslation($this->getStringTranslationStub());
34 $subscriber->onException($event);
35 $response = $event->getResponse();
37 $this->assertInstanceOf(Response::class, $response);
38 $this->stringStartsWith('The website encountered an unexpected error. Please try again later.</br></br><em class="placeholder">Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException</em>: test message in ', $response->getContent());
39 $this->assertEquals(405, $response->getStatusCode());
40 $this->assertEquals('POST, PUT', $response->headers->get('Allow'));
41 // Also check that that text/plain content type was added.
42 $this->assertEquals('text/plain', $response->headers->get('Content-Type'));
47 class TestDefaultExceptionSubscriber extends FinalExceptionSubscriber {
49 protected function isErrorDisplayable($error) {
53 protected function simplifyFileInError($error) {
57 protected function isErrorLevelVerbose() {