You are here

public function VariantRouteFilterTest::testFilterAllowedAccessFirstRoute in Page Manager 8.4

Same name and namespace in other branches
  1. 8 tests/src/Unit/VariantRouteFilterTest.php \Drupal\Tests\page_manager\Unit\VariantRouteFilterTest::testFilterAllowedAccessFirstRoute()

@covers ::filter @covers ::getVariantRouteName @covers ::sortRoutes

Tests when the first page_manager route is allowed, but other non-page_manager routes are also present.

File

tests/src/Unit/VariantRouteFilterTest.php, line 273

Class

VariantRouteFilterTest
@coversDefaultClass \Drupal\page_manager\Routing\VariantRouteFilter @group PageManager

Namespace

Drupal\Tests\page_manager\Unit

Code

public function testFilterAllowedAccessFirstRoute() {
  $route_collection = new RouteCollection();
  $request = new Request();

  // The selected route specifies a different base route.
  $defaults = [
    'page_manager_page_variant' => 'variant1',
    'page_manager_page_variant_weight' => -2,
    'overridden_route_name' => 'route_1',
  ];
  $route1 = new Route('/path/with/{slug}');
  $route2 = new Route('/path/with/{slug}', $defaults);
  $route3 = new Route('/path/with/{slug}', [
    'page_manager_page_variant' => 'variant2',
    'page_manager_page_variant_weight' => -1,
  ]);
  $route4 = new Route('/path/with/{slug}');

  // Add routes in different order to test sorting.
  $route_collection
    ->add('route_3', $route3);
  $route_collection
    ->add('route_2', $route2);
  $route_collection
    ->add('route_1', $route1);
  $route_collection
    ->add('route_4', $route4);
  $page_variant1 = $this
    ->prophesize(PageVariantInterface::class);
  $page_variant1
    ->access('view')
    ->willReturn(TRUE);
  $page_variant2 = $this
    ->prophesize(PageVariantInterface::class);
  $page_variant2
    ->access('view')
    ->willReturn(FALSE);
  $this->currentPath
    ->getPath($request)
    ->willReturn('');
  $this->pageVariantStorage
    ->load('variant1')
    ->willReturn($page_variant1
    ->reveal())
    ->shouldBeCalled();
  $result = $this->routeFilter
    ->filter($route_collection, $request);
  $expected = [
    'route_1' => $route2,
    'route_4' => $route4,
  ];
  $this
    ->assertSame($expected, $result
    ->all());
  $expected_attributes = $defaults + [
    '_route_object' => $route2,
    '_route' => 'route_1',
  ];
  $this
    ->assertSame($expected_attributes, $request->attributes
    ->all());
}