You are here

public function PageManagerRoutesTest::testAlterRoutesOverrideExisting 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::testAlterRoutesOverrideExisting()

Tests overriding an existing route.

@covers ::alterRoutes @covers ::findOverriddenRouteName

@dataProvider providerTestAlterRoutesOverrideExisting

File

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

Class

PageManagerRoutesTest
Tests the page manager route subscriber.

Namespace

Drupal\Tests\page_manager\Unit

Code

public function testAlterRoutesOverrideExisting($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)
    ->shouldBeCalled();
  $page
    ->getPath()
    ->willReturn($page_path)
    ->shouldBeCalled();
  $page
    ->getParameters()
    ->willReturn([])
    ->shouldBeCalled();
  $variant1 = $this
    ->prophesize(PageVariantInterface::class);
  $variant1
    ->getWeight()
    ->willReturn(0);
  $page
    ->getVariants()
    ->willReturn([
    'variant1' => $variant1
      ->reveal(),
  ]);
  $page
    ->id()
    ->willReturn('page1');
  $page
    ->label()
    ->willReturn(NULL);
  $page
    ->usesAdminTheme()
    ->willReturn(FALSE);
  $this->pageStorage
    ->loadMultiple()
    ->willReturn([
    'page1' => $page
      ->reveal(),
  ])
    ->shouldBeCalledTimes(1);
  $this->cacheTagsInvalidator
    ->invalidateTags([
    "page_manager_route_name:{$route_name}",
  ])
    ->shouldBeCalledTimes(1);
  $collection = new RouteCollection();
  $collection
    ->add("{$route_name}.POST", new Route($existing_route_path, [
    'default_exists' => 'default_value',
  ], $requirements, [
    'parameters' => [
      'foo' => [
        'type' => 'bar',
      ],
    ],
  ], '', [], [
    'POST',
  ]));
  $collection
    ->add("{$route_name}.POST_with_format", new Route($existing_route_path, [
    'default_exists' => 'default_value',
  ], $requirements + [
    '_format' => 'json',
  ], [
    'parameters' => [
      'foo' => [
        'type' => 'bar',
      ],
    ],
  ], '', [], [
    'GET',
    'POST',
  ]));
  $collection
    ->add($route_name, new Route($existing_route_path, [
    'default_exists' => 'default_value',
  ], $requirements, [
    'parameters' => [
      'foo' => [
        'type' => 'bar',
      ],
    ],
  ]));
  $route_event = new RouteBuildEvent($collection);
  $this->routeSubscriber
    ->onAlterRoutes($route_event);

  // The existing route name is not overridden.
  $this
    ->assertSame([
    'test_route.POST',
    'test_route.POST_with_format',
    'test_route',
    'page_manager.page_view_page1_variant1',
  ], array_keys($collection
    ->all()));
  $route = $collection
    ->get('page_manager.page_view_page1_variant1');
  $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' => 'test_route',
    'base_route_name' => 'test_route',
  ];
  $expected_requirements = $requirements + [
    '_page_access' => 'page_manager_page.view',
  ];
  $expected_options = [
    'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler',
    'parameters' => [
      'foo' => [
        'type' => 'bar',
      ],
      'page_manager_page_variant' => [
        'type' => 'entity:page_variant',
      ],
      'page_manager_page' => [
        'type' => 'entity:page',
      ],
    ],
    '_admin_route' => FALSE,
  ];
  $this
    ->assertMatchingRoute($route, $existing_route_path, $expected_defaults, $expected_requirements, $expected_options);
}