Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / block / tests / src / Unit / BlockRepositoryTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\block\Unit\BlockRepositoryTest.
6  */
7
8 namespace Drupal\Tests\block\Unit;
9
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;
15
16 /**
17  * @coversDefaultClass \Drupal\block\BlockRepository
18  * @group block
19  */
20 class BlockRepositoryTest extends UnitTestCase {
21
22   /**
23    * @var \Drupal\block\BlockRepository
24    */
25   protected $blockRepository;
26
27   /**
28    * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
29    */
30   protected $blockStorage;
31
32   /**
33    * @var string
34    */
35   protected $theme;
36
37   /**
38    * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
39    */
40   protected $contextHandler;
41
42   /**
43    * {@inheritdoc}
44    */
45   protected function setUp() {
46     parent::setUp();
47     $active_theme = $this->getMockBuilder('Drupal\Core\Theme\ActiveTheme')
48       ->disableOriginalConstructor()
49       ->getMock();
50     $this->theme = $this->randomMachineName();
51     $active_theme->expects($this->atLeastOnce())
52       ->method('getName')
53       ->willReturn($this->theme);
54     $active_theme->expects($this->atLeastOnce())
55       ->method('getRegions')
56       ->willReturn([
57         'top',
58         'center',
59         'bottom',
60       ]);
61
62     $theme_manager = $this->getMock('Drupal\Core\Theme\ThemeManagerInterface');
63     $theme_manager->expects($this->atLeastOnce())
64       ->method('getActiveTheme')
65       ->will($this->returnValue($active_theme));
66
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);
73
74     $this->blockRepository = new BlockRepository($entity_manager, $theme_manager, $this->contextHandler);
75   }
76
77   /**
78    * Tests the retrieval of block entities.
79    *
80    * @covers ::getVisibleBlocksPerRegion
81    *
82    * @dataProvider providerBlocksConfig
83    */
84   public function testGetVisibleBlocksPerRegion(array $blocks_config, array $expected_blocks) {
85     $blocks = [];
86     foreach ($blocks_config as $block_id => $block_config) {
87       $block = $this->getMock('Drupal\block\BlockInterface');
88       $block->expects($this->once())
89         ->method('access')
90         ->will($this->returnValue($block_config[0]));
91       $block->expects($block_config[0] ? $this->atLeastOnce() : $this->never())
92         ->method('getRegion')
93         ->willReturn($block_config[1]);
94       $block->expects($this->any())
95         ->method('label')
96         ->willReturn($block_id);
97       $block->expects($this->any())
98         ->method('getWeight')
99         ->willReturn($block_config[2]);
100       $blocks[$block_id] = $block;
101     }
102
103     $this->blockStorage->expects($this->once())
104       ->method('loadByProperties')
105       ->with(['theme' => $this->theme])
106       ->willReturn($blocks);
107     $result = [];
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;
113       }
114     }
115     $this->assertEquals($expected_blocks, $result);
116   }
117
118   public function providerBlocksConfig() {
119     $blocks_config = [
120       'block1' => [
121         AccessResult::allowed(), 'top', 0
122       ],
123       // Test a block without access.
124       'block2' => [
125         AccessResult::forbidden(), 'bottom', 0
126       ],
127       // Test some blocks in the same region with specific weight.
128       'block4' => [
129         AccessResult::allowed(), 'bottom', 5
130       ],
131       'block3' => [
132         AccessResult::allowed(), 'bottom', 5
133       ],
134       'block5' => [
135         AccessResult::allowed(), 'bottom', -5
136       ],
137     ];
138
139     $test_cases = [];
140     $test_cases[] = [$blocks_config,
141       [
142         'top' => ['block1'],
143         'center' => [],
144         'bottom' => ['block5', 'block3', 'block4'],
145       ]
146     ];
147     return $test_cases;
148   }
149
150   /**
151    * Tests the retrieval of block entities that are context-aware.
152    *
153    * @covers ::getVisibleBlocksPerRegion
154    */
155   public function testGetVisibleBlocksPerRegionWithContext() {
156     $block = $this->getMock('Drupal\block\BlockInterface');
157     $block->expects($this->once())
158       ->method('access')
159       ->willReturn(AccessResult::allowed()->addCacheTags(['config:block.block.block_id']));
160     $block->expects($this->once())
161       ->method('getRegion')
162       ->willReturn('top');
163     $blocks['block_id'] = $block;
164
165     $this->blockStorage->expects($this->once())
166       ->method('loadByProperties')
167       ->with(['theme' => $this->theme])
168       ->willReturn($blocks);
169     $result = [];
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;
175       }
176     }
177     $expected = [
178       'top' => [
179         'block_id',
180       ],
181       'center' => [],
182       'bottom' => [],
183     ];
184     $this->assertSame($expected, $result);
185
186     // Assert that the cacheable metadata from the block access results was
187     // collected.
188     $this->assertEquals(['config:block.block.block_id'], $cacheable_metadata['top']->getCacheTags());
189   }
190
191 }
192
193 interface TestContextAwareBlockInterface extends BlockPluginInterface, ContextAwarePluginInterface {
194 }