You are here

public function ViewsLocalTaskTest::testGetDerivativeDefinitionsWithExistingLocalTask in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/views/tests/src/Unit/Plugin/Derivative/ViewsLocalTaskTest.php \Drupal\Tests\views\Unit\Plugin\Derivative\ViewsLocalTaskTest::testGetDerivativeDefinitionsWithExistingLocalTask()
  2. 10 core/modules/views/tests/src/Unit/Plugin/Derivative/ViewsLocalTaskTest.php \Drupal\Tests\views\Unit\Plugin\Derivative\ViewsLocalTaskTest::testGetDerivativeDefinitionsWithExistingLocalTask()

Tests fetching the derivatives on a view with a local task and a parent.

The parent is defined by another module, not views.

File

core/modules/views/tests/src/Unit/Plugin/Derivative/ViewsLocalTaskTest.php, line 286
Contains \Drupal\Tests\views\Unit\Plugin\Derivative\ViewsLocalTaskTest.

Class

ViewsLocalTaskTest
@coversDefaultClass \Drupal\views\Plugin\Derivative\ViewsLocalTask @group views

Namespace

Drupal\Tests\views\Unit\Plugin\Derivative

Code

public function testGetDerivativeDefinitionsWithExistingLocalTask() {
  $executable = $this
    ->getMockBuilder('Drupal\\views\\ViewExecutable')
    ->disableOriginalConstructor()
    ->getMock();
  $storage = $this
    ->getMockBuilder('Drupal\\views\\Entity\\View')
    ->disableOriginalConstructor()
    ->getMock();
  $storage
    ->expects($this
    ->any())
    ->method('id')
    ->will($this
    ->returnValue('example_view'));
  $storage
    ->expects($this
    ->any())
    ->method('getExecutable')
    ->willReturn($executable);
  $executable->storage = $storage;
  $this->viewStorage
    ->expects($this
    ->any())
    ->method('load')
    ->with('example_view')
    ->willReturn($storage);
  $display_plugin = $this
    ->getMockBuilder('Drupal\\views\\Plugin\\views\\display\\PathPluginBase')
    ->setMethods([
    'getOption',
    'getPath',
  ])
    ->disableOriginalConstructor()
    ->getMockForAbstractClass();
  $display_plugin
    ->expects($this
    ->exactly(2))
    ->method('getOption')
    ->with('menu')
    ->will($this
    ->returnValue([
    'type' => 'tab',
    'weight' => 12,
    'title' => 'Example title',
  ]));
  $display_plugin
    ->expects($this
    ->once())
    ->method('getPath')
    ->will($this
    ->returnValue('path/example'));
  $executable->display_handler = $display_plugin;
  $result = [
    [
      'example_view',
      'page_1',
    ],
  ];
  $this->localTaskDerivative
    ->setApplicableMenuViews($result);

  // Mock the view route names state.
  $view_route_names = [];
  $view_route_names['example_view.page_1'] = 'view.example_view.page_1';
  $this->state
    ->expects($this
    ->exactly(2))
    ->method('get')
    ->with('views.view_route_names')
    ->will($this
    ->returnValue($view_route_names));

  // Mock the route provider.
  $route_collection = new RouteCollection();
  $route_collection
    ->add('test_route', new Route('/path'));
  $this->routeProvider
    ->expects($this
    ->any())
    ->method('getRoutesByPattern')
    ->with('/path')
    ->will($this
    ->returnValue($route_collection));

  // Setup the existing local task of the test_route.
  $definitions['test_route_tab'] = $other_tab = [
    'route_name' => 'test_route',
    'title' => 'Test route',
    'base_route' => 'test_route',
  ];
  $definitions += $this->localTaskDerivative
    ->getDerivativeDefinitions($this->baseDefinition);

  // Setup the prefix of the derivative.
  $definitions['views_view:view.example_view.page_1'] = $definitions['view.example_view.page_1'];
  unset($definitions['view.example_view.page_1']);
  $this->localTaskDerivative
    ->alterLocalTasks($definitions);
  $plugin = $definitions['views_view:view.example_view.page_1'];
  $this
    ->assertCount(2, $definitions);

  // Ensure the other local task was not changed.
  $this
    ->assertEquals($other_tab, $definitions['test_route_tab']);
  $this
    ->assertEquals('view.example_view.page_1', $plugin['route_name']);
  $this
    ->assertEquals(12, $plugin['weight']);
  $this
    ->assertEquals('Example title', $plugin['title']);
  $this
    ->assertEquals($this->baseDefinition['class'], $plugin['class']);
  $this
    ->assertEquals('test_route', $plugin['base_route']);
}