You are here

class M4032404EventSubscriberTest in 403 to 404 8

Tests for M4032404EventSubscriber.

@coversDefaultClass \Drupal\m4032404\EventSubscriber\M4032404EventSubscriber

@group m4032404

Hierarchy

Expanded class hierarchy of M4032404EventSubscriberTest

File

tests/src/Unit/EventSubscriber/M4032404EventSubscriberTest.php, line 20

Namespace

Drupal\Tests\m4032404\Unit\EventSubscriber
View 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

Namesort descending Modifiers Type Description Overrides
M4032404EventSubscriberTest::$adminContext protected property The admin context.
M4032404EventSubscriberTest::$configFactory protected property The config factory.
M4032404EventSubscriberTest::$currentUser protected property The current user.
M4032404EventSubscriberTest::$event protected property The event.
M4032404EventSubscriberTest::setUp protected function Overrides UnitTestCase::setUp
M4032404EventSubscriberTest::testAdminOnlyFailure public function Tests event handling for admin only routes when not admin route.
M4032404EventSubscriberTest::testAdminOnlySuccess public function Tests event handling for admin only routes when admin route.
M4032404EventSubscriberTest::testHandleAll public function Tests event handling for all routes.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.