commit 8eb25737d7fd7c9d4f1b5cf2905d9367d0f794f7 Author: Tim Plunkett Date: Sat Nov 8 14:59:01 2014 -0800 Update unit test coverage diff --git a/core/modules/block/tests/src/Unit/BlockRepositoryTest.php b/core/modules/block/tests/src/Unit/BlockRepositoryTest.php index fe9fa47..adb8417 100644 --- a/core/modules/block/tests/src/Unit/BlockRepositoryTest.php +++ b/core/modules/block/tests/src/Unit/BlockRepositoryTest.php @@ -7,6 +7,8 @@ namespace Drupal\Tests\block\Unit; +use Drupal\block\BlockInterface; +use Drupal\Component\Plugin\ContextAwarePluginInterface; use Drupal\Tests\UnitTestCase; /** @@ -16,44 +18,71 @@ class BlockRepositoryTest extends UnitTestCase { /** - * Tests the retrieval of block entities. - * - * @covers ::getVisibleBlocksPerRegion - * - * @dataProvider providerBlocksConfig + * @var \Drupal\block\BlockRepository */ - function testGetVisibleBlocksPerRegion(array $blocks_config, array $expected_blocks) { - $theme = $this->randomMachineName(); + protected $blockRepository; + + /** + * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $blockStorage; + + /** + * @var string + */ + protected $theme; + + /** + * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $contextHandler; + + /** + * {@inheritdoc} + */ + 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($theme); + ->willReturn($this->theme); + $theme_manager = $this->getMock('Drupal\Core\Theme\ThemeManagerInterface'); $theme_manager->expects($this->once()) ->method('getActiveTheme') ->will($this->returnValue($active_theme)); - $context_handler = $this->getMock('Drupal\Core\Plugin\Context\ContextHandlerInterface'); - $block_storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface'); + $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($block_storage); + ->willReturn($this->blockStorage); - $block_repository = $this->getMockBuilder('Drupal\block\BlockRepository') - ->setConstructorArgs([$entity_manager, $theme_manager, $context_handler]) + $this->blockRepository = $this->getMockBuilder('Drupal\block\BlockRepository') + ->setConstructorArgs([$entity_manager, $theme_manager, $this->contextHandler]) ->setMethods(['getRegionNames']) ->getMock(); - $block_repository->expects($this->once()) + $this->blockRepository->expects($this->once()) ->method('getRegionNames') ->willReturn([ 'top' => 'Top', 'center' => 'Center', 'bottom' => 'Bottom', ]); + } + /** + * Tests the retrieval of block entities. + * + * @covers ::getVisibleBlocksPerRegion + * + * @dataProvider providerBlocksConfig + */ + public function testGetVisibleBlocksPerRegion(array $blocks_config, array $expected_blocks) { $blocks = []; foreach ($blocks_config as $block_id => $block_config) { $block = $this->getMock('Drupal\block\BlockInterface'); @@ -70,12 +99,12 @@ function testGetVisibleBlocksPerRegion(array $blocks_config, array $expected_blo $blocks[$block_id] = $block; } - $block_storage->expects($this->once()) + $this->blockStorage->expects($this->once()) ->method('loadByProperties') - ->with(['theme' => $theme]) + ->with(['theme' => $this->theme]) ->willReturn($blocks); $result = []; - foreach ($block_repository->getVisibleBlocksPerRegion([]) as $region => $resulting_blocks) { + foreach ($this->blockRepository->getVisibleBlocksPerRegion([]) as $region => $resulting_blocks) { $result[$region] = []; foreach ($resulting_blocks as $plugin_id => $block) { $result[$region][] = $plugin_id; @@ -84,7 +113,6 @@ function testGetVisibleBlocksPerRegion(array $blocks_config, array $expected_blo $this->assertSame($result, $expected_blocks); } - public function providerBlocksConfig() { $blocks_config = array( 'block1' => array( @@ -114,4 +142,53 @@ public function providerBlocksConfig() { return $test_cases; } + /** + * Tests the retrieval of block entities that are context-aware. + * + * @covers ::getVisibleBlocksPerRegion + */ + public function testGetVisibleBlocksPerRegionWithContext() { + $block = $this->getMock('Drupal\block\BlockInterface'); + $block->expects($this->once()) + ->method('access') + ->willReturn(TRUE); + $block->expects($this->once()) + ->method('get') + ->with('region') + ->willReturn('top'); + $block_plugin = $this->getMock('Drupal\Tests\block\Unit\TestContextAwareBlockInterface'); + $block->expects($this->once()) + ->method('getPlugin') + ->willReturn($block_plugin); + $blocks['block_id'] = $block; + + $contexts = []; + $this->contextHandler->expects($this->once()) + ->method('applyContextMapping') + ->with($block_plugin, $contexts); + + $this->blockStorage->expects($this->once()) + ->method('loadByProperties') + ->with(['theme' => $this->theme]) + ->willReturn($blocks); + $result = []; + foreach ($this->blockRepository->getVisibleBlocksPerRegion($contexts) 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); + } + +} + +interface TestContextAwareBlockInterface extends BlockInterface, ContextAwarePluginInterface { }