5 * Contains \Drupal\Tests\Core\Plugin\CategorizingPluginManagerTraitTest.
8 namespace Drupal\Tests\Core\Plugin;
10 use Drupal\Component\Plugin\CategorizingPluginManagerInterface;
11 use Drupal\Core\Extension\ModuleHandlerInterface;
12 use Drupal\Core\Plugin\CategorizingPluginManagerTrait;
13 use Drupal\Core\Plugin\DefaultPluginManager;
14 use Drupal\Tests\UnitTestCase;
17 * @coversDefaultClass \Drupal\Core\Plugin\CategorizingPluginManagerTrait
20 class CategorizingPluginManagerTraitTest extends UnitTestCase {
23 * The plugin manager to test.
25 * @var \Drupal\Component\Plugin\CategorizingPluginManagerInterface|\PHPUnit_Framework_MockObject_MockObject
27 protected $pluginManager;
32 protected function setUp() {
33 $module_handler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
34 $module_handler->expects($this->any())
35 ->method('getModuleList')
36 ->willReturn(['node' => []]);
37 $module_handler->expects($this->any())
42 $this->pluginManager = new CategorizingPluginManager($module_handler);
43 $this->pluginManager->setStringTranslation($this->getStringTranslationStub());
47 * @covers ::getCategories
49 public function testGetCategories() {
53 ], array_values($this->pluginManager->getCategories()));
57 * @covers ::getSortedDefinitions
59 public function testGetSortedDefinitions() {
60 $sorted = $this->pluginManager->getSortedDefinitions();
61 $this->assertSame(['apple', 'mango', 'cucumber'], array_keys($sorted));
65 * @covers ::getGroupedDefinitions
67 public function testGetGroupedDefinitions() {
68 $grouped = $this->pluginManager->getGroupedDefinitions();
69 $this->assertSame(['fruits', 'vegetables'], array_keys($grouped));
70 $this->assertSame(['apple', 'mango'], array_keys($grouped['fruits']));
71 $this->assertSame(['cucumber'], array_keys($grouped['vegetables']));
75 * @covers ::processDefinitionCategory
77 public function testProcessDefinitionCategory() {
84 $this->pluginManager->processDefinition($definition, 'some');
85 $this->assertSame('bag', $definition['category']);
87 // No category, provider without label.
92 $this->pluginManager->processDefinition($definition, 'some');
93 $this->assertSame('core', $definition['category']);
95 // No category, provider is module with label.
100 $this->pluginManager->processDefinition($definition, 'some');
101 $this->assertSame('Node', $definition['category']);
107 * Class that allows testing the trait.
109 class CategorizingPluginManager extends DefaultPluginManager implements CategorizingPluginManagerInterface {
111 use CategorizingPluginManagerTrait;
114 * Replace the constructor so we can instantiate a stub.
116 * @param \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject $module_handler
117 * The module handler.
119 public function __construct(ModuleHandlerInterface $module_handler) {
120 $this->moduleHandler = $module_handler;
126 * Provides some test definitions to the trait.
128 public function getDefinitions() {
131 'label' => 'cucumber',
132 'category' => 'vegetables',
136 'category' => 'fruits',
140 'category' => 'fruits',
148 public function processDefinition(&$definition, $plugin_id) {
149 parent::processDefinition($definition, $plugin_id);
150 $this->processDefinitionCategory($definition);