You are here

public function ContextualLinkManagerTest::testGetContextualLinksArrayByGroupAccessCheck in Drupal 8

Tests the access checking of the getContextualLinksArrayByGroup method.

See also

\Drupal\Core\Menu\ContextualLinkManager::getContextualLinksArrayByGroup()

File

core/tests/Drupal/Tests/Core/Menu/ContextualLinkManagerTest.php, line 308

Class

ContextualLinkManagerTest
@coversDefaultClass \Drupal\Core\Menu\ContextualLinkManager @group Menu

Namespace

Drupal\Tests\Core\Menu

Code

public function testGetContextualLinksArrayByGroupAccessCheck() {
  $definitions = [
    'test_plugin1' => [
      'id' => 'test_plugin1',
      'class' => '\\Drupal\\Core\\Menu\\ContextualLinkDefault',
      'title' => 'Plugin 1',
      'weight' => 0,
      'group' => 'group1',
      'route_name' => 'test_route',
      'options' => [],
    ],
    'test_plugin2' => [
      'id' => 'test_plugin2',
      'class' => '\\Drupal\\Core\\Menu\\ContextualLinkDefault',
      'title' => 'Plugin 2',
      'weight' => 2,
      'group' => 'group1',
      'route_name' => 'test_route2',
      'options' => [
        'key' => 'value',
      ],
    ],
  ];
  $this->pluginDiscovery
    ->expects($this
    ->once())
    ->method('getDefinitions')
    ->will($this
    ->returnValue($definitions));
  $this->accessManager
    ->expects($this
    ->any())
    ->method('checkNamedRoute')
    ->will($this
    ->returnValueMap([
    [
      'test_route',
      [
        'key' => 'value',
      ],
      $this->account,
      FALSE,
      TRUE,
    ],
    [
      'test_route2',
      [
        'key' => 'value',
      ],
      $this->account,
      FALSE,
      FALSE,
    ],
  ]));

  // Set up mocking of the plugin factory.
  $map = [];
  foreach ($definitions as $plugin_id => $definition) {
    $plugin = $this
      ->createMock('Drupal\\Core\\Menu\\ContextualLinkInterface');
    $plugin
      ->expects($this
      ->any())
      ->method('getRouteName')
      ->will($this
      ->returnValue($definition['route_name']));
    $plugin
      ->expects($this
      ->any())
      ->method('getTitle')
      ->will($this
      ->returnValue($definition['title']));
    $plugin
      ->expects($this
      ->any())
      ->method('getWeight')
      ->will($this
      ->returnValue($definition['weight']));
    $plugin
      ->expects($this
      ->any())
      ->method('getOptions')
      ->will($this
      ->returnValue($definition['options']));
    $map[] = [
      $plugin_id,
      [],
      $plugin,
    ];
  }
  $this->factory
    ->expects($this
    ->any())
    ->method('createInstance')
    ->will($this
    ->returnValueMap($map));
  $result = $this->contextualLinkManager
    ->getContextualLinksArrayByGroup('group1', [
    'key' => 'value',
  ]);

  // Ensure that access checking was respected.
  $this
    ->assertTrue(isset($result['test_plugin1']));
  $this
    ->assertFalse(isset($result['test_plugin2']));
}