Projet

Général

Profil

Paste
Télécharger (13,4 ko) Statistiques
| Branche: | Révision:

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

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
   * Log in as user 1.
140
   */
141
  protected function loginUser1() {
142
    $password = user_password();
143
    // Reset the user 1 password.
144
    $account = user_load(1);
145
    $edit = array(
146
      'pass' => $password,
147
    );
148
    $account = user_save($account, $edit);
149
    $account->pass_raw = $password;
150

    
151
    // Log in as user 1.
152
    $this->drupalLogin($account);
153
  }
154

    
155
  /**
156
   * {@inheritdoc}
157
   */
158
  protected function verbose($message, $title = NULL) {
159
    // Handle arrays, objects, etc.
160
    if (!is_string($message)) {
161
      $message = "<pre>\n" . print_r($message, TRUE) . "\n</pre>\n";
162
    }
163

    
164
    // Optional title to go before the output.
165
    if (!empty($title)) {
166
      $title = '<h2>' . check_plain($title) . "</h2>\n";
167
    }
168

    
169
    parent::verbose($title . $message);
170
  }
171

    
172
}
173

    
174
/**
175
 *
176
 */
177
abstract class ViewsSqlTest extends ViewsTestCase {
178

    
179
  /**
180
   * {@inheritdoc}
181
   */
182
  protected function setUp() {
183
    parent::setUp('views', 'views_ui');
184

    
185
    // Define the schema and views data variable before enabling the test
186
    // module.
187
    variable_set('views_test_schema', $this->schemaDefinition());
188
    variable_set('views_test_views_data', $this->viewsData());
189
    variable_set('views_test_views_plugins', $this->viewsPlugins());
190
    module_enable(array('views_test'));
191
    $this->resetAll();
192

    
193
    // Load the test dataset.
194
    $data_set = $this->dataSet();
195
    $query = db_insert('views_test')
196
      ->fields(array_keys($data_set[0]));
197
    foreach ($data_set as $record) {
198
      $query->values($record);
199
    }
200
    $query->execute();
201
    $this->checkPermissions(array(), TRUE);
202
  }
203

    
204
  /**
205
   * Create a term.
206
   *
207
   * @param int $vid
208
   *   The vocabulary ID that the term is to be added to.
209
   *
210
   * @return object
211
   *   A full term object with a random name.
212
   */
213
  protected function drupalCreateTerm($vid) {
214
    $term = new stdClass();
215
    $term->name = $this->randomName();
216
    $term->description = $this->randomName();
217
    $term->vid = $vid;
218
    taxonomy_term_save($term);
219
    return $term;
220
  }
221

    
222
  /**
223
   * This function allows to enable views ui from a higher class which can't
224
   * change the setup function anymore.
225
   *
226
   * @todo Convert existing setUp functions.
227
   */
228
  function enableViewsUi() {
229
    module_enable(array('views_ui'));
230
    // @todo Figure out why it's required to clear the cache here.
231
    views_module_include('views_default', TRUE);
232
    views_get_all_views(TRUE);
233
    menu_rebuild();
234
  }
235

    
236
  /**
237
   * The schema definition.
238
   */
239
  protected function schemaDefinition() {
240
    $schema['views_test'] = array(
241
      'description' => 'Basic test table for Views tests.',
242
      'fields' => array(
243
        'id' => array(
244
          'type' => 'serial',
245
          'unsigned' => TRUE,
246
          'not null' => TRUE,
247
        ),
248
        'name' => array(
249
          'description' => "A person's name",
250
          'type' => 'varchar',
251
          'length' => 255,
252
          'not null' => TRUE,
253
          'default' => '',
254
        ),
255
        'age' => array(
256
          'description' => "The person's age",
257
          'type' => 'int',
258
          'unsigned' => TRUE,
259
          'not null' => TRUE,
260
          'default' => 0),
261
        'job' => array(
262
          'description' => "The person's job",
263
          'type' => 'varchar',
264
          'length' => 255,
265
          'not null' => TRUE,
266
          'default' => 'Undefined',
267
        ),
268
        'created' => array(
269
          'description' => "The creation date of this record",
270
          'type' => 'int',
271
          'unsigned' => TRUE,
272
          'not null' => TRUE,
273
          'default' => 0,
274
        ),
275
      ),
276
      'primary key' => array('id'),
277
      'unique keys' => array(
278
        'name' => array('name'),
279
      ),
280
      'indexes' => array(
281
        'ages' => array('age'),
282
      ),
283
    );
284
    return $schema;
285
  }
286

    
287
  /**
288
   * The views data definition.
289
   */
290
  protected function viewsData() {
291
    // Declaration of the base table.
292
    $data['views_test']['table'] = array(
293
      'group' => t('Views test'),
294
      'base' => array(
295
        'field' => 'id',
296
        'title' => t('Views test'),
297
        'help' => t('Users who have created accounts on your site.'),
298
      ),
299
    );
300

    
301
    // Declaration of fields.
302
    $data['views_test']['id'] = array(
303
      'title' => t('ID'),
304
      'help' => t('The test data ID'),
305
      'field' => array(
306
        'handler' => 'views_handler_field_numeric',
307
        'click sortable' => TRUE,
308
      ),
309
      'argument' => array(
310
        'handler' => 'views_handler_argument_numeric',
311
      ),
312
      'filter' => array(
313
        'handler' => 'views_handler_filter_numeric',
314
      ),
315
      'sort' => array(
316
        'handler' => 'views_handler_sort',
317
      ),
318
    );
319
    $data['views_test']['name'] = array(
320
      'title' => t('Name'),
321
      'help' => t('The name of the person'),
322
      'field' => array(
323
        'handler' => 'views_handler_field',
324
        'click sortable' => TRUE,
325
      ),
326
      'argument' => array(
327
        'handler' => 'views_handler_argument_string',
328
      ),
329
      'filter' => array(
330
        'handler' => 'views_handler_filter_string',
331
      ),
332
      'sort' => array(
333
        'handler' => 'views_handler_sort',
334
      ),
335
    );
336
    $data['views_test']['age'] = array(
337
      'title' => t('Age'),
338
      'help' => t('The age of the person'),
339
      'field' => array(
340
        'handler' => 'views_handler_field_numeric',
341
        'click sortable' => TRUE,
342
      ),
343
      'argument' => array(
344
        'handler' => 'views_handler_argument_numeric',
345
      ),
346
      'filter' => array(
347
        'handler' => 'views_handler_filter_numeric',
348
      ),
349
      'sort' => array(
350
        'handler' => 'views_handler_sort',
351
      ),
352
    );
353
    $data['views_test']['job'] = array(
354
      'title' => t('Job'),
355
      'help' => t('The job of the person'),
356
      'field' => array(
357
        'handler' => 'views_handler_field',
358
        'click sortable' => TRUE,
359
      ),
360
      'argument' => array(
361
        'handler' => 'views_handler_argument_string',
362
      ),
363
      'filter' => array(
364
        'handler' => 'views_handler_filter_string',
365
      ),
366
      'sort' => array(
367
        'handler' => 'views_handler_sort',
368
      ),
369
    );
370
    $data['views_test']['created'] = array(
371
      'title' => t('Created'),
372
      'help' => t('The creation date of this record'),
373
      'field' => array(
374
        'handler' => 'views_handler_field_date',
375
        'click sortable' => TRUE,
376
      ),
377
      'argument' => array(
378
        'handler' => 'views_handler_argument_date',
379
      ),
380
      'filter' => array(
381
        'handler' => 'views_handler_filter_date',
382
      ),
383
      'sort' => array(
384
        'handler' => 'views_handler_sort_date',
385
      ),
386
    );
387
    return $data;
388
  }
389

    
390
  /**
391
   *
392
   */
393
  protected function viewsPlugins() {
394
    return array();
395
  }
396

    
397
  /**
398
   * A very simple test dataset.
399
   */
400
  protected function dataSet() {
401
    return array(
402
      array(
403
        'name' => 'John',
404
        'age' => 25,
405
        'job' => 'Singer',
406
        'created' => gmmktime(0, 0, 0, 1, 1, 2000),
407
      ),
408
      array(
409
        'name' => 'George',
410
        'age' => 27,
411
        'job' => 'Singer',
412
        'created' => gmmktime(0, 0, 0, 1, 2, 2000),
413
      ),
414
      array(
415
        'name' => 'Ringo',
416
        'age' => 28,
417
        'job' => 'Drummer',
418
        'created' => gmmktime(6, 30, 30, 1, 1, 2000),
419
      ),
420
      array(
421
        'name' => 'Paul',
422
        'age' => 26,
423
        'job' => 'Songwriter',
424
        'created' => gmmktime(6, 0, 0, 1, 1, 2000),
425
      ),
426
      array(
427
        'name' => 'Meredith',
428
        'age' => 30,
429
        'job' => 'Speaker',
430
        'created' => gmmktime(6, 30, 10, 1, 1, 2000),
431
      ),
432
    );
433
  }
434

    
435
  /**
436
   * Build and return a basic view of the views_test table.
437
   *
438
   * @return view
439
   */
440
  protected function getBasicView() {
441
    views_include('view');
442

    
443
    // Create the basic view.
444
    $view = new view();
445
    $view->name = 'test_view';
446
    $view->add_display('default');
447
    $view->base_table = 'views_test';
448

    
449
    // Set up the fields we need.
450
    $display = $view->new_display('default', 'Master', 'default');
451
    $display->override_option('fields', array(
452
      'id' => array(
453
        'id' => 'id',
454
        'table' => 'views_test',
455
        'field' => 'id',
456
        'relationship' => 'none',
457
      ),
458
      'name' => array(
459
        'id' => 'name',
460
        'table' => 'views_test',
461
        'field' => 'name',
462
        'relationship' => 'none',
463
      ),
464
      'age' => array(
465
        'id' => 'age',
466
        'table' => 'views_test',
467
        'field' => 'age',
468
        'relationship' => 'none',
469
      ),
470
    ));
471

    
472
    // Set up the sort order.
473
    $display->override_option('sorts', array(
474
      'id' => array(
475
        'order' => 'ASC',
476
        'id' => 'id',
477
        'table' => 'views_test',
478
        'field' => 'id',
479
        'relationship' => 'none',
480
      ),
481
    ));
482

    
483
    // Set up the pager.
484
    $display->override_option('pager', array(
485
      'type' => 'none',
486
      'options' => array('offset' => 0),
487
    ));
488

    
489
    return $view;
490
  }
491

    
492
  /**
493
   * Build and return a Page view of the views_test table.
494
   *
495
   * @return view
496
   */
497
  protected function getBasicPageView() {
498
    views_include('view');
499
    $view = $this->getBasicView();
500

    
501
    // In order to test exposed filters, we have to disable the exposed forms
502
    // cache.
503
    drupal_static_reset('views_exposed_form_cache');
504

    
505
    $display = $view->new_display('page', 'Page', 'page_1');
506
    return $view;
507
  }
508

    
509
}