From 6451118caff1da1cbe50d2a20278b83b252c1b7f Mon Sep 17 00:00:00 2001 From: Jitendra Purohit Date: Wed, 20 Jun 2018 17:37:59 +0530 Subject: [PATCH] dev/core#192 - Search builder fails for != smart group filter --- CRM/Contact/BAO/Query.php | 14 ++++- .../CRM/Contact/BAO/GroupContactCacheTest.php | 63 +++++++++++++++++++ 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 0b6fb080bb..3858435b48 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -3042,11 +3042,16 @@ class CRM_Contact_BAO_Query { //CRM-19589: contact(s) removed from a Smart Group, resides in civicrm_group_contact table if (count($smartGroupIDs)) { - $groupClause[] = " ( " . $this->addGroupContactCache($smartGroupIDs, NULL, "contact_a", $op) . " ) "; + $groupContactCacheClause = $this->addGroupContactCache($smartGroupIDs, NULL, "contact_a", $op); + if (!empty($groupContactCacheClause)) { + $groupClause[] = " ( {$groupContactCacheClause} ) "; + } } $and = ($op == 'IS NULL') ? ' AND ' : ' OR '; - $this->_where[$grouping][] = ' ( ' . implode($and, $groupClause) . ' ) '; + if (!empty($groupClause)) { + $this->_where[$grouping][] = ' ( ' . implode($and, $groupClause) . ' ) '; + } list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Contact_DAO_Group', 'id', $value, $op); $this->_qill[$grouping][] = ts("Group(s) %1 %2", array(1 => $qillop, 2 => $qillVal)); @@ -3085,6 +3090,11 @@ class CRM_Contact_BAO_Query { public function addGroupContactCache($groups, $tableAlias = NULL, $joinTable = "contact_a", $op, $joinColumn = 'id') { $isNullOp = (strpos($op, 'NULL') !== FALSE); $groupsIds = $groups; + + $operator = ['=' => 'IN', '!=' => 'NOT IN']; + if (!empty($operator[$op]) && is_array($groups)) { + $op = $operator[$op]; + } if (!$isNullOp && !$groups) { return NULL; } diff --git a/tests/phpunit/CRM/Contact/BAO/GroupContactCacheTest.php b/tests/phpunit/CRM/Contact/BAO/GroupContactCacheTest.php index 40c1d54511..0b4f8ee25c 100644 --- a/tests/phpunit/CRM/Contact/BAO/GroupContactCacheTest.php +++ b/tests/phpunit/CRM/Contact/BAO/GroupContactCacheTest.php @@ -409,4 +409,67 @@ class CRM_Contact_BAO_GroupContactCacheTest extends CiviUnitTestCase { $this->assertTrue(empty($afterGroup['refresh_date']), 'refresh date should not be set as the cache is not built'); } + /** + * Test Smart group search + */ + public function testSmartGroupSearchBuilder() { + $returnProperties = array( + 'contact_type' => 1, + 'contact_sub_type' => 1, + 'sort_name' => 1, + 'group' => 1, + ); + list($group, $living, $deceased) = $this->setupSmartGroup(); + + $params = array( + 'name' => 'Living Contacts', + 'title' => 'Living Contacts', + 'is_active' => 1, + 'formValues' => array('is_deceased' => 0), + ); + $group2 = CRM_Contact_BAO_Group::createSmartGroup($params); + + //Filter on smart group with =, !=, IN and NOT IN operator. + $params = array(array('group', '=', $group2->id, 1, 0)); + $query = new CRM_Contact_BAO_Query( + $params, $returnProperties, + NULL, FALSE, FALSE, CRM_Contact_BAO_Query::MODE_CONTACTS, + FALSE, + FALSE, FALSE + ); + $expectedWhere = "`civicrm_group_contact_cache_{$group2->id}`.group_id IN (\"{$group2->id}\")"; + $this->assertContains($expectedWhere, $query->_whereClause); + + $params = array(array('group', '!=', $group->id, 1, 0)); + $query = new CRM_Contact_BAO_Query( + $params, $returnProperties, + NULL, FALSE, FALSE, CRM_Contact_BAO_Query::MODE_CONTACTS, + FALSE, + FALSE, FALSE + ); + //Assert if proper where clause is present. + $expectedWhere = "`civicrm_group_contact_cache_{$group->id}`.group_id NOT IN (\"{$group->id}\")"; + $this->assertContains($expectedWhere, $query->_whereClause); + + $params = array(array('group', 'IN', array($group->id, $group2->id), 1, 0)); + $query = new CRM_Contact_BAO_Query( + $params, $returnProperties, + NULL, FALSE, FALSE, CRM_Contact_BAO_Query::MODE_CONTACTS, + FALSE, + FALSE, FALSE + ); + $expectedWhere = "`civicrm_group_contact_cache_{$group->id},{$group2->id}`.group_id IN (\"{$group->id}\", \"{$group2->id}\")"; + $this->assertContains($expectedWhere, $query->_whereClause); + + $params = array(array('group', 'NOT IN', array($group->id), 1, 0)); + $query = new CRM_Contact_BAO_Query( + $params, $returnProperties, + NULL, FALSE, FALSE, CRM_Contact_BAO_Query::MODE_CONTACTS, + FALSE, + FALSE, FALSE + ); + $expectedWhere = "`civicrm_group_contact_cache_{$group->id}`.group_id NOT IN (\"{$group->id}\")"; + $this->assertContains($expectedWhere, $query->_whereClause); + } + } -- 2.25.1