diff --git a/r4032login.services.yml b/r4032login.services.yml index 71f5a12..b40266f 100644 --- a/r4032login.services.yml +++ b/r4032login.services.yml @@ -1,6 +1,6 @@ services: r4032login.subscriber: class: Drupal\r4032login\EventSubscriber\R4032LoginSubscriber - arguments: ['@config.factory', '@current_user', '@redirect.destination', '@path.matcher', '@event_dispatcher'] + arguments: ['@config.factory', '@current_user', '@redirect.destination', '@path.matcher', '@event_dispatcher', '@messenger'] tags: - { name: event_subscriber } diff --git a/src/EventSubscriber/R4032LoginSubscriber.php b/src/EventSubscriber/R4032LoginSubscriber.php index ce9bd74..0ff80ae 100644 --- a/src/EventSubscriber/R4032LoginSubscriber.php +++ b/src/EventSubscriber/R4032LoginSubscriber.php @@ -9,6 +9,7 @@ use Drupal\Core\Path\PathMatcherInterface; use Drupal\Core\Routing\TrustedRedirectResponse; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Routing\RedirectDestinationInterface; +use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Url; use Drupal\r4032login\Event\RedirectEvent; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -56,6 +57,12 @@ class R4032LoginSubscriber extends HttpExceptionSubscriberBase { */ protected $eventDispatcher; + /** + * The messenger service. + * + * @var \Drupal\Core\Messenger\MessengerInterface + */ + /** * Constructs a new R4032LoginSubscriber. * @@ -69,13 +76,16 @@ class R4032LoginSubscriber extends HttpExceptionSubscriberBase { * The path matcher. * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher * The event dispatcher. + * @param \Drupal\Core\Messenger\MessengerInterface $messenger + * The messenger service. */ - public function __construct(ConfigFactoryInterface $config_factory, AccountInterface $current_user, RedirectDestinationInterface $redirect_destination, PathMatcherInterface $path_matcher, EventDispatcherInterface $event_dispatcher) { + public function __construct(ConfigFactoryInterface $config_factory, AccountInterface $current_user, RedirectDestinationInterface $redirect_destination, PathMatcherInterface $path_matcher, EventDispatcherInterface $event_dispatcher, MessengerInterface $messenger) { $this->configFactory = $config_factory; $this->currentUser = $current_user; $this->redirectDestination = $redirect_destination; $this->pathMatcher = $path_matcher; $this->eventDispatcher = $event_dispatcher; + $this->messenger = $messenger; } /** @@ -166,7 +176,7 @@ class R4032LoginSubscriber extends HttpExceptionSubscriberBase { if ($this->currentUser->isAnonymous() && $config->get('display_denied_message')) { $message = $config->get('access_denied_message'); $messageType = $config->get('access_denied_message_type'); - drupal_set_message(Xss::filterAdmin($message), $messageType); + $this->messenger->addMessage(Xss::filterAdmin($message), $messageType); } if ($redirectPath === '') { diff --git a/tests/src/Unit/R4032LoginSubscriberTest.php b/tests/src/Unit/R4032LoginSubscriberTest.php index ba90866..998565a 100644 --- a/tests/src/Unit/R4032LoginSubscriberTest.php +++ b/tests/src/Unit/R4032LoginSubscriberTest.php @@ -9,7 +9,6 @@ namespace Drupal\Tests\r4032login\Unit { use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; - use Symfony\Component\HttpKernel\Exception; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\KernelEvents; @@ -62,6 +61,13 @@ namespace Drupal\Tests\r4032login\Unit { */ protected $eventDispatcher; + /** + * The messenger service. + * + * @var \Drupal\Core\Messenger\MessengerInterface + */ + protected $messenger; + /** * The mocked unrouted URL assembler. * @@ -80,7 +86,7 @@ namespace Drupal\Tests\r4032login\Unit { * {@inheritdoc} */ protected function setUp() { - $this->kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'); + $this->kernel = $this->createMock('Symfony\Component\HttpKernel\HttpKernelInterface'); $this->configFactory = $this->getConfigFactoryStub([ 'r4032login.settings' => [ 'display_denied_message' => TRUE, @@ -92,17 +98,18 @@ namespace Drupal\Tests\r4032login\Unit { 'match_noredirect_pages' => '', ], ]); - $this->currentUser = $this->getMock('Drupal\Core\Session\AccountInterface'); - $this->redirectDestination = $this->getMock('\Drupal\Core\Routing\RedirectDestinationInterface'); - $this->pathMatcher = $this->getMock('\Drupal\Core\Path\PathMatcherInterface'); - $this->eventDispatcher = $this->getMock('\Symfony\Component\EventDispatcher\EventDispatcherInterface'); + $this->currentUser = $this->createMock('Drupal\Core\Session\AccountInterface'); + $this->redirectDestination = $this->createMock('\Drupal\Core\Routing\RedirectDestinationInterface'); + $this->pathMatcher = $this->createMock('\Drupal\Core\Path\PathMatcherInterface'); + $this->eventDispatcher = $this->createMock('\Symfony\Component\EventDispatcher\EventDispatcherInterface'); + $this->messenger = $this->createMock('\Drupal\Core\Messenger\MessengerInterface'); - $this->urlAssembler = $this->getMock('Drupal\Core\Utility\UnroutedUrlAssemblerInterface'); + $this->urlAssembler = $this->createMock('Drupal\Core\Utility\UnroutedUrlAssemblerInterface'); $this->urlAssembler->expects($this->any())->method('assemble')->will($this->returnArgument(0)); - $this->router = $this->getMock('Drupal\Tests\Core\Routing\TestRouterInterface'); + $this->router = $this->createMock('Drupal\Tests\Core\Routing\TestRouterInterface'); $container = new ContainerBuilder(); - $container->set('path.validator', $this->getMock('Drupal\Core\Path\PathValidatorInterface')); + $container->set('path.validator', $this->createMock('Drupal\Core\Path\PathValidatorInterface')); $container->set('router.no_access_checks', $this->router); $container->set('unrouted_url_assembler', $this->urlAssembler); \Drupal::setContainer($container); @@ -114,7 +121,7 @@ namespace Drupal\Tests\r4032login\Unit { * @covers ::__construct */ public function testConstruct() { - $r4032login = new R4032LoginSubscriber($this->configFactory, $this->currentUser, $this->redirectDestination, $this->pathMatcher, $this->eventDispatcher); + $r4032login = new R4032LoginSubscriber($this->configFactory, $this->currentUser, $this->redirectDestination, $this->pathMatcher, $this->eventDispatcher, $this->messenger); $this->assertInstanceOf('\Drupal\r4032login\EventSubscriber\R4032LoginSubscriber', $r4032login); } @@ -138,7 +145,7 @@ namespace Drupal\Tests\r4032login\Unit { ]); $this->currentUser->expects($this->any())->method('isAnonymous')->willReturn(TRUE); - $r4032login = new R4032LoginSubscriber($config, $this->currentUser, $this->redirectDestination, $this->pathMatcher, $this->eventDispatcher); + $r4032login = new R4032LoginSubscriber($config, $this->currentUser, $this->redirectDestination, $this->pathMatcher, $this->eventDispatcher, $this->messenger); $event = new GetResponseForExceptionEvent($this->kernel, $request, HttpKernelInterface::MASTER_REQUEST, new AccessDeniedHttpException()); $dispatcher = new EventDispatcher(); $dispatcher->addListener(KernelEvents::EXCEPTION, [ @@ -172,7 +179,7 @@ namespace Drupal\Tests\r4032login\Unit { ]); $this->currentUser->expects($this->any())->method('isAuthenticated')->willReturn(TRUE); - $r4032login = new R4032LoginSubscriber($config, $this->currentUser, $this->redirectDestination, $this->pathMatcher, $this->eventDispatcher); + $r4032login = new R4032LoginSubscriber($config, $this->currentUser, $this->redirectDestination, $this->pathMatcher, $this->eventDispatcher, $this->messenger); $event = new GetResponseForExceptionEvent($this->kernel, $request, HttpKernelInterface::MASTER_REQUEST, new AccessDeniedHttpException()); $dispatcher = new EventDispatcher(); $dispatcher->addListener(KernelEvents::EXCEPTION, [ @@ -250,7 +257,7 @@ namespace Drupal\Tests\r4032login\Unit { * An array of GetResponseForExceptionEvent exception events. */ public function providerInvalidExceptions() { - $kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'); + $kernel = $this->createMock('Symfony\Component\HttpKernel\HttpKernelInterface'); $exceptions = []; foreach (get_declared_classes() as $name) { $class = new \ReflectionClass($name);