You are here

public function VariantRouteFilterTest::testFilterAllowedAccessSecondRoute in Page Manager 8

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

@covers ::filter @covers ::getVariantRouteName

File

tests/src/Unit/VariantRouteFilterTest.php, line 236
Contains \Drupal\Tests\page_manager\Unit\VariantRouteFilterTest.

Class

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

Namespace

Drupal\Tests\page_manager\Unit

Code

public function testFilterAllowedAccessSecondRoute() {
  $route_collection = new RouteCollection();
  $request = new Request();
  $route1 = new Route('/path/with/{slug}', [
    'page_manager_page_variant' => 'variant_1',
    'page_manager_page_variant_weight' => 1,
  ]);
  $defaults = [
    'page_manager_page_variant' => 'variant_2',
    'page_manager_page_variant_weight' => 2,
    'overridden_route_name' => 'overridden_route_name_for_selected_route',
  ];
  $route2 = new Route('/path/with/{slug}', $defaults);

  // Add route2 first to ensure that the routes get sorted by weight.
  $route_collection
    ->add('route_2', $route2);
  $route_collection
    ->add('route_1', $route1);
  $page_variant1 = $this
    ->prophesize(PageVariantInterface::class);
  $page_variant1
    ->access('view')
    ->willReturn(FALSE);
  $page_variant2 = $this
    ->prophesize(PageVariantInterface::class);
  $page_variant2
    ->access('view')
    ->willReturn(TRUE);
  $this->currentPath
    ->getPath($request)
    ->willReturn('');
  $this->pageVariantStorage
    ->load('variant_1')
    ->willReturn($page_variant1
    ->reveal())
    ->shouldBeCalled();
  $this->pageVariantStorage
    ->load('variant_2')
    ->willReturn($page_variant2
    ->reveal())
    ->shouldBeCalled();
  $result = $this->routeFilter
    ->filter($route_collection, $request);
  $expected = [
    'overridden_route_name_for_selected_route' => $route2,
  ];
  $this
    ->assertSame($expected, $result
    ->all());
  $expected_attributes = $defaults + [
    '_route_object' => $route2,
    '_route' => 'overridden_route_name_for_selected_route',
  ];
  $this
    ->assertSame($expected_attributes, $request->attributes
    ->all());
}