You are here

public function RegionViewBuilderTest::testRegionViewBuilder in Twig Tweak 3.x

Same name and namespace in other branches
  1. 3.1.x tests/src/Kernel/RegionViewBuilderTest.php \Drupal\Tests\twig_tweak\Kernel\RegionViewBuilderTest::testRegionViewBuilder()

Test callback.

File

tests/src/Kernel/RegionViewBuilderTest.php, line 59

Class

RegionViewBuilderTest
A test for RegionViewBuilder.

Namespace

Drupal\Tests\twig_tweak\Kernel

Code

public function testRegionViewBuilder() : void {
  $view_builder = $this->container
    ->get('twig_tweak.region_view_builder');
  $renderer = $this->container
    ->get('renderer');
  $build = $view_builder
    ->build('sidebar_first');

  // The build should be empty because 'stable' is not a default theme.
  $expected_build = [
    '#cache' => [
      'contexts' => [],
      'tags' => [
        'config:block_list',
      ],
      'max-age' => Cache::PERMANENT,
    ],
  ];
  self::assertSame($expected_build, $build);

  // Specify the theme name explicitly.
  $build = $view_builder
    ->build('sidebar_first', 'stable');
  $expected_build = [
    // Only public_block should be rendered.
    // @see twig_tweak_test_block_access()
    'public_block' => [
      '#cache' => [
        'keys' => [
          'entity_view',
          'block',
          'public_block',
        ],
        'contexts' => [],
        'tags' => [
          'block_view',
          'config:block.block.public_block',
        ],
        'max-age' => Cache::PERMANENT,
      ],
      '#weight' => NULL,
      '#lazy_builder' => [
        'Drupal\\block\\BlockViewBuilder::lazyBuilder',
        [
          'public_block',
          'full',
          NULL,
        ],
      ],
    ],
    '#region' => 'sidebar_first',
    '#theme_wrappers' => [
      'region',
    ],
    // Even if the block is not accessible its cache metadata from access
    // callback should be here.
    '#cache' => [
      'contexts' => [
        'user',
      ],
      'tags' => [
        'config:block.block.public_block',
        'config:block_list',
        'tag_for_private_block',
        'tag_for_public_block',
      ],
      'max-age' => 123,
    ],
  ];
  self::assertSame($expected_build, $build);
  $expected_html = <<<'HTML'
      <div>
        <div id="block-public-block">
          <span>Powered by <a href="https://www.drupal.org">Drupal</a></span>
        </div>
      </div>
HTML;
  $actual_html = $renderer
    ->renderPlain($build);
  self::assertSame(self::normalizeHtml($expected_html), self::normalizeHtml($actual_html));

  // Set 'stable' as default site theme and check if the view builder without
  // 'theme' argument returns the same result.
  $this->container
    ->get('config.factory')
    ->getEditable('system.theme')
    ->set('default', 'stable')
    ->save();
  $build = $view_builder
    ->build('sidebar_first');
  self::assertSame($expected_build, $build);
  Html::resetSeenIds();
  $actual_html = $renderer
    ->renderPlain($expected_build);
  self::assertSame(self::normalizeHtml($expected_html), self::normalizeHtml($actual_html));
}