You are here

public function ContextualLinkManagerTest::testGetContextualLinksArrayByGroup in Zircon Profile 8

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

Tests the getContextualLinksArrayByGroup method.

See also

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

File

core/tests/Drupal/Tests/Core/Menu/ContextualLinkManagerTest.php, line 244
Contains \Drupal\Tests\Core\Menu\ContextualLinkManagerTest.

Class

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

Namespace

Drupal\Tests\Core\Menu

Code

public function testGetContextualLinksArrayByGroup() {
  $definitions = array(
    'test_plugin1' => array(
      'id' => 'test_plugin1',
      'class' => '\\Drupal\\Core\\Menu\\ContextualLinkDefault',
      'title' => 'Plugin 1',
      'weight' => 0,
      'group' => 'group1',
      'route_name' => 'test_route',
      'options' => array(),
    ),
    'test_plugin2' => array(
      'id' => 'test_plugin2',
      'class' => '\\Drupal\\Core\\Menu\\ContextualLinkDefault',
      'title' => 'Plugin 2',
      'weight' => 2,
      'group' => 'group1',
      'route_name' => 'test_route2',
      'options' => array(
        'key' => 'value',
      ),
    ),
    'test_plugin3' => array(
      'id' => 'test_plugin3',
      'class' => '\\Drupal\\Core\\Menu\\ContextualLinkDefault',
      'title' => 'Plugin 3',
      'weight' => 5,
      'group' => 'group2',
      'route_name' => 'test_router3',
      'options' => array(),
    ),
  );
  $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 = array();
  foreach ($definitions as $plugin_id => $definition) {
    $plugin = $this
      ->getMock('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[] = array(
      $plugin_id,
      array(),
      $plugin,
    );
  }
  $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 \PHPUnit_Framework_Constraint_Count(2), $this
    ->equalTo('group1'), $this
    ->equalTo(array(
    'key' => 'value',
  )));
  $result = $this->contextualLinkManager
    ->getContextualLinksArrayByGroup('group1', array(
    'key' => 'value',
  ));
  $this
    ->assertCount(2, $result);
  foreach (array(
    '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']);
  }
}