Newer
Older
<?php
namespace Drupal\Tests\Core\Plugin;
Alex Pott
committed
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Tests\UnitTestCase;
/**
* Tests the DefaultPluginManager.
*
* @group Plugin
Alex Pott
committed
*
* @coversDefaultClass \Drupal\Core\Plugin\DefaultPluginManager
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
*/
class DefaultPluginManagerTest extends UnitTestCase {
/**
* The expected plugin definitions.
*
* @var array
*/
protected $expectedDefinitions;
/**
* The namespaces to look for plugin definitions.
*
* @var \Traversable
*/
protected $namespaces;
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->expectedDefinitions = array(
'apple' => array(
'id' => 'apple',
'label' => 'Apple',
'color' => 'green',
'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Apple',
),
'banana' => array(
'id' => 'banana',
'label' => 'Banana',
'color' => 'yellow',
'uses' => array(
'bread' => 'Banana bread',
'loaf' => array(
'singular' => '@count loaf',
'plural' => '@count loaves',
),
),
'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Banana',
),
);
$this->namespaces = new \ArrayObject();
$this->namespaces['Drupal\plugin_test'] = $this->root . '/core/modules/system/tests/modules/plugin_test/src';
/**
* Tests the plugin manager with a disabled module.
*/
public function testDefaultPluginManagerWithDisabledModule() {
$definitions = $this->expectedDefinitions;
$definitions['cherry'] = array(
'id' => 'cherry',
'label' => 'Cherry',
'color' => 'red',
'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Cherry',
'provider' => 'disabled_module',
);
$module_handler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
$module_handler->expects($this->once())
->method('moduleExists')
->with('disabled_module')
->will($this->returnValue(FALSE));
Alex Pott
committed
$plugin_manager = new TestPluginManager($this->namespaces, $definitions, $module_handler, 'test_alter_hook', '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface');
$this->assertEmpty($plugin_manager->getDefinition('cherry', FALSE), 'Plugin information of a disabled module is not available');
}
/**
* Tests the plugin manager and object plugin definitions.
*/
public function testDefaultPluginManagerWithObjects() {
$definitions = $this->expectedDefinitions;
$definitions['cherry'] = (object) array(
'id' => 'cherry',
'label' => 'Cherry',
'color' => 'red',
'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Cherry',
'provider' => 'disabled_module',
);
$module_handler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
$module_handler->expects($this->once())
->method('moduleExists')
->with('disabled_module')
->will($this->returnValue(FALSE));
Alex Pott
committed
$plugin_manager = new TestPluginManager($this->namespaces, $definitions, $module_handler, 'test_alter_hook', '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface');
Angie Byron
committed
$this->assertEmpty($plugin_manager->getDefinition('cherry', FALSE), 'Plugin information is available');
}
/**
* Tests the plugin manager with no cache and altering.
*/
public function testDefaultPluginManager() {
Alex Pott
committed
$plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, NULL, NULL, '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface');
$this->assertEquals($this->expectedDefinitions, $plugin_manager->getDefinitions());
$this->assertEquals($this->expectedDefinitions['banana'], $plugin_manager->getDefinition('banana'));
}
/**
* Tests the plugin manager with no cache and altering.
*/
public function testDefaultPluginManagerWithAlter() {
$module_handler = $this->getMockBuilder('Drupal\Core\Extension\ModuleHandler')
->disableOriginalConstructor()
->getMock();
// Configure the stub.
Alex Pott
committed
$alter_hook_name = $this->randomMachineName();
$module_handler->expects($this->once())
->method('alter')
->with($this->equalTo($alter_hook_name), $this->equalTo($this->expectedDefinitions));
Alex Pott
committed
$plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, $module_handler, $alter_hook_name, '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface');
$this->assertEquals($this->expectedDefinitions, $plugin_manager->getDefinitions());
$this->assertEquals($this->expectedDefinitions['banana'], $plugin_manager->getDefinition('banana'));
}
/**
* Tests the plugin manager with caching and altering.
*/
public function testDefaultPluginManagerWithEmptyCache() {
Alex Pott
committed
$cid = $this->randomMachineName();
$cache_backend = $this->getMockBuilder('Drupal\Core\Cache\MemoryBackend')
->disableOriginalConstructor()
->getMock();
$cache_backend
->expects($this->once())
->method('get')
->with($cid)
->will($this->returnValue(FALSE));
$cache_backend
->expects($this->once())
->method('set')
->with($cid, $this->expectedDefinitions);
Alex Pott
committed
$plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, NULL, NULL, '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface');
$plugin_manager->setCacheBackend($cache_backend, $cid);
$this->assertEquals($this->expectedDefinitions, $plugin_manager->getDefinitions());
$this->assertEquals($this->expectedDefinitions['banana'], $plugin_manager->getDefinition('banana'));
}
/**
* Tests the plugin manager with caching and altering.
*/
public function testDefaultPluginManagerWithFilledCache() {
Alex Pott
committed
$cid = $this->randomMachineName();
$cache_backend = $this->getMockBuilder('Drupal\Core\Cache\MemoryBackend')
->disableOriginalConstructor()
->getMock();
$cache_backend
->expects($this->once())
->method('get')
->with($cid)
->will($this->returnValue((object) array('data' => $this->expectedDefinitions)));
$cache_backend
->expects($this->never())
->method('set');
Alex Pott
committed
$plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, NULL, NULL, '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface');
$plugin_manager->setCacheBackend($cache_backend, $cid);
$this->assertEquals($this->expectedDefinitions, $plugin_manager->getDefinitions());
}
/**
* Tests the plugin manager with caching disabled.
*/
public function testDefaultPluginManagerNoCache() {
$plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, NULL, NULL, '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface');
$cid = $this->randomMachineName();
$cache_backend = $this->getMockBuilder('Drupal\Core\Cache\MemoryBackend')
->disableOriginalConstructor()
->getMock();
$cache_backend
->expects($this->never())
->method('get');
$cache_backend
->expects($this->never())
->method('set');
$plugin_manager->setCacheBackend($cache_backend, $cid);
$plugin_manager->useCaches(FALSE);
$this->assertEquals($this->expectedDefinitions, $plugin_manager->getDefinitions());
$this->assertEquals($this->expectedDefinitions['banana'], $plugin_manager->getDefinition('banana'));
}
Alex Pott
committed
/**
* Tests the plugin manager cache clear with tags.
*/
public function testCacheClearWithTags() {
Alex Pott
committed
$cid = $this->randomMachineName();
catch
committed
$cache_backend = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
$cache_tags_invalidator = $this->getMock('Drupal\Core\Cache\CacheTagsInvalidatorInterface');
$cache_tags_invalidator
Alex Pott
committed
->expects($this->once())
catch
committed
->method('invalidateTags')
->with(array('tag'));
Alex Pott
committed
$cache_backend
->expects($this->never())
->method('deleteMultiple');
catch
committed
$this->getContainerWithCacheTagsInvalidator($cache_tags_invalidator);
Alex Pott
committed
$plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, NULL, NULL, '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface');
$plugin_manager->setCacheBackend($cache_backend, $cid, array('tag'));
Alex Pott
committed
$plugin_manager->clearCachedDefinitions();
}
Alex Pott
committed
/**
* Tests plugins with the proper interface.
*
* @covers ::createInstance
*/
public function testCreateInstanceWithJustValidInterfaces() {
$plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, NULL, NULL, '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface');
foreach ($this->expectedDefinitions as $plugin_id => $definition) {
$this->assertNotNull($plugin_manager->createInstance($plugin_id));
Alex Pott
committed
}
}
/**
* Tests plugins without the proper interface.
*
* @covers ::createInstance
*
* @expectedException \Drupal\Component\Plugin\Exception\PluginException
Alex Pott
committed
* @expectedExceptionMessage Plugin "kale" (Drupal\plugin_test\Plugin\plugin_test\fruit\Kale) must implement interface \Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface
Alex Pott
committed
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
*/
public function testCreateInstanceWithInvalidInterfaces() {
$module_handler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
$module_handler->expects($this->any())
->method('moduleExists')
->with('plugin_test')
->willReturn(TRUE);
$this->expectedDefinitions['kale'] = array(
'id' => 'kale',
'label' => 'Kale',
'color' => 'green',
'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Kale',
'provider' => 'plugin_test',
);
$this->expectedDefinitions['apple']['provider'] = 'plugin_test';
$this->expectedDefinitions['banana']['provider'] = 'plugin_test';
$plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, $module_handler, NULL, '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface');
$plugin_manager->createInstance('kale');
}
/**
* Tests plugins without a required interface.
*
* @covers ::getDefinitions
*/
public function testGetDefinitionsWithoutRequiredInterface() {
$module_handler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
$module_handler->expects($this->any())
->method('moduleExists')
->with('plugin_test')
->willReturn(FALSE);
$this->expectedDefinitions['kale'] = array(
'id' => 'kale',
'label' => 'Kale',
'color' => 'green',
'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Kale',
'provider' => 'plugin_test',
);
$this->expectedDefinitions['apple']['provider'] = 'plugin_test';
$this->expectedDefinitions['banana']['provider'] = 'plugin_test';
$plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, $module_handler, NULL);
$this->assertInternalType('array', $plugin_manager->getDefinitions());
Alex Pott
committed
}
Alex Pott
committed
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/**
* @covers ::getCacheContexts
*/
public function testGetCacheContexts() {
$module_handler = $this->prophesize(ModuleHandlerInterface::class);
$plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, $module_handler->reveal(), NULL);
$cache_contexts = $plugin_manager->getCacheContexts();
$this->assertInternalType('array', $cache_contexts);
array_map(function ($cache_context) {
$this->assertInternalType('string', $cache_context);
}, $cache_contexts);
}
/**
* @covers ::getCacheTags
*/
public function testGetCacheTags() {
$module_handler = $this->prophesize(ModuleHandlerInterface::class);
$plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, $module_handler->reveal(), NULL);
$cache_tags = $plugin_manager->getCacheTags();
$this->assertInternalType('array', $cache_tags);
array_map(function ($cache_tag) {
$this->assertInternalType('string', $cache_tag);
}, $cache_tags);
}
/**
* @covers ::getCacheMaxAge
*/
public function testGetCacheMaxAge() {
$module_handler = $this->prophesize(ModuleHandlerInterface::class);
$plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, $module_handler->reveal(), NULL);
$cache_max_age = $plugin_manager->getCacheMaxAge();
$this->assertInternalType('int', $cache_max_age);
}
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/**
* @covers ::processDefinition
* @dataProvider providerTestProcessDefinition
*/
public function testProcessDefinition($definition, $expected) {
$module_handler = $this->prophesize(ModuleHandlerInterface::class);
$plugin_manager = new TestPluginManagerWithDefaults($this->namespaces, $this->expectedDefinitions, $module_handler->reveal(), NULL);
$plugin_manager->processDefinition($definition, 'the_plugin_id');
$this->assertEquals($expected, $definition);
}
public function providerTestProcessDefinition() {
$data = [];
$data['merge'][] = [
'foo' => [
'bar' => [
'asdf',
],
],
];
$data['merge'][] = [
'foo' => [
'bar' => [
'baz',
'asdf',
],
],
];
$object_definition = (object) [
'foo' => [
'bar' => [
'asdf',
],
],
];
$data['object_definition'] = [$object_definition, clone $object_definition];
$data['no_form'][] = ['class' => TestPluginForm::class];
$data['no_form'][] = [
'class' => TestPluginForm::class,
'forms' => ['configure' => TestPluginForm::class],
'foo' => ['bar' => ['baz']],
];
$data['default_form'][] = ['class' => TestPluginForm::class, 'forms' => ['configure' => 'stdClass']];
$data['default_form'][] = [
'class' => TestPluginForm::class,
'forms' => ['configure' => 'stdClass'],
'foo' => ['bar' => ['baz']],
];
return $data;
}
}
class TestPluginManagerWithDefaults extends TestPluginManager {
/**
* {@inheritdoc}
*/
protected $defaults = [
'foo' => [
'bar' => [
'baz',
],
],
];
}
class TestPluginForm implements PluginFormInterface {
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
return [];
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
}