9642949de2501c11b797023e350e30f6774a47e4
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Plugin / FilteredPluginManagerTraitTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Plugin;
4
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;
12
13 /**
14  * @coversDefaultClass \Drupal\Core\Plugin\FilteredPluginManagerTrait
15  * @group Plugin
16  */
17 class FilteredPluginManagerTraitTest extends UnitTestCase {
18
19   /**
20    * @covers ::getFilteredDefinitions
21    * @dataProvider providerTestGetFilteredDefinitions
22    */
23   public function testGetFilteredDefinitions($contexts, $expected) {
24     // Start with two plugins.
25     $definitions = [];
26     $definitions['plugin1'] = ['id' => 'plugin1'];
27     $definitions['plugin2'] = ['id' => 'plugin2'];
28
29     $type = 'the_type';
30     $consumer = 'the_consumer';
31     $extra = ['foo' => 'bar'];
32
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']]);
40
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();
45
46     $theme_manager = $this->prophesize(ThemeManagerInterface::class);
47     $theme_manager->alter($hooks, $expected, $extra, $consumer)->shouldBeCalled();
48
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);
52   }
53
54   /**
55    * Provides test data for ::testGetFilteredDefinitions().
56    */
57   public function providerTestGetFilteredDefinitions() {
58     $data = [];
59     $data['populated context'] = [
60       ['context1' => 'fake context'],
61       ['plugin1' => ['id' => 'plugin1']],
62     ];
63     $data['empty context'] = [
64       [],
65       ['plugin2' => ['id' => 'plugin2']],
66     ];
67     $data['null context'] = [
68       NULL,
69       [
70         'plugin1' => ['id' => 'plugin1'],
71         'plugin2' => ['id' => 'plugin2'],
72       ],
73     ];
74     return $data;
75   }
76
77 }
78
79 /**
80  * Class that allows testing the trait.
81  */
82 class TestFilteredPluginManager extends PluginManagerBase implements FilteredPluginManagerInterface {
83
84   use FilteredPluginManagerTrait;
85
86   protected $definitions = [];
87
88   protected $moduleHandler;
89
90   protected $themeManager;
91
92   protected $contextHandler;
93
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;
99   }
100
101   protected function contextHandler() {
102     return $this->contextHandler;
103   }
104
105   protected function moduleHandler() {
106     return $this->moduleHandler;
107   }
108
109   protected function themeManager() {
110     return $this->themeManager;
111   }
112
113   protected function getType() {
114     return 'the_type';
115   }
116
117   public function getDefinitions() {
118     return $this->definitions;
119   }
120
121 }