5 * Contains \Drupal\Tests\block\Unit\BlockRepositoryTest.
8 namespace Drupal\Tests\block\Unit;
10 use Drupal\block\BlockRepository;
11 use Drupal\Core\Access\AccessResult;
12 use Drupal\Core\Block\BlockPluginInterface;
13 use Drupal\Core\Plugin\ContextAwarePluginInterface;
14 use Drupal\Tests\UnitTestCase;
17 * @coversDefaultClass \Drupal\block\BlockRepository
20 class BlockRepositoryTest extends UnitTestCase {
23 * @var \Drupal\block\BlockRepository
25 protected $blockRepository;
28 * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
30 protected $blockStorage;
38 * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
40 protected $contextHandler;
45 protected function setUp() {
47 $active_theme = $this->getMockBuilder('Drupal\Core\Theme\ActiveTheme')
48 ->disableOriginalConstructor()
50 $this->theme = $this->randomMachineName();
51 $active_theme->expects($this->atLeastOnce())
53 ->willReturn($this->theme);
54 $active_theme->expects($this->atLeastOnce())
55 ->method('getRegions')
62 $theme_manager = $this->getMock('Drupal\Core\Theme\ThemeManagerInterface');
63 $theme_manager->expects($this->atLeastOnce())
64 ->method('getActiveTheme')
65 ->will($this->returnValue($active_theme));
67 $this->contextHandler = $this->getMock('Drupal\Core\Plugin\Context\ContextHandlerInterface');
68 $this->blockStorage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
69 $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
70 $entity_manager->expects($this->any())
71 ->method('getStorage')
72 ->willReturn($this->blockStorage);
74 $this->blockRepository = new BlockRepository($entity_manager, $theme_manager, $this->contextHandler);
78 * Tests the retrieval of block entities.
80 * @covers ::getVisibleBlocksPerRegion
82 * @dataProvider providerBlocksConfig
84 public function testGetVisibleBlocksPerRegion(array $blocks_config, array $expected_blocks) {
86 foreach ($blocks_config as $block_id => $block_config) {
87 $block = $this->getMock('Drupal\block\BlockInterface');
88 $block->expects($this->once())
90 ->will($this->returnValue($block_config[0]));
91 $block->expects($block_config[0] ? $this->atLeastOnce() : $this->never())
93 ->willReturn($block_config[1]);
94 $block->expects($this->any())
96 ->willReturn($block_id);
97 $block->expects($this->any())
99 ->willReturn($block_config[2]);
100 $blocks[$block_id] = $block;
103 $this->blockStorage->expects($this->once())
104 ->method('loadByProperties')
105 ->with(['theme' => $this->theme])
106 ->willReturn($blocks);
108 $cacheable_metadata = [];
109 foreach ($this->blockRepository->getVisibleBlocksPerRegion($cacheable_metadata) as $region => $resulting_blocks) {
110 $result[$region] = [];
111 foreach ($resulting_blocks as $plugin_id => $block) {
112 $result[$region][] = $plugin_id;
115 $this->assertEquals($expected_blocks, $result);
118 public function providerBlocksConfig() {
121 AccessResult::allowed(), 'top', 0
123 // Test a block without access.
125 AccessResult::forbidden(), 'bottom', 0
127 // Test some blocks in the same region with specific weight.
129 AccessResult::allowed(), 'bottom', 5
132 AccessResult::allowed(), 'bottom', 5
135 AccessResult::allowed(), 'bottom', -5
140 $test_cases[] = [$blocks_config,
144 'bottom' => ['block5', 'block3', 'block4'],
151 * Tests the retrieval of block entities that are context-aware.
153 * @covers ::getVisibleBlocksPerRegion
155 public function testGetVisibleBlocksPerRegionWithContext() {
156 $block = $this->getMock('Drupal\block\BlockInterface');
157 $block->expects($this->once())
159 ->willReturn(AccessResult::allowed()->addCacheTags(['config:block.block.block_id']));
160 $block->expects($this->once())
161 ->method('getRegion')
163 $blocks['block_id'] = $block;
165 $this->blockStorage->expects($this->once())
166 ->method('loadByProperties')
167 ->with(['theme' => $this->theme])
168 ->willReturn($blocks);
170 $cacheable_metadata = [];
171 foreach ($this->blockRepository->getVisibleBlocksPerRegion($cacheable_metadata) as $region => $resulting_blocks) {
172 $result[$region] = [];
173 foreach ($resulting_blocks as $plugin_id => $block) {
174 $result[$region][] = $plugin_id;
184 $this->assertSame($expected, $result);
186 // Assert that the cacheable metadata from the block access results was
188 $this->assertEquals(['config:block.block.block_id'], $cacheable_metadata['top']->getCacheTags());
193 interface TestContextAwareBlockInterface extends BlockPluginInterface, ContextAwarePluginInterface {