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