Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / tests / views_groupby.test @ 6f57d8c7

1
<?php
2

    
3
/**
4
 * @file
5
 * Tests aggregate functionality of Views.
6
 */
7

    
8
/**
9
 * Tests aggregate functionality of views, for example count.
10
 */
11
class ViewsQueryGroupByTest extends ViewsSqlTest {
12
  public static function getInfo() {
13
    return array(
14
      'name' => 'Groupby',
15
      'description' => 'Tests aggregate functionality of views, for example count.',
16
      'group' => 'Views',
17
    );
18

    
19
  }
20

    
21
  /**
22
   * Tests aggregate count feature.
23
   */
24
  public function testAggregateCount() {
25
    // Create 2 nodes of type1 and 3 nodes of type2
26
    $type1 = $this->drupalCreateContentType();
27
    $type2 = $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
    $node_2 = array(
38
      'type' => $type2->type,
39
    );
40
    $this->drupalCreateNode($node_2);
41
    $this->drupalCreateNode($node_2);
42
    $this->drupalCreateNode($node_2);
43

    
44
    $view = $this->viewsAggregateCountView();
45
    $output = $view->execute_display();
46

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

    
49
    $types = array();
50
    foreach ($view->result as $item) {
51
      // num_records is a alias for nid.
52
      $types[$item->node_type] = $item->num_records;
53
    }
54

    
55
    $this->assertEqual($types[$type1->type], 4);
56
    $this->assertEqual($types[$type2->type], 3);
57
  }
58

    
59
  //public function testAggregateSum() {
60
  //}
61

    
62
  public function viewsAggregateCountView() {
63
    $view = new view;
64
    $view->name = 'aggregate_count';
65
    $view->description = '';
66
    $view->tag = '';
67
    $view->base_table = 'node';
68
    $view->human_name = '';
69
    $view->core = 7;
70
    $view->api_version = '3.0';
71
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
72

    
73
    /* Display: Master */
74
    $handler = $view->new_display('default', 'Master', 'default');
75
    $handler->display->display_options['group_by'] = TRUE;
76
    $handler->display->display_options['access']['type'] = 'none';
77
    $handler->display->display_options['cache']['type'] = 'none';
78
    $handler->display->display_options['query']['type'] = 'views_query';
79
    $handler->display->display_options['query']['options']['query_comment'] = FALSE;
80
    $handler->display->display_options['exposed_form']['type'] = 'basic';
81
    $handler->display->display_options['pager']['type'] = 'some';
82
    $handler->display->display_options['style_plugin'] = 'default';
83
    $handler->display->display_options['row_plugin'] = 'fields';
84
    /* Field: Content: Title */
85
    $handler->display->display_options['fields']['nid']['id'] = 'nid';
86
    $handler->display->display_options['fields']['nid']['table'] = 'node';
87
    $handler->display->display_options['fields']['nid']['field'] = 'title';
88
    $handler->display->display_options['fields']['nid']['alter']['alter_text'] = 0;
89
    $handler->display->display_options['fields']['nid']['alter']['make_link'] = 0;
90
    $handler->display->display_options['fields']['nid']['alter']['word_boundary'] = 1;
91
    $handler->display->display_options['fields']['nid']['alter']['ellipsis'] = 1;
92
    $handler->display->display_options['fields']['nid']['alter']['strip_tags'] = 0;
93
    $handler->display->display_options['fields']['nid']['alter']['trim'] = 0;
94
    $handler->display->display_options['fields']['nid']['alter']['html'] = 0;
95
    $handler->display->display_options['fields']['nid']['hide_empty'] = 0;
96
    $handler->display->display_options['fields']['nid']['empty_zero'] = 0;
97
    $handler->display->display_options['fields']['nid']['link_to_node'] = 0;
98
    /* Contextual filter: Content: Type */
99
    $handler->display->display_options['arguments']['type']['id'] = 'type';
100
    $handler->display->display_options['arguments']['type']['table'] = 'node';
101
    $handler->display->display_options['arguments']['type']['field'] = 'type';
102
    $handler->display->display_options['arguments']['type']['default_action'] = 'summary';
103
    $handler->display->display_options['arguments']['type']['default_argument_type'] = 'fixed';
104
    $handler->display->display_options['arguments']['type']['summary']['format'] = 'default_summary';
105

    
106

    
107
    return $view;
108
  }
109

    
110
  /**
111
   * @param string|null $group_by
112
   *   (optional) Which group_by function should be used, for example sum or
113
   *   count. If omitted, the aggregation is tested with no group function.
114
   * @param array|null $values
115
   *   (optional) Expected values.
116
   */
117
  function GroupByTestHelper($group_by = NULL, $values = NULL) {
118
    // Create 4 nodes of type1 and 3 nodes of type2
119
    $type1 = $this->drupalCreateContentType();
120
    $type2 = $this->drupalCreateContentType();
121

    
122
    $node_1 = array(
123
      'type' => $type1->type,
124
    );
125
    // Nids from 1 to 4.
126
    $this->drupalCreateNode($node_1);
127
    $this->drupalCreateNode($node_1);
128
    $this->drupalCreateNode($node_1);
129
    $this->drupalCreateNode($node_1);
130
    $node_2 = array(
131
      'type' => $type2->type,
132
    );
133
    // Nids from 5 to 7.
134
    $this->drupalCreateNode($node_2);
135
    $this->drupalCreateNode($node_2);
136
    $this->drupalCreateNode($node_2);
137

    
138
    $view = $this->viewsGroupByViewHelper($group_by);
139
    $output = $view->execute_display();
140

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

    
143
    $results = array();
144
    // There's no need for a function in order to have aggregation.
145
    if (empty($group_by)) {
146
      $types = array($type1->type, $type2->type);
147
      $results = array_map(function ($item) { return $item->node_type; }, $view->result);
148
      sort($types);
149
      sort($results);
150
      $this->assertIdentical($results, $types);
151
      // Exit here with no aggregation function.
152
      return;
153
    }
154

    
155
    // Group by nodetype to identify the right count.
156
    foreach ($view->result as $item) {
157
      $results[$item->node_type] = $item->nid;
158
    }
159
    $this->assertEqual($results[$type1->type], $values[0]);
160
    $this->assertEqual($results[$type2->type], $values[1]);
161
  }
162

    
163
  function viewsGroupByViewHelper($group_by = NULL) {
164
    $view = new view;
165
    $view->name = 'group_by_count';
166
    $view->description = '';
167
    $view->tag = '';
168
    $view->view_php = '';
169
    $view->base_table = 'node';
170
    $view->is_cacheable = FALSE;
171
    $view->api_version = 2;
172
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
173

    
174
    /* Display: Master */
175
    $handler = $view->new_display('default', 'Master', 'default');
176
    $handler->display->display_options['group_by'] = TRUE;
177
    $handler->display->display_options['access']['type'] = 'none';
178
    $handler->display->display_options['cache']['type'] = 'none';
179
    $handler->display->display_options['exposed_form']['type'] = 'basic';
180
    $handler->display->display_options['pager']['type'] = 'some';
181
    $handler->display->display_options['style_plugin'] = 'default';
182
    $handler->display->display_options['row_plugin'] = 'fields';
183

    
184
    // The test view has 2 fields ('nid' and 'type'). Don't add 'nid' when
185
    // having no aggregation function. We just want to aggregate on node type.
186
    if (!empty($group_by)) {
187
      /* Field: Content: Nid */
188
      $handler->display->display_options['fields']['nid']['id'] = 'nid';
189
      $handler->display->display_options['fields']['nid']['table'] = 'node';
190
      $handler->display->display_options['fields']['nid']['field'] = 'nid';
191
      $handler->display->display_options['fields']['nid']['group_type'] = $group_by;
192
      $handler->display->display_options['fields']['nid']['alter']['alter_text'] = 0;
193
      $handler->display->display_options['fields']['nid']['alter']['make_link'] = 0;
194
      $handler->display->display_options['fields']['nid']['alter']['trim'] = 0;
195
      $handler->display->display_options['fields']['nid']['alter']['word_boundary'] = 1;
196
      $handler->display->display_options['fields']['nid']['alter']['ellipsis'] = 1;
197
      $handler->display->display_options['fields']['nid']['alter']['strip_tags'] = 0;
198
      $handler->display->display_options['fields']['nid']['alter']['html'] = 0;
199
      $handler->display->display_options['fields']['nid']['hide_empty'] = 0;
200
      $handler->display->display_options['fields']['nid']['empty_zero'] = 0;
201
      $handler->display->display_options['fields']['nid']['link_to_node'] = 0;
202
    }
203

    
204
    /* Field: Content: Type */
205
    $handler->display->display_options['fields']['type']['id'] = 'type';
206
    $handler->display->display_options['fields']['type']['table'] = 'node';
207
    $handler->display->display_options['fields']['type']['field'] = 'type';
208
    $handler->display->display_options['fields']['type']['alter']['alter_text'] = 0;
209
    $handler->display->display_options['fields']['type']['alter']['make_link'] = 0;
210
    $handler->display->display_options['fields']['type']['alter']['trim'] = 0;
211
    $handler->display->display_options['fields']['type']['alter']['word_boundary'] = 1;
212
    $handler->display->display_options['fields']['type']['alter']['ellipsis'] = 1;
213
    $handler->display->display_options['fields']['type']['alter']['strip_tags'] = 0;
214
    $handler->display->display_options['fields']['type']['alter']['html'] = 0;
215
    $handler->display->display_options['fields']['type']['hide_empty'] = 0;
216
    $handler->display->display_options['fields']['type']['empty_zero'] = 0;
217
    $handler->display->display_options['fields']['type']['link_to_node'] = 0;
218

    
219
    return $view;
220
  }
221

    
222
  public function testGroupByCount() {
223
    $this->GroupByTestHelper('count', array(4, 3));
224
  }
225

    
226
  function testGroupBySum() {
227
    $this->GroupByTestHelper('sum', array(10, 18));
228
  }
229

    
230

    
231
  function testGroupByAverage() {
232
    $this->GroupByTestHelper('avg', array(2.5, 6));
233
  }
234

    
235
  function testGroupByMin() {
236
    $this->GroupByTestHelper('min', array(1, 5));
237
  }
238

    
239
  function testGroupByMax() {
240
    $this->GroupByTestHelper('max', array(4, 7));
241
  }
242

    
243
  function testGroupByNone() {
244
    $this->GroupByTestHelper();
245
  }
246

    
247
  public function testGroupByCountOnlyFilters() {
248
    // Check if GROUP BY and HAVING are included when a view
249
    // Doesn't display SUM, COUNT, MAX... functions in SELECT statment
250

    
251
    $type1 = $this->drupalCreateContentType();
252

    
253
    $node_1 = array(
254
      'type' => $type1->type,
255
    );
256
    for ($x = 0; $x < 10; $x++) {
257
      $this->drupalCreateNode($node_1);
258
    }
259

    
260
    $view = $this->viewsGroupByCountViewOnlyFilters();
261
    $output = $view->execute_display();
262

    
263
    $this->assertTrue(strpos($view->build_info['query'], 'GROUP BY'), t('Make sure that GROUP BY is in the query'));
264
    $this->assertTrue(strpos($view->build_info['query'], 'HAVING'), t('Make sure that HAVING is in the query'));
265
  }
266

    
267
  function viewsGroupByCountViewOnlyFilters() {
268
    $view = new view;
269
    $view->name = 'group_by_in_filters';
270
    $view->description = '';
271
    $view->tag = '';
272
    $view->view_php = '';
273
    $view->base_table = 'node';
274
    $view->is_cacheable = FALSE;
275
    $view->api_version = 2;
276
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
277

    
278
    /* Display: Master */
279
    $handler = $view->new_display('default', 'Master', 'default');
280
    $handler->display->display_options['group_by'] = TRUE;
281
    $handler->display->display_options['access']['type'] = 'none';
282
    $handler->display->display_options['cache']['type'] = 'none';
283
    $handler->display->display_options['exposed_form']['type'] = 'basic';
284
    $handler->display->display_options['pager']['type'] = 'some';
285
    $handler->display->display_options['style_plugin'] = 'default';
286
    $handler->display->display_options['row_plugin'] = 'fields';
287
    /* Field: Nodo: Tipo */
288
    $handler->display->display_options['fields']['type']['id'] = 'type';
289
    $handler->display->display_options['fields']['type']['table'] = 'node';
290
    $handler->display->display_options['fields']['type']['field'] = 'type';
291
    $handler->display->display_options['fields']['type']['alter']['alter_text'] = 0;
292
    $handler->display->display_options['fields']['type']['alter']['make_link'] = 0;
293
    $handler->display->display_options['fields']['type']['alter']['trim'] = 0;
294
    $handler->display->display_options['fields']['type']['alter']['word_boundary'] = 1;
295
    $handler->display->display_options['fields']['type']['alter']['ellipsis'] = 1;
296
    $handler->display->display_options['fields']['type']['alter']['strip_tags'] = 0;
297
    $handler->display->display_options['fields']['type']['alter']['html'] = 0;
298
    $handler->display->display_options['fields']['type']['hide_empty'] = 0;
299
    $handler->display->display_options['fields']['type']['empty_zero'] = 0;
300
    $handler->display->display_options['fields']['type']['link_to_node'] = 0;
301
    /* Filtrar: Nodo: Nid */
302
    $handler->display->display_options['filters']['nid']['id'] = 'nid';
303
    $handler->display->display_options['filters']['nid']['table'] = 'node';
304
    $handler->display->display_options['filters']['nid']['field'] = 'nid';
305
    $handler->display->display_options['filters']['nid']['group_type'] = 'count';
306
    $handler->display->display_options['filters']['nid']['operator'] = '>';
307
    $handler->display->display_options['filters']['nid']['value']['value'] = '3';
308

    
309
    return $view;
310
  }
311
}
312

    
313
/**
314
 * Tests UI of aggregate functionality..
315
 */
