3 namespace Drupal\Tests\Core\Plugin;
5 use Drupal\Component\Plugin\Definition\PluginDefinition;
6 use Drupal\Component\Plugin\Exception\PluginException;
7 use Drupal\Core\Extension\ModuleHandlerInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Plugin\PluginFormInterface;
10 use Drupal\Tests\UnitTestCase;
13 * Tests the DefaultPluginManager.
17 * @coversDefaultClass \Drupal\Core\Plugin\DefaultPluginManager
19 class DefaultPluginManagerTest extends UnitTestCase {
22 * The expected plugin definitions.
26 protected $expectedDefinitions;
29 * The namespaces to look for plugin definitions.
33 protected $namespaces;
38 protected function setUp() {
39 $this->expectedDefinitions = [
44 'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Apple',
51 'bread' => 'Banana bread',
53 'singular' => '@count loaf',
54 'plural' => '@count loaves',
57 'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Banana',
61 $this->namespaces = new \ArrayObject();
62 $this->namespaces['Drupal\plugin_test'] = $this->root . '/core/modules/system/tests/modules/plugin_test/src';
66 * Tests the plugin manager with a plugin that extends a non-installed class.
68 public function testDefaultPluginManagerWithPluginExtendingNonInstalledClass() {
70 $definitions['extending_non_installed_class'] = [
71 'id' => 'extending_non_installed_class',
72 'label' => 'A plugin whose class is extending from a non-installed module class',
74 'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\ExtendingNonInstalledClass',
75 'provider' => 'plugin_test',
78 $module_handler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
79 $plugin_manager = new TestPluginManager($this->namespaces, $definitions, $module_handler, 'test_alter_hook', '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface');
80 $plugin_manager->getDefinition('plugin_test', FALSE);
81 $this->assertTrue(TRUE, 'No PHP fatal error occurred when retrieving the definitions of a module with plugins that depend on a non-installed module class should not cause a PHP fatal.');
85 * Tests the plugin manager with a disabled module.
87 public function testDefaultPluginManagerWithDisabledModule() {
88 $definitions = $this->expectedDefinitions;
89 $definitions['cherry'] = [
93 'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Cherry',
94 'provider' => 'disabled_module',
97 $module_handler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
99 $module_handler->expects($this->once())
100 ->method('moduleExists')
101 ->with('disabled_module')
102 ->will($this->returnValue(FALSE));
104 $plugin_manager = new TestPluginManager($this->namespaces, $definitions, $module_handler, 'test_alter_hook', '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface');
106 $this->assertEmpty($plugin_manager->getDefinition('cherry', FALSE), 'Plugin information of a disabled module is not available');
110 * Tests the plugin manager and object plugin definitions.
112 public function testDefaultPluginManagerWithObjects() {
113 $definitions = $this->expectedDefinitions;
114 $definitions['cherry'] = (object) [
118 'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Cherry',
119 'provider' => 'disabled_module',
122 $module_handler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
124 $module_handler->expects($this->once())
125 ->method('moduleExists')
126 ->with('disabled_module')
127 ->will($this->returnValue(FALSE));
129 $plugin_manager = new TestPluginManager($this->namespaces, $definitions, $module_handler, 'test_alter_hook', '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface');
131 $this->assertEmpty($plugin_manager->getDefinition('cherry', FALSE), 'Plugin information is available');
135 * Tests the plugin manager with no cache and altering.
137 public function testDefaultPluginManager() {
138 $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, NULL, NULL, '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface');
139 $this->assertEquals($this->expectedDefinitions, $plugin_manager->getDefinitions());
140 $this->assertEquals($this->expectedDefinitions['banana'], $plugin_manager->getDefinition('banana'));
144 * Tests the plugin manager with no cache and altering.
146 public function testDefaultPluginManagerWithAlter() {
147 $module_handler = $this->getMockBuilder('Drupal\Core\Extension\ModuleHandler')
148 ->disableOriginalConstructor()
151 // Configure the stub.
152 $alter_hook_name = $this->randomMachineName();
153 $module_handler->expects($this->once())
155 ->with($this->equalTo($alter_hook_name), $this->equalTo($this->expectedDefinitions));
157 $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, $module_handler, $alter_hook_name, '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface');
159 $this->assertEquals($this->expectedDefinitions, $plugin_manager->getDefinitions());
160 $this->assertEquals($this->expectedDefinitions['banana'], $plugin_manager->getDefinition('banana'));
164 * Tests the plugin manager with caching and altering.
166 public function testDefaultPluginManagerWithEmptyCache() {
167 $cid = $this->randomMachineName();
168 $cache_backend = $this->getMockBuilder('Drupal\Core\Cache\MemoryBackend')
169 ->disableOriginalConstructor()
172 ->expects($this->once())
175 ->will($this->returnValue(FALSE));
177 ->expects($this->once())
179 ->with($cid, $this->expectedDefinitions);
181 $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, NULL, NULL, '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface');
182 $plugin_manager->setCacheBackend($cache_backend, $cid);
184 $this->assertEquals($this->expectedDefinitions, $plugin_manager->getDefinitions());
185 $this->assertEquals($this->expectedDefinitions['banana'], $plugin_manager->getDefinition('banana'));
189 * Tests the plugin manager with caching and altering.
191 public function testDefaultPluginManagerWithFilledCache() {
192 $cid = $this->randomMachineName();
193 $cache_backend = $this->getMockBuilder('Drupal\Core\Cache\MemoryBackend')
194 ->disableOriginalConstructor()
197 ->expects($this->once())
200 ->will($this->returnValue((object) ['data' => $this->expectedDefinitions]));
202 ->expects($this->never())
205 $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, NULL, NULL, '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface');
206 $plugin_manager->setCacheBackend($cache_backend, $cid);
208 $this->assertEquals($this->expectedDefinitions, $plugin_manager->getDefinitions());
212 * Tests the plugin manager with caching disabled.
214 public function testDefaultPluginManagerNoCache() {
215 $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, NULL, NULL, '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface');
217 $cid = $this->randomMachineName();
218 $cache_backend = $this->getMockBuilder('Drupal\Core\Cache\MemoryBackend')
219 ->disableOriginalConstructor()
222 ->expects($this->never())
225 ->expects($this->never())
227 $plugin_manager->setCacheBackend($cache_backend, $cid);
229 $plugin_manager->useCaches(FALSE);
231 $this->assertEquals($this->expectedDefinitions, $plugin_manager->getDefinitions());
232 $this->assertEquals($this->expectedDefinitions['banana'], $plugin_manager->getDefinition('banana'));
236 * Tests the plugin manager cache clear with tags.
238 public function testCacheClearWithTags() {
239 $cid = $this->randomMachineName();
240 $cache_backend = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
241 $cache_tags_invalidator = $this->getMock('Drupal\Core\Cache\CacheTagsInvalidatorInterface');
242 $cache_tags_invalidator
243 ->expects($this->once())
244 ->method('invalidateTags')
247 ->expects($this->never())
248 ->method('deleteMultiple');
250 $this->getContainerWithCacheTagsInvalidator($cache_tags_invalidator);
252 $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, NULL, NULL, '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface');
253 $plugin_manager->setCacheBackend($cache_backend, $cid, ['tag']);
255 $plugin_manager->clearCachedDefinitions();
259 * Tests plugins with the proper interface.
261 * @covers ::createInstance
263 public function testCreateInstanceWithJustValidInterfaces() {
264 $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, NULL, NULL, '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface');
266 foreach ($this->expectedDefinitions as $plugin_id => $definition) {
267 $this->assertNotNull($plugin_manager->createInstance($plugin_id));
272 * Tests plugins without the proper interface.
274 * @covers ::createInstance
276 public function testCreateInstanceWithInvalidInterfaces() {
277 $module_handler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
279 $module_handler->expects($this->any())
280 ->method('moduleExists')
281 ->with('plugin_test')
284 $this->expectedDefinitions['kale'] = [
288 'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Kale',
289 'provider' => 'plugin_test',
291 $this->expectedDefinitions['apple']['provider'] = 'plugin_test';
292 $this->expectedDefinitions['banana']['provider'] = 'plugin_test';
294 $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, $module_handler, NULL, '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface');
295 $this->setExpectedException(PluginException::class, 'Plugin "kale" (Drupal\plugin_test\Plugin\plugin_test\fruit\Kale) must implement interface \Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface');
296 $plugin_manager->createInstance('kale');
300 * Tests plugins without a required interface.
302 * @covers ::getDefinitions
304 public function testGetDefinitionsWithoutRequiredInterface() {
305 $module_handler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
307 $module_handler->expects($this->any())
308 ->method('moduleExists')
309 ->with('plugin_test')
312 $this->expectedDefinitions['kale'] = [
316 'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Kale',
317 'provider' => 'plugin_test',
319 $this->expectedDefinitions['apple']['provider'] = 'plugin_test';
320 $this->expectedDefinitions['banana']['provider'] = 'plugin_test';
322 $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, $module_handler, NULL);
323 $this->assertInternalType('array', $plugin_manager->getDefinitions());
327 * @covers ::getCacheContexts
329 public function testGetCacheContexts() {
330 $module_handler = $this->prophesize(ModuleHandlerInterface::class);
331 $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, $module_handler->reveal(), NULL);
332 $cache_contexts = $plugin_manager->getCacheContexts();
333 $this->assertInternalType('array', $cache_contexts);
334 array_map(function ($cache_context) {
335 $this->assertInternalType('string', $cache_context);
340 * @covers ::getCacheTags
342 public function testGetCacheTags() {
343 $module_handler = $this->prophesize(ModuleHandlerInterface::class);
344 $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, $module_handler->reveal(), NULL);
345 $cache_tags = $plugin_manager->getCacheTags();
346 $this->assertInternalType('array', $cache_tags);
347 array_map(function ($cache_tag) {
348 $this->assertInternalType('string', $cache_tag);
353 * @covers ::getCacheMaxAge
355 public function testGetCacheMaxAge() {
356 $module_handler = $this->prophesize(ModuleHandlerInterface::class);
357 $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, $module_handler->reveal(), NULL);
358 $cache_max_age = $plugin_manager->getCacheMaxAge();
359 $this->assertInternalType('int', $cache_max_age);
363 * @covers ::findDefinitions
364 * @covers ::extractProviderFromDefinition
366 public function testProviderExists() {
368 $definitions['array_based_found'] = ['provider' => 'module_found'];
369 $definitions['array_based_missing'] = ['provider' => 'module_missing'];
370 $definitions['stdclass_based_found'] = (object) ['provider' => 'module_found'];
371 $definitions['stdclass_based_missing'] = (object) ['provider' => 'module_missing'];
372 $definitions['classed_object_found'] = new ObjectDefinition(['provider' => 'module_found']);
373 $definitions['classed_object_missing'] = new ObjectDefinition(['provider' => 'module_missing']);
376 $expected['array_based_found'] = $definitions['array_based_found'];
377 $expected['stdclass_based_found'] = $definitions['stdclass_based_found'];
378 $expected['classed_object_found'] = $definitions['classed_object_found'];
380 $module_handler = $this->prophesize(ModuleHandlerInterface::class);
381 $module_handler->moduleExists('module_found')->willReturn(TRUE)->shouldBeCalled();
382 $module_handler->moduleExists('module_missing')->willReturn(FALSE)->shouldBeCalled();
383 $plugin_manager = new TestPluginManager($this->namespaces, $definitions, $module_handler->reveal());
384 $result = $plugin_manager->getDefinitions();
385 $this->assertEquals($expected, $result);
389 * @covers ::processDefinition
390 * @dataProvider providerTestProcessDefinition
392 public function testProcessDefinition($definition, $expected) {
393 $module_handler = $this->prophesize(ModuleHandlerInterface::class);
394 $plugin_manager = new TestPluginManagerWithDefaults($this->namespaces, $this->expectedDefinitions, $module_handler->reveal(), NULL);
396 $plugin_manager->processDefinition($definition, 'the_plugin_id');
397 $this->assertEquals($expected, $definition);
400 public function providerTestProcessDefinition() {
419 $object_definition = (object) [
426 $data['object_definition'] = [$object_definition, clone $object_definition];
428 $data['no_form'][] = ['class' => TestPluginForm::class];
429 $data['no_form'][] = [
430 'class' => TestPluginForm::class,
431 'foo' => ['bar' => ['baz']],
434 $data['default_form'][] = ['class' => TestPluginForm::class, 'forms' => ['configure' => 'stdClass']];
435 $data['default_form'][] = [
436 'class' => TestPluginForm::class,
437 'forms' => ['configure' => 'stdClass'],
438 'foo' => ['bar' => ['baz']],
441 $data['class_with_slashes'][] = [
442 'class' => '\Drupal\Tests\Core\Plugin\TestPluginForm',
444 $data['class_with_slashes'][] = [
445 'class' => 'Drupal\Tests\Core\Plugin\TestPluginForm',
446 'foo' => ['bar' => ['baz']],
449 $data['object_with_class_with_slashes'][] = (new PluginDefinition())->setClass('\Drupal\Tests\Core\Plugin\TestPluginForm');
450 $data['object_with_class_with_slashes'][] = (new PluginDefinition())->setClass('Drupal\Tests\Core\Plugin\TestPluginForm');
456 class TestPluginManagerWithDefaults extends TestPluginManager {
461 protected $defaults = [
471 class TestPluginForm implements PluginFormInterface {
476 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
483 public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
489 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
493 class ObjectDefinition extends PluginDefinition {
496 * ObjectDefinition constructor.
498 * @param array $definition
500 public function __construct(array $definition) {
501 foreach ($definition as $property => $value) {
502 $this->{$property} = $value;