diff --git a/plugins/views_plugin_query_default.inc b/plugins/views_plugin_query_default.inc index 42268ce..a0c0788 100644 --- a/plugins/views_plugin_query_default.inc +++ b/plugins/views_plugin_query_default.inc @@ -1328,9 +1328,6 @@ class views_plugin_query_default extends views_plugin_query { if ($this->has_aggregate && (!empty($this->groupby) || !empty($non_aggregates))) { $groupby = array_unique(array_merge($this->groupby, $non_aggregates)); foreach ($groupby as $field) { - if ($fields_array[$field]['table'] == NULL && $this->count_field['field'] != $fields_array[$field]['field']) { - continue; - } $query->groupBy($field); } if (!empty($this->having) && $condition = $this->build_condition('having')) { diff --git a/tests/views_basic.test b/tests/views_basic.test index 50abf2e..5fc60d8 100644 --- a/tests/views_basic.test +++ b/tests/views_basic.test @@ -175,43 +175,4 @@ class ViewsBasicTest extends ViewsSqlTest { 'views_test_age' => 'age', )); } - - /** - * Test Aggregate. - * - * https://www.drupal.org/node/2159347 - */ - public function testAggregate() { - // Get the testing view. - $view = $this->getBasicView(); - - // Turn the view into an aggregate. - $view->display['default']->handler->override_option('group_by', 1); - - // Remove all fields but 'job'. - $view->display['default']->handler->override_option('fields', array( - 'job' => array( - 'id' => 'job', - 'table' => 'views_test', - 'field' => 'job', - 'relationship' => 'none', - ))); - - // Remove all sorts, this also become part of the aggreate - // if included. - $view->display['default']->handler->override_option('sorts', array()); - - // Execute the view. - $this->executeView($view); - - // Manually count the results using distinct. - $distinct_count = db_query('SELECT COUNT(DISTINCT job) as CT FROM {views_test}')->fetchObject(); - - // The views_test table has 5 records, with 2 duplicate jobs. - // So we can expect 4 results for the aggregate. - - // Verify the result. - $this->assertEqual($distinct_count->CT, count($view->result), t('The number of returned rows match.')); - - } } diff --git a/tests/views_groupby.test b/tests/views_groupby.test index 718d8dc..485e432 100644 --- a/tests/views_groupby.test +++ b/tests/views_groupby.test @@ -108,11 +108,13 @@ class ViewsQueryGroupByTest extends ViewsSqlTest { } /** - * @param $group_by + * @param string|null $group_by * Which group_by function should be used, for example sum or count. + * @param array|null $values + * Expected values. */ - function GroupByTestHelper($group_by, $values) { - // Create 2 nodes of type1 and 3 nodes of type2 + function GroupByTestHelper($group_by = NULL, $values = NULL) { + // Create 4 nodes of type1 and 3 nodes of type2 $type1 = $this->drupalCreateContentType(); $type2 = $this->drupalCreateContentType(); @@ -136,6 +138,19 @@ class ViewsQueryGroupByTest extends ViewsSqlTest { $output = $view->execute_display(); $this->assertEqual(count($view->result), 2, 'Make sure the count of items is right.'); + + $results = array(); + // There's no need for a function in order to have aggregation. + if (empty($group_by)) { + $types = array($type1->type, $type2->type); + $results = array_map(function ($item) { return $item->node_type; }, $view->result); + sort($types); + sort($results); + $this->assertIdentical($results, $types); + // Exit here with no aggregation function. + return; + } + // Group by nodetype to identify the right count. foreach ($view->result as $item) { $results[$item->node_type] = $item->nid; @@ -144,7 +159,7 @@ class ViewsQueryGroupByTest extends ViewsSqlTest { $this->assertEqual($results[$type2->type], $values[1]); } - function viewsGroupByViewHelper($group_by) { + function viewsGroupByViewHelper($group_by = NULL) { $view = new view; $view->name = 'group_by_count'; $view->description = ''; @@ -164,21 +179,27 @@ class ViewsQueryGroupByTest extends ViewsSqlTest { $handler->display->display_options['pager']['type'] = 'some'; $handler->display->display_options['style_plugin'] = 'default'; $handler->display->display_options['row_plugin'] = 'fields'; - /* Field: Content: Nid */ - $handler->display->display_options['fields']['nid']['id'] = 'nid'; - $handler->display->display_options['fields']['nid']['table'] = 'node'; - $handler->display->display_options['fields']['nid']['field'] = 'nid'; - $handler->display->display_options['fields']['nid']['group_type'] = $group_by; - $handler->display->display_options['fields']['nid']['alter']['alter_text'] = 0; - $handler->display->display_options['fields']['nid']['alter']['make_link'] = 0; - $handler->display->display_options['fields']['nid']['alter']['trim'] = 0; - $handler->display->display_options['fields']['nid']['alter']['word_boundary'] = 1; - $handler->display->display_options['fields']['nid']['alter']['ellipsis'] = 1; - $handler->display->display_options['fields']['nid']['alter']['strip_tags'] = 0; - $handler->display->display_options['fields']['nid']['alter']['html'] = 0; - $handler->display->display_options['fields']['nid']['hide_empty'] = 0; - $handler->display->display_options['fields']['nid']['empty_zero'] = 0; - $handler->display->display_options['fields']['nid']['link_to_node'] = 0; + + // The test view has 2 fields ('nid' and 'type'). Don't add 'nid' when + // having no aggregation function. We just want to aggregate on node type. + if (!empty($group_by)) { + /* Field: Content: Nid */ + $handler->display->display_options['fields']['nid']['id'] = 'nid'; + $handler->display->display_options['fields']['nid']['table'] = 'node'; + $handler->display->display_options['fields']['nid']['field'] = 'nid'; + $handler->display->display_options['fields']['nid']['group_type'] = $group_by; + $handler->display->display_options['fields']['nid']['alter']['alter_text'] = 0; + $handler->display->display_options['fields']['nid']['alter']['make_link'] = 0; + $handler->display->display_options['fields']['nid']['alter']['trim'] = 0; + $handler->display->display_options['fields']['nid']['alter']['word_boundary'] = 1; + $handler->display->display_options['fields']['nid']['alter']['ellipsis'] = 1; + $handler->display->display_options['fields']['nid']['alter']['strip_tags'] = 0; + $handler->display->display_options['fields']['nid']['alter']['html'] = 0; + $handler->display->display_options['fields']['nid']['hide_empty'] = 0; + $handler->display->display_options['fields']['nid']['empty_zero'] = 0; + $handler->display->display_options['fields']['nid']['link_to_node'] = 0; + } + /* Field: Content: Type */ $handler->display->display_options['fields']['type']['id'] = 'type'; $handler->display->display_options['fields']['type']['table'] = 'node'; @@ -218,6 +239,10 @@ class ViewsQueryGroupByTest extends ViewsSqlTest { $this->GroupByTestHelper('max', array(4, 7)); } + function testGroupByNone() { + $this->GroupByTestHelper(); + } + public function testGroupByCountOnlyFilters() { // Check if GROUP BY and HAVING are included when a view // Doesn't display SUM, COUNT, MAX... functions in SELECT statment