diff --git a/src/Routing/VariantRouteFilter.php b/src/Routing/VariantRouteFilter.php index d673105..207b5a3 100644 --- a/src/Routing/VariantRouteFilter.php +++ b/src/Routing/VariantRouteFilter.php @@ -121,19 +121,13 @@ public function filter(RouteCollection $collection, Request $request) { } } - // Once again iterate over routes $collection, - // and make sure that final route object's name in collection - // matches its original base_route_name. + // Iterate over the routes, using the base_route_name if one is specified. + // All routes must be removed and re-added to the collection to ensure the + // original order is kept. foreach ($collection as $route_name => $route_item) { - if ($base_route_name = $route_item->getDefault('base_route_name')) { - $collection->remove($route_name); - $collection->add($base_route_name, $route_item); - } - else { - // Maintain the order of collection. - $collection->remove($route_name); - $collection->add($route_name, $route_item); - } + $base_route_name = $route_item->getDefault('base_route_name') ?: $route_name; + $collection->remove($route_name); + $collection->add($base_route_name, $route_item); } return $collection; diff --git a/tests/src/Unit/VariantRouteFilterTest.php b/tests/src/Unit/VariantRouteFilterTest.php index 41e41e6..91e2e0a 100644 --- a/tests/src/Unit/VariantRouteFilterTest.php +++ b/tests/src/Unit/VariantRouteFilterTest.php @@ -360,10 +360,11 @@ public function testFilterPreservingBaseRouteName() { $page_variant1 = $this->prophesize(PageVariantInterface::class); $page_variant1->access('view')->willReturn(TRUE); $page_variant2 = $this->prophesize(PageVariantInterface::class); - $page_variant2->access('view')->willReturn(TRUE); + $page_variant2->access('view')->willReturn(FALSE); $this->currentPath->getPath($request)->willReturn(''); $this->pageVariantStorage->load('variant1')->willReturn($page_variant1->reveal())->shouldBeCalled(); + $this->pageVariantStorage->load('variant2')->willReturn($page_variant2->reveal())->shouldBeCalled(); $result = $this->routeFilter->filter($route_collection, $request);