ccc82369a0e48c8f1c93fc3ea87fddcf764ae8a9
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Plugin / PluginTestBase.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Plugin;
4
5 use Drupal\Core\Plugin\Context\ContextDefinition;
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;
12
13 /**
14  * Base class for Plugin API unit tests.
15  */
16 abstract class PluginTestBase extends KernelTestBase {
17
18   /**
19    * Modules to enable.
20    *
21    * @var array
22    */
23   public static $modules = ['plugin_test'];
24
25   protected $testPluginManager;
26   protected $testPluginExpectedDefinitions;
27   protected $mockBlockManager;
28   protected $mockBlockExpectedDefinitions;
29   protected $defaultsTestPluginManager;
30   protected $defaultsTestPluginExpectedDefinitions;
31
32   protected function setUp() {
33     parent::setUp();
34
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(\Drupal::root(), [], new MemoryBackend(), $this->container->get('event_dispatcher'));
46     $this->defaultsTestPluginManager = new DefaultsTestPluginManager($module_handler);
47
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 = [
54       'user_login' => [
55         'label' => 'User login',
56         'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockUserLoginBlock',
57       ],
58     ];
59     $this->mockBlockExpectedDefinitions = [
60       'user_login' => [
61         'id' => 'user_login',
62         'label' => 'User login',
63         'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockUserLoginBlock',
64       ],
65       'menu:main_menu' => [
66         'id' => 'menu',
67         'label' => 'Main menu',
68         'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockMenuBlock',
69       ],
70       'menu:navigation' => [
71         'id' => 'menu',
72         'label' => 'Navigation',
73         'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockMenuBlock',
74       ],
75       'menu:foo' => [
76         'id' => 'menu',
77         'label' => 'Base label',
78         'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockMenuBlock',
79         'setting' => 'default',
80       ],
81       'layout' => [
82         'id' => 'layout',
83         'label' => 'Layout',
84         'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockLayoutBlock',
85       ],
86       'layout:foo' => [
87         'id' => 'layout',
88         'label' => 'Layout Foo',
89         'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockLayoutBlock',
90       ],
91       'user_name' => [
92         'id' => 'user_name',
93         'label' => 'User name',
94         'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockUserNameBlock',
95         'context' => [
96           'user' => new ContextDefinition('entity:user', 'User'),
97         ],
98       ],
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',
103         'context' => [
104           'user' => new ContextDefinition('entity:user', 'User', FALSE),
105         ],
106       ],
107       'string_context' => [
108         'id' => 'string_context',
109         'label' => 'String typed data',
110         'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\TypedDataStringBlock',
111       ],
112       'complex_context' => [
113         'id' => 'complex_context',
114         'label' => 'Complex context',
115         'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockComplexContextBlock',
116         'context' => [
117           'user' => new ContextDefinition('entity:user', 'User'),
118           'node' => new ContextDefinition('entity:node', 'Node'),
119         ],
120       ],
121     ];
122     $this->defaultsTestPluginExpectedDefinitions = [
123       'test_block1' => [
124         'metadata' => [
125           'default' => TRUE,
126           'custom' => TRUE,
127         ],
128         'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockTestBlock',
129       ],
130       'test_block2' => [
131         'metadata' => [
132           'default' => FALSE,
133           'custom' => TRUE,
134         ],
135         'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockTestBlock',
136       ],
137     ];
138   }
139
140 }