From bc8ed7fec14b66c1d56e7b7e7ede30e807eee6d4 Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Tue, 1 Nov 2022 16:47:38 +0000 Subject: [PATCH] Issue #3247121 by ramil g, joelpittet: Aggregation of nid when using COUNT produces syntax error --- .../views/src/Plugin/views/query/Sql.php | 2 +- ...ews.view.test_aggregate_count_function.yml | 55 +++++++++++++++++++ .../tests/src/Kernel/QueryGroupByTest.php | 22 +++++++- 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 core/modules/views/tests/modules/views_test_config/test_views/views.view.test_aggregate_count_function.yml diff --git a/core/modules/views/src/Plugin/views/query/Sql.php b/core/modules/views/src/Plugin/views/query/Sql.php index ae00eac45465..035df8b59380 100644 --- a/core/modules/views/src/Plugin/views/query/Sql.php +++ b/core/modules/views/src/Plugin/views/query/Sql.php @@ -1357,7 +1357,7 @@ public function query($get_count = FALSE) { $entity_information = $this->getEntityTableInfo(); if ($entity_information) { $params = []; - if ($groupby) { + if ($this->hasAggregate) { // Handle grouping, by retrieving the minimum entity_id. $params = [ 'function' => 'min', diff --git a/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_aggregate_count_function.yml b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_aggregate_count_function.yml new file mode 100644 index 000000000000..1bd8840dfd6d --- /dev/null +++ b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_aggregate_count_function.yml @@ -0,0 +1,55 @@ +langcode: en +status: true +dependencies: { } +id: test_aggregate_count_function +label: '' +module: views +description: '' +tag: '' +base_table: entity_test +base_field: id +display: + default: + display_options: + access: + type: none + arguments: { } + cache: + type: tag + exposed_form: + type: basic + fields: + id: + alter: + alter_text: false + ellipsis: true + html: false + make_link: false + strip_tags: false + trim: false + word_boundary: true + empty_zero: false + field: id + group_type: count + hide_empty: false + id: id + table: entity_test + plugin_id: field + type: number_integer + entity_type: entity_test + entity_field: id + group_by: true + pager: + type: some + query: + options: + query_comment: '' + type: views_query + style: + type: default + row: + type: fields + display_plugin: default + display_title: Default + id: default + position: 0 diff --git a/core/modules/views/tests/src/Kernel/QueryGroupByTest.php b/core/modules/views/tests/src/Kernel/QueryGroupByTest.php index 62be526db265..171688321e4e 100644 --- a/core/modules/views/tests/src/Kernel/QueryGroupByTest.php +++ b/core/modules/views/tests/src/Kernel/QueryGroupByTest.php @@ -21,7 +21,14 @@ class QueryGroupByTest extends ViewsKernelTestBase { * * @var array */ - public static $testViews = ['test_group_by_in_filters', 'test_aggregate_count', 'test_group_by_count', 'test_group_by_count_multicardinality', 'test_group_by_field_not_within_bundle']; + public static $testViews = [ + 'test_group_by_in_filters', + 'test_aggregate_count', + 'test_aggregate_count_function', + 'test_group_by_count', + 'test_group_by_count_multicardinality', + 'test_group_by_field_not_within_bundle', + ]; /** * Modules to enable. @@ -79,6 +86,19 @@ public function testAggregateCount() { $this->assertEquals(3, $types['name2']); } + /** + * Tests aggregate count feature with no group by. + */ + public function testAggregateCountFunction() { + $this->setupTestEntities(); + + $view = Views::getView('test_aggregate_count_function'); + $this->executeView($view); + + $this->assertEquals(7, $view->result[0]->id); + $this->assertCount(1, $view->result, 'Make sure the count of rows is one.'); + } + /** * Provides a test helper which runs a view with some aggregation function. * -- GitLab