class ContextualLinkManagerTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Menu/ContextualLinkManagerTest.php \Drupal\Tests\Core\Menu\ContextualLinkManagerTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Menu/ContextualLinkManagerTest.php \Drupal\Tests\Core\Menu\ContextualLinkManagerTest
  3. 11.x core/tests/Drupal/Tests/Core/Menu/ContextualLinkManagerTest.php \Drupal\Tests\Core\Menu\ContextualLinkManagerTest

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

Hierarchy

Expanded class hierarchy of ContextualLinkManagerTest

File

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

Namespace

Drupal\Tests\Core\Menu
View source
class ContextualLinkManagerTest extends UnitTestCase {
    
    /**
     * The tested contextual link manager.
     *
     * @var \Drupal\Core\Menu\ContextualLinkManager
     */
    protected $contextualLinkManager;
    
    /**
     * The mocked plugin discovery.
     *
     * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $pluginDiscovery;
    
    /**
     * The cache backend used in the test.
     *
     * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $cacheBackend;
    
    /**
     * The mocked module handler.
     *
     * @var \Drupal\Core\Extension\ModuleHandlerInterface|\Prophecy\Prophecy\ObjectProphecy
     */
    protected $moduleHandler;
    
    /**
     * The mocked access manager.
     *
     * @var \Drupal\Core\Access\AccessManagerInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $accessManager;
    
    /**
     * The mocked account.
     *
     * @var \Drupal\Core\Session\AccountInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $account;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $language_manager = $this->createMock(LanguageManagerInterface::class);
        $language_manager->expects($this->any())
            ->method('getCurrentLanguage')
            ->willReturn(new Language([
            'id' => 'en',
        ]));
        $this->moduleHandler = $this->prophesize(ModuleHandlerInterface::class);
        $this->pluginDiscovery = $this->createMock('Drupal\\Component\\Plugin\\Discovery\\DiscoveryInterface');
        $this->cacheBackend = $this->createMock('Drupal\\Core\\Cache\\CacheBackendInterface');
        $this->accessManager = $this->createMock('Drupal\\Core\\Access\\AccessManagerInterface');
        $this->account = $this->createMock('Drupal\\Core\\Session\\AccountInterface');
        $this->contextualLinkManager = new ContextualLinkManager($this->createMock(ControllerResolverInterface::class), $this->moduleHandler
            ->reveal(), $this->cacheBackend, $language_manager, $this->accessManager, $this->account, new RequestStack());
        $property = new \ReflectionProperty('Drupal\\Core\\Menu\\ContextualLinkManager', 'discovery');
        $property->setValue($this->contextualLinkManager, $this->pluginDiscovery);
    }
    
    /**
     * Tests the getContextualLinkPluginsByGroup method.
     *
     * @see \Drupal\Core\Menu\ContextualLinkManager::getContextualLinkPluginsByGroup()
     */
    public function testGetContextualLinkPluginsByGroup() : void {
        $definitions = [
            'test_plugin1' => [
                'id' => 'test_plugin1',
                'class' => '\\Drupal\\Core\\Menu\\ContextualLinkDefault',
                'group' => 'group1',
                'route_name' => 'test_route',
            ],
            'test_plugin2' => [
                'id' => 'test_plugin2',
                'class' => '\\Drupal\\Core\\Menu\\ContextualLinkDefault',
                'group' => 'group1',
                'route_name' => 'test_route2',
            ],
            'test_plugin3' => [
                'id' => 'test_plugin3',
                'class' => '\\Drupal\\Core\\Menu\\ContextualLinkDefault',
                'group' => 'group2',
                'route_name' => 'test_router3',
            ],
        ];
        $this->pluginDiscovery
            ->expects($this->once())
            ->method('getDefinitions')
            ->willReturn($definitions);
        // Test with a non existing group.
        $result = $this->contextualLinkManager
            ->getContextualLinkPluginsByGroup('group_non_existing');
        $this->assertEmpty($result);
        $result = $this->contextualLinkManager
            ->getContextualLinkPluginsByGroup('group1');
        $this->assertEquals([
            'test_plugin1',
            'test_plugin2',
        ], array_keys($result));
        $result = $this->contextualLinkManager
            ->getContextualLinkPluginsByGroup('group2');
        $this->assertEquals([
            'test_plugin3',
        ], array_keys($result));
    }
    
    /**
     * Tests the getContextualLinkPluginsByGroup method with a prefilled cache.
     */
    public function testGetContextualLinkPluginsByGroupWithCache() : void {
        $definitions = [
            'test_plugin1' => [
                'id' => 'test_plugin1',
                'class' => '\\Drupal\\Core\\Menu\\ContextualLinkDefault',
                'group' => 'group1',
                'route_name' => 'test_route',
            ],
            'test_plugin2' => [
                'id' => 'test_plugin2',
                'class' => '\\Drupal\\Core\\Menu\\ContextualLinkDefault',
                'group' => 'group1',
                'route_name' => 'test_route2',
            ],
        ];
        $this->cacheBackend
            ->expects($this->once())
            ->method('get')
            ->with('contextual_links_plugins:en:group1')
            ->willReturn((object) [
            'data' => $definitions,
        ]);
        $result = $this->contextualLinkManager
            ->getContextualLinkPluginsByGroup('group1');
        $this->assertEquals($definitions, $result);
        // Ensure that the static cache works, so no second cache get is executed.
        $result = $this->contextualLinkManager
            ->getContextualLinkPluginsByGroup('group1');
        $this->assertEquals($definitions, $result);
    }
    
