3 namespace Drupal\Tests\Core\Block;
5 use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
6 use Drupal\Core\Block\BlockManager;
7 use Drupal\Core\Cache\CacheBackendInterface;
8 use Drupal\Core\Extension\ModuleHandlerInterface;
9 use Drupal\Tests\UnitTestCase;
12 * @coversDefaultClass \Drupal\Core\Block\BlockManager
16 class BlockManagerTest extends UnitTestCase {
19 * The block manager under test.
21 * @var \Drupal\Core\Block\BlockManager
23 protected $blockManager;
28 protected function setUp() {
31 $cache_backend = $this->prophesize(CacheBackendInterface::class);
32 $module_handler = $this->prophesize(ModuleHandlerInterface::class);
33 $this->blockManager = new BlockManager(new \ArrayObject(), $cache_backend->reveal(), $module_handler->reveal());
34 $this->blockManager->setStringTranslation($this->getStringTranslationStub());
36 $discovery = $this->prophesize(DiscoveryInterface::class);
37 // Specify the 'broken' block, as well as 3 other blocks with admin labels
38 // that are purposefully not in alphabetical order.
39 $discovery->getDefinitions()->willReturn([
41 'admin_label' => 'Broken/Missing',
42 'category' => 'Block',
45 'admin_label' => 'Coconut',
46 'category' => 'Group 2',
49 'admin_label' => 'Apple',
50 'category' => 'Group 1',
53 'admin_label' => 'Banana',
54 'category' => 'Group 2',
57 // Force the discovery object onto the block manager.
58 $property = new \ReflectionProperty(BlockManager::class, 'discovery');
59 $property->setAccessible(TRUE);
60 $property->setValue($this->blockManager, $discovery->reveal());
64 * @covers ::getDefinitions
66 public function testDefinitions() {
67 $definitions = $this->blockManager->getDefinitions();
68 $this->assertSame(['broken', 'block1', 'block2', 'block3'], array_keys($definitions));
72 * @covers ::getSortedDefinitions
74 public function testSortedDefinitions() {
75 $definitions = $this->blockManager->getSortedDefinitions();
76 $this->assertSame(['block2', 'block3', 'block1'], array_keys($definitions));
80 * @covers ::getGroupedDefinitions
82 public function testGroupedDefinitions() {
83 $definitions = $this->blockManager->getGroupedDefinitions();
84 $this->assertSame(['Group 1', 'Group 2'], array_keys($definitions));
85 $this->assertSame(['block2'], array_keys($definitions['Group 1']));
86 $this->assertSame(['block3', 'block1'], array_keys($definitions['Group 2']));