From 67f58f64abfb8457bf3e5db2d1db919670916cc7 Mon Sep 17 00:00:00 2001 From: Gabor Hojtsy Date: Tue, 13 Jun 2017 16:35:19 +0200 Subject: [PATCH] Issue #2884769 by tim.plunkett, larowlan, tedbow: \Drupal\Core\Block\BlockManager::getGroupedDefinitions() triggers a notice on every call --- core/lib/Drupal/Core/Block/BlockManager.php | 13 +-- .../Tests/Core/Block/BlockManagerTest.php | 89 +++++++++++++++++++ 2 files changed, 90 insertions(+), 12 deletions(-) create mode 100644 core/tests/Drupal/Tests/Core/Block/BlockManagerTest.php diff --git a/core/lib/Drupal/Core/Block/BlockManager.php b/core/lib/Drupal/Core/Block/BlockManager.php index a837340a3c24..30b52b612566 100644 --- a/core/lib/Drupal/Core/Block/BlockManager.php +++ b/core/lib/Drupal/Core/Block/BlockManager.php @@ -20,7 +20,6 @@ class BlockManager extends DefaultPluginManager implements BlockManagerInterface use CategorizingPluginManagerTrait { getSortedDefinitions as traitGetSortedDefinitions; - getGroupedDefinitions as traitGetGroupedDefinitions; } use ContextAwarePluginManagerTrait; @@ -54,23 +53,13 @@ class BlockManager extends DefaultPluginManager implements BlockManagerInterface * {@inheritdoc} */ public function getSortedDefinitions(array $definitions = NULL) { - // Sort the plugins first by category, then by label. + // Sort the plugins first by category, then by admin label. $definitions = $this->traitGetSortedDefinitions($definitions, 'admin_label'); // Do not display the 'broken' plugin in the UI. unset($definitions['broken']); return $definitions; } - /** - * {@inheritdoc} - */ - public function getGroupedDefinitions(array $definitions = NULL) { - $definitions = $this->traitGetGroupedDefinitions($definitions, 'admin_label'); - // Do not display the 'broken' plugin in the UI. - unset($definitions[$this->t('Block')]['broken']); - return $definitions; - } - /** * {@inheritdoc} */ diff --git a/core/tests/Drupal/Tests/Core/Block/BlockManagerTest.php b/core/tests/Drupal/Tests/Core/Block/BlockManagerTest.php new file mode 100644 index 000000000000..1e9bb436db5d --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Block/BlockManagerTest.php @@ -0,0 +1,89 @@ +prophesize(CacheBackendInterface::class); + $module_handler = $this->prophesize(ModuleHandlerInterface::class); + $this->blockManager = new BlockManager(new \ArrayObject(), $cache_backend->reveal(), $module_handler->reveal()); + $this->blockManager->setStringTranslation($this->getStringTranslationStub()); + + $discovery = $this->prophesize(DiscoveryInterface::class); + // Specify the 'broken' block, as well as 3 other blocks with admin labels + // that are purposefully not in alphabetical order. + $discovery->getDefinitions()->willReturn([ + 'broken' => [ + 'admin_label' => 'Broken/Missing', + 'category' => 'Block', + ], + 'block1' => [ + 'admin_label' => 'Coconut', + 'category' => 'Group 2', + ], + 'block2' => [ + 'admin_label' => 'Apple', + 'category' => 'Group 1', + ], + 'block3' => [ + 'admin_label' => 'Banana', + 'category' => 'Group 2', + ], + ]); + // Force the discovery object onto the block manager. + $property = new \ReflectionProperty(BlockManager::class, 'discovery'); + $property->setAccessible(TRUE); + $property->setValue($this->blockManager, $discovery->reveal()); + } + + /** + * @covers ::getDefinitions + */ + public function testDefinitions() { + $definitions = $this->blockManager->getDefinitions(); + $this->assertSame(['broken', 'block1', 'block2', 'block3'], array_keys($definitions)); + } + + /** + * @covers ::getSortedDefinitions + */ + public function testSortedDefinitions() { + $definitions = $this->blockManager->getSortedDefinitions(); + $this->assertSame(['block2', 'block3', 'block1'], array_keys($definitions)); + } + + /** + * @covers ::getGroupedDefinitions + */ + public function testGroupedDefinitions() { + $definitions = $this->blockManager->getGroupedDefinitions(); + $this->assertSame(['Group 1', 'Group 2'], array_keys($definitions)); + $this->assertSame(['block2'], array_keys($definitions['Group 1'])); + $this->assertSame(['block3', 'block1'], array_keys($definitions['Group 2'])); + } + +}