You are here

class PageManagerRoutesTest 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

Tests the page manager route subscriber.

@coversDefaultClass \Drupal\page_manager\Routing\PageManagerRoutes

@group PageManager

Hierarchy

Expanded class hierarchy of PageManagerRoutesTest

File

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

Namespace

Drupal\Tests\page_manager\Unit
View source
class PageManagerRoutesTest extends UnitTestCase {

  /**
   * The mocked entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityTypeManager;

  /**
   * The mocked page storage.
   *
   * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $pageStorage;

  /**
   * The cache tags invalidator.
   *
   * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $cacheTagsInvalidator;

  /**
   * The tested page route subscriber.
   *
   * @var \Drupal\page_manager\Routing\PageManagerRoutes
   */
  protected $routeSubscriber;

  /**
   * {@inheritdoc}
   *
   * @covers ::__construct
   */
  protected function setUp() {
    $this->pageStorage = $this
      ->prophesize(ConfigEntityStorageInterface::class);
    $this->entityTypeManager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $this->entityTypeManager
      ->getStorage('page')
      ->willReturn($this->pageStorage);
    $this->cacheTagsInvalidator = $this
      ->prophesize(CacheTagsInvalidatorInterface::class);
    $this->routeSubscriber = new PageManagerRoutes($this->entityTypeManager
      ->reveal(), $this->cacheTagsInvalidator
      ->reveal());
  }

