function CacheTest::testHeaderStorage

Tests css/js storage and restoring mechanism.

File

drupal/core/modules/views/lib/Drupal/views/Tests/Plugin/CacheTest.php, line 128
Definition of Drupal\views\Tests\Plugin\CacheTest.

Class

CacheTest
Basic test for pluggable caching.

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_get_view('test_view');
  $view
    ->setDisplay();
  $view->storage
    ->set('name', 'test_cache_header_storage');
  $view->display_handler
    ->overrideOption('cache', array(
    'type' => 'time',
    'options' => array(
      'output_lifespan' => '3600',
    ),
  ));
  $view
    ->preview();
  unset($view->pre_render_called);
  drupal_static_reset('drupal_add_css');
  drupal_static_reset('drupal_add_js');
  $view
    ->destroy();
  $view
    ->setDisplay();
  $view
    ->preview();
  $css = drupal_add_css();
  $css_path = drupal_get_path('module', 'views_test_data') . '/views_cache.test.css';
  $js_path = drupal_get_path('module', 'views_test_data') . '/views_cache.test.js';
  $js = drupal_add_js();
  $this
    ->assertTrue(isset($css[$css_path]), 'Make sure the css is added for cached views.');
  $this
    ->assertTrue(isset($js[$js_path]), 'Make sure the js is added for cached views.');
  $this
    ->assertFalse(!empty($view->build_info['pre_render_called']), 'Make sure hook_views_pre_render is not called for the cached view.');

  // Now add some css/jss before running the view.
  // Make sure that this css is not added when running the cached view.
  $view->storage
    ->set('name', 'test_cache_header_storage_2');
  $system_css_path = drupal_get_path('module', 'system') . '/system.maintenance.css';
  drupal_add_css($system_css_path);
  $system_js_path = drupal_get_path('module', 'system') . '/system.cron.js';
  drupal_add_js($system_js_path);
  $view
    ->destroy();
  $view
    ->setDisplay();
  $view
    ->preview();
  drupal_static_reset('drupal_add_css');
  drupal_static_reset('drupal_add_js');
  $view
    ->destroy();
  $view
    ->setDisplay();
  $view
    ->preview();
  $css = drupal_add_css();
  $js = drupal_add_js();
  $this
    ->assertFalse(isset($css[$system_css_path]), 'Make sure that unrelated css is not added.');
  $this
    ->assertFalse(isset($js[$system_js_path]), 'Make sure that unrelated js is not added.');
}