function ContextualLinkManagerTest::testGetContextualLinksArrayByGroup

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Menu/ContextualLinkManagerTest.php \Drupal\Tests\Core\Menu\ContextualLinkManagerTest::testGetContextualLinksArrayByGroup()
  2. 8.9.x core/tests/Drupal/Tests/Core/Menu/ContextualLinkManagerTest.php \Drupal\Tests\Core\Menu\ContextualLinkManagerTest::testGetContextualLinksArrayByGroup()
  3. 11.x 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 207

Class

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

Namespace

Drupal\Tests\Core\Menu

Code

public function testGetContextualLinksArrayByGroup() : void {
    $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')
        ->willReturn($definitions);
    $this->accessManager
        ->expects($this->any())
        ->method('checkNamedRoute')
        ->willReturn(AccessResult::allowed());
    $this->moduleHandler
        ->alter('contextual_links_plugins', Argument::cetera())
        ->shouldBeCalledOnce();
    $this->moduleHandler
        ->alter('contextual_links', Argument::size(2), 'group1', [
        'key' => 'value',
    ])
        ->shouldBeCalledOnce();
    $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']);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.