You are here

function CacheTest::testHeaderStorage in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/views/src/Tests/Plugin/CacheTest.php \Drupal\views\Tests\Plugin\CacheTest::testHeaderStorage()

Tests css/js storage and restoring mechanism.

File

core/modules/views/src/Tests/Plugin/CacheTest.php, line 269
Contains \Drupal\views\Tests\Plugin\CacheTest.

Class

CacheTest
Tests pluggable caching for views.

Namespace

Drupal\views\Tests\Plugin

Code

function testHeaderStorage() {

  // Create a view with output caching enabled.
  // Some hook_views_pre_render in views_test_data.module adds the test css/js file.
  // so they should be added to the css/js storage.
  $view = Views::getView('test_view');
  $view
    ->setDisplay();
  $view->storage
    ->set('id', 'test_cache_header_storage');
  $view->display_handler
    ->overrideOption('cache', array(
    'type' => 'time',
    'options' => array(
      'output_lifespan' => '3600',
    ),
  ));
  $output = $view
    ->buildRenderable();

  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = \Drupal::service('renderer');
  $renderer
    ->executeInRenderContext(new RenderContext(), function () use (&$output, $renderer) {
    return $renderer
      ->render($output);
  });
  unset($view->pre_render_called);
  $view
    ->destroy();
  $view
    ->setDisplay();
  $output = $view
    ->buildRenderable();
  $renderer
    ->executeInRenderContext(new RenderContext(), function () use (&$output, $renderer) {
    return $renderer
      ->render($output);
  });
  $this
    ->assertTrue(in_array('views_test_data/test', $output['#attached']['library']), 'Make sure libraries are added for cached views.');
  $this
    ->assertEqual([
    'foo' => 'bar',
  ], $output['#attached']['drupalSettings'], 'Make sure drupalSettings are added for cached views.');

  // Note: views_test_data_views_pre_render() adds some cache tags.
  $this
    ->assertEqual([
    'config:views.view.test_cache_header_storage',
    'views_test_data:1',
  ], $output['#cache']['tags']);
  $this
    ->assertEqual([
    'non-existing-placeholder-just-for-testing-purposes' => [
      '#lazy_builder' => [
        'views_test_data_placeholders',
        [
          'bar',
        ],
      ],
    ],
  ], $output['#attached']['placeholders']);
  $this
    ->assertFalse(!empty($view->build_info['pre_render_called']), 'Make sure hook_views_pre_render is not called for the cached view.');
}