CategorizingPluginManagerTraitTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\Tests\Core\Plugin\CategorizingPluginManagerTraitTest.
  5. */
  6. namespace Drupal\Tests\Core\Plugin;
  7. use Drupal\Component\Plugin\CategorizingPluginManagerInterface;
  8. use Drupal\Core\Extension\ModuleHandlerInterface;
  9. use Drupal\Core\Plugin\CategorizingPluginManagerTrait;
  10. use Drupal\Core\Plugin\DefaultPluginManager;
  11. use Drupal\Tests\UnitTestCase;
  12. /**
  13. * @coversDefaultClass \Drupal\Core\Plugin\CategorizingPluginManagerTrait
  14. * @group Plugin
  15. */
  16. class CategorizingPluginManagerTraitTest extends UnitTestCase {
  17. /**
  18. * The plugin manager to test.
  19. *
  20. * @var \Drupal\Component\Plugin\CategorizingPluginManagerInterface|\PHPUnit\Framework\MockObject\MockObject
  21. */
  22. protected $pluginManager;
  23. /**
  24. * {@inheritdoc}
  25. */
  26. protected function setUp() {
  27. $module_handler = $this->createMock('Drupal\Core\Extension\ModuleHandlerInterface');
  28. $module_handler->expects($this->any())
  29. ->method('getModuleList')
  30. ->willReturn(['node' => []]);
  31. $module_handler->expects($this->any())
  32. ->method('getName')
  33. ->with('node')
  34. ->willReturn('Node');
  35. $this->pluginManager = new CategorizingPluginManager($module_handler);
  36. $this->pluginManager->setStringTranslation($this->getStringTranslationStub());
  37. }
  38. /**
  39. * @covers ::getCategories
  40. */
  41. public function testGetCategories() {
  42. $this->assertSame([
  43. 'fruits',
  44. 'vegetables',
  45. ], array_values($this->pluginManager->getCategories()));
  46. }
  47. /**
  48. * @covers ::getSortedDefinitions
  49. */
  50. public function testGetSortedDefinitions() {
  51. $sorted = $this->pluginManager->getSortedDefinitions();
  52. $this->assertSame(['apple', 'mango', 'cucumber'], array_keys($sorted));
  53. }
  54. /**
  55. * @covers ::getGroupedDefinitions
  56. */
  57. public function testGetGroupedDefinitions() {
  58. $grouped = $this->pluginManager->getGroupedDefinitions();
  59. $this->assertSame(['fruits', 'vegetables'], array_keys($grouped));
  60. $this->assertSame(['apple', 'mango'], array_keys($grouped['fruits']));
  61. $this->assertSame(['cucumber'], array_keys($grouped['vegetables']));
  62. }
  63. /**
  64. * @covers ::processDefinitionCategory
  65. */
  66. public function testProcessDefinitionCategory() {
  67. // Existing category.
  68. $definition = [
  69. 'label' => 'some',
  70. 'provider' => 'core',
  71. 'category' => 'bag',
  72. ];
  73. $this->pluginManager->processDefinition($definition, 'some');
  74. $this->assertSame('bag', $definition['category']);
  75. // No category, provider without label.
  76. $definition = [
  77. 'label' => 'some',
  78. 'provider' => 'core',
  79. ];
  80. $this->pluginManager->processDefinition($definition, 'some');
  81. $this->assertSame('core', $definition['category']);
  82. // No category, provider is module with label.
  83. $definition = [
  84. 'label' => 'some',
  85. 'provider' => 'node',
  86. ];
  87. $this->pluginManager->processDefinition($definition, 'some');
  88. $this->assertSame('Node', $definition['category']);
  89. }
  90. }
  91. /**
  92. * Class that allows testing the trait.
  93. */
  94. class CategorizingPluginManager extends DefaultPluginManager implements CategorizingPluginManagerInterface {
  95. use CategorizingPluginManagerTrait;
  96. /**
  97. * Replace the constructor so we can instantiate a stub.
  98. *
  99. * @param \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit\Framework\MockObject\MockObject $module_handler
  100. * The module handler.
  101. */
  102. public function __construct(ModuleHandlerInterface $module_handler) {
  103. $this->moduleHandler = $module_handler;
  104. }
  105. /**
  106. * {@inheritdoc}
  107. *
  108. * Provides some test definitions to the trait.
  109. */
  110. public function getDefinitions() {
  111. return [
  112. 'cucumber' => [
  113. 'label' => 'cucumber',
  114. 'category' => 'vegetables',
  115. ],
  116. 'apple' => [
  117. 'label' => 'apple',
  118. 'category' => 'fruits',
  119. ],
  120. 'mango' => [
  121. 'label' => 'mango',
  122. 'category' => 'fruits',
  123. ],
  124. ];
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function processDefinition(&$definition, $plugin_id) {
  130. parent::processDefinition($definition, $plugin_id);
  131. $this->processDefinitionCategory($definition);
  132. }
  133. }