You are here

public function PageManagerRoutesTest::testAlterRoutesOverrideExistingWithConfiguredParameters in Page Manager 8

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

Tests overriding an existing route with configured parameters.

@covers ::alterRoutes @covers ::findOverriddenRouteName

@dataProvider providerTestAlterRoutesOverrideExisting

File

tests/src/Unit/PageManagerRoutesTest.php, line 364
Contains \Drupal\Tests\page_manager\Unit\PageManagerRoutesTest.

Class

PageManagerRoutesTest
Tests the page manager route subscriber.

Namespace

Drupal\Tests\page_manager\Unit

Code

public function testAlterRoutesOverrideExistingWithConfiguredParameters($page_path, $existing_route_path, $requirements = []) {
  $route_name = 'test_route';

  // Set up a page with the same path as an existing route.

  /** @var \Drupal\page_manager\PageInterface|\Prophecy\Prophecy\ProphecyInterface $page */
  $page = $this
    ->prophesize(PageInterface::class);
  $page
    ->status()
    ->willReturn(TRUE);
  $page
    ->getPath()
    ->willReturn($page_path);
  $page
    ->id()
    ->willReturn('page1');
  $page
    ->label()
    ->willReturn(NULL);
  $page
    ->usesAdminTheme()
    ->willReturn(FALSE);
  $page
    ->getParameters()
    ->willReturn([
    'foo' => [
      'machine_name' => 'foo',
      'type' => 'integer',
      'label' => 'Foo',
    ],
    'test_route' => [
      'machine_name' => 'test_route',
      'type' => '',
      'label' => '',
    ],
  ]);
  $variant1 = $this
    ->prophesize(PageVariantInterface::class);
  $variant1
    ->getWeight()
    ->willReturn(0);
  $page
    ->getVariants()
    ->willReturn([
    'variant1' => $variant1
      ->reveal(),
  ]);
  $this->pageStorage
    ->loadMultiple()
    ->willReturn([
    'page1' => $page
      ->reveal(),
  ]);
  $collection = new RouteCollection();
  $collection
    ->add($route_name, new Route($existing_route_path, [
    'default_exists' => 'default_value',
  ], $requirements, [
    'parameters' => [
      'foo' => [
        'bar' => 'bar',
      ],
    ],
  ]));
  $route_event = new RouteBuildEvent($collection);
  $this->routeSubscriber
    ->onAlterRoutes($route_event);
  $expected_defaults = [
    '_entity_view' => 'page_manager_page_variant',
    '_title' => NULL,
    'page_manager_page_variant' => 'variant1',
    'page_manager_page' => 'page1',
    'page_manager_page_variant_weight' => 0,
    'overridden_route_name' => $route_name,
    'base_route_name' => 'test_route',
  ];
  $expected_requirements = $requirements + [
    '_page_access' => 'page_manager_page.view',
  ];
  $expected_options = [
    'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler',
    'parameters' => [
      'foo' => [
        'bar' => 'bar',
        'type' => 'integer',
      ],
      'page_manager_page_variant' => [
        'type' => 'entity:page_variant',
      ],
      'page_manager_page' => [
        'type' => 'entity:page',
      ],
    ],
    '_admin_route' => FALSE,
  ];
  $this
    ->assertSame([
    'test_route',
    'page_manager.page_view_page1_variant1',
  ], array_keys($collection
    ->all()));
  $this
    ->assertMatchingRoute($collection
    ->get('page_manager.page_view_page1_variant1'), $existing_route_path, $expected_defaults, $expected_requirements, $expected_options);
}