M4032404EventSubscriberTest.php in 403 to 404 8
File
tests/src/Unit/EventSubscriber/M4032404EventSubscriberTest.php
View source
<?php
namespace Drupal\Tests\m4032404\Unit\EventSubscriber;
use Drupal\m4032404\EventSubscriber\M4032404EventSubscriber;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class M4032404EventSubscriberTest extends UnitTestCase {
protected $event;
protected $adminContext;
protected $currentUser;
protected $configFactory;
protected function setUp() {
parent::setUp();
$this->adminContext = $this
->getMockBuilder('\\Drupal\\Core\\Routing\\AdminContext')
->disableOriginalConstructor()
->getMock();
$this->currentUser = $this
->getMockBuilder('\\Drupal\\Core\\Session\\AccountProxy')
->disableOriginalConstructor()
->getMock();
$kernel = $this
->createMock('\\Symfony\\Component\\HttpKernel\\HttpKernelInterface');
$request = new Request();
$this->event = new GetResponseForExceptionEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, new AccessDeniedHttpException());
}
public function testHandleAll() {
$this->configFactory = $this
->getConfigFactoryStub([
'm4032404.settings' => [
'admin_only' => FALSE,
],
]);
$this->adminContext
->method('isAdminRoute')
->willReturn(FALSE);
$subscriber = new M4032404EventSubscriber($this->configFactory, $this->adminContext, $this->currentUser);
$subscriber
->onAccessDeniedException($this->event);
$e = $this->event
->getException();
$this
->assertTrue($e instanceof NotFoundHttpException);
}
public function testAdminOnlySuccess() {
$this->configFactory = $this
->getConfigFactoryStub([
'm4032404.settings' => [
'admin_only' => TRUE,
],
]);
$this->adminContext
->method('isAdminRoute')
->willReturn(TRUE);
$subscriber = new M4032404EventSubscriber($this->configFactory, $this->adminContext, $this->currentUser);
$subscriber
->onAccessDeniedException($this->event);
$e = $this->event
->getException();
$this
->assertTrue($e instanceof NotFoundHttpException);
}
public function testAdminOnlyFailure() {
$this->configFactory = $this
->getConfigFactoryStub([
'm4032404.settings' => [
'admin_only' => TRUE,
],
]);
$this->adminContext
->method('isAdminRoute')
->willReturn(FALSE);
$subscriber = new M4032404EventSubscriber($this->configFactory, $this->adminContext, $this->currentUser);
$subscriber
->onAccessDeniedException($this->event);
$e = $this->event
->getException();
$this
->assertTrue($e instanceof AccessDeniedHttpException);
}
}