assertBlockRenderedWithExpectedCacheability example

$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);
    
Home | Imprint | This part of the site doesn't use cookies.