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
|
}
|