From 1eb122344a6b383741265228cd2a286dc94ee8de Mon Sep 17 00:00:00 2001 From: Angie Byron <webchick@24967.no-reply.drupal.org> Date: Wed, 22 Dec 2010 08:13:58 +0000 Subject: [PATCH] #1003860 by dmitrig01, chx: Fixed Count query fails to remove fields and expressions. --- includes/database/select.inc | 6 +++--- modules/simpletest/tests/database_test.test | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/includes/database/select.inc b/includes/database/select.inc index b0d0eb275ec1..16091061ba39 100644 --- a/includes/database/select.inc +++ b/includes/database/select.inc @@ -1370,14 +1370,14 @@ public function countQuery() { // the GROUP BY clause need to be present in the query. $fields =& $count->getFields(); foreach (array_keys($fields) as $field) { - if (!empty($group_by[$field])) { + if (empty($group_by[$field])) { unset($fields[$field]); } } $expressions =& $count->getExpressions(); foreach (array_keys($expressions) as $field) { - if (!empty($group_by[$field])) { - unset($fields[$field]); + if (empty($group_by[$field])) { + unset($expressions[$field]); } } diff --git a/modules/simpletest/tests/database_test.test b/modules/simpletest/tests/database_test.test index 65d84c18075f..db74bd9e5e05 100644 --- a/modules/simpletest/tests/database_test.test +++ b/modules/simpletest/tests/database_test.test @@ -1989,6 +1989,25 @@ class DatabaseSelectComplexTestCase extends DatabaseTestCase { $this->assertEqual($count, 4, t('Counted the correct number of records.')); } + + /** + * Test that countQuery properly removes fields and expressions. + */ + function testCountQueryFieldRemovals() { + // countQuery should remove all fields and expressions, so this can be + // tested by adding a non-existant field and expression: if it ends + // up in the query, an error will be thrown. If not, it will return the + // number of records, which in this case happens to be 4 (there are four + // records in the {test} table). + $query = db_select('test'); + $query->fields('test', array('fail')); + $this->assertEqual(4, $query->countQuery()->execute()->fetchField(), t('Count Query removed fields')); + + $query = db_select('test'); + $query->addExpression('fail'); + $this->assertEqual(4, $query->countQuery()->execute()->fetchField(), t('Count Query removed expressions')); + } + /** * Test that we can generate a count query from a query with distinct. */ -- GitLab