1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Definition of ViewsModuleTest.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Tests basic functions from the Views module.
|
10
|
*/
|
11
|
class ViewsModuleTest extends ViewsSqlTest {
|
12
|
public static function getInfo() {
|
13
|
return array(
|
14
|
'name' => 'Tests views.module',
|
15
|
'description' => 'Tests some basic functions of views.module',
|
16
|
'group' => 'Views',
|
17
|
);
|
18
|
}
|
19
|
|
20
|
/**
|
21
|
* {@inheritdoc}
|
22
|
*/
|
23
|
public function setUp(array $modules = array()) {
|
24
|
parent::setUp($modules);
|
25
|
|
26
|
drupal_theme_rebuild();
|
27
|
}
|
28
|
|
29
|
public function viewsData() {
|
30
|
$data = parent::viewsData();
|
31
|
$data['views_test_previous'] = array();
|
32
|
$data['views_test_previous']['id']['field']['moved to'] = array('views_test', 'id');
|
33
|
$data['views_test_previous']['id']['filter']['moved to'] = array('views_test', 'id');
|
34
|
$data['views_test']['age_previous']['field']['moved to'] = array('views_test', 'age');
|
35
|
$data['views_test']['age_previous']['sort']['moved to'] = array('views_test', 'age');
|
36
|
$data['views_test_previous']['name_previous']['field']['moved to'] = array('views_test', 'name');
|
37
|
$data['views_test_previous']['name_previous']['argument']['moved to'] = array('views_test', 'name');
|
38
|
|
39
|
return $data;
|
40
|
}
|
41
|
|
42
|
public function test_views_trim_text() {
|
43
|
// Test unicode, @see http://drupal.org/node/513396#comment-2839416
|
44
|
$text = array(
|
45
|
'Tuy nhiên, những hi vọng',
|
46
|
'Giả sử chúng tôi có 3 Apple',
|
47
|
'siêu nhỏ này là bộ xử lý',
|
48
|
'Di động của nhà sản xuất Phần Lan',
|
49
|
'khoảng cách từ đại lí đến',
|
50
|
'của hãng bao gồm ba dòng',
|
51
|
'сд асд асд ас',
|
52
|
'асд асд асд ас',
|
53
|
);
|
54
|
// Just test maxlength without word boundry.
|
55
|
$alter = array(
|
56
|
'max_length' => 10,
|
57
|
);
|
58
|
$expect = array(
|
59
|
'Tuy nhiên,',
|
60
|
'Giả sử chú',
|
61
|
'siêu nhỏ n',
|
62
|
'Di động củ',
|
63
|
'khoảng các',
|
64
|
'của hãng b',
|
65
|
'сд асд асд',
|
66
|
'асд асд ас',
|
67
|
);
|
68
|
|
69
|
foreach ($text as $key => $line) {
|
70
|
$result_text = views_trim_text($alter, $line);
|
71
|
$this->assertEqual($result_text, $expect[$key]);
|
72
|
}
|
73
|
|
74
|
// Test also word_boundary
|
75
|
$alter['word_boundary'] = TRUE;
|
76
|
$expect = array(
|
77
|
'Tuy nhiên',
|
78
|
'Giả sử',
|
79
|
'siêu nhỏ',
|
80
|
'Di động',
|
81
|
'khoảng',
|
82
|
'của hãng',
|
83
|
'сд асд',
|
84
|
'асд асд',
|
85
|
);
|
86
|
|
87
|
foreach ($text as $key => $line) {
|
88
|
$result_text = views_trim_text($alter, $line);
|
89
|
$this->assertEqual($result_text, $expect[$key]);
|
90
|
}
|
91
|
}
|
92
|
|
93
|
/**
|
94
|
* Tests the dynamic includes of templates via module feature.
|
95
|
*/
|
96
|
function testModuleTemplates() {
|
97
|
$views_status = variable_get('views_defaults', array());
|
98
|
$views_status['frontpage'] = FALSE;
|
99
|
// false is enabled.
|
100
|
variable_set('views_defaults', $views_status);
|
101
|
|
102
|
$existing = array();
|
103
|
$type = array();
|
104
|
$theme = array();
|
105
|
$path = array();
|
106
|
$registry = views_theme($existing, $type, $theme, $path);
|
107
|
$this->assertTrue(isset($registry['views_view__frontpage']));
|
108
|
}
|
109
|
|
110
|
/**
|
111
|
* Tests the views_get_handler method.
|
112
|
*/
|
113
|
function testviews_get_handler() {
|
114
|
$types = array('field', 'area', 'filter');
|
115
|
foreach ($types as $type) {
|
116
|
$handler = views_get_handler($this->randomName(), $this->randomName(), $type);
|
117
|
$this->assertEqual('views_handler_' . $type . '_broken', get_class($handler), t('Make sure that a broken handler of type: @type are created', array('@type' => $type)));
|
118
|
}
|
119
|
|
120
|
$views_data = $this->viewsData();
|
121
|
$test_tables = array('views_test' => array('id', 'name'));
|
122
|
foreach ($test_tables as $table => $fields) {
|
123
|
foreach ($fields as $field) {
|
124
|
$data = $views_data[$table][$field];
|
125
|
foreach ($data as $id => $field_data) {
|
126
|
if (!in_array($id, array('title', 'help'))) {
|
127
|
$handler = views_get_handler($table, $field, $id);
|
128
|
$this->assertInstanceHandler($handler, $table, $field, $id);
|
129
|
}
|
130
|
}
|
131
|
}
|
132
|
}
|
133
|
|
134
|
// Test the automatic conversion feature.
|
135
|
// Test the automatic table renaming.
|
136
|
$handler = views_get_handler('views_test_previous', 'id', 'field');
|
137
|
$this->assertInstanceHandler($handler, 'views_test', 'id', 'field');
|
138
|
$handler = views_get_handler('views_test_previous', 'id', 'filter');
|
139
|
$this->assertInstanceHandler($handler, 'views_test', 'id', 'filter');
|
140
|
|
141
|
// Test the automatic field renaming.
|
142
|
$handler = views_get_handler('views_test', 'age_previous', 'field');
|
143
|
$this->assertInstanceHandler($handler, 'views_test', 'age', 'field');
|
144
|
$handler = views_get_handler('views_test', 'age_previous', 'sort');
|
145
|
$this->assertInstanceHandler($handler, 'views_test', 'age', 'sort');
|
146
|
|
147
|
// Test the automatic table and field renaming.
|
148
|
$handler = views_get_handler('views_test_previous', 'name_previous', 'field');
|
149
|
$this->assertInstanceHandler($handler, 'views_test', 'name', 'field');
|
150
|
$handler = views_get_handler('views_test_previous', 'name_previous', 'argument');
|
151
|
$this->assertInstanceHandler($handler, 'views_test', 'name', 'argument');
|
152
|
|
153
|
// Test the override handler feature.
|
154
|
$handler = views_get_handler('views_test', 'job', 'filter', 'views_handler_filter');
|
155
|
$this->assertEqual('views_handler_filter', get_class($handler));
|
156
|
}
|
157
|
|
158
|
/**
|
159
|
* Tests views_fetch_data().
|
160
|
*/
|
161
|
function testFetchData() {
|
162
|
|
163
|
// Make sure we start with a empty cache.
|
164
|
$this->resetStaticViewsDataCache();
|
165
|
cache_clear_all('*', 'cache_views', TRUE);
|
166
|
variable_set('views_test_views_data_count', 0);
|
167
|
|
168
|
// Request info about an existing table.
|
169
|
$this->assertTrue(views_fetch_data('views_test'), 'Data about existing table returned');
|
170
|
// This should have triggered a views data rebuild, and written a cache
|
171
|
// entry for all tables and the requested table but no other tables.
|
172
|
$this->assertEqual(variable_get('views_test_views_data_count', 0), 1, 'Views data rebuilt once');
|
173
|
$this->assertTrue(cache_get('views_data:en', 'cache_views'), 'Cache for all tables written.');
|
174
|
$this->assertTrue(cache_get('views_data:views_test:en', 'cache_views'), 'Cache for requested table written.');
|
175
|
$this->assertFalse(cache_get('views_data:views_test_previous:en', 'cache_views'), 'No Cache written for not requested table.');
|
176
|
$this->assertTrue(drupal_static('_views_fetch_data_fully_loaded'), 'Views data is fully loaded');
|
177
|
|
178
|
$this->resetStaticViewsDataCache();
|
179
|
|
180
|
// Request the same table again.
|
181
|
$this->assertTrue(views_fetch_data('views_test'), 'Data about existing table returned');
|
182
|
$this->assertEqual(variable_get('views_test_views_data_count', 0), 1, 'Views data rebuilt once');
|
183
|
$this->assertFalse(drupal_static('_views_fetch_data_fully_loaded'), 'Views data is not fully loaded');
|
184
|
|
185
|
$this->resetStaticViewsDataCache();
|
186
|
|
187
|
// Request a missing table, this should load the full cache from cache but
|
188
|
// not rebuilt.
|
189
|
$this->assertFalse(views_fetch_data('views_test_missing'), 'No data about missing table returned');
|
190
|
$this->assertEqual(variable_get('views_test_views_data_count', 0), 1, 'Views data rebuilt once');
|
191
|
$this->assertTrue(drupal_static('_views_fetch_data_fully_loaded'), 'Views data is fully loaded');
|
192
|
|
193
|
$this->resetStaticViewsDataCache();
|
194
|
|
195
|
// Request the same empty table again, this should load only that (empty)
|
196
|
// cache for that table.
|
197
|
$this->assertFalse(views_fetch_data('views_test_missing'), 'No data about missing table returned');
|
198
|
$this->assertEqual(variable_get('views_test_views_data_count', 0), 1, 'Views data rebuilt once');
|
199
|
$this->assertFalse(drupal_static('_views_fetch_data_fully_loaded'), 'Views data is not fully loaded');
|
200
|
|
201
|
// Test if the cache consistency is ensured. There was an issue where
|
202
|
// calling _views_fetch_data() first with a table would prevent the function
|
203
|
// from properly rebuilt a missing the general cache entry.
|
204
|
// See https://www.drupal.org/node/2475669 for details.
|
205
|
// Make sure we start with a empty cache.
|
206
|
$this->resetStaticViewsDataCache();
|
207
|
cache_clear_all('*', 'cache_views', TRUE);
|
208
|
|
209
|
// Prime the static cache of _views_fetch_data() by calling it with a table
|
210
|
// first.
|
211
|
views_fetch_data('views_test');
|
212
|
// Now remove the general cache.
|
213
|
cache_clear_all('views_data:en', 'cache_views');
|
214
|
// Reset the static cache to see if fetches from the persistent cache
|
215
|
// properly rebuild the static cache.
|
216
|
$this->resetStaticViewsDataCache();
|
217
|
// Prime the static cache of _views_fetch_data() by calling it with a table
|
218
|
// first.
|
219
|
views_fetch_data('views_test');
|
220
|
// Fetch the general cache, which was deleted, an see if it is rebuild
|
221
|
// properly.
|
222
|
views_fetch_data();
|
223
|
$this->assertTrue(cache_get('views_data:en', 'cache_views'), 'Cache for all tables was properly rebuild.');
|
224
|
}
|
225
|
|
226
|
/**
|
227
|
* Ensure that a certain handler is a instance of a certain table/field.
|
228
|
*/
|
229
|
function assertInstanceHandler($handler, $table, $field, $id) {
|
230
|
$table_data = views_fetch_data($table);
|
231
|
$field_data = $table_data[$field][$id];
|
232
|
|
233
|
$this->assertEqual($field_data['handler'], get_class($handler));
|
234
|
}
|
235
|
|
236
|
/**
|
237
|
* Resets the views data cache.
|
238
|
*/
|
239
|
protected function resetStaticViewsDataCache() {
|
240
|
drupal_static_reset('_views_fetch_data_cache');
|
241
|
drupal_static_reset('_views_fetch_data_recursion_protected');
|
242
|
drupal_static_reset('_views_fetch_data_fully_loaded');
|
243
|
}
|
244
|
|
245
|
}
|