You are here

public function RoutePreloaderTest::testOnAlterRoutesWithNonAdminRoutes in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Routing/RoutePreloaderTest.php \Drupal\Tests\Core\Routing\RoutePreloaderTest::testOnAlterRoutesWithNonAdminRoutes()

Tests onAlterRoutes with admin routes and non admin routes.

File

core/tests/Drupal/Tests/Core/Routing/RoutePreloaderTest.php, line 103

Class

RoutePreloaderTest
@coversDefaultClass \Drupal\Core\Routing\RoutePreloader @group Routing

Namespace

Drupal\Tests\Core\Routing

Code

public function testOnAlterRoutesWithNonAdminRoutes() {
  $event = $this
    ->getMockBuilder('Drupal\\Core\\Routing\\RouteBuildEvent')
    ->disableOriginalConstructor()
    ->getMock();
  $route_collection = new RouteCollection();
  $route_collection
    ->add('test', new Route('/admin/foo', [
    '_controller' => 'Drupal\\ExampleController',
  ]));
  $route_collection
    ->add('test2', new Route('/bar', [
    '_controller' => 'Drupal\\ExampleController',
  ]));

  // Non content routes, like ajax callbacks should be ignored.
  $route3 = new Route('/bar', [
    '_controller' => 'Drupal\\ExampleController',
  ]);
  $route3
    ->setMethods([
    'POST',
  ]);
  $route_collection
    ->add('test3', $route3);

  // Routes with the option _admin_route set to TRUE will be included.
  $route4 = new Route('/bar', [
    '_controller' => 'Drupal\\ExampleController',
  ]);
  $route4
    ->setOption('_admin_route', TRUE);
  $route_collection
    ->add('test4', $route4);

  // Non-HTML routes, like api_json routes should be ignored.
  $route5 = new Route('/bar', [
    '_controller' => 'Drupal\\ExampleController',
  ]);
  $route5
    ->setRequirement('_format', 'api_json');
  $route_collection
    ->add('test5', $route5);

  // Routes which include HTML should be included.
  $route6 = new Route('/bar', [
    '_controller' => 'Drupal\\ExampleController',
  ]);
  $route6
    ->setRequirement('_format', 'json_api|html');
  $route_collection
    ->add('test6', $route6);
  $event
    ->expects($this
    ->once())
    ->method('getRouteCollection')
    ->will($this
    ->returnValue($route_collection));
  $this->state
    ->expects($this
    ->once())
    ->method('set')
    ->with('routing.non_admin_routes', [
    'test2',
    'test4',
    'test6',
  ]);
  $this->preloader
    ->onAlterRoutes($event);
  $this->preloader
    ->onFinishedRoutes(new Event());
}