Projet

Général

Profil

Paste
Télécharger (12,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / tests / views_query.test @ 27370441

1
<?php
2

    
3
/**
4
 * @file
5
 * Tests for Views query features.
6
 */
7

    
8
/**
9
 * Abstract class for views testing.
10
 */
11
abstract 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 = array(), $message = 'Identical result set') {
28
    return $this->assertIdenticalResultsetHelper($view, $expected_result, $column_map, $message, 'assertIdentical');
29
  }
30

    
31
  /**
32
   * Helper function: verify a result set returned by view..
33
   *
34
   * Inverse of ViewsTestCase::assertIdenticalResultset().
35
   *
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).
43
   */
44
  protected function assertNotIdenticalResultset($view, $expected_result, $column_map = array(), $message = 'Identical result set') {
45
    return $this->assertIdenticalResultsetHelper($view, $expected_result, $column_map, $message, 'assertNotIdentical');
46
  }
47

    
48
  protected function assertIdenticalResultsetHelper($view, $expected_result, $column_map, $message, $assert_method) {
49
    // Convert $view->result to an array of arrays.
50
    $result = array();
51
    foreach ($view->result as $key => $value) {
52
      $row = array();
53
      foreach ($column_map as $view_column => $expected_column) {
54
        // The comparison will be done on the string representation of the value.
55
        $row[$expected_column] = (string) $value->$view_column;
56
      }
57
      $result[$key] = $row;
58
    }
59

    
60
    // Remove the columns we don't need from the expected result.
61
    foreach ($expected_result as $key => $value) {
62
      $row = array();
63
      foreach ($column_map as $expected_column) {
64
        // The comparison will be done on the string representation of the value.
65
        $row[$expected_column] = (string) (is_object($value) ? $value->$expected_column : $value[$expected_column]);
66
      }
67
      $expected_result[$key] = $row;
68
    }
69

    
70
    // Reset the numbering of the arrays.
71
    $result = array_values($result);
72
    $expected_result = array_values($expected_result);
73

    
74
    $this->verbose('<pre>Returned data set: ' . print_r($result, TRUE) . "\n\nExpected: ". print_r($expected_result, TRUE));
75

    
76
    // Do the actual comparison.
77
    return $this->$assert_method($result, $expected_result, $message);
78
  }
79

    
80
  /**
81
   * Helper function: order an array of array based on a column.
82
   */
83
  protected function orderResultSet($result_set, $column, $reverse = FALSE) {
84
    $this->sort_column = $column;
85
    $this->sort_order = $reverse ? -1 : 1;
86
    usort($result_set, array($this, 'helperCompareFunction'));
87
    return $result_set;
88
  }
89

    
90
  protected $sort_column = NULL;
91
  protected $sort_order = 1;
92

    
93
  /**
94
   * Helper comparison function for orderResultSet().
95
   */
96
  protected function helperCompareFunction($a, $b) {
97
    $value1 = $a[$this->sort_column];
98
    $value2 = $b[$this->sort_column];
99
    if ($value1 == $value2) {
100
        return 0;
101
    }
102
    return $this->sort_order * (($value1 < $value2) ? -1 : 1);
103
  }
104

    
105
  /**
106
   * Helper function to check whether a button with a certain id exists and has a certain label.
107
   */
108
  protected function helperButtonHasLabel($id, $expected_label, $message = 'Label has the expected value: %label.') {
109
    return $this->assertFieldById($id, $expected_label, t($message, array('%label' => $expected_label)));
110
  }
111

    
112
  /**
113
   * Helper function to execute a view with debugging.
114
   *
115
   * @param view $view
116
   * @param array $args
117
   */
118
  protected function executeView($view, $args = array()) {
119
    $view->set_display();
120
    $view->pre_execute($args);
121
    $view->execute();
122
    $this->verbose('<pre>Executed view: ' . ((string) $view->build_info['query']) . '</pre>');
123
  }
