function BlockRepositoryTest::testGetVisibleBlocksPerRegion

Same name in other branches
  1. 9 core/modules/block/tests/src/Unit/BlockRepositoryTest.php \Drupal\Tests\block\Unit\BlockRepositoryTest::testGetVisibleBlocksPerRegion()
  2. 10 core/modules/block/tests/src/Unit/BlockRepositoryTest.php \Drupal\Tests\block\Unit\BlockRepositoryTest::testGetVisibleBlocksPerRegion()
  3. 11.x core/modules/block/tests/src/Unit/BlockRepositoryTest.php \Drupal\Tests\block\Unit\BlockRepositoryTest::testGetVisibleBlocksPerRegion()

Tests the retrieval of block entities.

@covers ::getVisibleBlocksPerRegion

@dataProvider providerBlocksConfig

File

core/modules/block/tests/src/Unit/BlockRepositoryTest.php, line 86

Class

BlockRepositoryTest
@coversDefaultClass \Drupal\block\BlockRepository @group block

Namespace

Drupal\Tests\block\Unit

Code

public function testGetVisibleBlocksPerRegion(array $blocks_config, array $expected_blocks) {
    $blocks = [];
    foreach ($blocks_config as $block_id => $block_config) {
        $block = $this->createMock('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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.