Skip to content

Commit b49e891

Browse files
CRM-17123 remove damaging OR from smart group query
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
1 parent c3137c0 commit b49e891

File tree

5 files changed

+22
-17
lines changed

5 files changed

+22
-17
lines changed

CRM/Contact/BAO/Group.php

-1
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,6 @@ public static function create(&$params) {
460460
}
461461

462462
if (!empty($params['organization_id'])) {
463-
$groupOrg = array();
464463
$groupOrg = $params;
465464
$groupOrg['group_id'] = $group->id;
466465
CRM_Contact_BAO_GroupOrganization::add($groupOrg);

CRM/Contact/BAO/Query.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,10 @@ public function buildParamsLookup() {
586586
if (!array_key_exists($value[0], $this->_paramLookup)) {
587587
$this->_paramLookup[$value[0]] = array();
588588
}
589-
$this->_paramLookup[$value[0]][] = $value;
589+
if ($value[0] !== 'group') {
590+
// Just trying to unravel how group interacts here! This whole function is wieid.
591+
$this->_paramLookup[$value[0]][] = $value;
592+
}
590593
}
591594
}
592595

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

29122915
$groupIds = NULL;
29132916

2914-
$isSmart = FALSE;
29152917
$isNotOp = ($op == 'NOT IN' || $op == '!=');
29162918

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

2932-
$skipGroup = FALSE;
2934+
$ssClause = $this->addGroupContactCache($value, NULL, "contact_a", $op);
2935+
$isSmart = (!$ssClause) ? FALSE : TRUE;
29332936
if (!is_array($value) &&
29342937
count($statii) == 1 &&
29352938
$statii[0] == '"Added"' &&
@@ -2939,9 +2942,6 @@ public function group($values) {
29392942
$isSmart = TRUE;
29402943
}
29412944
}
2942-
2943-
$ssClause = $this->addGroupContactCache($value, NULL, "contact_a", $op);
2944-
$isSmart = (!$ssClause) ? FALSE : $isSmart;
29452945
$groupClause = NULL;
29462946

29472947
if (!$isSmart) {
@@ -3038,6 +3038,9 @@ public function addGroupContactCache($groups, $tableAlias = NULL, $joinTable = "
30383038
CRM_Contact_BAO_GroupContactCache::load($group);
30393039
}
30403040
}
3041+
if ($group->N == 0) {
3042+
return NULL;
3043+
}
30413044

30423045
if (!$tableAlias) {
30433046
$tableAlias = "`civicrm_group_contact_cache_";

tests/phpunit/CRM/Contact/BAO/GroupContactTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function testContactSearchByParentGroup() {
110110
'title' => 'Child Group',
111111
'description' => 'Child Group',
112112
'visibility' => 'User and User Admin Only',
113-
'parents' => $parentGroup->id,
113+
'parents' => $parentGroup['id'],
114114
'is_active' => 1,
115115
);
116116
$childGroup = $this->callAPISuccess('Group', 'create', $groupParams2);
@@ -119,21 +119,21 @@ public function testContactSearchByParentGroup() {
119119
$parentContactParams = array(
120120
'first_name' => 'Parent1 Fname',
121121
'last_name' => 'Parent1 Lname',
122-
'group' => array($parentGroup->id => 1),
122+
'group' => array($parentGroup['id'] => 1),
123123
);
124124
$parentContact = Contact::createIndividual($parentContactParams);
125125

126126
// create a contact within child dgroup
127127
$childContactParams = array(
128128
'first_name' => 'Child1 Fname',
129129
'last_name' => 'Child2 Lname',
130-
'group' => array($childGroup->id => 1),
130+
'group' => array($childGroup['id'] => 1),
131131
);
132132
$childContact = Contact::createIndividual($childContactParams);
133133

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

147147
// Check if searching by child group returns just child group contacts
148148
$searchParams = array(
149-
'group' => $childGroup->id,
149+
'group' => $childGroup['id'],
150150
);
151151
$result = $this->callAPISuccess('contact', 'get', $searchParams);
152152
$validChildContactIds = array($childContact);

tests/phpunit/CRM/Contact/BAO/QueryTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ public function testGroupClause() {
290290
$this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupID, 'contact_id' => $individualID, 'status' => 'Added'));
291291
$this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupID, 'contact_id' => $householdID, 'status' => 'Added'));
292292

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

304307
}

tests/phpunit/CRM/Event/BAO/ParticipantTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -177,18 +177,18 @@ public function testimportableFields() {
177177
* ParticipantDetails() method ( Checking the Participant Details )
178178
*/
179179
public function testparticipantDetails() {
180-
$participantId = Participant::create($this->_contactId, $this->_eventId);
181-
$params = array('name' => 'Doe, John', 'title' => 'Test Event');
180+
$participantId = $this->callAPISuccess('Participant', 'create', array('contact_id' => $this->_contactId, 'event_id' => $this->_eventId));
181+
$params = array('name' => 'Doe, John', 'title' => 'Annual CiviCRM meet');
182182

183183
$participantDetails = CRM_Event_BAO_Participant::participantDetails($participantId);
184184

185185
$this->assertEquals(count($participantDetails), 3, 'Equating the array contains.');
186186
$this->assertEquals($participantDetails['name'], $params['name'], 'Checking Name of Participant.');
187187
$this->assertEquals($participantDetails['title'], $params['title'], 'Checking Event Title in which participant is enroled.');
188188

189-
Participant::delete($participantId);
189+
$this->callAPISuccess('Participant', 'delete', array('id' => $participantId));
190190
$this->contactDelete($this->_contactId);
191-
Event::delete($this->_eventId);
191+
$this->callAPISuccess('Event', 'delete', array('id' => $this->_eventId));
192192
}
193193

194194
/**
@@ -222,7 +222,7 @@ public function testdeleteParticipant() {
222222
$this->assertDBNull('CRM_Event_BAO_Participant', $participant->id, 'contact_id', 'id', 'Check DB for deleted Participant.');
223223

224224
$this->contactDelete($this->_contactId);
225-
Event::delete($this->_eventId);
225+
$this->callAPISuccess('Event', 'delete', array('id' => $this->_eventId));
226226
}
227227

228228
/**

0 commit comments

Comments
 (0)