class M4032404EventSubscriberTest in 403 to 404 8
Tests for M4032404EventSubscriber.
@coversDefaultClass \Drupal\m4032404\EventSubscriber\M4032404EventSubscriber
@group m4032404
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\m4032404\Unit\EventSubscriber\M4032404EventSubscriberTest
Expanded class hierarchy of M4032404EventSubscriberTest
File
- tests/
src/ Unit/ EventSubscriber/ M4032404EventSubscriberTest.php, line 20
Namespace
Drupal\Tests\m4032404\Unit\EventSubscriberView source
class M4032404EventSubscriberTest extends UnitTestCase {
/**
* The event.
*
* @var \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent
*/
protected $event;
/**
* The admin context.
*
* @var \PHPUnit_Framework_MockObject_MockObject|\Drupal\Core\Routing\AdminContext
*/
protected $adminContext;
/**
* The current user.
*
* @var \PHPUnit_Framework_MockObject_MockObject|\Drupal\Core\Session\AccountProxy
*/
protected $currentUser;
/**
* The config factory.
*
* @var \PHPUnit_Framework_MockObject_MockBuilder|\Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* {@inheritdoc}
*/
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());
}
/**
* Tests event handling for all routes.
*
* @covers ::onAccessDeniedException
*/
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);
}
/**
* Tests event handling for admin only routes when admin route.
*
* @covers ::onAccessDeniedException
*/
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);
}
/**
* Tests event handling for admin only routes when not admin route.
*
* @covers ::onAccessDeniedException
*/
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);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
M4032404EventSubscriberTest:: |
protected | property | The admin context. | |
M4032404EventSubscriberTest:: |
protected | property | The config factory. | |
M4032404EventSubscriberTest:: |
protected | property | The current user. | |
M4032404EventSubscriberTest:: |
protected | property | The event. | |
M4032404EventSubscriberTest:: |
protected | function |
Overrides UnitTestCase:: |
|
M4032404EventSubscriberTest:: |
public | function | Tests event handling for admin only routes when not admin route. | |
M4032404EventSubscriberTest:: |
public | function | Tests event handling for admin only routes when admin route. | |
M4032404EventSubscriberTest:: |
public | function | Tests event handling for all routes. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |