Unverified Commit 3b606077 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3359649 by arnested, shalini_jha, alexpott, arunkumark, cilefen,...

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 a6a40068)
parent 138d5889
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -51,7 +51,7 @@ public function onRoutingAlterAddFormats(RouteBuildEvent $event) {
    ];
    $routes = $event->getRouteCollection();
    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 = array_unique(array_merge($formats, $this->serializerFormats));
        $route->setRequirement('_format', implode('|', $formats));
+37 −0
Original line number Diff line number Diff line
<?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;
  }

}
+4 −0
Original line number Diff line number Diff line
name: 'User route alter test support'
description: 'Provides test support for user route alter tests.'
type: module
version: VERSION
+4 −0
Original line number Diff line number Diff line
services:
  _defaults:
    autoconfigure: true
  Drupal\user_route_alter_test\Routing\RouteSubscriber: ~
+46 −0
Original line number Diff line number Diff line
<?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');
  }

}