  /**
   * Tests adding routes for enabled and disabled pages.
   *
   * @covers ::alterRoutes
   */
  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);
  }

  /**
   * Tests overriding an existing route.
   *
   * @covers ::alterRoutes
   * @covers ::findOverriddenRouteName
   *
   * @dataProvider providerTestAlterRoutesOverrideExisting
   */
  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);
  }
  public function providerTestAlterRoutesOverrideExisting() {
    $data = [];
    $data['no_slug'] = [
      '/test_route',
      '/test_route',
    ];
    $data['slug'] = [
      '/test_route/{test_route}',
      '/test_route/{test_route}',
    ];
    $data['placeholder'] = [
      '/test_route/%',
      '/test_route/{test_route}',
    ];
    $data['slug_with_default'] = [
      '/test_route/{default_exists}',
      '/test_route/{default_exists}',
    ];
    $data['placeholder_with_default'] = [
      '/test_route/%',
      '/test_route/{default_exists}',
    ];
    $data['with_requirement'] = [
      '/test_route/{foo}',
      '/test_route/{foo}',
      [
        'foo' => '\\d+',
      ],
    ];
    return $data;
  }

  /**
   * @covers ::alterRoutes
   */
  public function testAlterRoutesMultipleVariantsDifferentRequirements() {
    $variant1 = $this
      ->prophesize(PageVariantInterface::class);
    $variant2 = $this
      ->prophesize(PageVariantInterface::class);
    $variant1
      ->getWeight()
      ->willReturn(0);
    $page1 = $this
      ->prophesize(PageInterface::class);
    $page1
      ->status()
      ->willReturn(TRUE);
    $page1
      ->getVariants()
      ->willReturn([
      'variant1' => $variant1
        ->reveal(),
    ]);
    $page1
      ->getPath()
      ->willReturn('/test_route1');
    $page1
      ->getParameters()
      ->willReturn([]);
    $page1
      ->id()
      ->willReturn('page1');
    $page1
      ->label()
      ->willReturn('Page 1');
    $page1
      ->usesAdminTheme()
      ->willReturn(FALSE);
    $page2 = $this
      ->prophesize(PageInterface::class);
    $page2
      ->status()
      ->willReturn(TRUE);
    $page2
      ->getVariants()
      ->willReturn([
      'variant2' => $variant2
        ->reveal(),
    ]);
    $page2
      ->getPath()
      ->willReturn('/test_route2');
    $page2
      ->getParameters()
      ->willReturn([]);
    $page2
      ->id()
      ->willReturn('page2');
    $page2
      ->label()
      ->willReturn('Page 2');
    $page2
      ->usesAdminTheme()
      ->willReturn(FALSE);
    $this->pageStorage
      ->loadMultiple()
      ->willReturn([
      'page1' => $page1
        ->reveal(),
      'page2' => $page2
        ->reveal(),
    ]);
    $collection = new RouteCollection();
    $collection
      ->add('test_route', new Route('/test_route1', [], [
      '_access' => 'TRUE',
    ], []));
    $route_event = new RouteBuildEvent($collection);
    $this->routeSubscriber
      ->onAlterRoutes($route_event);
    $this
      ->assertSame([
      'test_route',
      'page_manager.page_view_page1_variant1',
      'page_manager.page_view_page2_variant2',
    ], array_keys($collection
      ->all()));
    $expected = [
      'test_route' => [
        'path' => '/test_route1',
        'defaults' => [],
        'requirements' => [
          '_access' => 'TRUE',
        ],
        'options' => [
          'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler',
        ],
      ],
      'page_manager.page_view_page1_variant1' => [
        'path' => '/test_route1',
        'defaults' => [
          '_entity_view' => 'page_manager_page_variant',
          '_title' => 'Page 1',
          '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',
        ],
        'requirements' => [
          '_access' => 'TRUE',
          '_page_access' => 'page_manager_page.view',
        ],
        'options' => [
          'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler',
          'parameters' => [
            'page_manager_page_variant' => [
              'type' => 'entity:page_variant',
            ],
            'page_manager_page' => [
              'type' => 'entity:page',
            ],
          ],
          '_admin_route' => FALSE,
        ],
      ],
      'page_manager.page_view_page2_variant2' => [
        'path' => '/test_route2',
        'defaults' => [
          '_entity_view' => 'page_manager_page_variant',
          '_title' => 'Page 2',
          'page_manager_page_variant' => 'variant2',
          'page_manager_page' => 'page2',
          'page_manager_page_variant_weight' => 0,
          'base_route_name' => 'page_manager.page_view_page2',
        ],
        'requirements' => [
          '_page_access' => 'page_manager_page.view',
        ],
        'options' => [
          'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler',
          'parameters' => [
            'page_manager_page_variant' => [
              'type' => 'entity:page_variant',
            ],
            'page_manager_page' => [
              'type' => 'entity:page',
            ],
          ],
          '_admin_route' => FALSE,
        ],
      ],
    ];
    foreach ($collection as $route_name => $route) {
      $this
        ->assertMatchingRoute($route, $expected[$route_name]['path'], $expected[$route_name]['defaults'], $expected[$route_name]['requirements'], $expected[$route_name]['options']);
    }
  }

  /**
   * Tests overriding an existing route with configured parameters.
   *
   * @covers ::alterRoutes
   * @covers ::findOverriddenRouteName
   *
   * @dataProvider providerTestAlterRoutesOverrideExisting
   */
  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);
  }

  /**
   * Asserts that a route object has the expected properties.
   *
   * @param \Symfony\Component\Routing\Route $route
   *   The route to test.
   * @param string $expected_path
   *   The expected path for the route.
   * @param array $expected_defaults
   *   The expected defaults for the route.
   * @param array $expected_requirements
   *   The expected requirements for the route.
   * @param array $expected_options
   *   The expected options for the route.
   */
  protected function assertMatchingRoute(Route $route, $expected_path, array $expected_defaults, array $expected_requirements, array $expected_options) {
    $this
      ->assertEquals($expected_path, $route
      ->getPath());
    $this
      ->assertEquals($expected_defaults, $route
      ->getDefaults());
    $this
      ->assertEquals($expected_requirements, $route
      ->getRequirements());
    $this
      ->assertEquals($expected_options, $route
      ->getOptions());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PageManagerRoutesTest::$cacheTagsInvalidator protected property The cache tags invalidator.
PageManagerRoutesTest::$entityTypeManager protected property The mocked entity type manager.
PageManagerRoutesTest::$pageStorage protected property The mocked page storage.
PageManagerRoutesTest::$routeSubscriber protected property The tested page route subscriber.
PageManagerRoutesTest::assertMatchingRoute protected function Asserts that a route object has the expected properties.
PageManagerRoutesTest::providerTestAlterRoutesOverrideExisting public function
PageManagerRoutesTest::setUp protected function @covers ::__construct Overrides UnitTestCase::setUp
PageManagerRoutesTest::testAlterRoutesMultipleVariantsDifferentRequirements public function @covers ::alterRoutes
PageManagerRoutesTest::testAlterRoutesOverrideExisting public function Tests overriding an existing route.
PageManagerRoutesTest::testAlterRoutesOverrideExistingWithConfiguredParameters public function Tests overriding an existing route with configured parameters.
PageManagerRoutesTest::testAlterRoutesWithStatus public function Tests adding routes for enabled and disabled pages.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.