function FilteredPluginManagerTraitTest::testGetFilteredDefinitions
Same name in other branches
- 9 core/tests/Drupal/Tests/Core/Plugin/FilteredPluginManagerTraitTest.php \Drupal\Tests\Core\Plugin\FilteredPluginManagerTraitTest::testGetFilteredDefinitions()
- 8.9.x core/tests/Drupal/Tests/Core/Plugin/FilteredPluginManagerTraitTest.php \Drupal\Tests\Core\Plugin\FilteredPluginManagerTraitTest::testGetFilteredDefinitions()
- 11.x core/tests/Drupal/Tests/Core/Plugin/FilteredPluginManagerTraitTest.php \Drupal\Tests\Core\Plugin\FilteredPluginManagerTraitTest::testGetFilteredDefinitions()
@covers ::getFilteredDefinitions @dataProvider providerTestGetFilteredDefinitions
File
-
core/
tests/ Drupal/ Tests/ Core/ Plugin/ FilteredPluginManagerTraitTest.php, line 25
Class
- FilteredPluginManagerTraitTest
- @coversDefaultClass \Drupal\Core\Plugin\FilteredPluginManagerTrait @group Plugin
Namespace
Drupal\Tests\Core\PluginCode
public function testGetFilteredDefinitions($contexts, $expected) : void {
// Start with two plugins.
$definitions = [];
$definitions['plugin1'] = [
'id' => 'plugin1',
];
$definitions['plugin2'] = [
'id' => 'plugin2',
];
$type = 'the_type';
$consumer = 'the_consumer';
$extra = [
'foo' => 'bar',
];
$context_handler = $this->prophesize(ContextHandlerInterface::class);
// Remove the second plugin when context1 is provided.
$context_handler->filterPluginDefinitionsByContexts([
'context1' => 'fake context',
], $definitions)
->willReturn([
'plugin1' => $definitions['plugin1'],
]);
// Remove the first plugin when no contexts are provided.
$context_handler->filterPluginDefinitionsByContexts([], $definitions)
->willReturn([
'plugin2' => $definitions['plugin2'],
]);
// After context filtering, the alter hook will be invoked.
$module_handler = $this->prophesize(ModuleHandlerInterface::class);
$hooks = [
"plugin_filter_{$type}",
"plugin_filter_{$type}__{$consumer}",
];
$module_handler->alter($hooks, $expected, $extra, $consumer)
->shouldBeCalled();
$theme_manager = $this->prophesize(ThemeManagerInterface::class);
$theme_manager->alter($hooks, $expected, $extra, $consumer)
->shouldBeCalled();
$plugin_manager = new TestFilteredPluginManager($definitions, $module_handler->reveal(), $theme_manager->reveal(), $context_handler->reveal());
$result = $plugin_manager->getFilteredDefinitions($consumer, $contexts, $extra);
$this->assertSame($expected, $result);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.