You are here

public function BlockViewBuilderTest::testBlockViewBuilderBuildAlter in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/block/tests/src/Kernel/BlockViewBuilderTest.php \Drupal\Tests\block\Kernel\BlockViewBuilderTest::testBlockViewBuilderBuildAlter()

Tests block build altering.

See also

hook_block_build_alter()

hook_block_build_BASE_BLOCK_ID_alter()

File

core/modules/block/tests/src/Kernel/BlockViewBuilderTest.php, line 221

Class

BlockViewBuilderTest
Tests the block view builder.

Namespace

Drupal\Tests\block\Kernel

Code

public function testBlockViewBuilderBuildAlter() {

  // Force a request via GET so we can test the render cache.
  $request = \Drupal::request();
  $request_method = $request->server
    ->get('REQUEST_METHOD');
  $request
    ->setMethod('GET');
  $default_keys = [
    'entity_view',
    'block',
    'test_block',
  ];
  $default_contexts = [];
  $default_tags = [
    'block_view',
    'config:block.block.test_block',
  ];
  $default_max_age = Cache::PERMANENT;

  // hook_block_build_alter() adds an additional cache key.
  $alter_add_key = $this
    ->randomMachineName();
  \Drupal::state()
    ->set('block_test_block_alter_cache_key', $alter_add_key);
  $this
    ->assertBlockRenderedWithExpectedCacheability(array_merge($default_keys, [
    $alter_add_key,
  ]), $default_contexts, $default_tags, $default_max_age);
  \Drupal::state()
    ->set('block_test_block_alter_cache_key', NULL);

  // hook_block_build_alter() adds an additional cache context.
  $alter_add_context = 'url.query_args:' . $this
    ->randomMachineName();
  \Drupal::state()
    ->set('block_test_block_alter_cache_context', $alter_add_context);
  $this
    ->assertBlockRenderedWithExpectedCacheability($default_keys, Cache::mergeContexts($default_contexts, [
    $alter_add_context,
  ]), $default_tags, $default_max_age);
  \Drupal::state()
    ->set('block_test_block_alter_cache_context', NULL);

  // hook_block_build_alter() adds an additional cache tag.
  $alter_add_tag = $this
    ->randomMachineName();
  \Drupal::state()
    ->set('block_test_block_alter_cache_tag', $alter_add_tag);
  $this
    ->assertBlockRenderedWithExpectedCacheability($default_keys, $default_contexts, Cache::mergeTags($default_tags, [
    $alter_add_tag,
  ]), $default_max_age);
  \Drupal::state()
    ->set('block_test_block_alter_cache_tag', NULL);

  // hook_block_build_alter() alters the max-age.
  $alter_max_age = 300;
  \Drupal::state()
    ->set('block_test_block_alter_cache_max_age', $alter_max_age);
  $this
    ->assertBlockRenderedWithExpectedCacheability($default_keys, $default_contexts, $default_tags, $alter_max_age);
  \Drupal::state()
    ->set('block_test_block_alter_cache_max_age', NULL);

  // hook_block_build_alter() alters cache keys, contexts, tags and max-age.
  \Drupal::state()
    ->set('block_test_block_alter_cache_key', $alter_add_key);
  \Drupal::state()
    ->set('block_test_block_alter_cache_context', $alter_add_context);
  \Drupal::state()
    ->set('block_test_block_alter_cache_tag', $alter_add_tag);
  \Drupal::state()
    ->set('block_test_block_alter_cache_max_age', $alter_max_age);
  $this
    ->assertBlockRenderedWithExpectedCacheability(array_merge($default_keys, [
    $alter_add_key,
  ]), Cache::mergeContexts($default_contexts, [
    $alter_add_context,
  ]), Cache::mergeTags($default_tags, [
    $alter_add_tag,
  ]), $alter_max_age);
  \Drupal::state()
    ->set('block_test_block_alter_cache_key', NULL);
  \Drupal::state()
    ->set('block_test_block_alter_cache_context', NULL);
  \Drupal::state()
    ->set('block_test_block_alter_cache_tag', NULL);
  \Drupal::state()
    ->set('block_test_block_alter_cache_max_age', NULL);

  // hook_block_build_alter() sets #create_placeholder.
  foreach ([
    TRUE,
    FALSE,
  ] as $value) {
    \Drupal::state()
      ->set('block_test_block_alter_create_placeholder', $value);
    $build = $this
      ->getBlockRenderArray();
    $this
      ->assertTrue(isset($build['#create_placeholder']));
    $this
      ->assertSame($value, $build['#create_placeholder']);
  }
  \Drupal::state()
    ->set('block_test_block_alter_create_placeholder', NULL);

  // Restore the previous request method.
  $request
    ->setMethod($request_method);
}