6
* Tests for Views query features.
10
* Basic test class for Views query builder tests.
12
class ViewsBasicTest extends DrupalWebTestCase {
13
public static function getInfo() {
15
'name' => t('Basic test'),
16
'description' => t('A basic test for Views.'),
21
protected function setUp() {
22
parent::setUp('autoload', 'dbtng', 'views', 'views_test');
24
// Load the test dataset.
25
$query = db_insert('views_test')
26
->fields(array('name', 'age', 'job'));
27
foreach ($this->testDataSet() as $record) {
28
$query->values($record);
34
* A very simple test dataset.
36
protected function testDataSet() {
56
'job' => 'Songwriter',
67
* Build an return a basic view of the views_test table.
69
protected function getBasicView() {
70
views_include('view');
72
// Create the basic view.
74
$view->vid = 'test_view';
75
$view->add_display('default');
76
$view->base_table = 'views_test';
78
// Set up the fields we need.
79
$display = $view->new_display('default', 'Defaults', 'default');
80
$display->override_option('fields', array(
83
'table' => 'views_test',
85
'relationship' => 'none',
89
'table' => 'views_test',
91
'relationship' => 'none',
95
'table' => 'views_test',
97
'relationship' => 'none',
101
// Set up the sort order.
102
$display->override_option('sorts', array(
106
'table' => 'views_test',
108
'relationship' => 'none',
116
* Test a trivial result set.
118
protected function testSimpleResultSet() {
119
$view = $this->getBasicView();
124
// Verify the result.
125
$this->assertEqual(5, count($view->result), t('The number of returned rows match.'));
126
$this->assertIdenticalResultset($view, $this->testDataSet(), array(
127
'views_test_name' => 'name',
128
'views_test_age' => 'age',
133
* Test ordering of the result set.
135
protected function testSimpleOrdering() {
136
$view = $this->getBasicView();
138
// Change the ordering
139
$view->display['default']->handler->override_option('sorts', array(
143
'table' => 'views_test',
145
'relationship' => 'none',
152
// Verify the result.
153
$this->assertEqual(5, count($view->result), t('The number of returned rows match.'));
154
$this->assertIdenticalResultset($view, $this->orderResultSet($this->testDataSet(), 'age'), array(
155
'views_test_name' => 'name',
156
'views_test_age' => 'age',
161
* Helper function: verify a result set returned by view.
163
* The comparison is done on the string representation of the columns of the
164
* column map, taking the order of the rows into account, but not the order
169
* @param $expected_result
170
* An expected result set.
172
* An associative array mapping the columns of the result set from the view
173
* (as keys) and the expected result set (as values).
175
protected function assertIdenticalResultset($view, $expected_result, $column_map, $message = 'Identical result set') {
176
// Convert $view->result to an array of arrays.
178
foreach ($view->result as $key => $value) {
180
foreach ($column_map as $view_column => $expected_column) {
181
// The comparison will be done on the string representation of the value.
182
$row[$expected_column] = (string) $value->$view_column;
184
$result[$key] = $row;
187
// Remove the columns we don't need from the expected result.
188
foreach ($expected_result as $key => $value) {
190
foreach ($column_map as $expected_column) {
191
// The comparison will be done on the string representation of the value.
192
$row[$expected_column] = (string) $value[$expected_column];
194
$expected_result[$key] = $row;
197
// Reset the numbering of the arrays.
198
$result = array_values($result);
199
$expected_result = array_values($expected_result);
201
// Do the actual comparison.
202
return $this->assertIdentical($result, $expected_result, $message);
206
* Helper function: order an array of array based on a column.
208
protected function orderResultSet($result_set, $column, $reverse = FALSE) {
209
$this->sort_column = $column;
210
$this->sort_order = $reverse ? -1 : 1;
211
usort($result_set, array($this, 'helperCompareFunction'));
215
protected $sort_column = NULL;
216
protected $sort_order = 1;
219
* Helper comparison function for orderResultSet().
221
protected function helperCompareFunction($a, $b) {
222
$value1 = $a[$this->sort_column];
223
$value2 = $b[$this->sort_column];
224
if ($value1 == $value2) {
227
return $this->sort_order * (($value1 < $value2) ? -1 : 1);