From 82ae55f4ffa9fc2d9e3cd49b27be891106f7c4fc Mon Sep 17 00:00:00 2001 From: eileenmcnaugton Date: Mon, 21 Sep 2015 16:43:26 +1200 Subject: [PATCH] CRM-17254 retrieving groups: don't append extra params when only contact_id is requested in returnProperties this should be respected --- CRM/Contact/BAO/Query.php | 15 +++++++++++---- tests/phpunit/CRM/Contact/BAO/QueryTest.php | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index cd69b39b1e..77a48ea607 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -1347,10 +1347,17 @@ class CRM_Contact_BAO_Query { if (!isset($group->saved_search_id)) { $tbName = "`civicrm_group_contact-{$groupId}`"; - $this->_select['group_contact_id'] = "$tbName.id as group_contact_id"; - $this->_element['group_contact_id'] = 1; - $this->_select['status'] = "$tbName.status as status"; - $this->_element['status'] = 1; + // CRM-17254 don't retrieve extra fields if contact_id is specifically requested + // as this will add load to an intentionally light query. + // ideally this code would be removed as it appears to be to support CRM-1203 + // and passing in the required returnProperties from the url would + // make more sense that globally applying the requirements of one form. + if (($this->_returnProperties != array('contact_id'))) { + $this->_select['group_contact_id'] = "$tbName.id as group_contact_id"; + $this->_element['group_contact_id'] = 1; + $this->_select['status'] = "$tbName.status as status"; + $this->_element['status'] = 1; + } } } $this->_useGroupBy = TRUE; diff --git a/tests/phpunit/CRM/Contact/BAO/QueryTest.php b/tests/phpunit/CRM/Contact/BAO/QueryTest.php index 53303312e7..5aeb61c656 100644 --- a/tests/phpunit/CRM/Contact/BAO/QueryTest.php +++ b/tests/phpunit/CRM/Contact/BAO/QueryTest.php @@ -191,4 +191,25 @@ class CRM_Contact_BAO_QueryTest extends CiviUnitTestCase { } } + /** + * Test set up to test calling the query object per GroupContactCache BAO usage. + * + * CRM-17254 ensure that if only the contact_id is required other fields should + * not be appended. + */ + public function testGroupContactCacheAddSearch() { + $returnProperties = array('contact_id'); + $params = array(array('group', 'IN', array(1 => 1), 0, 0)); + + $query = new CRM_Contact_BAO_Query( + $params, $returnProperties, + NULL, TRUE, FALSE, 1, + TRUE, + TRUE, FALSE + ); + + list($select) = $query->query(FALSE); + $this->assertEquals('SELECT contact_a.id as contact_id', $select); + } + } -- 2.25.1