3 namespace Drupal\Tests\Core\Plugin;
5 use Drupal\Component\Plugin\PluginManagerBase;
6 use Drupal\Core\Extension\ModuleHandlerInterface;
7 use Drupal\Core\Plugin\Context\ContextHandlerInterface;
8 use Drupal\Core\Plugin\FilteredPluginManagerInterface;
9 use Drupal\Core\Plugin\FilteredPluginManagerTrait;
10 use Drupal\Core\Theme\ThemeManagerInterface;
11 use Drupal\Tests\UnitTestCase;
14 * @coversDefaultClass \Drupal\Core\Plugin\FilteredPluginManagerTrait
17 class FilteredPluginManagerTraitTest extends UnitTestCase {
20 * @covers ::getFilteredDefinitions
21 * @dataProvider providerTestGetFilteredDefinitions
23 public function testGetFilteredDefinitions($contexts, $expected) {
24 // Start with two plugins.
26 $definitions['plugin1'] = ['id' => 'plugin1'];
27 $definitions['plugin2'] = ['id' => 'plugin2'];
30 $consumer = 'the_consumer';
31 $extra = ['foo' => 'bar'];
33 $context_handler = $this->prophesize(ContextHandlerInterface::class);
34 // Remove the second plugin when context1 is provided.
35 $context_handler->filterPluginDefinitionsByContexts(['context1' => 'fake context'], $definitions)
36 ->willReturn(['plugin1' => $definitions['plugin1']]);
37 // Remove the first plugin when no contexts are provided.
38 $context_handler->filterPluginDefinitionsByContexts([], $definitions)
39 ->willReturn(['plugin2' => $definitions['plugin2']]);
41 // After context filtering, the alter hook will be invoked.
42 $module_handler = $this->prophesize(ModuleHandlerInterface::class);
43 $hooks = ["plugin_filter_{$type}", "plugin_filter_{$type}__{$consumer}"];
44 $module_handler->alter($hooks, $expected, $extra, $consumer)->shouldBeCalled();
46 $theme_manager = $this->prophesize(ThemeManagerInterface::class);
47 $theme_manager->alter($hooks, $expected, $extra, $consumer)->shouldBeCalled();
49 $plugin_manager = new TestFilteredPluginManager($definitions, $module_handler->reveal(), $theme_manager->reveal(), $context_handler->reveal());
50 $result = $plugin_manager->getFilteredDefinitions($consumer, $contexts, $extra);
51 $this->assertSame($expected, $result);
55 * Provides test data for ::testGetFilteredDefinitions().
57 public function providerTestGetFilteredDefinitions() {
59 $data['populated context'] = [
60 ['context1' => 'fake context'],
61 ['plugin1' => ['id' => 'plugin1']],
63 $data['empty context'] = [
65 ['plugin2' => ['id' => 'plugin2']],
67 $data['null context'] = [
70 'plugin1' => ['id' => 'plugin1'],
71 'plugin2' => ['id' => 'plugin2'],
80 * Class that allows testing the trait.
82 class TestFilteredPluginManager extends PluginManagerBase implements FilteredPluginManagerInterface {
84 use FilteredPluginManagerTrait;
86 protected $definitions = [];
88 protected $moduleHandler;
90 protected $themeManager;
92 protected $contextHandler;
94 public function __construct(array $definitions, ModuleHandlerInterface $module_handler, ThemeManagerInterface $theme_manager, ContextHandlerInterface $context_handler) {
95 $this->definitions = $definitions;
96 $this->moduleHandler = $module_handler;
97 $this->themeManager = $theme_manager;
98 $this->contextHandler = $context_handler;
101 protected function contextHandler() {
102 return $this->contextHandler;
105 protected function moduleHandler() {
106 return $this->moduleHandler;
109 protected function themeManager() {
110 return $this->themeManager;
113 protected function getType() {
117 public function getDefinitions() {
118 return $this->definitions;