Projet

Général

Profil

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

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Abstract class for views testing.
6
 */
7

    
8
/**
9
 *
10
 */
11
abstract class ViewsTestCase extends DrupalWebTestCase {
12

    
13
  /**
14
   *
15
   */
16
  protected $sort_column = NULL;
17

    
18
  /**
19
   *
20
   */
21
  protected $sort_order = 1;
22

    
23
  /**
24
   * Helper function: verify a result set returned by view.
25
   *
26
   * The comparison is done on the string representation of the columns of the
27
   * column map, taking the order of the rows into account, but not the order
28
   * of the columns.
29
   *
30
   * @param view $view
31
   *   An executed View.
32
   * @param array $expected_result
33
   *   An expected result set.
34
   * @param array $column_map
35
   *   An associative array mapping the columns of the result set from the view
36
   *   (as keys) and the expected result set (as values).
37
   */
38
  protected function assertIdenticalResultset($view, $expected_result, $column_map = array(), $message = 'Identical result set') {
39
    return $this->assertIdenticalResultsetHelper($view, $expected_result, $column_map, $message, 'assertIdentical');
40
  }
41

    
42
  /**
43
   * Helper function: verify a result set returned by view..
44
   *
45
   * Inverse of ViewsTestCase::assertIdenticalResultset().
46
   *
47
   * @param view $view
48
   *   An executed View.
49
   * @param array $expected_result
50
   *   An expected result set.
51
   * @param array $column_map
52
   *   An associative array mapping the columns of the result set from the view
53
   *   (as keys) and the expected result set (as values).
54
   */
55
  protected function assertNotIdenticalResultset($view, $expected_result, $column_map = array(), $message = 'Identical result set') {
56
    return $this->assertIdenticalResultsetHelper($view, $expected_result, $column_map, $message, 'assertNotIdentical');
57
  }
58

    
59
  /**
60
   *
61
   */
62
  protected function assertIdenticalResultsetHelper($view, $expected_result, $column_map, $message, $assert_method) {
63
    // Convert $view->result to an array of arrays.
64
    $result = array();
65
    foreach ($view->result as $key => $value) {
66
      $row = array();
67
      foreach ($column_map as $view_column => $expected_column) {
68
        // The comparison will be done on the string representation of the
69
        // value.
70
        $row[$expected_column] = (string) $value->$view_column;
71
      }
72
      $result[$key] = $row;
73
    }
74

    
75
    // Remove the columns we don't need from the expected result.
76
    foreach ($expected_result as $key => $value) {
77
      $row = array();
78
      foreach ($column_map as $expected_column) {
79
        // The comparison will be done on the string representation of the
80
        // value.
81
        $row[$expected_column] = (string) (is_object($value) ? $value->$expected_column : $value[$expected_column]);
82
      }
83
      $expected_result[$key] = $row;
84
    }
85

    
86
    // Reset the numbering of the arrays.
87
    $result = array_values($result);
88
    $expected_result = array_values($expected_result);
89

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

    
92
    // Do the actual comparison.
93
    return $this->$assert_method($result, $expected_result, $message);
94
  }
95

    
96
  /**
97
   * Order an array of array based on a column.
98
   */
99
  protected function orderResultSet($result_set, $column, $reverse = FALSE) {
100
    $this->sort_column = $column;
101
    $this->sort_order = $reverse ? -1 : 1;
102
    usort($result_set, array($this, 'helperCompareFunction'));
103
    return $result_set;
104
  }
105

    
106
  /**
107
   * Helper comparison function for orderResultSet().
108
   */
109
  protected function helperCompareFunction($a, $b) {
110
    $value1 = $a[$this->sort_column];
111
    $value2 = $b[$this->sort_column];
112
    if ($value1 == $value2) {
113
      return 0;
114
    }
115
    return $this->sort_order * (($value1 < $value2) ? -1 : 1);
116
  }
117

    
118
  /**
119
   * Check whether a button with a certain id exists and has a certain label.
120
   */
121
  protected function helperButtonHasLabel($id, $expected_label, $message = 'Label has the expected value: %label.') {
122
    return $this->assertFieldById($id, $expected_label, t($message, array('%label' => $expected_label)));
123
  }
124

    
125
  /**
126
   * Execute a view with debugging.
127
   *
128
   * @param view $view
129
   * @param array $args
130
   */
131
  protected function executeView($view, $args = array()) {
132
    $view->set_display();
133
    $view->pre_execute($args);
134
    $view->execute();
135
    $this->verbose('<pre>Executed view: ' . ((string) $view->build_info['query']) . '</pre>');
136
  }
137

    
138
}
139

    
140
/**
141
 *
142
 */
