Skip to content

Commit

Permalink
dev/core#192 - Search builder fails for != smart group filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Jitendra Purohit committed Jun 20, 2018
1 parent 9f06cdc commit efadb31
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
14 changes: 12 additions & 2 deletions CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -3049,11 +3049,16 @@ public function group($values) {

//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));
Expand Down Expand Up @@ -3092,6 +3097,11 @@ public function getGroupsFromTypeCriteria($value) {
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;
}
Expand Down
63 changes: 63 additions & 0 deletions tests/phpunit/CRM/Contact/BAO/GroupContactCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,67 @@ protected function assertCacheRefreshed($group) {
$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);
}

}

0 comments on commit efadb31

Please sign in to comment.