    /**
     * Tests processDefinition() by passing a plugin definition without a route.
     *
     * @see \Drupal\Core\Menu\ContextualLinkManager::processDefinition()
     */
    public function testProcessDefinitionWithoutRoute() : void {
        $definition = [
            'class' => '\\Drupal\\Core\\Menu\\ContextualLinkDefault',
            'group' => 'example',
            'id' => 'test_plugin',
        ];
        $this->expectException(PluginException::class);
        $this->contextualLinkManager
            ->processDefinition($definition, 'test_plugin');
    }
    
    /**
     * Tests processDefinition() by passing a plugin definition without a group.
     *
     * @see \Drupal\Core\Menu\ContextualLinkManager::processDefinition()
     */
    public function testProcessDefinitionWithoutGroup() : void {
        $definition = [
            'class' => '\\Drupal\\Core\\Menu\\ContextualLinkDefault',
            'route_name' => 'example',
            'id' => 'test_plugin',
        ];
        $this->expectException(PluginException::class);
        $this->contextualLinkManager
            ->processDefinition($definition, 'test_plugin');
    }
    
    /**
     * Tests the getContextualLinksArrayByGroup method.
     *
     * @see \Drupal\Core\Menu\ContextualLinkManager::getContextualLinksArrayByGroup()
     */
    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']);
        }
    }
    
    /**
     * Tests the access checking of the getContextualLinksArrayByGroup method.
     *
     * @see \Drupal\Core\Menu\ContextualLinkManager::getContextualLinksArrayByGroup()
     */
    public function testGetContextualLinksArrayByGroupAccessCheck() : 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',
                ],
            ],
        ];
        $this->pluginDiscovery
            ->expects($this->once())
            ->method('getDefinitions')
            ->willReturn($definitions);
        $this->accessManager
            ->expects($this->any())
            ->method('checkNamedRoute')
            ->willReturnMap([
            [
                'test_route',
                [
                    'key' => 'value',
                ],
                $this->account,
                FALSE,
                TRUE,
            ],
            [
                'test_route2',
                [
                    'key' => 'value',
                ],
                $this->account,
                FALSE,
                FALSE,
            ],
        ]);
        $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']));
    }
    
    /**
     * Tests the plugins alter hook.
     */
    public function testPluginDefinitionAlter() : void {
        $definitions['test_plugin'] = [
            'id' => 'test_plugin',
            'class' => ContextualLinkDefault::class,
            'title' => 'Plugin',
            'weight' => 2,
            'group' => 'group1',
            'route_name' => 'test_route',
            'options' => [
                'key' => 'value',
            ],
        ];
        $this->pluginDiscovery
            ->expects($this->once())
            ->method('getDefinitions')
            ->willReturn($definitions);
        $this->moduleHandler
            ->alter('contextual_links_plugins', $definitions)
            ->shouldBeCalledOnce();
        $this->contextualLinkManager
            ->getDefinition('test_plugin');
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
ContextualLinkManagerTest::$accessManager protected property The mocked access manager.
ContextualLinkManagerTest::$account protected property The mocked account.
ContextualLinkManagerTest::$cacheBackend protected property The cache backend used in the test.
ContextualLinkManagerTest::$contextualLinkManager protected property The tested contextual link manager.
ContextualLinkManagerTest::$moduleHandler protected property The mocked module handler.
ContextualLinkManagerTest::$pluginDiscovery protected property The mocked plugin discovery.
ContextualLinkManagerTest::setUp protected function Overrides UnitTestCase::setUp
ContextualLinkManagerTest::testGetContextualLinkPluginsByGroup public function Tests the getContextualLinkPluginsByGroup method.
ContextualLinkManagerTest::testGetContextualLinkPluginsByGroupWithCache public function Tests the getContextualLinkPluginsByGroup method with a prefilled cache.
ContextualLinkManagerTest::testGetContextualLinksArrayByGroup public function Tests the getContextualLinksArrayByGroup method.
ContextualLinkManagerTest::testGetContextualLinksArrayByGroupAccessCheck public function Tests the access checking of the getContextualLinksArrayByGroup method.
ContextualLinkManagerTest::testPluginDefinitionAlter public function Tests the plugins alter hook.
ContextualLinkManagerTest::testProcessDefinitionWithoutGroup public function Tests processDefinition() by passing a plugin definition without a group.
ContextualLinkManagerTest::testProcessDefinitionWithoutRoute public function Tests processDefinition() by passing a plugin definition without a route.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function

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