143
abstract class ViewsSqlTest extends ViewsTestCase {
144

    
145
  /**
146
   * {@inheritdoc}
147
   */
148
  protected function setUp() {
149
    parent::setUp('views', 'views_ui');
150

    
151
    // Define the schema and views data variable before enabling the test
152
    // module.
153
    variable_set('views_test_schema', $this->schemaDefinition());
154
    variable_set('views_test_views_data', $this->viewsData());
155
    variable_set('views_test_views_plugins', $this->viewsPlugins());
156
    module_enable(array('views_test'));
157
    $this->resetAll();
158

    
159
    // Load the test dataset.
160
    $data_set = $this->dataSet();
161
    $query = db_insert('views_test')
162
      ->fields(array_keys($data_set[0]));
163
    foreach ($data_set as $record) {
164
      $query->values($record);
165
    }
166
    $query->execute();
167
    $this->checkPermissions(array(), TRUE);
168
  }
169

    
170
  /**
171
   * This function allows to enable views ui from a higher class which can't
172
   * change the setup function anymore.
173
   *
174
   * @todo Convert existing setUp functions.
175
   */
176
  function enableViewsUi() {
177
    module_enable(array('views_ui'));
178
    // @todo Figure out why it's required to clear the cache here.
179
    views_module_include('views_default', TRUE);
180
    views_get_all_views(TRUE);
181
    menu_rebuild();
182
  }
183

    
184
  /**
185
   * The schema definition.
186
   */
187
  protected function schemaDefinition() {
188
    $schema['views_test'] = array(
189
      'description' => 'Basic test table for Views tests.',
190
      'fields' => array(
191
        'id' => array(
192
          'type' => 'serial',
193
          'unsigned' => TRUE,
194
          'not null' => TRUE,
195
        ),
196
        'name' => array(
197
          'description' => "A person's name",
198
          'type' => 'varchar',
199
          'length' => 255,
200
          'not null' => TRUE,
201
          'default' => '',
202
        ),
203
        'age' => array(
204
          'description' => "The person's age",
205
          'type' => 'int',
206
          'unsigned' => TRUE,
207
          'not null' => TRUE,
208
          'default' => 0),
209
        'job' => array(
210
          'description' => "The person's job",
211
          'type' => 'varchar',
212
          'length' => 255,
213
          'not null' => TRUE,
214
          'default' => 'Undefined',
215
        ),
216
        'created' => array(
217
          'description' => "The creation date of this record",
218
          'type' => 'int',
219
          'unsigned' => TRUE,
220
          'not null' => TRUE,
221
          'default' => 0,
222
        ),
223
      ),
224
      'primary key' => array('id'),
225
      'unique keys' => array(
226
        'name' => array('name'),
227
      ),
228
      'indexes' => array(
229
        'ages' => array('age'),
230
      ),
231
    );
232
    return $schema;
233
  }
234

    
235
  /**
236
   * The views data definition.
237
   */
238
  protected function viewsData() {
239
    // Declaration of the base table.
240
    $data['views_test']['table'] = array(
241
      'group' => t('Views test'),
242
      'base' => array(
243
        'field' => 'id',
244
        'title' => t('Views test'),
245
        'help' => t('Users who have created accounts on your site.'),
246
      ),
247
    );
248

    
249
    // Declaration of fields.
250
    $data['views_test']['id'] = array(
251
      'title' => t('ID'),
252
      'help' => t('The test data ID'),
253
      'field' => array(
254
        'handler' => 'views_handler_field_numeric',
255
        'click sortable' => TRUE,
256
      ),
257
      'argument' => array(
258
        'handler' => 'views_handler_argument_numeric',
259
      ),
260
      'filter' => array(
261
        'handler' => 'views_handler_filter_numeric',
262
      ),
263
      'sort' => array(
264
        'handler' => 'views_handler_sort',
265
      ),
266
    );
267
    $data['views_test']['name'] = array(
268
      'title' => t('Name'),
269
      'help' => t('The name of the person'),
270
      'field' => array(
271
        'handler' => 'views_handler_field',
272
        'click sortable' => TRUE,
273
      ),
274
      'argument' => array(
275
        'handler' => 'views_handler_argument_string',
276
      ),
277
      'filter' => array(
278
        'handler' => 'views_handler_filter_string',
279
      ),
280
      'sort' => array(
281
        'handler' => 'views_handler_sort',
282
      ),
283
    );
284
    $data['views_test']['age'] = array(
285
      'title' => t('Age'),
286
      'help' => t('The age of the person'),
287
      'field' => array(
288
        'handler' => 'views_handler_field_numeric',
289
        'click sortable' => TRUE,
290
      ),
291
      'argument' => array(
292
        'handler' => 'views_handler_argument_numeric',
293
      ),
294
      'filter' => array(
295
        'handler' => 'views_handler_filter_numeric',
296
      ),
297
      'sort' => array(
298
        'handler' => 'views_handler_sort',
299
      ),
300
    );
301
    $data['views_test']['job'] = array(
302
      'title' => t('Job'),
303
      'help' => t('The job of the person'),
304
      'field' => array(
305
        'handler' => 'views_handler_field',
306
        'click sortable' => TRUE,
307
      ),
308
      'argument' => array(
309
        'handler' => 'views_handler_argument_string',
310
      ),
311
      'filter' => array(
312
        'handler' => 'views_handler_filter_string',
313
      ),
314
      'sort' => array(
315
        'handler' => 'views_handler_sort',
316
      ),
317
    );
318
    $data['views_test']['created'] = array(
319
      'title' => t('Created'),
320
      'help' => t('The creation date of this record'),
321
      'field' => array(
322
        'handler' => 'views_handler_field_date',
323
        'click sortable' => TRUE,
324
      ),
325
      'argument' => array(
326
        'handler' => 'views_handler_argument_date',
327
      ),
328
      'filter' => array(
329
        'handler' => 'views_handler_filter_date',
330
      ),
331
      'sort' => array(
332
        'handler' => 'views_handler_sort_date',
333
      ),
334
    );
335
    return $data;
336
  }
337

    
338
  /**
339
   *
340
   */
341
  protected function viewsPlugins() {
342
    return array();
343
  }
344

    
345
  /**
346
   * A very simple test dataset.
347
   */
348
  protected function dataSet() {
349
    return array(
350
      array(
351
        'name' => 'John',
352
        'age' => 25,
353
        'job' => 'Singer',
354
        'created' => gmmktime(0, 0, 0, 1, 1, 2000),
355
      ),
356
      array(
357
        'name' => 'George',
358
        'age' => 27,
359
        'job' => 'Singer',
360
        'created' => gmmktime(0, 0, 0, 1, 2, 2000),
361
      ),
362
      array(
363
        'name' => 'Ringo',
364
        'age' => 28,
365
        'job' => 'Drummer',
366
        'created' => gmmktime(6, 30, 30, 1, 1, 2000),
367
      ),
368
      array(
369
        'name' => 'Paul',
370
        'age' => 26,
371
        'job' => 'Songwriter',
372
        'created' => gmmktime(6, 0, 0, 1, 1, 2000),
373
      ),
374
      array(
375
        'name' => 'Meredith',
376
        'age' => 30,
377
        'job' => 'Speaker',
378
        'created' => gmmktime(6, 30, 10, 1, 1, 2000),
379
      ),
380
    );
381
  }
382

    
383
  /**
384
   * Build and return a basic view of the views_test table.
385
   *
386
   * @return view
387
   */
388
  protected function getBasicView() {
389
    views_include('view');
390

    
391
    // Create the basic view.
392
    $view = new view();
393
    $view->name = 'test_view';
394
    $view->add_display('default');
395
    $view->base_table = 'views_test';
396

    
397
    // Set up the fields we need.
398
    $display = $view->new_display('default', 'Master', 'default');
399
    $display->override_option('fields', array(
400
      'id' => array(
401
        'id' => 'id',
402
        'table' => 'views_test',
403
        'field' => 'id',
404
        'relationship' => 'none',
405
      ),
406
      'name' => array(
407
        'id' => 'name',
408
        'table' => 'views_test',
409
        'field' => 'name',
410
        'relationship' => 'none',
411
      ),
412
      'age' => array(
413
        'id' => 'age',
414
        'table' => 'views_test',
415
        'field' => 'age',
416
        'relationship' => 'none',
417
      ),
418
    ));
419

    
420
    // Set up the sort order.
421
    $display->override_option('sorts', array(
422
      'id' => array(
423
        'order' => 'ASC',
424
        'id' => 'id',
425
        'table' => 'views_test',
426
        'field' => 'id',
427
        'relationship' => 'none',
428
      ),
429
    ));
430

    
431
    // Set up the pager.
432
    $display->override_option('pager', array(
433
      'type' => 'none',
434
      'options' => array('offset' => 0),
435
    ));
436

    
437
    return $view;
438
  }
439

    
440
  /**
441
   * Build and return a Page view of the views_test table.
442
   *
443
   * @return view
444
   */
445
  protected function getBasicPageView() {
446
    views_include('view');
447
    $view = $this->getBasicView();
448

    
449
    // In order to test exposed filters, we have to disable the exposed forms
450
    // cache.
451
    drupal_static_reset('views_exposed_form_cache');
452

    
453
    $display = $view->new_display('page', 'Page', 'page_1');
454
    return $view;
455
  }
456

    
457
}