Issue #3359649 by arnested, shalini_jha, alexpott, arunkumark, cilefen, smustgrave, aduthois, catch, quietone: User routes alter in custom module throwing error on "_format"

(cherry picked from commit a6a4006816)
merge-requests/9004/merge
Alex Pott 2024-10-07 14:11:03 +01:00
parent ffecc0bccf
commit 19853cff8e
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
5 changed files with 92 additions and 1 deletions

View File

@ -51,7 +51,7 @@ class UserRouteAlterSubscriber implements EventSubscriberInterface {
]; ];
$routes = $event->getRouteCollection(); $routes = $event->getRouteCollection();
foreach ($route_names as $route_name) { foreach ($route_names as $route_name) {
if ($route = $routes->get($route_name)) { if (($route = $routes->get($route_name)) && $route->hasRequirement('_format')) {
$formats = explode('|', $route->getRequirement('_format')); $formats = explode('|', $route->getRequirement('_format'));
$formats = array_unique(array_merge($formats, $this->serializerFormats)); $formats = array_unique(array_merge($formats, $this->serializerFormats));
$route->setRequirement('_format', implode('|', $formats)); $route->setRequirement('_format', implode('|', $formats));

View File

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace Drupal\user_route_alter_test\Routing;
use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\Core\Routing\RoutingEvents;
use Symfony\Component\Routing\RouteCollection;
/**
* Alter the 'user.pass.http' route.
*/
class RouteSubscriber extends RouteSubscriberBase {
/**
* {@inheritdoc}
*/
protected function alterRoutes(RouteCollection $collection): void {
if ($route = $collection->get('user.pass.http')) {
$route->setRequirements([]);
$route->setRequirement('_access', 'FALSE');
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
$events = parent::getSubscribedEvents();
// Ensure this event is triggered before
// \Drupal\serialization\EventSubscriber\UserRouteAlterSubscriber.
$events[RoutingEvents::ALTER] = ['onAlterRoutes', 1];
return $events;
}
}

View File

@ -0,0 +1,4 @@
name: 'User route alter test support'
description: 'Provides test support for user route alter tests.'
type: module
version: VERSION

View File

@ -0,0 +1,4 @@
services:
_defaults:
autoconfigure: true
Drupal\user_route_alter_test\Routing\RouteSubscriber: ~

View File

@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\serialization\Kernel;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests that the user routes can be altered.
*
* @group serialization
*/
class UserRouteAlterTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'serialization',
'user',
'user_route_alter_test',
];
/**
* Tests the altered 'user.login.http' route.
*/
public function testUserAlteredRoute(): void {
/** @var \Drupal\Core\Routing\RouteProviderInterface $route_provider */
$route_provider = $this->container->get('router.route_provider');
// Ensure '_format' is set for the 'user.login.http' route.
$requirements = $route_provider->getRouteByName('user.login.http')->getRequirements();
$this->assertArrayHasKey('_format', $requirements, 'user.login.http route has "_format" requirement');
$this->assertEquals('json|xml', $requirements['_format'], 'user.login.http route "_format" requirement is "json|xml"');
// Ensure the '_access' requirement is set to FALSE for the 'user.pass.http'
// route.
$requirements = $route_provider->getRouteByName('user.pass.http')->getRequirements();
$this->assertArrayHasKey('_access', $requirements, 'user.pass.http route has "_access" requirement');
$this->assertEquals('FALSE', $requirements['_access'], 'user.pass.http route "_access" requirement is "FALSE"');
// Ensure '_format' is not set for the 'user.pass.http' route.
$this->assertArrayNotHasKey('_format', $requirements, 'user.pass.http route does not have "_format" requirement');
}
}