Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Plugin / CategorizingPluginManagerTraitTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\Core\Plugin\CategorizingPluginManagerTraitTest.
6  */
7
8 namespace Drupal\Tests\Core\Plugin;
9
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;
15
16 /**
17  * @coversDefaultClass \Drupal\Core\Plugin\CategorizingPluginManagerTrait
18  * @group Plugin
19  */
20 class CategorizingPluginManagerTraitTest extends UnitTestCase {
21
22   /**
23    * The plugin manager to test.
24    *
25    * @var \Drupal\Component\Plugin\CategorizingPluginManagerInterface|\PHPUnit_Framework_MockObject_MockObject
26    */
27   protected $pluginManager;
28
29   /**
30    * {@inheritdoc}
31    */
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())
38       ->method('getName')
39       ->with('node')
40       ->willReturn('Node');
41
42     $this->pluginManager = new CategorizingPluginManager($module_handler);
43     $this->pluginManager->setStringTranslation($this->getStringTranslationStub());
44   }
45
46   /**
47    * @covers ::getCategories
48    */
49   public function testGetCategories() {
50     $this->assertSame([
51         'fruits',
52         'vegetables',
53       ], array_values($this->pluginManager->getCategories()));
54   }
55
56   /**
57    * @covers ::getSortedDefinitions
58    */
59   public function testGetSortedDefinitions() {
60     $sorted = $this->pluginManager->getSortedDefinitions();
61     $this->assertSame(['apple', 'mango', 'cucumber'], array_keys($sorted));
62   }
63
64   /**
65    * @covers ::getGroupedDefinitions
66    */
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']));
72   }
73
74   /**
75    * @covers ::processDefinitionCategory
76    */
77   public function testProcessDefinitionCategory() {
78     // Existing category.
79     $definition = [
80       'label' => 'some',
81       'provider' => 'core',
82       'category' => 'bag',
83     ];
84     $this->pluginManager->processDefinition($definition, 'some');
85     $this->assertSame('bag', $definition['category']);
86
87     // No category, provider without label.
88     $definition = [
89       'label' => 'some',
90       'provider' => 'core',
91     ];
92     $this->pluginManager->processDefinition($definition, 'some');
93     $this->assertSame('core', $definition['category']);
94
95     // No category, provider is module with label.
96     $definition = [
97       'label' => 'some',
98       'provider' => 'node',
99     ];
100     $this->pluginManager->processDefinition($definition, 'some');
101     $this->assertSame('Node', $definition['category']);
102   }
103
104 }
105
106 /**
107  * Class that allows testing the trait.
108  */
109 class CategorizingPluginManager extends DefaultPluginManager implements CategorizingPluginManagerInterface {
110
111   use CategorizingPluginManagerTrait;
112
113   /**
114    * Replace the constructor so we can instantiate a stub.
115    *
116    * @param \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject $module_handler
117    *   The module handler.
118    */
119   public function __construct(ModuleHandlerInterface $module_handler) {
120     $this->moduleHandler = $module_handler;
121   }
122
123   /**
124    * {@inheritdoc}
125    *
126    * Provides some test definitions to the trait.
127    */
128   public function getDefinitions() {
129     return [
130       'cucumber' => [
131         'label' => 'cucumber',
132         'category' => 'vegetables',
133       ],
134       'apple' => [
135         'label' => 'apple',
136         'category' => 'fruits',
137       ],
138       'mango' => [
139         'label' => 'mango',
140         'category' => 'fruits',
141       ],
142     ];
143   }
144
145   /**
146    * {@inheritdoc}
147    */
148   public function processDefinition(&$definition, $plugin_id) {
149     parent::processDefinition($definition, $plugin_id);
150     $this->processDefinitionCategory($definition);
151   }
152
153 }