More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / EventSubscriber / FinalExceptionSubscriberTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\EventSubscriber;
4
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;
12
13 /**
14  * @coversDefaultClass \Drupal\Core\EventSubscriber\FinalExceptionSubscriber
15  * @group EventSubscriber
16  */
17 class FinalExceptionSubscriberTest extends UnitTestCase {
18
19   /**
20    * @covers ::onException
21    */
22   public function testOnExceptionWithUnknownFormat() {
23     $config_factory = $this->getConfigFactoryStub();
24
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();
36
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'));
43   }
44
45 }
46
47 class TestDefaultExceptionSubscriber extends FinalExceptionSubscriber {
48
49   protected function isErrorDisplayable($error) {
50     return TRUE;
51   }
52
53   protected function simplifyFileInError($error) {
54     return $error;
55   }
56
57   protected function isErrorLevelVerbose() {
58     return TRUE;
59   }
60
61 }