~views-port/+junk/Views3-d6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
// $Id$

/**
 * @file
 * Tests for Views query features.
 */

/**
 * Basic test class for Views query builder tests.
 */
class ViewsBasicTest extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => t('Basic test'),
      'description' => t('A basic test for Views.'),
      'group' => t('Views')
    );
  }

  protected function setUp() {
    parent::setUp('autoload', 'dbtng', 'views', 'views_test');

    // Load the test dataset.
    $query = db_insert('views_test')
      ->fields(array('name', 'age', 'job'));
    foreach ($this->testDataSet() as $record) {
      $query->values($record);
    }
    $query->execute();
  }

  /**
   * A very simple test dataset.
   */
  protected function testDataSet() {
    return array(
      array(
        'name' => 'John',
        'age' => 25,
        'job' => 'Singer',
      ),
      array(
        'name' => 'George',
        'age' => 27,
        'job' => 'Singer',
      ),
      array(
        'name' => 'Ringo',
        'age' => 28,
        'job' => 'Drummer',
      ),
      array(
        'name' => 'Paul',
        'age' => 26,
        'job' => 'Songwriter',
      ),
      array(
        'name' => 'Meredith',
        'age' => 30,
        'job' => 'Speaker',
      ),
    );
  }

  /**
   * Build an return a basic view of the views_test table.
   */
  protected function getBasicView() {
    views_include('view');

    // Create the basic view.
    $view = new view();
    $view->vid = 'test_view';
    $view->add_display('default');
    $view->base_table = 'views_test';

    // Set up the fields we need.
    $display = $view->new_display('default', 'Defaults', 'default');
    $display->override_option('fields', array(
      'id' => array(
        'id' => 'id',
        'table' => 'views_test',
        'field' => 'id',
        'relationship' => 'none',
      ),
      'name' => array(
        'id' => 'name',
        'table' => 'views_test',
        'field' => 'name',
        'relationship' => 'none',
      ),
      'age' => array(
        'id' => 'age',
        'table' => 'views_test',
        'field' => 'age',
        'relationship' => 'none',
      ),
    ));

    // Set up the sort order.
    $display->override_option('sorts', array(
      'id' => array(
        'order' => 'ASC',
        'id' => 'id',
        'table' => 'views_test',
        'field' => 'id',
        'relationship' => 'none',
      ),
    ));

    return $view;
  }

  /**
   * Test a trivial result set.
   */
  protected function testSimpleResultSet() {
    $view = $this->getBasicView();

    // Execute the view.
    $view->execute();

    // Verify the result.
    $this->assertEqual(5, count($view->result), t('The number of returned rows match.'));
    $this->assertIdenticalResultset($view, $this->testDataSet(), array(
      'views_test_name' => 'name',
      'views_test_age' => 'age',
    ));
  }

  /**
   * Test ordering of the result set.
   */
  protected function testSimpleOrdering() {
    $view = $this->getBasicView();

    // Change the ordering
    $view->display['default']->handler->override_option('sorts', array(
      'age' => array(
        'order' => 'ASC',
        'id' => 'age',
        'table' => 'views_test',
        'field' => 'age',
        'relationship' => 'none',
      ),
    ));

    // Execute the view.
    $view->execute();

    // Verify the result.
    $this->assertEqual(5, count($view->result), t('The number of returned rows match.'));
    $this->assertIdenticalResultset($view, $this->orderResultSet($this->testDataSet(), 'age'), array(
      'views_test_name' => 'name',
      'views_test_age' => 'age',
    ));
  }

  /**
   * Helper function: verify a result set returned by view.
   *
   * The comparison is done on the string representation of the columns of the
   * column map, taking the order of the rows into account, but not the order
   * of the columns.
   *
   * @param $view
   *  An executed View.
   * @param $expected_result
   *  An expected result set.
   * @param $column_map
   *  An associative array mapping the columns of the result set from the view
   *  (as keys) and the expected result set (as values).
   */
  protected function assertIdenticalResultset($view, $expected_result, $column_map, $message = 'Identical result set') {
    // Convert $view->result to an array of arrays.
    $result = array();
    foreach ($view->result as $key => $value) {
      $row = array();
      foreach ($column_map as $view_column => $expected_column) {
        // The comparison will be done on the string representation of the value.
        $row[$expected_column] = (string) $value->$view_column;
      }
      $result[$key] = $row;
    }

    // Remove the columns we don't need from the expected result.
    foreach ($expected_result as $key => $value) {
      $row = array();
      foreach ($column_map as $expected_column) {
        // The comparison will be done on the string representation of the value.
        $row[$expected_column] = (string) $value[$expected_column];
      }
      $expected_result[$key] = $row;
    }

    // Reset the numbering of the arrays.
    $result = array_values($result);
    $expected_result = array_values($expected_result);

    // Do the actual comparison.
    return $this->assertIdentical($result, $expected_result, $message);
  }

  /**
   * Helper function: order an array of array based on a column.
   */
  protected function orderResultSet($result_set, $column, $reverse = FALSE) {
    $this->sort_column = $column;
    $this->sort_order = $reverse ? -1 : 1;
    usort($result_set, array($this, 'helperCompareFunction'));
    return $result_set;
  }

  protected $sort_column = NULL;
  protected $sort_order = 1;

  /**
   * Helper comparison function for orderResultSet().
   */
  protected function helperCompareFunction($a, $b) {
    $value1 = $a[$this->sort_column];
    $value2 = $b[$this->sort_column];
    if ($value1 == $value2) {
        return 0;
    }
    return $this->sort_order * (($value1 < $value2) ? -1 : 1);
  }
}