You are here

public function LocalTaskManagerTest::testGetTasksBuildWithCacheabilityMetadata in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/Menu/LocalTaskManagerTest.php \Drupal\Tests\Core\Menu\LocalTaskManagerTest::testGetTasksBuildWithCacheabilityMetadata()

@covers ::getTasksBuild

File

core/tests/Drupal/Tests/Core/Menu/LocalTaskManagerTest.php, line 414
Contains \Drupal\Tests\Core\Menu\LocalTaskManagerTest.

Class

LocalTaskManagerTest
@coversDefaultClass \Drupal\Core\Menu\LocalTaskManager @group Menu

Namespace

Drupal\Tests\Core\Menu

Code

public function testGetTasksBuildWithCacheabilityMetadata() {
  $definitions = $this
    ->getLocalTaskFixtures();
  $this->pluginDiscovery
    ->expects($this
    ->once())
    ->method('getDefinitions')
    ->will($this
    ->returnValue($definitions));

  // Set up some cacheablity metadata and ensure its merged together.
  $definitions['menu_local_task_test_tasks_settings']['cache_tags'] = [
    'tag.example1',
  ];
  $definitions['menu_local_task_test_tasks_settings']['cache_contexts'] = [
    'context.example1',
  ];
  $definitions['menu_local_task_test_tasks_edit']['cache_tags'] = [
    'tag.example2',
  ];
  $definitions['menu_local_task_test_tasks_edit']['cache_contexts'] = [
    'context.example2',
  ];

  // Test the cacheability metadata of access checking.
  $definitions['menu_local_task_test_tasks_view_child1']['access'] = AccessResult::allowed()
    ->addCacheContexts([
    'user.permissions',
  ]);
  $this
    ->setupFactoryAndLocalTaskPlugins($definitions, 'menu_local_task_test_tasks_view');
  $this
    ->setupLocalTaskManager();
  $this->controllerResolver
    ->expects($this
    ->any())
    ->method('getArguments')
    ->willReturn([]);
  $this->routeMatch
    ->expects($this
    ->any())
    ->method('getRouteName')
    ->willReturn('menu_local_task_test_tasks_view');
  $this->routeMatch
    ->expects($this
    ->any())
    ->method('getRawParameters')
    ->willReturn(new ParameterBag());
  $cacheability = new CacheableMetadata();
  $local_tasks = $this->manager
    ->getTasksBuild('menu_local_task_test_tasks_view', $cacheability);

  // Ensure that all cacheability metadata is merged together.
  $this
    ->assertEquals([
    'tag.example1',
    'tag.example2',
  ], $cacheability
    ->getCacheTags());
  $this
    ->assertEquals([
    'context.example1',
    'context.example2',
    'route',
    'user.permissions',
  ], $cacheability
    ->getCacheContexts());
}