Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Block / BlockManagerTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Block;
4
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;
10
11 /**
12  * @coversDefaultClass \Drupal\Core\Block\BlockManager
13  *
14  * @group block
15  */
16 class BlockManagerTest extends UnitTestCase {
17
18   /**
19    * The block manager under test.
20    *
21    * @var \Drupal\Core\Block\BlockManager
22    */
23   protected $blockManager;
24
25   /**
26    * {@inheritdoc}
27    */
28   protected function setUp() {
29     parent::setUp();
30
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());
35
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([
40       'broken' => [
41         'admin_label' => 'Broken/Missing',
42         'category' => 'Block',
43       ],
44       'block1' => [
45         'admin_label' => 'Coconut',
46         'category' => 'Group 2',
47       ],
48       'block2' => [
49         'admin_label' => 'Apple',
50         'category' => 'Group 1',
51       ],
52       'block3' => [
53         'admin_label' => 'Banana',
54         'category' => 'Group 2',
55       ],
56     ]);
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());
61   }
62
63   /**
64    * @covers ::getDefinitions
65    */
66   public function testDefinitions() {
67     $definitions = $this->blockManager->getDefinitions();
68     $this->assertSame(['broken', 'block1', 'block2', 'block3'], array_keys($definitions));
69   }
70
71   /**
72    * @covers ::getSortedDefinitions
73    */
74   public function testSortedDefinitions() {
75     $definitions = $this->blockManager->getSortedDefinitions();
76     $this->assertSame(['block2', 'block3', 'block1'], array_keys($definitions));
77   }
78
79   /**
80    * @covers ::getGroupedDefinitions
81    */
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']));
87   }
88
89 }