124
}
125

    
126
abstract class ViewsSqlTest extends ViewsTestCase {
127

    
128
  protected function setUp() {
129
    parent::setUp('views', 'views_ui');
130

    
131
    // Define the schema and views data variable before enabling the test module.
132
    variable_set('views_test_schema', $this->schemaDefinition());
133
    variable_set('views_test_views_data', $this->viewsData());
134
    variable_set('views_test_views_plugins', $this->viewsPlugins());
135

    
136
    module_enable(array('views_test'));
137
    $this->resetAll();
138

    
139
    // Load the test dataset.
140
    $data_set = $this->dataSet();
141
    $query = db_insert('views_test')
142
      ->fields(array_keys($data_set[0]));
143
    foreach ($data_set as $record) {
144
      $query->values($record);
145
    }
146
    $query->execute();
147
    $this->checkPermissions(array(), TRUE);
148
  }
149

    
150
  /**
151
   * This function allows to enable views ui from a higher class which can't change the setup function anymore.
152
   *
153
   * @TODO
154
   *   Convert existing setUp functions.
155
   */
156
  function enableViewsUi() {
157
    module_enable(array('views_ui'));
158
    // @TODO Figure out why it's required to clear the cache here.
159
    views_module_include('views_default', TRUE);
160
    views_get_all_views(TRUE);
161
    menu_rebuild();
162
  }
163

    
164
  /**
165
   * The schema definition.
166
   */
167
  protected function schemaDefinition() {
168
    $schema['views_test'] = array(
169
      'description' => 'Basic test table for Views tests.',
170
      'fields' => array(
171
        'id' => array(
172
          'type' => 'serial',
173
          'unsigned' => TRUE,
174
          'not null' => TRUE,
175
        ),
176
        'name' => array(
177
          'description' => "A person's name",
178
          'type' => 'varchar',
179
          'length' => 255,
180
          'not null' => TRUE,
181
          'default' => '',
182
        ),
183
        'age' => array(
184
          'description' => "The person's age",
185
          'type' => 'int',
186
          'unsigned' => TRUE,
187
          'not null' => TRUE,
188
          'default' => 0),
189
        'job' => array(
190
          'description' => "The person's job",
191
          'type' => 'varchar',
192
          'length' => 255,
193
          'not null' => TRUE,
194
          'default' => 'Undefined',
195
        ),
196
        'created' => array(
197
          'description' => "The creation date of this record",
198
          'type' => 'int',
199
          'unsigned' => TRUE,
200
          'not null' => TRUE,
201
          'default' => 0,
202
        ),
203
      ),
204
      'primary key' => array('id'),
205
      'unique keys' => array(
206
        'name' => array('name')
207
      ),
208
      'indexes' => array(
209
        'ages' => array('age'),
210
      ),
211
    );
212
    return $schema;
213
  }
214

    
215
  /**
216
   * The views data definition.
217
   */
218
  protected function viewsData() {
219
    // Declaration of the base table.
220
    $data['views_test']['table'] = array(
221
      'group' => t('Views test'),
222
      'base' => array(
223
        'field' => 'id',
224
        'title' => t('Views test'),
225
        'help' => t('Users who have created accounts on your site.'),
226
      ),
227
    );
228

    
229
    // Declaration of fields.
230
    $data['views_test']['id'] = array(
231
      'title' => t('ID'),
232
      'help' => t('The test data ID'),
233
      'field' => array(
234
        'handler' => 'views_handler_field_numeric',
235
        'click sortable' => TRUE,
236
      ),
237
      'argument' => array(
238
        'handler' => 'views_handler_argument_numeric',
239
      ),
240
      'filter' => array(
241
        'handler' => 'views_handler_filter_numeric',
242
      ),
243
      'sort' => array(
244
        'handler' => 'views_handler_sort',
245
      ),
246
    );
247
    $data['views_test']['name'] = array(
248
      'title' => t('Name'),
249
      'help' => t('The name of the person'),
250
      'field' => array(
251
        'handler' => 'views_handler_field',
252
        'click sortable' => TRUE,
253
      ),
254
      'argument' => array(
255
        'handler' => 'views_handler_argument_string',
256
      ),
257
      'filter' => array(
258
        'handler' => 'views_handler_filter_string',
259
      ),
260
      'sort' => array(
261
        'handler' => 'views_handler_sort',
262
      ),
263
    );
264
    $data['views_test']['age'] = array(
265
      'title' => t('Age'),
266
      'help' => t('The age of the person'),
267
      'field' => array(
268
        'handler' => 'views_handler_field_numeric',
269
        'click sortable' => TRUE,
270
      ),
271
      'argument' => array(
272
        'handler' => 'views_handler_argument_numeric',
273
      ),
274
      'filter' => array(
275
        'handler' => 'views_handler_filter_numeric',
276
      ),
277
      'sort' => array(
278
        'handler' => 'views_handler_sort',
279
      ),
280
    );
281
    $data['views_test']['job'] = array(
282
      'title' => t('Job'),
283
      'help' => t('The job of the person'),
284
      'field' => array(
285
        'handler' => 'views_handler_field',
286
        'click sortable' => TRUE,
287
      ),
288
      'argument' => array(
289
        'handler' => 'views_handler_argument_string',
290
      ),
291
      'filter' => array(
292
        'handler' => 'views_handler_filter_string',
293
      ),
294
      'sort' => array(
295
        'handler' => 'views_handler_sort',
296
      ),
297
    );
298
    $data['views_test']['created'] = array(
299
      'title' => t('Created'),
300
      'help' => t('The creation date of this record'),
301
      'field' => array(
302
        'handler' => 'views_handler_field_date',
303
        'click sortable' => TRUE,
304
      ),
305
      'argument' => array(
306
        'handler' => 'views_handler_argument_date',
307
      ),
308
      'filter' => array(
309
        'handler' => 'views_handler_filter_date',
310
      ),
311
      'sort' => array(
312
        'handler' => 'views_handler_sort_date',
313
      ),
314
    );
315
    return $data;
316
  }
317

    
318
  protected function viewsPlugins() {
319
    return array();
320
  }
321

    
322
  /**
323
   * A very simple test dataset.
324
   */
325
  protected function dataSet() {
326
    return array(
327
      array(
328
        'name' => 'John',
329
        'age' => 25,
330
        'job' => 'Singer',
331
        'created' => gmmktime(0, 0, 0, 1, 1, 2000),
332
      ),
333
      array(
334
        'name' => 'George',
335
        'age' => 27,
336
        'job' => 'Singer',
337
        'created' => gmmktime(0, 0, 0, 1, 2, 2000),
338
      ),
339
      array(
340
        'name' => 'Ringo',
341
        'age' => 28,
342
        'job' => 'Drummer',
343
        'created' => gmmktime(6, 30, 30, 1, 1, 2000),
344
      ),
345
      array(
346
        'name' => 'Paul',
347
        'age' => 26,
348
        'job' => 'Songwriter',
349
        'created' => gmmktime(6, 0, 0, 1, 1, 2000),
350
      ),
351
      array(
352
        'name' => 'Meredith',
353
        'age' => 30,
354
        'job' => 'Speaker',
355
        'created' => gmmktime(6, 30, 10, 1, 1, 2000),
356
      ),
357
    );
358
  }
359

    
360
  /**
361
   * Build and return a basic view of the views_test table.
362
   *
363
   * @return view
364
   */
365
  protected function getBasicView() {
366
    views_include('view');
367

    
368
    // Create the basic view.
369
    $view = new view();
370
    $view->name = 'test_view';
371
    $view->add_display('default');
372
    $view->base_table = 'views_test';
373

    
374
    // Set up the fields we need.
375
    $display = $view->new_display('default', 'Master', 'default');
376
    $display->override_option('fields', array(
377
      'id' => array(
378
        'id' => 'id',
379
        'table' => 'views_test',
380
        'field' => 'id',
381
        'relationship' => 'none',
382
      ),
383
      'name' => array(
384
        'id' => 'name',
385
        'table' => 'views_test',
386
        'field' => 'name',
387
        'relationship' => 'none',
388
      ),
389
      'age' => array(
390
        'id' => 'age',
391
        'table' => 'views_test',
392
        'field' => 'age',
393
        'relationship' => 'none',
394
      ),
395
    ));
396

    
397
    // Set up the sort order.
398
    $display->override_option('sorts', array(
399
      'id' => array(
400
        'order' => 'ASC',
401
        'id' => 'id',
402
        'table' => 'views_test',
403
        'field' => 'id',
404
        'relationship' => 'none',
405
      ),
406
    ));
407

    
408
    // Set up the pager.
409
    $display->override_option('pager', array(
410
      'type' => 'none',
411
      'options' => array('offset' => 0),
412
    ));
413

    
414
    return $view;
415
  }
416

    
417
  /**
418
   * Build and return a Page view of the views_test table.
419
   *
420
   * @return view
421
   */
422
  protected function getBasicPageView() {
423
    views_include('view');
424
    $view = $this->getBasicView();
425

    
426
    // In order to test exposed filters, we have to disable
427
    // the exposed forms cache.
428
    drupal_static_reset('views_exposed_form_cache');
429

    
430
    $display = $view->new_display('page', 'Page', 'page_1');
431
    return $view;
432
  }
433
}