View source
<?php
namespace Drupal\Tests\Core\EventSubscriber;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\EventSubscriber\CustomPageExceptionHtmlSubscriber;
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;
class CustomPageExceptionHtmlSubscriberTest extends UnitTestCase {
protected $kernel;
protected $configFactory;
protected $aliasManager;
protected $logger;
protected $errorLog;
protected $customPageSubscriber;
protected $redirectDestination;
protected function setUp() {
$this->configFactory = $this
->getConfigFactoryStub([
'system.site' => [
'page.403' => 'access-denied-page',
'page.404' => 'not-found-page',
],
]);
$this->aliasManager = $this
->getMock('Drupal\\Core\\Path\\AliasManagerInterface');
$this->kernel = $this
->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
$this->logger = $this
->getMock('Psr\\Log\\LoggerInterface');
$this->redirectDestination = $this
->getMock('\\Drupal\\Core\\Routing\\RedirectDestinationInterface');
$this->redirectDestination
->expects($this
->any())
->method('getAsArray')
->willReturn([
'destination' => 'test',
]);
$this->customPageSubscriber = new CustomPageExceptionHtmlSubscriber($this->configFactory, $this->aliasManager, $this->kernel, $this->logger, $this->redirectDestination);
$this->errorLog = ini_set('error_log', file_exists('/dev/null') ? '/dev/null' : 'nul');
}
protected function tearDown() {
ini_set('error_log', $this->errorLog);
}
protected function setupStubAliasManager() {
$this->aliasManager
->expects($this
->any())
->method('getPathByAlias')
->willReturnArgument(0);
}
public function testHandleWithPostRequest() {
$this
->setupStubAliasManager();
$request = Request::create('/test', 'POST', array(
'name' => 'druplicon',
'pass' => '12345',
));
$this->kernel
->expects($this
->once())
->method('handle')
->will($this
->returnCallback(function (Request $request) {
return new Response($request
->getMethod());
}));
$event = new GetResponseForExceptionEvent($this->kernel, $request, 'foo', 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);
}
public function testHandleWithGetRequest() {
$this
->setupStubAliasManager();
$request = Request::create('/test', 'GET', array(
'name' => 'druplicon',
'pass' => '12345',
));
$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, 'foo', 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);
}
}