View source
<?php
namespace Drupal\Tests\block\Unit;
use Drupal\block\BlockRepository;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Tests\UnitTestCase;
class BlockRepositoryTest extends UnitTestCase {
protected $blockRepository;
protected $blockStorage;
protected $theme;
protected $contextHandler;
protected function setUp() {
parent::setUp();
$active_theme = $this
->getMockBuilder('Drupal\\Core\\Theme\\ActiveTheme')
->disableOriginalConstructor()
->getMock();
$this->theme = $this
->randomMachineName();
$active_theme
->expects($this
->atLeastOnce())
->method('getName')
->willReturn($this->theme);
$active_theme
->expects($this
->atLeastOnce())
->method('getRegions')
->willReturn([
'top',
'center',
'bottom',
]);
$theme_manager = $this
->getMock('Drupal\\Core\\Theme\\ThemeManagerInterface');
$theme_manager
->expects($this
->atLeastOnce())
->method('getActiveTheme')
->will($this
->returnValue($active_theme));
$this->contextHandler = $this
->getMock('Drupal\\Core\\Plugin\\Context\\ContextHandlerInterface');
$this->blockStorage = $this
->getMock('Drupal\\Core\\Entity\\EntityStorageInterface');
$entity_manager = $this
->getMock('Drupal\\Core\\Entity\\EntityManagerInterface');
$entity_manager
->expects($this
->any())
->method('getStorage')
->willReturn($this->blockStorage);
$this->blockRepository = new BlockRepository($entity_manager, $theme_manager, $this->contextHandler);
}
public function testGetVisibleBlocksPerRegion(array $blocks_config, array $expected_blocks) {
$blocks = [];
foreach ($blocks_config as $block_id => $block_config) {
$block = $this
->getMock('Drupal\\block\\BlockInterface');
$block
->expects($this
->once())
->method('access')
->will($this
->returnValue($block_config[0]));
$block
->expects($block_config[0] ? $this
->atLeastOnce() : $this
->never())
->method('getRegion')
->willReturn($block_config[1]);
$block
->expects($this
->any())
->method('label')
->willReturn($block_id);
$block
->expects($this
->any())
->method('getWeight')
->willReturn($block_config[2]);
$blocks[$block_id] = $block;
}
$this->blockStorage
->expects($this
->once())
->method('loadByProperties')
->with([
'theme' => $this->theme,
])
->willReturn($blocks);
$result = [];
$cacheable_metadata = [];
foreach ($this->blockRepository
->getVisibleBlocksPerRegion($cacheable_metadata) as $region => $resulting_blocks) {
$result[$region] = [];
foreach ($resulting_blocks as $plugin_id => $block) {
$result[$region][] = $plugin_id;
}
}
$this
->assertEquals($expected_blocks, $result);
}
public function providerBlocksConfig() {
$blocks_config = array(
'block1' => array(
AccessResult::allowed(),
'top',
0,
),
'block2' => array(
AccessResult::forbidden(),
'bottom',
0,
),
'block4' => array(
AccessResult::allowed(),
'bottom',
5,
),
'block3' => array(
AccessResult::allowed(),
'bottom',
5,
),
'block5' => array(
AccessResult::allowed(),
'bottom',
-5,
),
);
$test_cases = [];
$test_cases[] = [
$blocks_config,
[
'top' => [
'block1',
],
'center' => [],
'bottom' => [
'block5',
'block3',
'block4',
],
],
];
return $test_cases;
}
public function testGetVisibleBlocksPerRegionWithContext() {
$block = $this
->getMock('Drupal\\block\\BlockInterface');
$block
->expects($this
->once())
->method('access')
->willReturn(AccessResult::allowed()
->addCacheTags([
'config:block.block.block_id',
]));
$block
->expects($this
->once())
->method('getRegion')
->willReturn('top');
$blocks['block_id'] = $block;
$this->blockStorage
->expects($this
->once())
->method('loadByProperties')
->with([
'theme' => $this->theme,
])
->willReturn($blocks);
$result = [];
$cacheable_metadata = [];
foreach ($this->blockRepository
->getVisibleBlocksPerRegion($cacheable_metadata) as $region => $resulting_blocks) {
$result[$region] = [];
foreach ($resulting_blocks as $plugin_id => $block) {
$result[$region][] = $plugin_id;
}
}
$expected = [
'top' => [
'block_id',
],
'center' => [],
'bottom' => [],
];
$this
->assertSame($expected, $result);
$this
->assertEquals([
'config:block.block.block_id',
], $cacheable_metadata['top']
->getCacheTags());
}
}
interface TestContextAwareBlockInterface extends BlockPluginInterface, ContextAwarePluginInterface {
}