source: sipes/modules_contrib/views/tests/views_query.test @ 8a8efa8

stableversion-3.0
Last change on this file since 8a8efa8 was 65dadeb, checked in by José Gregorio Puentes <jpuentes@…>, 8 años ago

se agregaron los archivos de la nueva version del modulo

  • Propiedad mode establecida a 100644
File size: 10.4 KB
Línea 
1<?php
2
3/**
4 * @file
5 * Tests for Views query features.
6 */
7
8/**
9 * Abstract class for views testing
10 */
11abstract class ViewsTestCase extends DrupalWebTestCase {
12  /**
13   * Helper function: verify a result set returned by view.
14   *
15   * The comparison is done on the string representation of the columns of the
16   * column map, taking the order of the rows into account, but not the order
17   * of the columns.
18   *
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).
26   */
27  protected function assertIdenticalResultset($view, $expected_result, $column_map, $message = 'Identical result set') {
28    // Convert $view->result to an array of arrays.
29    $result = array();
30    foreach ($view->result as $key => $value) {
31      $row = array();
32      foreach ($column_map as $view_column => $expected_column) {
33        // The comparison will be done on the string representation of the value.
34        $row[$expected_column] = (string) $value->$view_column;
35      }
36      $result[$key] = $row;
37    }
38
39    // Remove the columns we don't need from the expected result.
40    foreach ($expected_result as $key => $value) {
41      $row = array();
42      foreach ($column_map as $expected_column) {
43        // The comparison will be done on the string representation of the value.
44        $row[$expected_column] = (string) $value[$expected_column];
45      }
46      $expected_result[$key] = $row;
47    }
48
49    // Reset the numbering of the arrays.
50    $result = array_values($result);
51    $expected_result = array_values($expected_result);
52
53    $this->verbose('<pre>Returned data set: ' . print_r($result, TRUE) . "\n\nExpected: ". print_r($expected_result, TRUE));
54
55    // Do the actual comparison.
56    return $this->assertIdentical($result, $expected_result, $message);
57  }
58
59  /**
60   * Helper function: order an array of array based on a column.
61   */
62  protected function orderResultSet($result_set, $column, $reverse = FALSE) {
63    $this->sort_column = $column;
64    $this->sort_order = $reverse ? -1 : 1;
65    usort($result_set, array($this, 'helperCompareFunction'));
66    return $result_set;
67  }
68
69  protected $sort_column = NULL;
70  protected $sort_order = 1;
71
72  /**
73   * Helper comparison function for orderResultSet().
74   */
75  protected function helperCompareFunction($a, $b) {
76    $value1 = $a[$this->sort_column];
77    $value2 = $b[$this->sort_column];
78    if ($value1 == $value2) {
79        return 0;
80    }
81    return $this->sort_order * (($value1 < $value2) ? -1 : 1);
82  }
83
84  /**
85   * Helper function to check whether a button with a certain id exists and has a certain label.
86   */
87  protected function helperButtonHasLabel($id, $expected_label, $message = 'Label has the expected value: %label.') {
88    return $this->assertFieldById($id, $expected_label, t($message, array('%label' => $expected_label)));
89  }
90}
91
92abstract class ViewsSqlTest extends ViewsTestCase {
93
94  protected function setUp() {
95    parent::setUp('views', 'views_ui');
96
97    // Define the schema and views data variable before enabling the test module.
98    variable_set('views_test_schema', $this->schemaDefinition());
99    variable_set('views_test_views_data', $this->viewsData());
100    variable_set('views_test_views_plugins', $this->viewsPlugins());
101
102    module_enable(array('views_test'));
103    drupal_install_modules(array('views_test'));
104
105    drupal_get_schema('views_test', TRUE);
106    // Load the test dataset.
107    foreach ($this->dataSet() as $record) {
108      drupal_write_record('views_test', $record);
109    }
110    $this->checkPermissions(array(), TRUE);
111  }
112
113
114  /**
115   * This function allows to enable views ui from a higher class which can't change the setup function anymore.
116   *
117   * @TODO
118   *   Convert existing setUp functions.
119   */
120  function enableViewsUi() {
121    module_enable(array('views_ui'));
122    // @TODO Figure out why it's required to clear the cache here.
123    views_module_include('views_default.inc', TRUE);
124    views_get_all_views(TRUE);
125    menu_rebuild();
126  }
127
128  /**
129  * The schema definition.
130  */
131  protected function schemaDefinition() {
132    $schema['views_test'] = array(
133      'description' => 'Basic test table for Views tests.',
134      'fields' => array(
135        'id' => array(
136          'type' => 'serial',
137          'unsigned' => TRUE,
138          'not null' => TRUE,
139        ),
140        'name' => array(
141          'description' => "A person's name",
142          'type' => 'varchar',
143          'length' => 255,
144          'not null' => TRUE,
145          'default' => '',
146        ),
147        'age' => array(
148          'description' => "The person's age",
149          'type' => 'int',
150          'unsigned' => TRUE,
151          'not null' => TRUE,
152          'default' => 0),
153        'job' => array(
154          'description' => "The person's job",
155          'type' => 'varchar',
156          'length' => 255,
157          'not null' => TRUE,
158          'default' => 'Undefined',
159        ),
160        'created' => array(
161          'description' => "The creation date of this record",
162          'type' => 'int',
163          'unsigned' => TRUE,
164          'not null' => TRUE,
165          'default' => 0,
166        ),
167      ),
168      'primary key' => array('id'),
169      'unique keys' => array(
170        'name' => array('name')
171      ),
172      'indexes' => array(
173        'ages' => array('age'),
174      ),
175    );
176    return $schema;
177  }
178
179  protected function viewsPlugins() {
180    return array();
181  }
182
183  /**
184   * The views data definition.
185   */
186  protected function viewsData() {
187    // Declaration of the base table.
188    $data['views_test']['table'] = array(
189      'group' => t('Views test'),
190      'base' => array(
191        'field' => 'id',
192        'title' => t('Views test'),
193        'help' => t('Users who have created accounts on your site.'),
194      ),
195    );
196
197    // Declaration of fields.
198    $data['views_test']['id'] = array(
199      'title' => t('ID'),
200      'help' => t('The test data ID'),
201      'field' => array(
202        'handler' => 'views_handler_field_numeric',
203        'click sortable' => TRUE,
204      ),
205      'argument' => array(
206        'handler' => 'views_handler_argument_numeric',
207      ),
208      'filter' => array(
209        'handler' => 'views_handler_filter_numeric',
210      ),
211      'sort' => array(
212        'handler' => 'views_handler_sort',
213      ),
214    );
215    $data['views_test']['name'] = array(
216      'title' => t('Name'),
217      'help' => t('The name of the person'),
218      'field' => array(
219        'handler' => 'views_handler_field',
220        'click sortable' => TRUE,
221      ),
222      'argument' => array(
223        'handler' => 'views_handler_argument_string',
224      ),
225      'filter' => array(
226        'handler' => 'views_handler_filter_string',
227      ),
228      'sort' => array(
229        'handler' => 'views_handler_sort',
230      ),
231    );
232    $data['views_test']['age'] = array(
233      'title' => t('Age'),
234      'help' => t('The age of the person'),
235      'field' => array(
236        'handler' => 'views_handler_field_numeric',
237        'click sortable' => TRUE,
238      ),
239      'argument' => array(
240        'handler' => 'views_handler_argument_numeric',
241      ),
242      'filter' => array(
243        'handler' => 'views_handler_filter_numeric',
244      ),
245      'sort' => array(
246        'handler' => 'views_handler_sort',
247      ),
248    );
249    $data['views_test']['job'] = array(
250      'title' => t('Job'),
251      'help' => t('The job of the person'),
252      'field' => array(
253        'handler' => 'views_handler_field',
254        'click sortable' => TRUE,
255      ),
256      'argument' => array(
257        'handler' => 'views_handler_argument_string',
258      ),
259      'filter' => array(
260        'handler' => 'views_handler_filter_string',
261      ),
262      'sort' => array(
263        'handler' => 'views_handler_sort',
264      ),
265    );
266    $data['views_test']['created'] = array(
267      'title' => t('Created'),
268      'help' => t('The creation date of this record'),
269      'field' => array(
270        'handler' => 'views_handler_field_date',
271        'click sortable' => TRUE,
272      ),
273      'argument' => array(
274        'handler' => 'views_handler_argument_date',
275      ),
276      'filter' => array(
277        'handler' => 'views_handler_filter_date',
278      ),
279      'sort' => array(
280        'handler' => 'views_handler_sort_date',
281      ),
282    );
283    return $data;
284  }
285
286  /**
287   * A very simple test dataset.
288   */
289  protected function dataSet() {
290    return array(
291      array(
292        'name' => 'John',
293        'age' => 25,
294        'job' => 'Singer',
295        'created' => gmmktime(0, 0, 0, 1, 1, 2000),
296      ),
297      array(
298        'name' => 'George',
299        'age' => 27,
300        'job' => 'Singer',
301        'created' => gmmktime(0, 0, 0, 1, 2, 2000),
302      ),
303      array(
304        'name' => 'Ringo',
305        'age' => 28,
306        'job' => 'Drummer',
307        'created' => gmmktime(6, 30, 30, 1, 1, 2000),
308      ),
309      array(
310        'name' => 'Paul',
311        'age' => 26,
312        'job' => 'Songwriter',
313        'created' => gmmktime(6, 0, 0, 1, 1, 2000),
314      ),
315      array(
316        'name' => 'Meredith',
317        'age' => 30,
318        'job' => 'Speaker',
319        'created' => gmmktime(6, 30, 10, 1, 1, 2000),
320      ),
321    );
322  }
323
324  /**
325  * Build and return a basic view of the views_test table.
326  */
327  protected function getBasicView() {
328    views_include('view');
329
330    // Create the basic view.
331    $view = new view();
332    $view->vid = 'test_view';
333    $view->add_display('default');
334    $view->base_table = 'views_test';
335
336    // Set up the fields we need.
337    $display = $view->new_display('default', 'Defaults', 'default');
338    $display->override_option('fields', array(
339      'id' => array(
340        'id' => 'id',
341        'table' => 'views_test',
342        'field' => 'id',
343        'relationship' => 'none',
344      ),
345      'name' => array(
346        'id' => 'name',
347        'table' => 'views_test',
348        'field' => 'name',
349        'relationship' => 'none',
350      ),
351      'age' => array(
352        'id' => 'age',
353        'table' => 'views_test',
354        'field' => 'age',
355        'relationship' => 'none',
356      ),
357    ));
358
359    // Set up the sort order.
360    $display->override_option('sorts', array(
361      'id' => array(
362        'order' => 'ASC',
363        'id' => 'id',
364        'table' => 'views_test',
365        'field' => 'id',
366        'relationship' => 'none',
367      ),
368    ));
369
370    return $view;
371  }
372
373  /**
374   * Helper function to execute a view with debugging.
375   */
376  protected function executeView($view) {
377    $view->execute();
378    $this->verbose('<pre>Executed view: ' . print_r($view->build_info, TRUE) . '</pre>');
379  }
380}
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.