You are here

class BlockRepositoryTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/block/tests/src/Unit/BlockRepositoryTest.php \Drupal\Tests\block\Unit\BlockRepositoryTest

@coversDefaultClass \Drupal\block\BlockRepository @group block

Hierarchy

Expanded class hierarchy of BlockRepositoryTest

File

core/modules/block/tests/src/Unit/BlockRepositoryTest.php, line 20
Contains \Drupal\Tests\block\Unit\BlockRepositoryTest.

Namespace

Drupal\Tests\block\Unit
View source
class BlockRepositoryTest extends UnitTestCase {

  /**
   * @var \Drupal\block\BlockRepository
   */
  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($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);
  }

  /**
   * 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');
      $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,
      ),
      // Test a block without access.
      'block2' => array(
        AccessResult::forbidden(),
        'bottom',
        0,
      ),
      // Test some blocks in the same region with specific weight.
      '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;
  }

  /**
   * 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(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);

    // Assert that the cacheable metadata from the block access results was
    // collected.
    $this
      ->assertEquals([
      'config:block.block.block_id',
    ], $cacheable_metadata['top']
      ->getCacheTags());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlockRepositoryTest::$blockRepository protected property
BlockRepositoryTest::$blockStorage protected property
BlockRepositoryTest::$contextHandler protected property
BlockRepositoryTest::$theme protected property
BlockRepositoryTest::providerBlocksConfig public function
BlockRepositoryTest::setUp protected function Overrides UnitTestCase::setUp
BlockRepositoryTest::testGetVisibleBlocksPerRegion public function Tests the retrieval of block entities.
BlockRepositoryTest::testGetVisibleBlocksPerRegionWithContext public function Tests the retrieval of block entities that are context-aware.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.