More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / EventSubscriber / CustomPageExceptionHtmlSubscriberTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\EventSubscriber;
4
5 use Drupal\Component\Utility\UrlHelper;
6 use Drupal\Core\Access\AccessResult;
7 use Drupal\Core\DependencyInjection\ContainerBuilder;
8 use Drupal\Core\EventSubscriber\CustomPageExceptionHtmlSubscriber;
9 use Drupal\Core\Render\HtmlResponse;
10 use Drupal\Core\Routing\AccessAwareRouterInterface;
11 use Drupal\Core\Url;
12 use Drupal\Tests\UnitTestCase;
13 use Symfony\Component\HttpFoundation\Request;
14 use Symfony\Component\HttpFoundation\Response;
15 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
16 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
17 use Symfony\Component\Routing\RequestContext;
18
19 /**
20  * @coversDefaultClass \Drupal\Core\EventSubscriber\CustomPageExceptionHtmlSubscriber
21  * @group EventSubscriber
22  */
23 class CustomPageExceptionHtmlSubscriberTest extends UnitTestCase {
24
25   /**
26    * The mocked HTTP kernel.
27    *
28    * @var \Symfony\Component\HttpKernel\HttpKernelInterface|\PHPUnit_Framework_MockObject_MockObject
29    */
30   protected $kernel;
31
32   /**
33    * The mocked config factory
34    *
35    * @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
36    */
37   protected $configFactory;
38
39   /**
40    * The mocked logger.
41    *
42    * @var \Psr\Log\LoggerInterface
43    */
44   protected $logger;
45
46   /**
47    * The PHP error log settings before the test.
48    *
49    * @var string
50    */
51   protected $errorLog;
52
53   /**
54    * The tested custom page exception subscriber.
55    *
56    * @var \Drupal\Core\EventSubscriber\CustomPageExceptionHtmlSubscriber|\Drupal\Tests\Core\EventSubscriber\TestCustomPageExceptionHtmlSubscriber
57    */
58   protected $customPageSubscriber;
59
60   /**
61    * The mocked redirect.destination service.
62    *
63    * @var \Drupal\Core\Routing\RedirectDestinationInterface|\PHPUnit_Framework_MockObject_MockObject
64    */
65   protected $redirectDestination;
66
67   /**
68    * The mocked access unaware router.
69    * @var \Symfony\Component\Routing\Matcher\UrlMatcherInterface|\PHPUnit_Framework_MockObject_MockObject
70    */
71   protected $accessUnawareRouter;
72
73   /**
74    * The access manager.
75    *
76    * @var \Drupal\Core\Access\AccessManagerInterface
77    */
78   protected $accessManager;
79
80   /**
81    * {@inheritdoc}
82    */
83   protected function setUp() {
84     $this->configFactory = $this->getConfigFactoryStub(['system.site' => ['page.403' => '/access-denied-page', 'page.404' => '/not-found-page']]);
85
86     $this->kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
87     $this->logger = $this->getMock('Psr\Log\LoggerInterface');
88     $this->redirectDestination = $this->getMock('\Drupal\Core\Routing\RedirectDestinationInterface');
89     $this->redirectDestination->expects($this->any())
90       ->method('getAsArray')
91       ->willReturn(['destination' => 'test']);
92     $this->accessUnawareRouter = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
93     $this->accessUnawareRouter->expects($this->any())
94       ->method('match')
95       ->willReturn([
96         '_controller' => 'mocked',
97       ]);
98     $this->accessManager = $this->getMock('Drupal\Core\Access\AccessManagerInterface');
99     $this->accessManager->expects($this->any())
100       ->method('checkNamedRoute')
101       ->willReturn(AccessResult::allowed()->addCacheTags(['foo', 'bar']));
102
103     $this->customPageSubscriber = new CustomPageExceptionHtmlSubscriber($this->configFactory, $this->kernel, $this->logger, $this->redirectDestination, $this->accessUnawareRouter, $this->accessManager);
104
105     $path_validator = $this->getMock('Drupal\Core\Path\PathValidatorInterface');
106     $path_validator->expects($this->any())
107       ->method('getUrlIfValidWithoutAccessCheck')
108       ->willReturn(Url::fromRoute('foo', ['foo' => 'bar']));
109     $container = new ContainerBuilder();
110     $container->set('path.validator', $path_validator);
111     \Drupal::setContainer($container);
112
113     // You can't create an exception in PHP without throwing it. Store the
114     // current error_log, and disable it temporarily.
115     $this->errorLog = ini_set('error_log', file_exists('/dev/null') ? '/dev/null' : 'nul');
116   }
117
118   /**
119    * {@inheritdoc}
120    */
121   protected function tearDown() {
122     ini_set('error_log', $this->errorLog);
123   }
124
125   /**
126    * Tests onHandleException with a POST request.
127    */
128   public function testHandleWithPostRequest() {
129     $request = Request::create('/test', 'POST', ['name' => 'druplicon', 'pass' => '12345']);
130
131     $request_context = new RequestContext();
132     $request_context->fromRequest($request);
133     $this->accessUnawareRouter->expects($this->any())
134       ->method('getContext')
135       ->willReturn($request_context);
136
137     $this->kernel->expects($this->once())->method('handle')->will($this->returnCallback(function (Request $request) {
138       return new HtmlResponse($request->getMethod());
139     }));
140
141     $event = new GetResponseForExceptionEvent($this->kernel, $request, 'foo', new NotFoundHttpException('foo'));
142
143     $this->customPageSubscriber->onException($event);
144
145     $response = $event->getResponse();
146     $result = $response->getContent() . " " . UrlHelper::buildQuery($request->request->all());
147     $this->assertEquals('POST name=druplicon&pass=12345', $result);
148     $this->assertEquals(AccessResult::allowed()->addCacheTags(['foo', 'bar']), $request->attributes->get(AccessAwareRouterInterface::ACCESS_RESULT));
149   }
150
151   /**
152    * Tests onHandleException with a GET request.
153    */
154   public function testHandleWithGetRequest() {
155     $request = Request::create('/test', 'GET', ['name' => 'druplicon', 'pass' => '12345']);
156     $request->attributes->set(AccessAwareRouterInterface::ACCESS_RESULT, AccessResult::forbidden()->addCacheTags(['druplicon']));
157
158     $request_context = new RequestContext();
159     $request_context->fromRequest($request);
160     $this->accessUnawareRouter->expects($this->any())
161       ->method('getContext')
162       ->willReturn($request_context);
163
164     $this->kernel->expects($this->once())->method('handle')->will($this->returnCallback(function (Request $request) {
165       return new Response($request->getMethod() . ' ' . UrlHelper::buildQuery($request->query->all()));
166     }));
167
168     $event = new GetResponseForExceptionEvent($this->kernel, $request, 'foo', new NotFoundHttpException('foo'));
169     $this->customPageSubscriber->onException($event);
170
171     $response = $event->getResponse();
172     $result = $response->getContent() . " " . UrlHelper::buildQuery($request->request->all());
173     $this->assertEquals('GET name=druplicon&pass=12345&destination=test&_exception_statuscode=404 ', $result);
174     $this->assertEquals(AccessResult::forbidden()->addCacheTags(['druplicon', 'foo', 'bar']), $request->attributes->get(AccessAwareRouterInterface::ACCESS_RESULT));
175   }
176
177 }