$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);