View source
<?php
namespace Drupal\Tests\Core\EventSubscriber;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\EventSubscriber\CustomPageExceptionHtmlSubscriber;
use Drupal\Core\Render\HtmlResponse;
use Drupal\Core\Routing\AccessAwareRouterInterface;
use Drupal\Core\Url;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Routing\RequestContext;
class CustomPageExceptionHtmlSubscriberTest extends UnitTestCase {
protected $kernel;
protected $configFactory;
protected $logger;
protected $errorLog;
protected $customPageSubscriber;
protected $redirectDestination;
protected $accessUnawareRouter;
protected $accessManager;
protected function setUp() {
$this->configFactory = $this
->getConfigFactoryStub([
'system.site' => [
'page.403' => '/access-denied-page',
'page.404' => '/not-found-page',
],
]);
$this->kernel = $this
->createMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
$this->logger = $this
->createMock('Psr\\Log\\LoggerInterface');
$this->redirectDestination = $this
->createMock('\\Drupal\\Core\\Routing\\RedirectDestinationInterface');
$this->redirectDestination
->expects($this
->any())
->method('getAsArray')
->willReturn([
'destination' => 'test',
]);
$this->accessUnawareRouter = $this
->createMock('Symfony\\Component\\Routing\\Matcher\\UrlMatcherInterface');
$this->accessUnawareRouter
->expects($this
->any())
->method('match')
->willReturn([
'_controller' => 'mocked',
]);
$this->accessManager = $this
->createMock('Drupal\\Core\\Access\\AccessManagerInterface');
$this->accessManager
->expects($this
->any())
->method('checkNamedRoute')
->willReturn(AccessResult::allowed()
->addCacheTags([
'foo',
'bar',
]));
$this->customPageSubscriber = new CustomPageExceptionHtmlSubscriber($this->configFactory, $this->kernel, $this->logger, $this->redirectDestination, $this->accessUnawareRouter, $this->accessManager);
$path_validator = $this
->createMock('Drupal\\Core\\Path\\PathValidatorInterface');
$path_validator
->expects($this
->any())
->method('getUrlIfValidWithoutAccessCheck')
->willReturn(Url::fromRoute('foo', [
'foo' => 'bar',
]));
$container = new ContainerBuilder();
$container
->set('path.validator', $path_validator);
\Drupal::setContainer($container);
$this->errorLog = ini_set('error_log', file_exists('/dev/null') ? '/dev/null' : 'nul');
}
protected function tearDown() {
ini_set('error_log', $this->errorLog);
}
public function testHandleWithPostRequest() {
$request = Request::create('/test', 'POST', [
'name' => 'druplicon',
'pass' => '12345',
]);
$request_context = new RequestContext();
$request_context
->fromRequest($request);
$this->accessUnawareRouter
->expects($this
->any())
->method('getContext')
->willReturn($request_context);
$this->kernel
->expects($this
->once())
->method('handle')
->will($this
->returnCallback(function (Request $request) {
return new HtmlResponse($request
->getMethod());
}));
$event = new GetResponseForExceptionEvent($this->kernel, $request, HttpKernelInterface::MASTER_REQUEST, new NotFoundHttpException('foo'));
$this->customPageSubscriber
->onException($event);
$response = $event
->getResponse();
$result = $response
->getContent() . " " . UrlHelper::buildQuery($request->request
->all());
$this
->assertEquals('POST name=druplicon&pass=12345', $result);
$this
->assertEquals(AccessResult::allowed()
->addCacheTags([
'foo',
'bar',
]), $request->attributes
->get(AccessAwareRouterInterface::ACCESS_RESULT));
}
public function testHandleWithGetRequest() {
$request = Request::create('/test', 'GET', [
'name' => 'druplicon',
'pass' => '12345',
]);
$request->attributes
->set(AccessAwareRouterInterface::ACCESS_RESULT, AccessResult::forbidden()
->addCacheTags([
'druplicon',
]));
$request_context = new RequestContext();
$request_context
->fromRequest($request);
$this->accessUnawareRouter
->expects($this
->any())
->method('getContext')
->willReturn($request_context);
$this->kernel
->expects($this
->once())
->method('handle')
->will($this
->returnCallback(function (Request $request) {
return new Response($request
->getMethod() . ' ' . UrlHelper::buildQuery($request->query
->all()));
}));
$event = new GetResponseForExceptionEvent($this->kernel, $request, HttpKernelInterface::MASTER_REQUEST, new NotFoundHttpException('foo'));
$this->customPageSubscriber
->onException($event);
$response = $event
->getResponse();
$result = $response
->getContent() . " " . UrlHelper::buildQuery($request->request
->all());
$this
->assertEquals('GET name=druplicon&pass=12345&destination=test&_exception_statuscode=404 ', $result);
$this
->assertEquals(AccessResult::forbidden()
->addCacheTags([
'druplicon',
'foo',
'bar',
]), $request->attributes
->get(AccessAwareRouterInterface::ACCESS_RESULT));
}
}