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 @ 4003efde

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
  public function setUp(array $modules = array()) {
183
    $modules[] = 'views';
184
    $modules[] = 'views_ui';
185
    parent::setUp($modules);
186

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
491
    return $view;
492
  }
493

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

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

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

    
511
}