Project

General

Profile

Paste
Download (9.47 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / views / tests / views_cache.test @ 6de61b86

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of ViewsCacheTest.
6
 */
7

    
8
/**
9
 * Basic test for pluggable caching.
10
 *
11
 * @see views_plugin_cache
12
 */
13
class ViewsCacheTest extends ViewsSqlTest {
14

    
15
  /**
16
   *
17
   */
18
  public static function getInfo() {
19
    return array(
20
      'name' => 'Cache',
21
      'description' => 'Tests pluggable caching for views.',
22
      'group' => 'Views Plugins',
23
    );
24
  }
25

    
26
  /**
27
   * Build and return a basic view of the views_test table.
28
   *
29
   * @return view
30
   */
31
  protected function getBasicView() {
32
    views_include('view');
33

    
34
    // Create the basic view.
35
    $view = new view();
36
    $view->name = 'test_view';
37
    $view->add_display('default');
38
    $view->base_table = 'views_test';
39

    
40
    // Set up the fields we need.
41
    $display = $view->new_display('default', 'Master', 'default');
42
    $display->override_option('fields', array(
43
      'id' => array(
44
        'id' => 'id',
45
        'table' => 'views_test',
46
        'field' => 'id',
47
        'relationship' => 'none',
48
      ),
49
      'name' => array(
50
        'id' => 'name',
51
        'table' => 'views_test',
52
        'field' => 'name',
53
        'relationship' => 'none',
54
      ),
55
      'age' => array(
56
        'id' => 'age',
57
        'table' => 'views_test',
58
        'field' => 'age',
59
        'relationship' => 'none',
60
      ),
61
    ));
62

    
63
    // Set up the sort order.
64
    $display->override_option('sorts', array(
65
      'id' => array(
66
        'order' => 'ASC',
67
        'id' => 'id',
68
        'table' => 'views_test',
69
        'field' => 'id',
70
        'relationship' => 'none',
71
      ),
72
    ));
73

    
74
    return $view;
75
  }
76

    
77
  /**
78
   * Tests time based caching.
79
   *
80
   * @see views_plugin_cache_time
81
   */
82
  function testTimeCaching() {
83
    // Create a basic result which just 2 results.
84
    $view = $this->getBasicView();
85
    $view->set_display();
86
    $view->display_handler->override_option('cache', array(
87
      'type' => 'time',
88
      'results_lifespan' => '3600',
89
      'output_lifespan' => '3600',
90
    ));
91

    
92
    $this->executeView($view);
93
    // Verify the result.
94
    $this->assertEqual(5, count($view->result), t('The number of returned rows match.'));
95

    
96
    // Add another man to the beatles.
97
    $record = array(
98
      'name' => 'Rod Davis',
99
      'age' => 29,
100
      'job' => 'Banjo',
101
    );
102
    drupal_write_record('views_test', $record);
103

    
104
    // The Result should be the same as before, because of the caching.
105
    $view = $this->getBasicView();
106
    $view->set_display();
107
    $view->display_handler->override_option('cache', array(
108
      'type' => 'time',
109
      'results_lifespan' => '3600',
110
      'output_lifespan' => '3600',
111
    ));
112

    
113
    $this->executeView($view);
114
    // Verify the result.
115
    $this->assertEqual(5, count($view->result), t('The number of returned rows match.'));
116
  }
117

    
118
  /**
119
   * Tests no caching.
120
   *
121
   * @see views_plugin_cache_time
122
   */
123
  function testNoneCaching() {
124
    // Create a basic result which just 2 results.
125
    $view = $this->getBasicView();
126
    $view->set_display();
127
    $view->display_handler->override_option('cache', array(
128
      'type' => 'none',
129
    ));
130

    
131
    $this->executeView($view);
132
    // Verify the result.
133
    $this->assertEqual(5, count($view->result), t('The number of returned rows match.'));
134

    
135
    // Add another man to the beatles.
136
    $record = array(
137
      'name' => 'Rod Davis',
138
      'age' => 29,
139
      'job' => 'Banjo',
140
    );
141

    
142
    drupal_write_record('views_test', $record);
143

    
144
    // The Result changes, because the view is not cached.
145
    $view = $this->getBasicView();
146
    $view->set_display();
147
    $view->display_handler->override_option('cache', array(
148
      'type' => 'none',
149
    ));
150

    
151
    $this->executeView($view);
152
    // Verify the result.
153
    $this->assertEqual(6, count($view->result), t('The number of returned rows match.'));
154
  }
155

    
156
  /**
157
   * Tests css/js storage and restoring mechanism.
158
   */
159
  function testHeaderStorage() {
160
    // Create a view with output caching enabled. Some hook_views_pre_render in
161
    // views_test.module adds the test css/js file, so they should be added to
162
    // the css/js storage.
163
    $view = $this->getBasicView();
164
    $view->init_display();
165
    $view->name = 'test_cache_header_storage';
166
    $view->display_handler->override_option('cache', array(
167
      'type' => 'time',
168
      'output_lifespan' => '3600',
169
    ));
170

    
171
    $view->preview();
172
    $view->destroy();
173
    unset($view->pre_render_called);
174
    drupal_static_reset('drupal_add_css');
175
    drupal_static_reset('drupal_add_js');
176

    
177
    $view->init_display();
178
    $view->preview();
179
    $css = drupal_add_css();
180
    $css_path = drupal_get_path('module', 'views_test') . '/views_cache.test.css';
181
    $js_path = drupal_get_path('module', 'views_test') . '/views_cache.test.js';
182
    $js = drupal_add_js();
183

    
184
    $this->assertTrue(isset($css[$css_path]), 'Make sure the css is added for cached views.');
185
    $this->assertTrue(isset($js[$js_path]), 'Make sure the js is added for cached views.');
186
    $this->assertFalse(!empty($view->pre_render_called), 'Make sure hook_views_pre_render is not called for the cached view.');
187
    $view->destroy();
188

    
189
    // Now add some css/jss before running the view.
190
    // Make sure that this css is not added when running the cached view.
191
    $view->name = 'test_cache_header_storage_2';
192

    
193
    $system_css_path = drupal_get_path('module', 'system') . '/system.maintenance.css';
194
    drupal_add_css($system_css_path);
195
    $system_js_path = drupal_get_path('module', 'system') . '/system.cron.js';
196
    drupal_add_js($system_js_path);
197

    
198
    $view->init_display();
199
    $view->preview();
200
    $view->destroy();
201
    drupal_static_reset('drupal_add_css');
202
    drupal_static_reset('drupal_add_js');
203

    
204
    $view->init_display();
205
    $view->preview();
206

    
207
    $css = drupal_add_css();
208
    $js = drupal_add_js();
209

    
210
    $this->assertFalse(isset($css[$system_css_path]), 'Make sure that unrelated css is not added.');
211
    $this->assertFalse(isset($js[$system_js_path]), 'Make sure that unrelated js is not added.');
212

    
213
  }
214

    
215
  /**
216
   * Check that HTTP headers are cached for views.
217
   */
218
  function testHttpHeadersCaching() {
219
    // Create a few nodes to appear in RSS feed.
220
    for ($i = 0; $i < 5; $i++) {
221
      $this->drupalCreateNode();
222
    }
223

    
224
    // Make the first request and check returned content type.
225
    $this->drupalGet('test_feed_http_headers_caching');
226
    $first_content = $this->drupalGetContent();
227
    $first_content_type = $this->drupalGetHeader('content-type');
228
    $expected_type = 'application/rss+xml';
229
    $this->assertIdentical(0, strpos(trim($first_content_type), $expected_type), t('Expected content type returned.'));
230

    
231
    // Check that we have 5 items in RSS feed returned by the first request.
232
    $xml = simplexml_load_string($first_content);
233
    $items = $xml->xpath('/rss/channel/item');
234
    $this->assertEqual(5, count($items), t('The number of RSS feed items matched.'));
235

    
236
    // Create another node to be sure we get cached results on the second
237
    // request.
238
    $this->drupalCreateNode();
239

    
240
    // Make the second request, check content type and content matching.
241
    $this->drupalGet('test_feed_http_headers_caching');
242
    $second_content = $this->drupalGetContent();
243
    $this->assertEqual($first_content, $second_content, t('The second result fetched from cache.'));
244
    $second_content_type = $this->drupalGetHeader('content-type');
245
    $this->assertEqual($first_content_type, $second_content_type, t('Content types of responses are equal.'));
246
  }
247

    
248
  /**
249
   * Test caching of different exposed filter values with the same view result.
250
   *
251
   * Make sure the output is different.
252
   */
253
  function testExposedFilterSameResultsCaching() {
254
    // Create the view with time-based cache with hour lifetimes and add exposed
255
    // filter to it with "Starts with" operator.
256
    $view = $this->getBasicView();
257
    $view->set_display();
258
    $view->display_handler->override_option('cache', array(
259
      'type' => 'time',
260
      'results_lifespan' => '3600',
261
      'output_lifespan' => '3600',
262
    ));
263
    $view->display_handler->override_option('filters', array(
264
      'name' => array(
265
        'id' => 'name',
266
        'table' => 'views_test',
267
        'field' => 'name',
268
        'relationship' => 'none',
269
        'operator' => 'starts',
270
        'exposed' => TRUE,
271
        'expose' => array(
272
          'operator_id' => 'name_op',
273
          'operator' => 'name_op',
274
          'identifier' => 'name',
275
        ),
276
      ),
277
    ));
278

    
279
    // Clone the view before setting exposed input.
280
    $clone = $view->copy();
281

    
282
    // Pass "Rin" to the exposed filter and check that only one row returned.
283
    $view->set_exposed_input(array(
284
      'name' => 'Rin',
285
    ));
286
    $this->executeView($view);
287
    $first_result = $view->result;
288
    $first_output = $view->render();
289
    $this->assertEqual(1, count($first_result), t('The number of rows returned by the first view match.'));
290

    
291
    // Pass full "Ringo" to the exposed filter at the second time and make sure
292
    // results are the same.
293
    $clone->set_exposed_input(array(
294
      'name' => 'Ringo',
295
    ));
296
    $this->executeView($clone);
297
    $second_result = $clone->result;
298
    $second_output = $clone->render();
299
    $this->assertEqual($first_result, $second_result, t('Results of both views are the same.'));
300

    
301
    // Check that output is not the same and it contains full "Ringo" word in
302
    // default value of exposed input.
303
    $this->assertNotEqual($first_output, $second_output, t('Output of the second view is different.'));
304
    $this->drupalSetContent($second_output);
305
    $element = $this->xpath('//input[@name="name" and @value="Ringo"]');
306
    $this->assertTrue(!empty($element), t('Input field of exposed filter has the second value.'));
307

    
308
    $view->destroy();
309
    $clone->destroy();
310
  }
311

    
312
}