diff --git a/r4032login.info.yml b/r4032login.info.yml index 6f0da3e..5fe1a48 100644 --- a/r4032login.info.yml +++ b/r4032login.info.yml @@ -1,5 +1,6 @@ name: 'Redirect 403 to User Login' -type: module description: 'Redirect anonymous users from 403 Access Denied pages to the /user/login page.' -core: 8.x +core_version_requirement: ^8.8 || ^9 +type: module + configure: system.site_information_settings diff --git a/r4032login.services.yml b/r4032login.services.yml index ae25815..7e2bc86 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', '@request_stack', '@path.matcher', '@event_dispatcher'] + arguments: ['@config.factory', '@current_user', '@request_stack', '@path.matcher', '@event_dispatcher', '@messenger'] tags: - { name: event_subscriber } diff --git a/src/EventSubscriber/R4032LoginSubscriber.php b/src/EventSubscriber/R4032LoginSubscriber.php index 6f9b5f1..50cdd8c 100644 --- a/src/EventSubscriber/R4032LoginSubscriber.php +++ b/src/EventSubscriber/R4032LoginSubscriber.php @@ -5,6 +5,7 @@ namespace Drupal\r4032login\EventSubscriber; use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\EventSubscriber\HttpExceptionSubscriberBase; +use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Path\PathMatcherInterface; use Drupal\Core\Routing\TrustedRedirectResponse; use Drupal\Core\Session\AccountInterface; @@ -56,6 +57,13 @@ class R4032LoginSubscriber extends HttpExceptionSubscriberBase { */ protected $eventDispatcher; + /** + * The messenger service. + * + * @var \Drupal\Core\Messenger\MessengerInterface + */ + protected $messenger; + /** * Constructs a new R4032LoginSubscriber. * @@ -69,13 +77,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, RequestStack $request_stack, PathMatcherInterface $path_matcher, EventDispatcherInterface $event_dispatcher) { + public function __construct(ConfigFactoryInterface $config_factory, AccountInterface $current_user, RequestStack $request_stack, PathMatcherInterface $path_matcher, EventDispatcherInterface $event_dispatcher, MessengerInterface $messenger) { $this->configFactory = $config_factory; $this->currentUser = $current_user; $this->requestStack = $request_stack; $this->pathMatcher = $path_matcher; $this->eventDispatcher = $event_dispatcher; + $this->messenger = $messenger; } /** @@ -160,7 +171,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 e06bd2a..2f06cd6 100644 --- a/tests/src/Unit/R4032LoginSubscriberTest.php +++ b/tests/src/Unit/R4032LoginSubscriberTest.php @@ -62,6 +62,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 +87,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,20 +99,21 @@ namespace Drupal\Tests\r4032login\Unit { 'match_noredirect_pages' => '', ], ]); - $this->currentUser = $this->getMock('Drupal\Core\Session\AccountInterface'); + $this->currentUser = $this->createMock('Drupal\Core\Session\AccountInterface'); $this->requestStack = new RequestStack(); $this->requestStack->push(new Request()); - $this->pathMatcher = $this->getMock('\Drupal\Core\Path\PathMatcherInterface'); - $this->eventDispatcher = $this->getMock('\Symfony\Component\EventDispatcher\EventDispatcherInterface'); + $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); @@ -117,7 +125,7 @@ namespace Drupal\Tests\r4032login\Unit { * @covers ::__construct */ public function testConstruct() { - $r4032login = new R4032LoginSubscriber($this->configFactory, $this->currentUser, $this->requestStack, $this->pathMatcher, $this->eventDispatcher); + $r4032login = new R4032LoginSubscriber($this->configFactory, $this->currentUser, $this->requestStack, $this->pathMatcher, $this->eventDispatcher, $this->messenger); $this->assertInstanceOf('\Drupal\r4032login\EventSubscriber\R4032LoginSubscriber', $r4032login); } @@ -141,7 +149,7 @@ namespace Drupal\Tests\r4032login\Unit { ]); $this->currentUser->expects($this->any())->method('isAnonymous')->willReturn(TRUE); - $r4032login = new R4032LoginSubscriber($config, $this->currentUser, $this->requestStack, $this->pathMatcher, $this->eventDispatcher); + $r4032login = new R4032LoginSubscriber($config, $this->currentUser, $this->requestStack, $this->pathMatcher, $this->eventDispatcher, $this->messenger); $event = new GetResponseForExceptionEvent($this->kernel, $request, HttpKernelInterface::MASTER_REQUEST, new AccessDeniedHttpException()); $dispatcher = new EventDispatcher(); $dispatcher->addListener(KernelEvents::EXCEPTION, [ @@ -175,7 +183,7 @@ namespace Drupal\Tests\r4032login\Unit { ]); $this->currentUser->expects($this->any())->method('isAuthenticated')->willReturn(TRUE); - $r4032login = new R4032LoginSubscriber($config, $this->currentUser, $this->requestStack, $this->pathMatcher, $this->eventDispatcher); + $r4032login = new R4032LoginSubscriber($config, $this->currentUser, $this->requestStack, $this->pathMatcher, $this->eventDispatcher, $this->messenger); $event = new GetResponseForExceptionEvent($this->kernel, $request, HttpKernelInterface::MASTER_REQUEST, new AccessDeniedHttpException()); $dispatcher = new EventDispatcher(); $dispatcher->addListener(KernelEvents::EXCEPTION, [ @@ -253,13 +261,13 @@ 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); if ($class->isSubclassOf('Exception')) { $exceptions[] = [ - new GetResponseForExceptionEvent($kernel, new Request(), HttpKernelInterface::MASTER_REQUEST, $this->getMockBuilder($name)->disableOriginalConstructor()->getMock()), + new GetResponseForExceptionEvent($kernel, new Request(), HttpKernelInterface::MASTER_REQUEST, $this->createMock($name)->disableOriginalConstructor()->getMock()), ]; } } @@ -268,16 +276,3 @@ namespace Drupal\Tests\r4032login\Unit { } } - -namespace { - - if (!function_exists('drupal_set_message')) { - - /** - * Replaces drupal_set_message(). - */ - function drupal_set_message() { - } - - } -}