316
class viewsUiGroupbyTestCase extends DrupalWebTestCase {
317
  function setUp() {
318
    // Enable views_ui.
319
    parent::setUp('views_ui', 'views_test');
320

    
321
    // Create and log in a user with administer views permission.
322
    $views_admin = $this->drupalCreateUser(array('administer views', 'administer blocks', 'bypass node access', 'access user profiles', 'view revisions'));
323
    $this->drupalLogin($views_admin);
324
  }
325

    
326
  public static function getInfo() {
327
    return array(
328
      'name' => 'Groupby UI',
329
      'description' => 'Tests UI of aggregate functionality.',
330
      'group' => 'Views UI',
331
    );
332
  }
333

    
334
  /**
335
   * Tests whether basic saving works.
336
   *
337
   * @todo: this should check the change of the settings as well.
338
   */
339
  function testGroupBySave() {
340
    $this->drupalGet('admin/structure/views/view/test_views_groupby_save/edit');
341

    
342
    $edit = array(
343
      'group_by' => TRUE,
344
    );
345
    $this->drupalPost('admin/structure/views/nojs/display/test_views_groupby_save/default/group_by', $edit, t('Apply'));
346

    
347
    $this->drupalGet('admin/structure/views/view/test_views_groupby_save/edit');
348
    $this->drupalPost('admin/structure/views/view/test_views_groupby_save/edit', array(), t('Save'));
349

    
350
    $this->drupalGet('admin/structure/views/nojs/display/test_views_groupby_save/default/group_by');
351
  }
352
}