3 namespace Drupal\KernelTests\Core\Plugin;
5 use Drupal\Core\Plugin\Context\EntityContextDefinition;
6 use Drupal\KernelTests\KernelTestBase;
7 use Drupal\plugin_test\Plugin\TestPluginManager;
8 use Drupal\plugin_test\Plugin\MockBlockManager;
9 use Drupal\plugin_test\Plugin\DefaultsTestPluginManager;
10 use Drupal\Core\Cache\MemoryBackend;
11 use Drupal\Core\Extension\ModuleHandler;
14 * Base class for Plugin API unit tests.
16 abstract class PluginTestBase extends KernelTestBase {
23 public static $modules = ['plugin_test'];
25 protected $testPluginManager;
26 protected $testPluginExpectedDefinitions;
27 protected $mockBlockManager;
28 protected $mockBlockExpectedDefinitions;
29 protected $defaultsTestPluginManager;
30 protected $defaultsTestPluginExpectedDefinitions;
32 protected function setUp() {
35 // Real modules implementing plugin types may expose a module-specific API
36 // for retrieving each type's plugin manager, or make them available in
37 // Drupal's dependency injection container, but for unit testing, we get
38 // the managers directly.
39 // - TestPluginManager is a bare bones manager with no support for
40 // derivatives, and uses DefaultFactory for plugin instantiation.
41 // - MockBlockManager is used for testing more advanced functionality such
42 // as derivatives and ReflectionFactory.
43 $this->testPluginManager = new TestPluginManager();
44 $this->mockBlockManager = new MockBlockManager();
45 $module_handler = new ModuleHandler($this->root, [], new MemoryBackend(), $this->container->get('event_dispatcher'));
46 $this->defaultsTestPluginManager = new DefaultsTestPluginManager($module_handler);
48 // The expected plugin definitions within each manager. Several tests assert
49 // that these plugins and their definitions are found and returned by the
50 // necessary API functions.
51 // @see TestPluginManager::_construct().
52 // @see MockBlockManager::_construct().
53 $this->testPluginExpectedDefinitions = [
55 'label' => 'User login',
56 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockUserLoginBlock',
59 $this->mockBlockExpectedDefinitions = [
62 'label' => 'User login',
63 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockUserLoginBlock',
67 'label' => 'Main menu',
68 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockMenuBlock',
70 'menu:navigation' => [
72 'label' => 'Navigation',
73 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockMenuBlock',
77 'label' => 'Base label',
78 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockMenuBlock',
79 'setting' => 'default',
84 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockLayoutBlock',
88 'label' => 'Layout Foo',
89 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockLayoutBlock',
93 'label' => 'User name',
94 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockUserNameBlock',
96 'user' => EntityContextDefinition::fromEntityTypeId('user')->setLabel('User'),
99 'user_name_optional' => [
100 'id' => 'user_name_optional',
101 'label' => 'User name optional',
102 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockUserNameBlock',
104 'user' => EntityContextDefinition::fromEntityTypeId('user')->setLabel('User')->setRequired(FALSE),
107 'string_context' => [
108 'id' => 'string_context',
109 'label' => 'String typed data',
110 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\TypedDataStringBlock',
112 'complex_context' => [
113 'id' => 'complex_context',
114 'label' => 'Complex context',
115 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockComplexContextBlock',
117 'user' => EntityContextDefinition::fromEntityTypeId('user')->setLabel('User'),
118 'node' => EntityContextDefinition::fromEntityTypeId('node')->setLabel('Node'),
122 $this->defaultsTestPluginExpectedDefinitions = [
128 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockTestBlock',
135 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockTestBlock',