CacheTest.php 5.41 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\views\Tests\Plugin\CacheTest.
6 7
 */

8
namespace Drupal\views\Tests\Plugin;
9

10
use Drupal\views\ViewExecutable;
11

12 13 14 15 16
/**
 * Basic test for pluggable caching.
 *
 * @see views_plugin_cache
 */
17
class CacheTest extends PluginTestBase {
18

19 20 21 22 23 24 25
  /**
   * Views used by this test.
   *
   * @var array
   */
  public static $testViews = array('test_view', 'test_cache');

26 27 28 29 30 31 32 33
  public static function getInfo() {
    return array(
      'name' => 'Cache',
      'description' => 'Tests pluggable caching for views.',
      'group' => 'Views Plugins'
    );
  }

34 35 36 37 38 39
  protected function setUp() {
    parent::setUp();

    $this->enableViewsTestModule();
  }

40 41 42 43 44
  /**
   * Tests time based caching.
   *
   * @see views_plugin_cache_time
   */
45
  public function testTimeCaching() {
46
    // Create a basic result which just 2 results.
47 48
    $view = views_get_view('test_cache');
    $view->setDisplay();
49
    $view->display_handler->overrideOption('cache', array(
50
      'type' => 'time',
51 52 53 54
      'options' => array(
        'results_lifespan' => '3600',
        'output_lifespan' => '3600'
      )
55 56 57 58
    ));

    $this->executeView($view);
    // Verify the result.
59
    $this->assertEqual(5, count($view->result), 'The number of returned rows match.');
60 61 62 63 64 65 66

    // Add another man to the beatles.
    $record = array(
      'name' => 'Rod Davis',
      'age' => 29,
      'job' => 'Banjo',
    );
67
    drupal_write_record('views_test_data', $record);
68 69

    // The Result should be the same as before, because of the caching.
70 71
    $view = views_get_view('test_cache');
    $view->setDisplay();
72
    $view->display_handler->overrideOption('cache', array(
73
      'type' => 'time',
74 75 76 77
      'options' => array(
        'results_lifespan' => '3600',
        'output_lifespan' => '3600'
      )
78 79 80 81
    ));

    $this->executeView($view);
    // Verify the result.
82
    $this->assertEqual(5, count($view->result), 'The number of returned rows match.');
83 84 85 86 87 88 89 90 91
  }

  /**
   * Tests no caching.
   *
   * @see views_plugin_cache_time
   */
  function testNoneCaching() {
    // Create a basic result which just 2 results.
92 93
    $view = views_get_view('test_cache');
    $view->setDisplay();
94
    $view->display_handler->overrideOption('cache', array(
95
      'type' => 'none',
96
      'options' => array()
97 98 99 100
    ));

    $this->executeView($view);
    // Verify the result.
101
    $this->assertEqual(5, count($view->result), 'The number of returned rows match.');
102 103 104 105 106 107 108 109

    // Add another man to the beatles.
    $record = array(
      'name' => 'Rod Davis',
      'age' => 29,
      'job' => 'Banjo',
    );

110
    drupal_write_record('views_test_data', $record);
111 112

    // The Result changes, because the view is not cached.
113 114
    $view = views_get_view('test_cache');
    $view->setDisplay();
115
    $view->display_handler->overrideOption('cache', array(
116
      'type' => 'none',
117
      'options' => array()
118 119 120 121
    ));

    $this->executeView($view);
    // Verify the result.
122
    $this->assertEqual(6, count($view->result), 'The number of returned rows match.');
123
  }
124 125 126 127 128 129

  /**
   * Tests css/js storage and restoring mechanism.
   */
  function testHeaderStorage() {
    // Create a view with output caching enabled.
130
    // Some hook_views_pre_render in views_test_data.module adds the test css/js file.
131
    // so they should be added to the css/js storage.
132 133
    $view = views_get_view('test_view');
    $view->setDisplay();
134
    $view->storage->set('id', 'test_cache_header_storage');
135
    $view->display_handler->overrideOption('cache', array(
136
      'type' => 'time',
137 138 139
      'options' => array(
        'output_lifespan' => '3600'
      )
140 141
    ));

142 143
    $output = $view->preview();
    drupal_render($output);
144 145 146
    unset($view->pre_render_called);
    drupal_static_reset('drupal_add_css');
    drupal_static_reset('drupal_add_js');
147
    $view->destroy();
148

149
    $view->setDisplay();
150 151
    $output = $view->preview();
    drupal_render($output);
152
    $css = drupal_add_css();
153 154
    $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';
155 156
    $js = drupal_add_js();

157
    $this->assertTrue(isset($css[basename($css_path)]), 'Make sure the css is added for cached views.');
158
    $this->assertTrue(isset($js[$js_path]), 'Make sure the js is added for cached views.');
159
    $this->assertFalse(!empty($view->build_info['pre_render_called']), 'Make sure hook_views_pre_render is not called for the cached view.');
160 161 162

    // Now add some css/jss before running the view.
    // Make sure that this css is not added when running the cached view.
163
    $view->storage->set('id', 'test_cache_header_storage_2');
164 165 166 167 168 169 170 171 172 173 174
    $attached = array(
      '#attached' => array(
        'css' => array(
          drupal_get_path('module', 'system') . '/css/system.maintenance.css' => array(),
        ),
        'js' => array(
          drupal_get_path('module', 'user') . '/user.permissions.js' => array(),
        ),
      ),
    );
    drupal_render($attached);
175
    $view->destroy();
176

177 178
    $output = $view->preview();
    drupal_render($output);
179 180
    drupal_static_reset('drupal_add_css');
    drupal_static_reset('drupal_add_js');
181
    $view->destroy();
182

183 184
    $output = $view->preview();
    drupal_render($output);
185 186 187 188

    $css = drupal_add_css();
    $js = drupal_add_js();

189 190
    $this->assertFalse(isset($css['system.maintenance.css']), 'Make sure that unrelated css is not added.');
    $this->assertFalse(isset($js[drupal_get_path('module', 'user') . '/user.permissions.js']), 'Make sure that unrelated js is not added.');
191 192
  }

193
}