Skip to content

Commit

Permalink
CRM-17123 remove damaging OR from smart group query
Browse files Browse the repository at this point in the history
Note that I added a group refresh to pick up the 'hard-adds'. I think the correct place to deal with this is in fact to alter the GroupContact Add functionality - ie.

IF (CRM_Core_DAO::executeQuery('SELECT id FROM civicrm_group_contact_cache WHERE group_id = 1 LIMIT 1)) {
  // Add the contact just added to the group to the group_contact_cache table.
}

I started that a bit but I think it should be dealt with separately after this is resolved
  • Loading branch information
eileenmcnaughton committed Jun 30, 2016
1 parent c3137c0 commit 485a3a1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
1 change: 0 additions & 1 deletion CRM/Contact/BAO/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,6 @@ public static function create(&$params) {
}

if (!empty($params['organization_id'])) {
$groupOrg = array();
$groupOrg = $params;
$groupOrg['group_id'] = $group->id;
CRM_Contact_BAO_GroupOrganization::add($groupOrg);
Expand Down
15 changes: 9 additions & 6 deletions CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,10 @@ public function buildParamsLookup() {
if (!array_key_exists($value[0], $this->_paramLookup)) {
$this->_paramLookup[$value[0]] = array();
}
$this->_paramLookup[$value[0]][] = $value;
if ($value[0] !== 'group') {
// Just trying to unravel how group interacts here! This whole function is wieid.
$this->_paramLookup[$value[0]][] = $value;
}
}
}

Expand Down Expand Up @@ -2911,7 +2914,6 @@ public function group($values) {

$groupIds = NULL;

$isSmart = FALSE;
$isNotOp = ($op == 'NOT IN' || $op == '!=');

$statii = array();
Expand All @@ -2929,7 +2931,8 @@ public function group($values) {
$statii[] = '"Added"';
}

$skipGroup = FALSE;
$ssClause = $this->addGroupContactCache($value, NULL, "contact_a", $op);
$isSmart = (!$ssClause) ? FALSE : TRUE;
if (!is_array($value) &&
count($statii) == 1 &&
$statii[0] == '"Added"' &&
Expand All @@ -2939,9 +2942,6 @@ public function group($values) {
$isSmart = TRUE;
}
}

$ssClause = $this->addGroupContactCache($value, NULL, "contact_a", $op);
$isSmart = (!$ssClause) ? FALSE : $isSmart;
$groupClause = NULL;

if (!$isSmart) {
Expand Down Expand Up @@ -3038,6 +3038,9 @@ public function addGroupContactCache($groups, $tableAlias = NULL, $joinTable = "
CRM_Contact_BAO_GroupContactCache::load($group);
}
}
if ($group->N == 0) {
return NULL;
}

if (!$tableAlias) {
$tableAlias = "`civicrm_group_contact_cache_";
Expand Down
10 changes: 5 additions & 5 deletions tests/phpunit/CRM/Contact/BAO/GroupContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function testContactSearchByParentGroup() {
'title' => 'Child Group',
'description' => 'Child Group',
'visibility' => 'User and User Admin Only',
'parents' => $parentGroup->id,
'parents' => $parentGroup['id'],
'is_active' => 1,
);
$childGroup = $this->callAPISuccess('Group', 'create', $groupParams2);
Expand All @@ -119,21 +119,21 @@ public function testContactSearchByParentGroup() {
$parentContactParams = array(
'first_name' => 'Parent1 Fname',
'last_name' => 'Parent1 Lname',
'group' => array($parentGroup->id => 1),
'group' => array($parentGroup['id'] => 1),
);
$parentContact = Contact::createIndividual($parentContactParams);

// create a contact within child dgroup
$childContactParams = array(
'first_name' => 'Child1 Fname',
'last_name' => 'Child2 Lname',
'group' => array($childGroup->id => 1),
'group' => array($childGroup['id'] => 1),
);
$childContact = Contact::createIndividual($childContactParams);

// Check if searching by parent group returns both parent and child group contacts
$searchParams = array(
'group' => $parentGroup->id,
'group' => $parentGroup['id'],
);
$result = $this->callAPISuccess('contact', 'get', $searchParams);
$validContactIds = array($parentContact, $childContact);
Expand All @@ -146,7 +146,7 @@ public function testContactSearchByParentGroup() {

// Check if searching by child group returns just child group contacts
$searchParams = array(
'group' => $childGroup->id,
'group' => $childGroup['id'],
);
$result = $this->callAPISuccess('contact', 'get', $searchParams);
$validChildContactIds = array($childContact);
Expand Down
3 changes: 3 additions & 0 deletions tests/phpunit/CRM/Contact/BAO/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ public function testGroupClause() {
$this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupID, 'contact_id' => $individualID, 'status' => 'Added'));
$this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupID, 'contact_id' => $householdID, 'status' => 'Added'));

// Refresh the cache for test purposes. It would be better to alter to alter the GroupContact add function to add contacts to the cache.
CRM_Contact_BAO_GroupContactCache::remove($groupID, FALSE);
$query = new CRM_Contact_BAO_Query(
array(array('group', 'IN', array($groupID), 0, 0)),
array('contact_id')
Expand All @@ -299,6 +301,7 @@ public function testGroupClause() {
$queryString = implode(' ', $sql);
$dao = CRM_Core_DAO::executeQuery($queryString);
$this->assertEquals(3, $dao->N);
$this->assertFalse(strstr(implode(' ', $sql), ' OR '));
}

}

0 comments on commit 485a3a1

Please sign in to comment.