~views-port/+junk/Views3-d6

« back to all changes in this revision

Viewing changes to tests/views_query.test

  • Committer: damz
  • Date: 2009-09-12 18:36:46 UTC
  • mto: This revision was merged to the branch mainline in revision 7.
  • Revision ID: damz@newborn-20090912183646-2ykuk1yfjjfqru1o
First query tests for Views.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
// $Id$
 
3
 
 
4
/**
 
5
 * @file
 
6
 * Tests for Views query features.
 
7
 */
 
8
 
 
9
/**
 
10
 * Basic test class for Views query builder tests.
 
11
 */
 
12
class ViewsBasicTest extends DrupalWebTestCase {
 
13
  public static function getInfo() {
 
14
    return array(
 
15
      'name' => t('Basic test'),
 
16
      'description' => t('A basic test for Views.'),
 
17
      'group' => t('Views')
 
18
    );
 
19
  }
 
20
 
 
21
  protected function setUp() {
 
22
    parent::setUp('autoload', 'dbtng', 'views', 'views_test');
 
23
 
 
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);
 
29
    }
 
30
    $query->execute();
 
31
  }
 
32
 
 
33
  /**
 
34
   * A very simple test dataset.
 
35
   */
 
36
  protected function testDataSet() {
 
37
    return array(
 
38
      array(
 
39
        'name' => 'John',
 
40
        'age' => 25,
 
41
        'job' => 'Singer',
 
42
      ),
 
43
      array(
 
44
        'name' => 'George',
 
45
        'age' => 27,
 
46
        'job' => 'Singer',
 
47
      ),
 
48
      array(
 
49
        'name' => 'Ringo',
 
50
        'age' => 28,
 
51
        'job' => 'Drummer',
 
52
      ),
 
53
      array(
 
54
        'name' => 'Paul',
 
55
        'age' => 26,
 
56
        'job' => 'Songwriter',
 
57
      ),
 
58
      array(
 
59
        'name' => 'Meredith',
 
60
        'age' => 30,
 
61
        'job' => 'Speaker',
 
62
      ),
 
63
    );
 
64
  }
 
65
 
 
66
  /**
 
67
   * Build an return a basic view of the views_test table.
 
68
   */
 
69
  protected function getBasicView() {
 
70
    views_include('view');
 
71
 
 
72
    // Create the basic view.
 
73
    $view = new view();
 
74
    $view->vid = 'test_view';
 
75
    $view->add_display('default');
 
76
    $view->base_table = 'views_test';
 
77
 
 
78
    // Set up the fields we need.
 
79
    $display = $view->new_display('default', 'Defaults', 'default');
 
80
    $display->override_option('fields', array(
 
81
      'id' => array(
 
82
        'id' => 'id',
 
83
        'table' => 'views_test',
 
84
        'field' => 'id',
 
85
        'relationship' => 'none',
 
86
      ),
 
87
      'name' => array(
 
88
        'id' => 'name',
 
89
        'table' => 'views_test',
 
90
        'field' => 'name',
 
91
        'relationship' => 'none',
 
92
      ),
 
93
      'age' => array(
 
94
        'id' => 'age',
 
95
        'table' => 'views_test',
 
96
        'field' => 'age',
 
97
        'relationship' => 'none',
 
98
      ),
 
99
    ));
 
100
 
 
101
    // Set up the sort order.
 
102
    $display->override_option('sorts', array(
 
103
      'id' => array(
 
104
        'order' => 'ASC',
 
105
        'id' => 'id',
 
106
        'table' => 'views_test',
 
107
        'field' => 'id',
 
108
        'relationship' => 'none',
 
109
      ),
 
110
    ));
 
111
 
 
112
    return $view;
 
113
  }
 
114
 
 
115
  /**
 
116
   * Test a trivial result set.
 
117
   */
 
118
  protected function testSimpleResultSet() {
 
119
    $view = $this->getBasicView();
 
120
 
 
121
    // Execute the view.
 
122
    $view->execute();
 
123
 
 
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',
 
129
    ));
 
130
  }
 
131
 
 
132
  /**
 
133
   * Test ordering of the result set.
 
134
   */
 
135
  protected function testSimpleOrdering() {
 
136
    $view = $this->getBasicView();
 
137
 
 
138
    // Change the ordering
 
139
    $view->display['default']->handler->override_option('sorts', array(
 
140
      'age' => array(
 
141
        'order' => 'ASC',
 
142
        'id' => 'age',
 
143
        'table' => 'views_test',
 
144
        'field' => 'age',
 
145
        'relationship' => 'none',
 
146
      ),
 
147
    ));
 
148
 
 
149
    // Execute the view.
 
150
    $view->execute();
 
151
 
 
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',
 
157
    ));
 
158
  }
 
159
 
 
160
  /**
 
161
   * Helper function: verify a result set returned by view.
 
162
   *
 
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
 
165
   * of the columns.
 
166
   *
 
167
   * @param $view
 
168
   *  An executed View.
 
169
   * @param $expected_result
 
170
   *  An expected result set.
 
171
   * @param $column_map
 
172
   *  An associative array mapping the columns of the result set from the view
 
173
   *  (as keys) and the expected result set (as values).
 
174
   */
 
175
  protected function assertIdenticalResultset($view, $expected_result, $column_map, $message = 'Identical result set') {
 
176
    // Convert $view->result to an array of arrays.
 
177
    $result = array();
 
178
    foreach ($view->result as $key => $value) {
 
179
      $row = array();
 
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;
 
183
      }
 
184
      $result[$key] = $row;
 
185
    }
 
186
 
 
187
    // Remove the columns we don't need from the expected result.
 
188
    foreach ($expected_result as $key => $value) {
 
189
      $row = array();
 
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];
 
193
      }
 
194
      $expected_result[$key] = $row;
 
195
    }
 
196
 
 
197
    // Reset the numbering of the arrays.
 
198
    $result = array_values($result);
 
199
    $expected_result = array_values($expected_result);
 
200
 
 
201
    // Do the actual comparison.
 
202
    return $this->assertIdentical($result, $expected_result, $message);
 
203
  }
 
204
 
 
205
  /**
 
206
   * Helper function: order an array of array based on a column.
 
207
   */
 
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'));
 
212
    return $result_set;
 
213
  }
 
214
 
 
215
  protected $sort_column = NULL;
 
216
  protected $sort_order = 1;
 
217
 
 
218
  /**
 
219
   * Helper comparison function for orderResultSet().
 
220
   */
 
221
  protected function helperCompareFunction($a, $b) {
 
222
    $value1 = $a[$this->sort_column];
 
223
    $value2 = $b[$this->sort_column];
 
224
    if ($value1 == $value2) {
 
225
        return 0;
 
226
    }
 
227
    return $this->sort_order * (($value1 < $value2) ? -1 : 1);
 
228
  }
 
229
}