You are here

public function PathPluginBaseTest::testAlterRouteWithAlterCallback in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/views/tests/src/Unit/Plugin/display/PathPluginBaseTest.php \Drupal\Tests\views\Unit\Plugin\display\PathPluginBaseTest::testAlterRouteWithAlterCallback()

Tests the alter route method with preexisting title callback.

File

core/modules/views/tests/src/Unit/Plugin/display/PathPluginBaseTest.php, line 343
Contains \Drupal\Tests\views\Unit\Plugin\display\PathPluginBaseTest.

Class

PathPluginBaseTest
@coversDefaultClass \Drupal\views\Plugin\views\display\PathPluginBase @group views

Namespace

Drupal\Tests\views\Unit\Plugin\display

Code

public function testAlterRouteWithAlterCallback() {
  $collection = new RouteCollection();
  $collection
    ->add('test_route', new Route('test_route', [
    '_controller' => 'Drupal\\Tests\\Core\\Controller\\TestController::content',
    '_title_callback' => '\\Drupal\\Tests\\views\\Unit\\Plugin\\display\\TestController::testTitle',
  ]));
  $route_2 = new Route('test_route/example', [
    '_controller' => 'Drupal\\Tests\\Core\\Controller\\TestController::content',
  ]);
  $collection
    ->add('test_route_2', $route_2);
  list($view) = $this
    ->setupViewExecutableAccessPlugin();
  $display = [];
  $display['display_plugin'] = 'page';
  $display['id'] = 'page_1';
  $display['display_options'] = [
    'path' => 'test_route',
  ];
  $this->pathPlugin
    ->initDisplay($view, $display);
  $view_route_names = $this->pathPlugin
    ->alterRoutes($collection);
  $this
    ->assertEquals([
    'test_id.page_1' => 'test_route',
  ], $view_route_names);

  // Ensure that the test_route is overridden.
  $route = $collection
    ->get('test_route');
  $this
    ->assertInstanceOf(Route::class, $route);
  $this
    ->assertEquals('test_id', $route
    ->getDefault('view_id'));
  $this
    ->assertEquals('\\Drupal\\Tests\\views\\Unit\\Plugin\\display\\TestController::testTitle', $route
    ->getDefault('_title_callback'));
  $this
    ->assertEquals('page_1', $route
    ->getDefault('display_id'));

  // Ensure that the test_route_2 is not overridden.
  $route = $collection
    ->get('test_route_2');
  $this
    ->assertInstanceOf(Route::class, $route);
  $this
    ->assertFalse($route
    ->hasDefault('view_id'));
  $this
    ->assertFalse($route
    ->hasDefault('display_id'));
  $this
    ->assertSame($collection
    ->get('test_route_2'), $route_2);
}