Projet

Général

Profil

Paste
Télécharger (9,45 ko) Statistiques
| Branche: | Révision:

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

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
  public static function getInfo() {
15
    return array(
16
      'name' => 'Cache',
17
      'description' => 'Tests pluggable caching for views.',
18
      'group' => 'Views Plugins'
19
    );
20
  }
21

    
22
  /**
23
   * Build and return a basic view of the views_test table.
24
   *
25
   * @return view
26
   */
27
  protected function getBasicView() {
28
    views_include('view');
29

    
30
    // Create the basic view.
31
    $view = new view();
32
    $view->name = 'test_view';
33
    $view->add_display('default');
34
    $view->base_table = 'views_test';
35

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

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

    
70
    return $view;
71
  }
72

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

    
88
    $this->executeView($view);
89
    // Verify the result.
90
    $this->assertEqual(5, count($view->result), t('The number of returned rows match.'));
91

    
92
    // Add another man to the beatles.
93
    $record = array(
94
      'name' => 'Rod Davis',
95
      'age' => 29,
96
      'job' => 'Banjo',
97
    );
98
    drupal_write_record('views_test', $record);
99

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

    
109
    $this->executeView($view);
110
    // Verify the result.
111
    $this->assertEqual(5, count($view->result), t('The number of returned rows match.'));
112
  }
113

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

    
127
    $this->executeView($view);
128
    // Verify the result.
129
    $this->assertEqual(5, count($view->result), t('The number of returned rows match.'));
130

    
131
    // Add another man to the beatles.
132
    $record = array(
133
      'name' => 'Rod Davis',
134
      'age' => 29,
135
      'job' => 'Banjo',
136
    );
137

    
138
    drupal_write_record('views_test', $record);
139

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

    
147
    $this->executeView($view);
148
    // Verify the result.
149
    $this->assertEqual(6, count($view->result), t('The number of returned rows match.'));
150
  }
151

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

    
167
    $view->preview();
168
    $view->destroy();
169
    unset($view->pre_render_called);
170
    drupal_static_reset('drupal_add_css');
171
    drupal_static_reset('drupal_add_js');
172

    
173
    $view->init_display();
174
    $view->preview();
175
    $css = drupal_add_css();
176
    $css_path = drupal_get_path('module', 'views_test') . '/views_cache.test.css';
177
    $js_path = drupal_get_path('module', 'views_test') . '/views_cache.test.js';
178
    $js = drupal_add_js();
179

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

    
185
    // Now add some css/jss before running the view.
186
    // Make sure that this css is not added when running the cached view.
187
    $view->name = 'test_cache_header_storage_2';
188

    
189
    $system_css_path = drupal_get_path('module', 'system') . '/system.maintenance.css';
190
    drupal_add_css($system_css_path);
191
    $system_js_path = drupal_get_path('module', 'system') . '/system.cron.js';
192
    drupal_add_js($system_js_path);
193

    
194
    $view->init_display();
195
    $view->preview();
196
    $view->destroy();
197
    drupal_static_reset('drupal_add_css');
198
    drupal_static_reset('drupal_add_js');
199

    
200
    $view->init_display();
201
    $view->preview();
202

    
203
    $css = drupal_add_css();
204
    $js = drupal_add_js();
205

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

    
209
  }
210

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

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

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

    
232
    // Create another node to be sure we get cached results on the second
233
    // request.
234
    $this->drupalCreateNode();
235

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

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

    
275
    // Clone the view before setting exposed input.
276
    $clone = $view->copy();
277

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

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

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

    
304
    $view->destroy();
305
    $clone->destroy();
306
  }
307

    
308
}