You are here

public function ContextualLinkManagerTest::testGetContextualLinksArrayByGroup in Drupal 8

Tests the getContextualLinksArrayByGroup method.

See also

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

File

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

Class

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

Namespace

Drupal\Tests\Core\Menu

Code

public function testGetContextualLinksArrayByGroup() {
  $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',
      ],
    ],
    'test_plugin3' => [
      'id' => 'test_plugin3',
      'class' => '\\Drupal\\Core\\Menu\\ContextualLinkDefault',
      'title' => 'Plugin 3',
      'weight' => 5,
      'group' => 'group2',
      'route_name' => 'test_router3',
      'options' => [],
    ],
  ];
  $this->pluginDiscovery
    ->expects($this
    ->once())
    ->method('getDefinitions')
    ->will($this
    ->returnValue($definitions));
  $this->accessManager
    ->expects($this
    ->any())
    ->method('checkNamedRoute')
    ->will($this
    ->returnValue(AccessResult::allowed()));

  // Set up mocking of the plugin factory.
  $map = [];
  foreach ($definitions as $plugin_id => $definition) {
    $map[] = [
      $plugin_id,
      [],
      new ContextualLinkDefault([], $plugin_id, $definition),
    ];
  }
  $this->factory
    ->expects($this
    ->any())
    ->method('createInstance')
    ->will($this
    ->returnValueMap($map));
  $this->moduleHandler
    ->expects($this
    ->at(1))
    ->method('alter')
    ->with($this
    ->equalTo('contextual_links'), new Count(2), $this
    ->equalTo('group1'), $this
    ->equalTo([
    'key' => 'value',
  ]));
  $result = $this->contextualLinkManager
    ->getContextualLinksArrayByGroup('group1', [
    'key' => 'value',
  ]);
  $this
    ->assertCount(2, $result);
  foreach ([
    'test_plugin1',
    'test_plugin2',
  ] as $plugin_id) {
    $definition = $definitions[$plugin_id];
    $this
      ->assertEquals($definition['weight'], $result[$plugin_id]['weight']);
    $this
      ->assertEquals($definition['title'], $result[$plugin_id]['title']);
    $this
      ->assertEquals($definition['route_name'], $result[$plugin_id]['route_name']);
    $this
      ->assertEquals($definition['options'], $result[$plugin_id]['localized_options']);
  }
}