Révision 5d12d676
Ajouté par Assos Assos il y a environ 6 ans
drupal7/sites/all/modules/views/tests/views_query.test | ||
---|---|---|
2 | 2 |
|
3 | 3 |
/** |
4 | 4 |
* @file |
5 |
* Tests for Views query features.
|
|
5 |
* Abstract class for views testing.
|
|
6 | 6 |
*/ |
7 | 7 |
|
8 | 8 |
/** |
9 |
* Abstract class for views testing.
|
|
9 |
* |
|
10 | 10 |
*/ |
11 | 11 |
abstract class ViewsTestCase extends DrupalWebTestCase { |
12 |
|
|
13 |
/** |
|
14 |
* |
|
15 |
*/ |
|
16 |
protected $sort_column = NULL; |
|
17 |
|
|
18 |
/** |
|
19 |
* |
|
20 |
*/ |
|
21 |
protected $sort_order = 1; |
|
22 |
|
|
12 | 23 |
/** |
13 | 24 |
* Helper function: verify a result set returned by view. |
14 | 25 |
* |
... | ... | |
16 | 27 |
* column map, taking the order of the rows into account, but not the order |
17 | 28 |
* of the columns. |
18 | 29 |
* |
19 |
* @param $view |
|
20 |
* An executed View. |
|
21 |
* @param $expected_result |
|
22 |
* An expected result set. |
|
23 |
* @param $column_map |
|
24 |
* An associative array mapping the columns of the result set from the view |
|
25 |
* (as keys) and the expected result set (as values). |
|
30 |
* @param view $view
|
|
31 |
* An executed View.
|
|
32 |
* @param array $expected_result
|
|
33 |
* An expected result set.
|
|
34 |
* @param array $column_map
|
|
35 |
* An associative array mapping the columns of the result set from the view
|
|
36 |
* (as keys) and the expected result set (as values).
|
|
26 | 37 |
*/ |
27 | 38 |
protected function assertIdenticalResultset($view, $expected_result, $column_map = array(), $message = 'Identical result set') { |
28 | 39 |
return $this->assertIdenticalResultsetHelper($view, $expected_result, $column_map, $message, 'assertIdentical'); |
... | ... | |
33 | 44 |
* |
34 | 45 |
* Inverse of ViewsTestCase::assertIdenticalResultset(). |
35 | 46 |
* |
36 |
* @param $view |
|
37 |
* An executed View. |
|
38 |
* @param $expected_result |
|
39 |
* An expected result set. |
|
40 |
* @param $column_map |
|
41 |
* An associative array mapping the columns of the result set from the view |
|
42 |
* (as keys) and the expected result set (as values). |
|
47 |
* @param view $view
|
|
48 |
* An executed View.
|
|
49 |
* @param array $expected_result
|
|
50 |
* An expected result set.
|
|
51 |
* @param array $column_map
|
|
52 |
* An associative array mapping the columns of the result set from the view
|
|
53 |
* (as keys) and the expected result set (as values).
|
|
43 | 54 |
*/ |
44 | 55 |
protected function assertNotIdenticalResultset($view, $expected_result, $column_map = array(), $message = 'Identical result set') { |
45 | 56 |
return $this->assertIdenticalResultsetHelper($view, $expected_result, $column_map, $message, 'assertNotIdentical'); |
46 | 57 |
} |
47 | 58 |
|
59 |
/** |
|
60 |
* |
|
61 |
*/ |
|
48 | 62 |
protected function assertIdenticalResultsetHelper($view, $expected_result, $column_map, $message, $assert_method) { |
49 | 63 |
// Convert $view->result to an array of arrays. |
50 | 64 |
$result = array(); |
51 | 65 |
foreach ($view->result as $key => $value) { |
52 | 66 |
$row = array(); |
53 | 67 |
foreach ($column_map as $view_column => $expected_column) { |
54 |
// The comparison will be done on the string representation of the value. |
|
68 |
// The comparison will be done on the string representation of the |
|
69 |
// value. |
|
55 | 70 |
$row[$expected_column] = (string) $value->$view_column; |
56 | 71 |
} |
57 | 72 |
$result[$key] = $row; |
... | ... | |
61 | 76 |
foreach ($expected_result as $key => $value) { |
62 | 77 |
$row = array(); |
63 | 78 |
foreach ($column_map as $expected_column) { |
64 |
// The comparison will be done on the string representation of the value. |
|
79 |
// The comparison will be done on the string representation of the |
|
80 |
// value. |
|
65 | 81 |
$row[$expected_column] = (string) (is_object($value) ? $value->$expected_column : $value[$expected_column]); |
66 | 82 |
} |
67 | 83 |
$expected_result[$key] = $row; |
... | ... | |
71 | 87 |
$result = array_values($result); |
72 | 88 |
$expected_result = array_values($expected_result); |
73 | 89 |
|
74 |
$this->verbose('<pre>Returned data set: ' . print_r($result, TRUE) . "\n\nExpected: ". print_r($expected_result, TRUE)); |
|
90 |
$this->verbose('<pre>Returned data set: ' . print_r($result, TRUE) . "\n\nExpected: " . print_r($expected_result, TRUE));
|
|
75 | 91 |
|
76 | 92 |
// Do the actual comparison. |
77 | 93 |
return $this->$assert_method($result, $expected_result, $message); |
78 | 94 |
} |
79 | 95 |
|
80 | 96 |
/** |
81 |
* Helper function: order an array of array based on a column.
|
|
97 |
* Order an array of array based on a column.
|
|
82 | 98 |
*/ |
83 | 99 |
protected function orderResultSet($result_set, $column, $reverse = FALSE) { |
84 | 100 |
$this->sort_column = $column; |
... | ... | |
87 | 103 |
return $result_set; |
88 | 104 |
} |
89 | 105 |
|
90 |
protected $sort_column = NULL; |
|
91 |
protected $sort_order = 1; |
|
92 |
|
|
93 | 106 |
/** |
94 | 107 |
* Helper comparison function for orderResultSet(). |
95 | 108 |
*/ |
... | ... | |
97 | 110 |
$value1 = $a[$this->sort_column]; |
98 | 111 |
$value2 = $b[$this->sort_column]; |
99 | 112 |
if ($value1 == $value2) { |
100 |
return 0;
|
|
113 |
return 0; |
|
101 | 114 |
} |
102 | 115 |
return $this->sort_order * (($value1 < $value2) ? -1 : 1); |
103 | 116 |
} |
104 | 117 |
|
105 | 118 |
/** |
106 |
* Helper function to check whether a button with a certain id exists and has a certain label.
|
|
119 |
* Check whether a button with a certain id exists and has a certain label.
|
|
107 | 120 |
*/ |
108 | 121 |
protected function helperButtonHasLabel($id, $expected_label, $message = 'Label has the expected value: %label.') { |
109 | 122 |
return $this->assertFieldById($id, $expected_label, t($message, array('%label' => $expected_label))); |
110 | 123 |
} |
111 | 124 |
|
112 | 125 |
/** |
113 |
* Helper function to execute a view with debugging.
|
|
126 |
* Execute a view with debugging.
|
|
114 | 127 |
* |
115 | 128 |
* @param view $view |
116 | 129 |
* @param array $args |
... | ... | |
121 | 134 |
$view->execute(); |
122 | 135 |
$this->verbose('<pre>Executed view: ' . ((string) $view->build_info['query']) . '</pre>'); |
123 | 136 |
} |
137 |
|
|
124 | 138 |
} |
125 | 139 |
|
140 |
/** |
|
141 |
* |
|
142 |
*/ |
|
126 | 143 |
abstract class ViewsSqlTest extends ViewsTestCase { |
127 | 144 |
|
145 |
/** |
|
146 |
* {@inheritdoc} |
|
147 |
*/ |
|
128 | 148 |
protected function setUp() { |
129 | 149 |
parent::setUp('views', 'views_ui'); |
130 | 150 |
|
131 |
// Define the schema and views data variable before enabling the test module. |
|
151 |
// Define the schema and views data variable before enabling the test |
|
152 |
// module. |
|
132 | 153 |
variable_set('views_test_schema', $this->schemaDefinition()); |
133 | 154 |
variable_set('views_test_views_data', $this->viewsData()); |
134 | 155 |
variable_set('views_test_views_plugins', $this->viewsPlugins()); |
... | ... | |
147 | 168 |
} |
148 | 169 |
|
149 | 170 |
/** |
150 |
* This function allows to enable views ui from a higher class which can't change the setup function anymore. |
|
171 |
* This function allows to enable views ui from a higher class which can't |
|
172 |
* change the setup function anymore. |
|
151 | 173 |
* |
152 |
* @TODO |
|
153 |
* Convert existing setUp functions. |
|
174 |
* @todo Convert existing setUp functions. |
|
154 | 175 |
*/ |
155 | 176 |
function enableViewsUi() { |
156 | 177 |
module_enable(array('views_ui')); |
157 |
// @TODO Figure out why it's required to clear the cache here.
|
|
178 |
// @todo Figure out why it's required to clear the cache here.
|
|
158 | 179 |
views_module_include('views_default', TRUE); |
159 | 180 |
views_get_all_views(TRUE); |
160 | 181 |
menu_rebuild(); |
... | ... | |
202 | 223 |
), |
203 | 224 |
'primary key' => array('id'), |
204 | 225 |
'unique keys' => array( |
205 |
'name' => array('name') |
|
226 |
'name' => array('name'),
|
|
206 | 227 |
), |
207 | 228 |
'indexes' => array( |
208 | 229 |
'ages' => array('age'), |
... | ... | |
314 | 335 |
return $data; |
315 | 336 |
} |
316 | 337 |
|
338 |
/** |
|
339 |
* |
|
340 |
*/ |
|
317 | 341 |
protected function viewsPlugins() { |
318 | 342 |
return array(); |
319 | 343 |
} |
... | ... | |
422 | 446 |
views_include('view'); |
423 | 447 |
$view = $this->getBasicView(); |
424 | 448 |
|
425 |
// In order to test exposed filters, we have to disable |
|
426 |
// the exposed forms cache.
|
|
449 |
// In order to test exposed filters, we have to disable the exposed forms
|
|
450 |
// cache. |
|
427 | 451 |
drupal_static_reset('views_exposed_form_cache'); |
428 | 452 |
|
429 | 453 |
$display = $view->new_display('page', 'Page', 'page_1'); |
430 | 454 |
return $view; |
431 | 455 |
} |
456 |
|
|
432 | 457 |
} |
Formats disponibles : Unified diff
Weekly update of contrib modules