You are here

public function ContextualLinkManagerTest::testGetContextualLinksArrayByGroupAccessCheck 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::testGetContextualLinksArrayByGroupAccessCheck()

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 324
Contains \Drupal\Tests\Core\Menu\ContextualLinkManagerTest.

Class

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

Namespace

Drupal\Tests\Core\Menu

Code

public function testGetContextualLinksArrayByGroupAccessCheck() {
  $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',
      ),
    ),
  );
  $this->pluginDiscovery
    ->expects($this
    ->once())
    ->method('getDefinitions')
    ->will($this
    ->returnValue($definitions));
  $this->accessManager
    ->expects($this
    ->any())
    ->method('checkNamedRoute')
    ->will($this
    ->returnValueMap(array(
    array(
      'test_route',
      array(
        'key' => 'value',
      ),
      $this->account,
      FALSE,
      TRUE,
    ),
    array(
      'test_route2',
      array(
        'key' => 'value',
      ),
      $this->account,
      FALSE,
      FALSE,
    ),
  )));

  // 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));
  $result = $this->contextualLinkManager
    ->getContextualLinksArrayByGroup('group1', array(
    'key' => 'value',
  ));

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