diff --git a/core/lib/Drupal/Core/Plugin/CategorizingPluginManagerTrait.php b/core/lib/Drupal/Core/Plugin/CategorizingPluginManagerTrait.php index 66fa31c..b3cc23b 100644 --- a/core/lib/Drupal/Core/Plugin/CategorizingPluginManagerTrait.php +++ b/core/lib/Drupal/Core/Plugin/CategorizingPluginManagerTrait.php @@ -24,13 +24,6 @@ use StringTranslationTrait; /** - * An array of all available modules and their data. - * - * @var array - */ - protected $moduleData; - - /** * Processes a plugin definition to ensure there is a category. * * If the definition lacks a category, it defaults to the providing module. @@ -43,31 +36,43 @@ protected function processDefinitionCategory(&$definition) { if (empty($definition['category'])) { // Default to the human readable module name if the provider is a module; // otherwise the provider machine name is used. - $definition['category'] = $this->getModuleName($definition['provider']); + $definition['category'] = $this->getProviderName($definition['provider']); } } /** - * Gets the name of the module. + * Gets the name of a provider. * - * @param string $module - * The machine name of a module. + * @param string $provider + * The machine name of a plugin provider. * * @return string * The human-readable module name if it exists, otherwise the - * machine-readable module name. + * machine-readable name passed. */ - protected function getModuleName($module) { - // Gather module data. - if (!isset($this->moduleData)) { - $this->moduleData = system_get_info('module'); - } + protected function getProviderName($provider) { + $list = $this->getModuleHandler()->getModuleList(); // If the module exists, return its human-readable name. - if (isset($this->moduleData[$module])) { - return $this->t($this->moduleData[$module]['name']); + if (isset($list[$provider])) { + return $this->getModuleHandler()->getName($provider); } // Otherwise, return the machine name. - return $module; + return $provider; + } + + /** + * Returns the module handler used. + * + * @return \Drupal\Core\Extension\ModuleHandlerInterface + * The module handler. + */ + public function getModuleHandler() { + // If the class has an injected module handler, use it. Otherwise fall back + // to fetch it from the service container. + if (isset($this->moduleHandler)) { + return $this->moduleHandler; + } + return \Drupal::moduleHandler(); } /** @@ -75,6 +80,7 @@ protected function getModuleName($module) { */ public function getCategories() { /** @var \Drupal\Core\Plugin\CategorizingPluginManagerTrait|\Drupal\Component\Plugin\PluginManagerInterface $this */ + // Fetch all categories from definitions and remove duplicates. $categories = array_unique(array_values(array_map(function ($definition) { return $definition['category']; }, $this->getDefinitions()))); diff --git a/core/tests/Drupal/Tests/Core/Plugin/CategorizingPluginManagerTraitTest.php b/core/tests/Drupal/Tests/Core/Plugin/CategorizingPluginManagerTraitTest.php index 8c6fee9..db67ad7 100644 --- a/core/tests/Drupal/Tests/Core/Plugin/CategorizingPluginManagerTraitTest.php +++ b/core/tests/Drupal/Tests/Core/Plugin/CategorizingPluginManagerTraitTest.php @@ -5,152 +5,149 @@ * Contains \Drupal\Tests\Core\Plugin\CategorizingPluginManagerTraitTest. */ -namespace Drupal\Tests\Core\Plugin { +namespace Drupal\Tests\Core\Plugin; - use Drupal\Component\Plugin\CategorizingPluginManagerInterface; - use Drupal\Core\Plugin\CategorizingPluginManagerTrait; - use Drupal\Core\Plugin\DefaultPluginManager; - use Drupal\Tests\UnitTestCase; +use Drupal\Component\Plugin\CategorizingPluginManagerInterface; +use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\Plugin\CategorizingPluginManagerTrait; +use Drupal\Core\Plugin\DefaultPluginManager; +use Drupal\Tests\UnitTestCase; + +/** + * @coversDefaultClass \Drupal\Core\Plugin\CategorizingPluginManagerTrait + * @group Plugin + */ +class CategorizingPluginManagerTraitTest extends UnitTestCase { /** - * @coversDefaultClass \Drupal\Core\Plugin\CategorizingPluginManagerTrait - * @group Plugin + * The plugin manager to test. + * + * @var \Drupal\Component\Plugin\CategorizingPluginManagerInterface|\PHPUnit_Framework_MockObject_MockObject */ - class CategorizingPluginManagerTraitTest extends UnitTestCase { - - /** - * The plugin manager to test. - * - * @var \Drupal\Component\Plugin\CategorizingPluginManagerInterface|\PHPUnit_Framework_MockObject_MockObject - */ - protected $pluginManager; - - /** - * {@inheritdoc} - */ - protected function setUp() { - $this->pluginManager = new CategorizingPluginManagerImpl(); - $this->pluginManager->setStringTranslation($this->getStringTranslationStub()); - } - - /** - * @covers ::getCategories - */ - public function testGetCategories() { - $this->assertSame(array_values($this->pluginManager->getCategories()), [ - 'fruits', - 'vegetables', - ]); - } - - /** - * @covers ::getSortedDefinitions - */ - public function testGetSortedDefinitions() { - $sorted = $this->pluginManager->getSortedDefinitions(); - $this->assertSame(array_keys($sorted), ['apple', 'mango', 'cucumber']); - } - - /** - * @covers ::getGroupedDefinitions - */ - public function testGetGroupedDefinitions() { - $grouped = $this->pluginManager->getGroupedDefinitions(); - $this->assertSame(array_keys($grouped), ['fruits', 'vegetables']); - $this->assertSame(array_keys($grouped['fruits']), ['apple', 'mango']); - $this->assertSame(array_keys($grouped['vegetables']), ['cucumber']); - } - - /** - * @covers ::processDefinitionCategory - */ - public function testProcessDefinitionCategory() { - // Existing category. - $definition = [ - 'label' => 'some', - 'provider' => 'core', - 'category' => 'bag', - ]; - $this->pluginManager->processDefinition($definition, 'some'); - $this->assertSame($definition['category'], 'bag'); - - // No category, provider without label. - $definition = [ - 'label' => 'some', - 'provider' => 'core', - ]; - $this->pluginManager->processDefinition($definition, 'some'); - $this->assertSame($definition['category'], 'core'); - - // No category, provider is module with label. - $definition = [ - 'label' => 'some', - 'provider' => 'node', - ]; - $this->pluginManager->processDefinition($definition, 'some'); - $this->assertSame($definition['category'], 'Node'); - } + protected $pluginManager; + /** + * {@inheritdoc} + */ + protected function setUp() { + $module_handler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface'); + $module_handler->expects($this->any()) + ->method('getModuleList') + ->willReturn(['node' => []]); + $module_handler->expects($this->any()) + ->method('getName') + ->with('node') + ->willReturn('Node'); + + $this->pluginManager = new CategorizingPluginManager($module_handler); + $this->pluginManager->setStringTranslation($this->getStringTranslationStub()); } /** - * Class that allows testing the trait. + * @covers ::getCategories */ - class CategorizingPluginManagerImpl extends DefaultPluginManager implements CategorizingPluginManagerInterface { - - use CategorizingPluginManagerTrait; - - /** - * Replace the constructor so we can instantiate a stub. - */ - public function __construct() { - } - - /** - * {@inheritdoc} - * - * Provides some test definitions to the trait. - */ - public function getDefinitions() { - return [ - 'cucumber' => [ - 'label' => 'cucumber', - 'category' => 'vegetables', - ], - 'apple' => [ - 'label' => 'apple', - 'category' => 'fruits', - ], - 'mango' => [ - 'label' => 'mango', - 'category' => 'fruits', - ], - ]; - } - - /** - * {@inheritdoc} - */ - public function processDefinition(&$definition, $plugin_id) { - parent::processDefinition($definition, $plugin_id); - $this->processDefinitionCategory($definition); - } + public function testGetCategories() { + $this->assertSame(array_values($this->pluginManager->getCategories()), [ + 'fruits', + 'vegetables', + ]); + } + /** + * @covers ::getSortedDefinitions + */ + public function testGetSortedDefinitions() { + $sorted = $this->pluginManager->getSortedDefinitions(); + $this->assertSame(array_keys($sorted), ['apple', 'mango', 'cucumber']); + } + + /** + * @covers ::getGroupedDefinitions + */ + public function testGetGroupedDefinitions() { + $grouped = $this->pluginManager->getGroupedDefinitions(); + $this->assertSame(array_keys($grouped), ['fruits', 'vegetables']); + $this->assertSame(array_keys($grouped['fruits']), ['apple', 'mango']); + $this->assertSame(array_keys($grouped['vegetables']), ['cucumber']); + } + + /** + * @covers ::processDefinitionCategory + */ + public function testProcessDefinitionCategory() { + // Existing category. + $definition = [ + 'label' => 'some', + 'provider' => 'core', + 'category' => 'bag', + ]; + $this->pluginManager->processDefinition($definition, 'some'); + $this->assertSame($definition['category'], 'bag'); + + // No category, provider without label. + $definition = [ + 'label' => 'some', + 'provider' => 'core', + ]; + $this->pluginManager->processDefinition($definition, 'some'); + $this->assertSame($definition['category'], 'core'); + + // No category, provider is module with label. + $definition = [ + 'label' => 'some', + 'provider' => 'node', + ]; + $this->pluginManager->processDefinition($definition, 'some'); + $this->assertSame($definition['category'], 'Node'); } } -namespace { +/** + * Class that allows testing the trait. + */ +class CategorizingPluginManager extends DefaultPluginManager implements CategorizingPluginManagerInterface { + + use CategorizingPluginManagerTrait; /** - * "Mocks" system_get_info() as needed for the test. + * Replace the constructor so we can instantiate a stub. + * + * @param \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject $module_handler + * The module handler. */ - if (!function_exists('system_get_info')) { + public function __construct(ModuleHandlerInterface $module_handler) { + $this->moduleHandler = $module_handler; + } - function system_get_info($type, $name = NULL) { - return ['node' => ['name' => 'Node']]; - } + /** + * {@inheritdoc} + * + * Provides some test definitions to the trait. + */ + public function getDefinitions() { + return [ + 'cucumber' => [ + 'label' => 'cucumber', + 'category' => 'vegetables', + ], + 'apple' => [ + 'label' => 'apple', + 'category' => 'fruits', + ], + 'mango' => [ + 'label' => 'mango', + 'category' => 'fruits', + ], + ]; + } + /** + * {@inheritdoc} + */ + public function processDefinition(&$definition, $plugin_id) { + parent::processDefinition($definition, $plugin_id); + $this->processDefinitionCategory($definition); } }