PluginTestBase.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace Drupal\KernelTests\Core\Plugin;
  3. use Drupal\Core\Plugin\Context\ContextDefinition;
  4. use Drupal\KernelTests\KernelTestBase;
  5. use Drupal\plugin_test\Plugin\TestPluginManager;
  6. use Drupal\plugin_test\Plugin\MockBlockManager;
  7. use Drupal\plugin_test\Plugin\DefaultsTestPluginManager;
  8. use Drupal\Core\Cache\MemoryBackend;
  9. use Drupal\Core\Extension\ModuleHandler;
  10. /**
  11. * Base class for Plugin API unit tests.
  12. */
  13. abstract class PluginTestBase extends KernelTestBase {
  14. /**
  15. * Modules to enable.
  16. *
  17. * @var array
  18. */
  19. public static $modules = ['plugin_test'];
  20. protected $testPluginManager;
  21. protected $testPluginExpectedDefinitions;
  22. protected $mockBlockManager;
  23. protected $mockBlockExpectedDefinitions;
  24. protected $defaultsTestPluginManager;
  25. protected $defaultsTestPluginExpectedDefinitions;
  26. protected function setUp() {
  27. parent::setUp();
  28. // Real modules implementing plugin types may expose a module-specific API
  29. // for retrieving each type's plugin manager, or make them available in
  30. // Drupal's dependency injection container, but for unit testing, we get
  31. // the managers directly.
  32. // - TestPluginManager is a bare bones manager with no support for
  33. // derivatives, and uses DefaultFactory for plugin instantiation.
  34. // - MockBlockManager is used for testing more advanced functionality such
  35. // as derivatives and ReflectionFactory.
  36. $this->testPluginManager = new TestPluginManager();
  37. $this->mockBlockManager = new MockBlockManager();
  38. $module_handler = new ModuleHandler(\Drupal::root(), [], new MemoryBackend(), $this->container->get('event_dispatcher'));
  39. $this->defaultsTestPluginManager = new DefaultsTestPluginManager($module_handler);
  40. // The expected plugin definitions within each manager. Several tests assert
  41. // that these plugins and their definitions are found and returned by the
  42. // necessary API functions.
  43. // @see TestPluginManager::_construct().
  44. // @see MockBlockManager::_construct().
  45. $this->testPluginExpectedDefinitions = [
  46. 'user_login' => [
  47. 'label' => 'User login',
  48. 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockUserLoginBlock',
  49. ],
  50. ];
  51. $this->mockBlockExpectedDefinitions = [
  52. 'user_login' => [
  53. 'id' => 'user_login',
  54. 'label' => 'User login',
  55. 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockUserLoginBlock',
  56. ],
  57. 'menu:main_menu' => [
  58. 'id' => 'menu',
  59. 'label' => 'Main menu',
  60. 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockMenuBlock',
  61. ],
  62. 'menu:navigation' => [
  63. 'id' => 'menu',
  64. 'label' => 'Navigation',
  65. 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockMenuBlock',
  66. ],
  67. 'menu:foo' => [
  68. 'id' => 'menu',
  69. 'label' => 'Base label',
  70. 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockMenuBlock',
  71. 'setting' => 'default',
  72. ],
  73. 'layout' => [
  74. 'id' => 'layout',
  75. 'label' => 'Layout',
  76. 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockLayoutBlock',
  77. ],
  78. 'layout:foo' => [
  79. 'id' => 'layout',
  80. 'label' => 'Layout Foo',
  81. 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockLayoutBlock',
  82. ],
  83. 'user_name' => [
  84. 'id' => 'user_name',
  85. 'label' => 'User name',
  86. 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockUserNameBlock',
  87. 'context' => [
  88. 'user' => new ContextDefinition('entity:user', 'User'),
  89. ],
  90. ],
  91. 'user_name_optional' => [
  92. 'id' => 'user_name_optional',
  93. 'label' => 'User name optional',
  94. 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockUserNameBlock',
  95. 'context' => [
  96. 'user' => new ContextDefinition('entity:user', 'User', FALSE),
  97. ],
  98. ],
  99. 'string_context' => [
  100. 'id' => 'string_context',
  101. 'label' => 'String typed data',
  102. 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\TypedDataStringBlock',
  103. ],
  104. 'complex_context' => [
  105. 'id' => 'complex_context',
  106. 'label' => 'Complex context',
  107. 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockComplexContextBlock',
  108. 'context' => [
  109. 'user' => new ContextDefinition('entity:user', 'User'),
  110. 'node' => new ContextDefinition('entity:node', 'Node'),
  111. ],
  112. ],
  113. ];
  114. $this->defaultsTestPluginExpectedDefinitions = [
  115. 'test_block1' => [
  116. 'metadata' => [
  117. 'default' => TRUE,
  118. 'custom' => TRUE,
  119. ],
  120. 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockTestBlock',
  121. ],
  122. 'test_block2' => [
  123. 'metadata' => [
  124. 'default' => FALSE,
  125. 'custom' => TRUE,
  126. ],
  127. 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockTestBlock',
  128. ],
  129. ];
  130. }
  131. }