function AssertViewsCacheTagsTrait::assertViewsCacheTagsFromStaticRenderArray

Same name in other branches
  1. 9 core/modules/views/src/Tests/AssertViewsCacheTagsTrait.php \Drupal\views\Tests\AssertViewsCacheTagsTrait::assertViewsCacheTagsFromStaticRenderArray()
  2. 8.9.x core/modules/views/src/Tests/AssertViewsCacheTagsTrait.php \Drupal\views\Tests\AssertViewsCacheTagsTrait::assertViewsCacheTagsFromStaticRenderArray()
  3. 10 core/modules/views/src/Tests/AssertViewsCacheTagsTrait.php \Drupal\views\Tests\AssertViewsCacheTagsTrait::assertViewsCacheTagsFromStaticRenderArray()

Asserts a view's result & render cache items' cache tags.

This method starts with a pre bubbling basic render array.

Parameters

\Drupal\views\ViewExecutable $view: The view.

string[] $expected_render_array_cache_tags: The expected render cache tags.

bool $views_caching_is_enabled: Defines whether views output / render caching is enabled.

Return value

array The render array.

2 calls to AssertViewsCacheTagsTrait::assertViewsCacheTagsFromStaticRenderArray()
RenderCacheIntegrationTest::assertCacheTagsForEntityBasedView in core/modules/views/tests/src/Kernel/RenderCacheIntegrationTest.php
Tests cache tags on output & result cache items for an entity-based view.
RenderCacheIntegrationTest::assertCacheTagsForFieldBasedView in core/modules/views/tests/src/Kernel/RenderCacheIntegrationTest.php
Tests cache tags on output & result cache items for a field-based view.

File

core/modules/views/src/Tests/AssertViewsCacheTagsTrait.php, line 117

Class

AssertViewsCacheTagsTrait

Namespace

Drupal\views\Tests

Code

protected function assertViewsCacheTagsFromStaticRenderArray(ViewExecutable $view, array $expected_render_array_cache_tags, $views_caching_is_enabled) {
    $original = $build = DisplayPluginBase::buildBasicRenderable($view->id(), $view->current_display ?: 'default', $view->args);
    
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');
    
    /** @var \Drupal\Core\Render\RenderCacheInterface $render_cache */
    $render_cache = \Drupal::service('render_cache');
    // Ensure the current request is a GET request so that render caching is
    // active for direct rendering of views, just like for actual requests.
    
    /** @var \Symfony\Component\HttpFoundation\RequestStack $request_stack */
    $request_stack = \Drupal::service('request_stack');
    $request = new Request();
    $request->server
        ->set('REQUEST_TIME', \Drupal::time()->getRequestTime());
    $request->setSession(new Session(new MockArraySessionStorage()));
    $request_stack->push($request);
    $renderer->renderRoot($build);
    // Check render array cache tags.
    $this->assertEqualsCanonicalizing($expected_render_array_cache_tags, $build['#cache']['tags']);
    // Check Views render cache item cache tags.
    $original['#cache'] += [
        'contexts' => [],
    ];
    $original['#cache']['contexts'] = Cache::mergeContexts($original['#cache']['contexts'], $this->container
        ->getParameter('renderer.config')['required_cache_contexts']);
    $render_cache_item = $render_cache->get($original);
    if ($views_caching_is_enabled) {
        $this->assertNotEmpty($render_cache_item, 'Render cache item found.');
        if ($render_cache_item) {
            $this->assertEqualsCanonicalizing($expected_render_array_cache_tags, $render_cache_item['#cache']['tags']);
        }
    }
    else {
        $this->assertFalse($render_cache_item, 'Render cache item not found.');
    }
    return $build;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.