Projet

Général

Profil

Paste
Télécharger (18,9 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / tests / views_groupby.test @ 4003efde

1
<?php
2

    
3
/**
4
 * @file
5
 * Definitions of ViewsQueryGroupByTest and ViewsUiGroupbyTestCase.
6
 */
7

    
8
/**
9
 * Tests aggregate functionality of views, for example count.
10
 */
11
class ViewsQueryGroupByTest extends ViewsSqlTest {
12

    
13
  /**
14
   * Test meta data.
15
   */
16
  public static function getInfo() {
17
    return array(
18
      'name' => 'Groupby',
19
      'description' => 'Tests aggregate functionality of views, for example count.',
20
      'group' => 'Views',
21
    );
22
  }
23

    
24
  // tests ambiguous group by column error (postgresql)
25
  public function testAggregateAmbiguity() {
26
    // Create 4 nodes of type1
27
    $type1 = $this->drupalCreateContentType();
28

    
29
    $node_1 = array(
30
      'type' => $type1->type,
31
    );
32
    $this->drupalCreateNode($node_1);
33
    $this->drupalCreateNode($node_1);
34
    $this->drupalCreateNode($node_1);
35
    $this->drupalCreateNode($node_1);
36

    
37
    $view = $this->viewsAggregateAmbiguityView();
38
    $output = $view->execute_display();
39

    
40
    $this->assertEqual(count($view->result), 1, 'Make sure there are no ambiguity problems with the group by operation.');
41
  }
42

    
43
  public function viewsAggregateAmbiguityView() {
44
    $view = new view();
45
    $view->name = 'aggregate_ambiguity';
46
    $view->description = '';
47
    $view->tag = 'default';
48
    $view->base_table = 'node';
49
    $view->human_name = '';
50
    $view->core = 7;
51
    $view->api_version = '3.0';
52
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
53

    
54
    /* Display: Master */
55
    $handler = $view->new_display('default', 'Master', 'default');
56
    $handler->display->display_options['use_more_always'] = FALSE;
57
    $handler->display->display_options['group_by'] = TRUE;
58
    $handler->display->display_options['access']['type'] = 'none';
59
    $handler->display->display_options['cache']['type'] = 'none';
60
    $handler->display->display_options['query']['type'] = 'views_query';
61
    $handler->display->display_options['exposed_form']['type'] = 'basic';
62
    $handler->display->display_options['pager']['type'] = 'full';
63
    $handler->display->display_options['style_plugin'] = 'default';
64
    $handler->display->display_options['row_plugin'] = 'fields';
65
    /* Field: COUNT(Content revision: Nid) */
66
    $handler->display->display_options['fields']['nid']['id'] = 'nid';
67
    $handler->display->display_options['fields']['nid']['table'] = 'node_revision';
68
    $handler->display->display_options['fields']['nid']['field'] = 'nid';
69
    $handler->display->display_options['fields']['nid']['group_type'] = 'count';
70
    $handler->display->display_options['fields']['nid']['alter']['alter_text'] = 0;
71
    $handler->display->display_options['fields']['nid']['alter']['make_link'] = 0;
72
    $handler->display->display_options['fields']['nid']['alter']['word_boundary'] = 1;
73
    $handler->display->display_options['fields']['nid']['alter']['ellipsis'] = 1;
74
    $handler->display->display_options['fields']['nid']['alter']['strip_tags'] = 0;
75
    $handler->display->display_options['fields']['nid']['alter']['trim'] = 0;
76
    $handler->display->display_options['fields']['nid']['alter']['html'] = 0;
77
    $handler->display->display_options['fields']['nid']['hide_empty'] = 0;
78
    $handler->display->display_options['fields']['nid']['empty_zero'] = 0;
79
    /* Field: Content: Nid */
80
    $handler->display->display_options['fields']['nid_1']['id'] = 'nid_1';
81
    $handler->display->display_options['fields']['nid_1']['table'] = 'node';
82
    $handler->display->display_options['fields']['nid_1']['field'] = 'nid';
83
    $handler->display->display_options['fields']['nid_1']['alter']['alter_text'] = 0;
84
    $handler->display->display_options['fields']['nid_1']['alter']['make_link'] = 0;
85
    $handler->display->display_options['fields']['nid_1']['alter']['word_boundary'] = 1;
86
    $handler->display->display_options['fields']['nid_1']['alter']['ellipsis'] = 1;
87
    $handler->display->display_options['fields']['nid_1']['alter']['strip_tags'] = 0;
88
    $handler->display->display_options['fields']['nid_1']['alter']['trim'] = 0;
89
    $handler->display->display_options['fields']['nid_1']['alter']['html'] = 0;
90
    $handler->display->display_options['fields']['nid_1']['hide_empty'] = 0;
91
    $handler->display->display_options['fields']['nid_1']['empty_zero'] = 0;
92
    /* Contextual filter: Content: Type */
93
    $handler->display->display_options['arguments']['type']['id'] = 'type';
94
    $handler->display->display_options['arguments']['type']['table'] = 'node';
95
    $handler->display->display_options['arguments']['type']['field'] = 'type';
96
    $handler->display->display_options['arguments']['type']['default_action'] = 'summary';
97
    $handler->display->display_options['arguments']['type']['default_argument_type'] = 'fixed';
98
    $handler->display->display_options['arguments']['type']['summary']['format'] = 'default_summary';
99

    
100

    
101
    return $view;
102
  }
103

    
104
  /**
105
   * Tests aggregate count feature.
106
   */
107
  public function testAggregateCount() {
108
    // Create 2 nodes of type1 and 3 nodes of type2
109
    $type1 = $this->drupalCreateContentType();
110
    $type2 = $this->drupalCreateContentType();
111

    
112
    $node_1 = array(
113
      'type' => $type1->type,
114
    );
115
    $this->drupalCreateNode($node_1);
116
    $this->drupalCreateNode($node_1);
117
    $this->drupalCreateNode($node_1);
118
    $this->drupalCreateNode($node_1);
119

    
120
    $node_2 = array(
121
      'type' => $type2->type,
122
    );
123
    $this->drupalCreateNode($node_2);
124
    $this->drupalCreateNode($node_2);
125
    $this->drupalCreateNode($node_2);
126

    
127
    $view = $this->viewsAggregateCountView();
128
    $output = $view->execute_display();
129

    
130
    $this->assertEqual(count($view->result), 2, 'Make sure the count of items is right.');
131

    
132
    $types = array();
133
    foreach ($view->result as $item) {
134
      // 'num_records' is a alias for nid.
135
      $types[$item->node_type] = $item->num_records;
136
    }
137

    
138
    $this->assertEqual($types[$type1->type], 4);
139
    $this->assertEqual($types[$type2->type], 3);
140
  }
141

    
142
  /**
143
   *
144
   */
145
  // public function testAggregateSum() {
146
  // }
147

    
148
  /**
149
   *
150
   */
151
  public function viewsAggregateCountView() {
152
    $view = new view;
153
    $view->name = 'aggregate_count';
154
    $view->description = '';
155
    $view->tag = '';
156
    $view->base_table = 'node';
157
    $view->human_name = '';
158
    $view->core = 7;
159
    $view->api_version = '3.0';
160
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
161

    
162
    /* Display: Master */
163
    $handler = $view->new_display('default', 'Master', 'default');
164
    $handler->display->display_options['group_by'] = TRUE;
165
    $handler->display->display_options['access']['type'] = 'none';
166
    $handler->display->display_options['cache']['type'] = 'none';
167
    $handler->display->display_options['query']['type'] = 'views_query';
168
    $handler->display->display_options['query']['options']['query_comment'] = FALSE;
169
    $handler->display->display_options['exposed_form']['type'] = 'basic';
170
    $handler->display->display_options['pager']['type'] = 'some';
171
    $handler->display->display_options['style_plugin'] = 'default';
172
    $handler->display->display_options['row_plugin'] = 'fields';
173
    /* Field: Content: Title */
174
    $handler->display->display_options['fields']['nid']['id'] = 'nid';
175
    $handler->display->display_options['fields']['nid']['table'] = 'node';
176
    $handler->display->display_options['fields']['nid']['field'] = 'title';
177
    $handler->display->display_options['fields']['nid']['alter']['alter_text'] = 0;
178
    $handler->display->display_options['fields']['nid']['alter']['make_link'] = 0;
179
    $handler->display->display_options['fields']['nid']['alter']['word_boundary'] = 1;
180
    $handler->display->display_options['fields']['nid']['alter']['ellipsis'] = 1;
181
    $handler->display->display_options['fields']['nid']['alter']['strip_tags'] = 0;
182
    $handler->display->display_options['fields']['nid']['alter']['trim'] = 0;
183
    $handler->display->display_options['fields']['nid']['alter']['html'] = 0;
184
    $handler->display->display_options['fields']['nid']['hide_empty'] = 0;
185
    $handler->display->display_options['fields']['nid']['empty_zero'] = 0;
186
    $handler->display->display_options['fields']['nid']['link_to_node'] = 0;
187
    /* Contextual filter: Content: Type */
188
    $handler->display->display_options['arguments']['type']['id'] = 'type';
189
    $handler->display->display_options['arguments']['type']['table'] = 'node';
190
    $handler->display->display_options['arguments']['type']['field'] = 'type';
191
    $handler->display->display_options['arguments']['type']['default_action'] = 'summary';
192
    $handler->display->display_options['arguments']['type']['default_argument_type'] = 'fixed';
193
    $handler->display->display_options['arguments']['type']['summary']['format'] = 'default_summary';
194

    
195
    return $view;
196
  }
197

    
198
  /**
199
   * @param string|null $group_by
200
   *   (optional) Which group_by function should be used, for example sum or
201
   *   count. If omitted, the aggregation is tested with no group function.
202
   * @param array|null $values
203
   *   (optional) Expected values.
204
   */
205
  function GroupByTestHelper($group_by = NULL, $values = NULL) {
206
    // Create 4 nodes of type1 and 3 nodes of type2
207
    $type1 = $this->drupalCreateContentType();
208
    $type2 = $this->drupalCreateContentType();
209

    
210
    $node_1 = array(
211
      'type' => $type1->type,
212
    );
213
    // Nids from 1 to 4.
214
    $this->drupalCreateNode($node_1);
215
    $this->drupalCreateNode($node_1);
216
    $this->drupalCreateNode($node_1);
217
    $this->drupalCreateNode($node_1);
218
    $node_2 = array(
219
      'type' => $type2->type,
220
    );
221
    // Nids from 5 to 7.
222
    $this->drupalCreateNode($node_2);
223
    $this->drupalCreateNode($node_2);
224
    $this->drupalCreateNode($node_2);
225

    
226
    $view = $this->viewsGroupByViewHelper($group_by);
227
    $output = $view->execute_display();
228

    
229
    $this->assertEqual(count($view->result), 2, 'Make sure the count of items is right.');
230

    
231
    $results = array();
232
    // There's no need for a function in order to have aggregation.
233
    if (empty($group_by)) {
234
      $types = array($type1->type, $type2->type);
235
      $results = array_map(function ($item) {
236
        return $item->node_type;
237
      }, $view->result);
238
      sort($types);
239
      sort($results);
240
      $this->assertIdentical($results, $types);
241
      // Exit here with no aggregation function.
242
      return;
243
    }
244

    
245
    // Group by nodetype to identify the right count.
246
    foreach ($view->result as $item) {
247
      $results[$item->node_type] = $item->nid;
248
    }
249
    $this->assertEqual($results[$type1->type], $values[0]);
250
    $this->assertEqual($results[$type2->type], $values[1]);
251
  }
252

    
253
  /**
254
   *
255
   */
256
  function viewsGroupByViewHelper($group_by = NULL) {
257
    $view = new view;
258
    $view->name = 'group_by_count';
259
    $view->description = '';
260
    $view->tag = '';
261
    $view->view_php = '';
262
    $view->base_table = 'node';
263
    $view->is_cacheable = FALSE;
264
    $view->api_version = 2;
265
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
266

    
267
    /* Display: Master */
268
    $handler = $view->new_display('default', 'Master', 'default');
269
    $handler->display->display_options['group_by'] = TRUE;
270
    $handler->display->display_options['access']['type'] = 'none';
271
    $handler->display->display_options['cache']['type'] = 'none';
272
    $handler->display->display_options['exposed_form']['type'] = 'basic';
273
    $handler->display->display_options['pager']['type'] = 'some';
274
    $handler->display->display_options['style_plugin'] = 'default';
275
    $handler->display->display_options['row_plugin'] = 'fields';
276

    
277
    // The test view has 2 fields ('nid' and 'type'). Don't add 'nid' when
278
    // having no aggregation function. We just want to aggregate on node type.
279
    if (!empty($group_by)) {
280
      /* Field: Content: Nid */
281
      $handler->display->display_options['fields']['nid']['id'] = 'nid';
282
      $handler->display->display_options['fields']['nid']['table'] = 'node';
283
      $handler->display->display_options['fields']['nid']['field'] = 'nid';
284
      $handler->display->display_options['fields']['nid']['group_type'] = $group_by;
285
      $handler->display->display_options['fields']['nid']['alter']['alter_text'] = 0;
286
      $handler->display->display_options['fields']['nid']['alter']['make_link'] = 0;
287
      $handler->display->display_options['fields']['nid']['alter']['trim'] = 0;
288
      $handler->display->display_options['fields']['nid']['alter']['word_boundary'] = 1;
289
      $handler->display->display_options['fields']['nid']['alter']['ellipsis'] = 1;
290
      $handler->display->display_options['fields']['nid']['alter']['strip_tags'] = 0;
291
      $handler->display->display_options['fields']['nid']['alter']['html'] = 0;
292
      $handler->display->display_options['fields']['nid']['hide_empty'] = 0;
293
      $handler->display->display_options['fields']['nid']['empty_zero'] = 0;
294
      $handler->display->display_options['fields']['nid']['link_to_node'] = 0;
295
    }
296

    
297
    /* Field: Content: Type */
298
    $handler->display->display_options['fields']['type']['id'] = 'type';
299
    $handler->display->display_options['fields']['type']['table'] = 'node';
300
    $handler->display->display_options['fields']['type']['field'] = 'type';
301
    $handler->display->display_options['fields']['type']['alter']['alter_text'] = 0;
302
    $handler->display->display_options['fields']['type']['alter']['make_link'] = 0;
303
    $handler->display->display_options['fields']['type']['alter']['trim'] = 0;
304
    $handler->display->display_options['fields']['type']['alter']['word_boundary'] = 1;
305
    $handler->display->display_options['fields']['type']['alter']['ellipsis'] = 1;
306
    $handler->display->display_options['fields']['type']['alter']['strip_tags'] = 0;
307
    $handler->display->display_options['fields']['type']['alter']['html'] = 0;
308
    $handler->display->display_options['fields']['type']['hide_empty'] = 0;
309
    $handler->display->display_options['fields']['type']['empty_zero'] = 0;
310
    $handler->display->display_options['fields']['type']['link_to_node'] = 0;
311

    
312
    return $view;
313
  }
314

    
315
  /**
316
   *
317
   */
318
  public function testGroupByCount() {
319
    $this->GroupByTestHelper('count', array(4, 3));
320
  }
321

    
322
  /**
323
   *
324
   */
325
  public function testGroupBySum() {
326
    $this->GroupByTestHelper('sum', array(10, 18));
327
  }
328

    
329
  /**
330
   *
331
   */
332
  public function testGroupByAverage() {
333
    $this->GroupByTestHelper('avg', array(2.5, 6));
334
  }
335

    
336
  /**
337
   *
338
   */
339
  public function testGroupByMin() {
340
    $this->GroupByTestHelper('min', array(1, 5));
341
  }
342

    
343
  /**
344
   * {@inheritdoc}
345
   */
346
  public function testGroupByMax() {
347
    $this->GroupByTestHelper('max', array(4, 7));
348
  }
349

    
350
  /**
351
   *
352
   */
353
  public function testGroupByNone() {
354
    $this->GroupByTestHelper();
355
  }
356

    
357
  /**
358
   *
359
   */
360
  public function testGroupByCountOnlyFilters() {
361
    // Check if GROUP BY and HAVING are included when a view
362
    // Doesn't display SUM, COUNT, MAX... functions in SELECT statment
363

    
364
    $type1 = $this->drupalCreateContentType();
365

    
366
    $node_1 = array(
367
      'type' => $type1->type,
368
    );
369
    for ($x = 0; $x < 10; $x++) {
370
      $this->drupalCreateNode($node_1);
371
    }
372

    
373
    $view = $this->viewsGroupByCountViewOnlyFilters();
374
    $output = $view->execute_display();
375

    
376
    $this->assertTrue(strpos($view->build_info['query'], 'GROUP BY'), t('Make sure that GROUP BY is in the query'));
377
    $this->assertTrue(strpos($view->build_info['query'], 'HAVING'), t('Make sure that HAVING is in the query'));
378
  }
379

    
380
  /**
381
   *
382
   */
383
  function viewsGroupByCountViewOnlyFilters() {
384
    $view = new view;
385
    $view->name = 'group_by_in_filters';
386
    $view->description = '';
387
    $view->tag = '';
388
    $view->view_php = '';
389
    $view->base_table = 'node';
390
    $view->is_cacheable = FALSE;
391
    $view->api_version = 2;
392
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
393

    
394
    /* Display: Master */
395
    $handler = $view->new_display('default', 'Master', 'default');
396
    $handler->display->display_options['group_by'] = TRUE;
397
    $handler->display->display_options['access']['type'] = 'none';
398
    $handler->display->display_options['cache']['type'] = 'none';
399
    $handler->display->display_options['exposed_form']['type'] = 'basic';
400
    $handler->display->display_options['pager']['type'] = 'some';
401
    $handler->display->display_options['style_plugin'] = 'default';
402
    $handler->display->display_options['row_plugin'] = 'fields';
403
    /* Field: Nodo: Tipo */
404
    $handler->display->display_options['fields']['type']['id'] = 'type';
405
    $handler->display->display_options['fields']['type']['table'] = 'node';
406
    $handler->display->display_options['fields']['type']['field'] = 'type';
407
    $handler->display->display_options['fields']['type']['alter']['alter_text'] = 0;
408
    $handler->display->display_options['fields']['type']['alter']['make_link'] = 0;
409
    $handler->display->display_options['fields']['type']['alter']['trim'] = 0;
410
    $handler->display->display_options['fields']['type']['alter']['word_boundary'] = 1;
411
    $handler->display->display_options['fields']['type']['alter']['ellipsis'] = 1;
412
    $handler->display->display_options['fields']['type']['alter']['strip_tags'] = 0;
413
    $handler->display->display_options['fields']['type']['alter']['html'] = 0;
414
    $handler->display->display_options['fields']['type']['hide_empty'] = 0;
415
    $handler->display->display_options['fields']['type']['empty_zero'] = 0;
416
    $handler->display->display_options['fields']['type']['link_to_node'] = 0;
417
    /* Filtrar: Nodo: Nid */
418
    $handler->display->display_options['filters']['nid']['id'] = 'nid';
419
    $handler->display->display_options['filters']['nid']['table'] = 'node';
420
    $handler->display->display_options['filters']['nid']['field'] = 'nid';
421
    $handler->display->display_options['filters']['nid']['group_type'] = 'count';
422
    $handler->display->display_options['filters']['nid']['operator'] = '>';
423
    $handler->display->display_options['filters']['nid']['value']['value'] = '3';
424

    
425
    return $view;
426
  }
427

    
428
}
429

    
430
/**
431
 * Tests UI of aggregate functionality.
432
 */
433
class ViewsUiGroupbyTestCase extends DrupalWebTestCase {
434

    
435
  /**
436
   * {@inheritdoc}
437
   */
438
  public function setUp(array $modules = array()) {
439
    $modules[] = 'views';
440
    $modules[] = 'views_ui';
441
    $modules[] = 'views_test';
442
    parent::setUp($modules);
443

    
444
    // Create and log in a user with administer views permission.
445
    $views_admin = $this->drupalCreateUser(array('administer views', 'administer blocks', 'bypass node access', 'access user profiles', 'view revisions'));
446
    $this->drupalLogin($views_admin);
447
  }
448

    
449
  /**
450
   * Test meta data.
451
   */
452
  public static function getInfo() {
453
    return array(
454
      'name' => 'Groupby UI',
455
      'description' => 'Tests UI of aggregate functionality.',
456
      'group' => 'Views UI',
457
    );
458
  }
459

    
460
  /**
461
   * Tests whether basic saving works.
462
   *
463
   * @todo: this should check the change of the settings as well.
464
   */
465
  function testGroupBySave() {
466
    $this->drupalGet('admin/structure/views/view/test_views_groupby_save/edit');
467

    
468
    $edit = array(
469
      'group_by' => TRUE,
470
    );
471
    $this->drupalPost('admin/structure/views/nojs/display/test_views_groupby_save/default/group_by', $edit, t('Apply'));
472

    
473
    $this->drupalGet('admin/structure/views/view/test_views_groupby_save/edit');
474
    $this->drupalPost('admin/structure/views/view/test_views_groupby_save/edit', array(), t('Save'));
475

    
476
    $this->drupalGet('admin/structure/views/nojs/display/test_views_groupby_save/default/group_by');
477
  }
478

    
479
}