You are here

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

Tests adding routes for enabled and disabled pages.

@covers ::alterRoutes

File

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

Class

PageManagerRoutesTest
Tests the page manager route subscriber.

Namespace

Drupal\Tests\page_manager\Unit

Code

public function testAlterRoutesWithStatus() {

  // Set up a valid page.

  /** @var \Drupal\page_manager\PageInterface|\Prophecy\Prophecy\ProphecyInterface $page1 */
  $page1 = $this
    ->prophesize(PageInterface::class);
  $page1
    ->status()
    ->willReturn(TRUE)
    ->shouldBeCalled();
  $page1
    ->getPath()
    ->willReturn('/page1')
    ->shouldBeCalled();
  $page1
    ->id()
    ->willReturn('page1');
  $variant1 = $this
    ->prophesize(PageVariantInterface::class);
  $variant1
    ->getWeight()
    ->willReturn(0);
  $page1
    ->getVariants()
    ->willReturn([
    'variant1' => $variant1
      ->reveal(),
  ]);
  $page1
    ->label()
    ->willReturn('Page label')
    ->shouldBeCalled();
  $page1
    ->usesAdminTheme()
    ->willReturn(TRUE)
    ->shouldBeCalled();
  $page1
    ->getParameters()
    ->willReturn([])
    ->shouldBeCalled();
  $pages['page1'] = $page1
    ->reveal();

  // Set up a disabled page.

  /** @var \Drupal\page_manager\PageInterface|\Prophecy\Prophecy\ProphecyInterface $page2 */
  $page2 = $this
    ->prophesize(PageInterface::class);
  $page2
    ->status()
    ->willReturn(FALSE)
    ->shouldBeCalled();
  $page2
    ->getVariants()
    ->willReturn([
    'variant2' => 'variant2',
  ]);
  $page2
    ->id()
    ->willReturn('page1');
  $page2
    ->getPath()
    ->willReturn('/page2');
  $page2
    ->getParameters()
    ->willReturn([]);
  $pages['page2'] = $page2
    ->reveal();
  $this->pageStorage
    ->loadMultiple()
    ->willReturn($pages)
    ->shouldBeCalledTimes(1);
  $collection = new RouteCollection();
  $route_event = new RouteBuildEvent($collection);
  $this->routeSubscriber
    ->onAlterRoutes($route_event);

  // Only the valid page should be in the collection.
  $this
    ->assertSame([
    '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' => 'Page label',
    'page_manager_page_variant' => 'variant1',
    'page_manager_page' => 'page1',
    'page_manager_page_variant_weight' => 0,
    'base_route_name' => 'page_manager.page_view_page1',
  ];
  $expected_requirements = [
    '_page_access' => 'page_manager_page.view',
  ];
  $expected_options = [
    'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler',
    'parameters' => [
      'page_manager_page_variant' => [
        'type' => 'entity:page_variant',
      ],
      'page_manager_page' => [
        'type' => 'entity:page',
      ],
    ],
    '_admin_route' => TRUE,
  ];
  $this
    ->assertMatchingRoute($route, '/page1', $expected_defaults, $expected_requirements, $expected_options);
}