diff --git a/core/lib/Drupal/Core/Routing/RouteProvider.php b/core/lib/Drupal/Core/Routing/RouteProvider.php index ccfb070..77b9449 100644 --- a/core/lib/Drupal/Core/Routing/RouteProvider.php +++ b/core/lib/Drupal/Core/Routing/RouteProvider.php @@ -319,11 +319,12 @@ public function getRoutesByPattern($pattern) { } /** - * Get all routes which match a certain pattern. + * Get all routes which match a certain path pattern. + * + * The path will be converted to lowercase before performing the match. * * @param string $path - * The route pattern to search for (contains % as placeholders) and it will - * be converted to lowercase. + * The route path pattern to search for (contains % as placeholders). * * @return \Symfony\Component\Routing\RouteCollection * Returns a route collection of matching routes. diff --git a/core/modules/system/src/Tests/Routing/RouterTest.php b/core/modules/system/src/Tests/Routing/RouterTest.php index 277f383..0b24960 100644 --- a/core/modules/system/src/Tests/Routing/RouterTest.php +++ b/core/modules/system/src/Tests/Routing/RouterTest.php @@ -213,9 +213,12 @@ public function testRouterMatching() { /** * Tests the case insensitivity of route paths and path variable handling. + * + * The un-routed URLs are constructed using base: so that we are sure they are + * used as written without being processed by the routing system. */ public function testRoutePathMixedCase() { - $this->drupalGet('router_test/TEST14/1'); + $this->drupalGet(Url::fromUri('base:router_test/TEST14/1')); $this->assertResponse(200); $this->assertText('User route "entity.user.canonical" was matched.'); @@ -225,15 +228,15 @@ public function testRoutePathMixedCase() { // Verify that the route path is stored as lowercase despite being defined // with mixed case in the YAML file. $this->assertIdentical('/router_test/mixedcase2', $route2->getPath()); - $this->drupalGet('/router_test/mixEDCASE2'); + $this->drupalGet(Url::fromUri('base:router_test/mixEDCASE2')); $this->assertResponse(200); $this->assertRaw('test2', 'The correct string was returned because the route was successful.'); - $this->drupalGet('/router_TEST/MIXEDcase2'); + $this->drupalGet(Url::fromUri('base:router_TEST/MIXEDcase2')); $this->assertResponse(200); $this->assertRaw('test2', 'The correct string was returned because the route was successful.'); - $this->drupalGet('/router_test/mixedcase2'); + $this->drupalGet(Url::fromUri('base:router_test/mixedcase2')); $this->assertResponse(200); $this->assertRaw('test2', 'The correct string was returned because the route was successful.'); @@ -244,17 +247,33 @@ public function testRoutePathMixedCase() { // Verify that data in variable path parts is retained as the original, // mixed-case value. Matches route path /router_test/mixedcase3/{value} - $this->drupalGet('router_test/mixedcase3/mixedCASEstring'); + $this->drupalGet(Url::fromUri('base:router_test/mixedcase3/mixedCASEstring')); $this->assertResponse(200); $this->assertRaw('mixedCASEstring', 'The correct string was returned because the route was successful.'); - $this->drupalGet('router_test/mixedcase3/mixedcasestring'); + $this->drupalGet(Url::fromUri('base:router_test/mixedcase3/mixedcasestring')); $this->assertResponse(200); $this->assertRaw('mixedcasestring', 'The correct string was returned because the route was successful.'); - $this->drupalGet('router_test/mixedcase3/MIXEDCASESTRING'); + $this->drupalGet(Url::fromUri('base:router_test/mixedcase3/MIXEDCASESTRING')); $this->assertResponse(200); $this->assertRaw('MIXEDCASESTRING', 'The correct string was returned because the route was successful.'); + // Test routes added by \Drupal\router_test\RouteTestSubscriber. + // Check that a dynamically added route has path changed to lower-case. + $route4 = $route_provider->getRouteByName('router_test.mixedcase.4'); + $this->assertIdentical('/router_test/mixedcase4', $route4->getPath()); + $this->drupalGet(Url::fromUri('base:router_TEST/MixedCASE4')); + $this->assertResponse(200); + // Check that a route added dynamically and altered has path lower-cased. + $route5 = $route_provider->getRouteByName('router_test.mixedcase.5'); + $this->assertIdentical('/router_test/mixedcase5/altered', $route5->getPath()); + $this->drupalGet(Url::fromUri('base:router_test/MixedCASE5/altereD')); + $this->assertResponse(200); + // Check that a route added during the alter event has path lower-cased. + $route6 = $route_provider->getRouteByName('router_test.mixedcase.6'); + $this->assertIdentical('/router_test/mixedcase6', $route6->getPath()); + $this->drupalGet(Url::fromUri('base:router_test/MixedCASE6')); + $this->assertResponse(200); } /** diff --git a/core/modules/system/tests/modules/router_test_directory/src/RouteTestSubscriber.php b/core/modules/system/tests/modules/router_test_directory/src/RouteTestSubscriber.php index 3bc703b..72b824b 100644 --- a/core/modules/system/tests/modules/router_test_directory/src/RouteTestSubscriber.php +++ b/core/modules/system/tests/modules/router_test_directory/src/RouteTestSubscriber.php @@ -7,21 +7,82 @@ namespace Drupal\router_test; -use Drupal\Core\Routing\RouteSubscriberBase; -use Symfony\Component\Routing\RouteCollection; +use Drupal\Core\Routing\RouteBuildEvent; +use Drupal\Core\Routing\RoutingEvents; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\Routing\Route; /** * Listens to the dynamic route event and add a test route. */ -class RouteTestSubscriber extends RouteSubscriberBase { +class RouteTestSubscriber implements EventSubscriberInterface { /** * {@inheritdoc} */ - protected function alterRoutes(RouteCollection $collection) { + public static function getSubscribedEvents() { + $events[RoutingEvents::DYNAMIC] = 'onDynamicRoutes'; + $events[RoutingEvents::ALTER] = 'onAlterRoutes'; + return $events; + } + + /** + * Adds dynamic routes for a specific collection from an event. + * + * @param \Drupal\Core\Routing\RouteBuildEvent $event + * The route build event. + */ + public function onDynamicRoutes(RouteBuildEvent $event) { + $collection = $event->getRouteCollection(); + $route = new Route( + '/router_test/mixedCASE4', + [ + '_controller' => '\Drupal\router_test\TestControllers::test2', + ], + [ + '_access' => 'TRUE', + ] + ); + $collection->add("router_test.mixedcase.4", $route); + $route = new Route( + '/router_test/tobealtered', + [ + '_controller' => '\Drupal\router_test\TestControllers::test2', + ], + [ + '_access' => 'TRUE', + ] + ); + $collection->add("router_test.mixedcase.5", $route); + } + + /** + * Alters existing routes for a specific collection from an event. + * + * @param \Drupal\Core\Routing\RouteBuildEvent $event + * The route build event. + */ + public function onAlterRoutes(RouteBuildEvent $event) { + $collection = $event->getRouteCollection(); $route = $collection->get('router_test.6'); // Change controller method from test1 to test5. $route->setDefault('_controller', '\Drupal\router_test\TestControllers::test5'); + // Change a route path from lower case to mixed case. + $route = $collection->get('router_test.mixedcase.5'); + $route->setPath('/router_tesT/MIXedcase5/ALTERED'); + // We can also add routes in the alter, which happens in classes like + // \Drupal\content_translation\Routing\ContentTranslationRouteSubscriber + // that need to add routes based on other routes that are added dynamically. + $route = new Route( + '/router_test/mixedCASE6', + [ + '_controller' => '\Drupal\router_test\TestControllers::test2', + ], + [ + '_access' => 'TRUE', + ] + ); + $collection->add("router_test.mixedcase.6", $route); } }