Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-21675: scheduled reminders: limit to group doesn't support parent groups #11629

Merged
merged 1 commit into from
May 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CRM/Contact/BAO/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,7 @@ protected function assignTestValue($fieldName, &$fieldDef, $counter) {
public static function getChildGroupIds($regularGroupIDs) {
$childGroupIDs = array();

foreach ($regularGroupIDs as $regularGroupID) {
foreach ((array) $regularGroupIDs as $regularGroupID) {
// temporary store the child group ID(s) of regular group identified by $id,
// later merge with main child group array
$tempChildGroupIDs = array();
Expand Down
33 changes: 23 additions & 10 deletions Civi/ActionSchedule/RecipientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,18 +419,31 @@ protected function prepareContactFilter($contactIdField) {
$actionSchedule = $this->actionSchedule;

if ($actionSchedule->group_id) {
if ($this->isSmartGroup($actionSchedule->group_id)) {
// Check that the group is in place in the cache and up to date
\CRM_Contact_BAO_GroupContactCache::check($actionSchedule->group_id);
return \CRM_Utils_SQL_Select::fragment()
->join('grp', "INNER JOIN civicrm_group_contact_cache grp ON {$contactIdField} = grp.contact_id")
->where(" grp.group_id IN ({$actionSchedule->group_id})");
$regularGroupIDs = $smartGroupIDs = $groupWhereCLause = array();
$query = \CRM_Utils_SQL_Select::fragment();

// get child group IDs if any
$childGroupIDs = \CRM_Contact_BAO_Group::getChildGroupIds($actionSchedule->group_id);
foreach (array_merge(array($actionSchedule->group_id), $childGroupIDs) as $groupID) {
if ($this->isSmartGroup($groupID)) {
// Check that the group is in place in the cache and up to date
\CRM_Contact_BAO_GroupContactCache::check($groupID);
$smartGroupIDs[] = $groupID;
}
else {
$regularGroupIDs[] = $groupID;
}
}
else {
return \CRM_Utils_SQL_Select::fragment()
->join('grp', " INNER JOIN civicrm_group_contact grp ON {$contactIdField} = grp.contact_id AND grp.status = 'Added'")
->where(" grp.group_id IN ({$actionSchedule->group_id})");

if (!empty($smartGroupIDs)) {
$query->join('sg', "LEFT JOIN civicrm_group_contact_cache sg ON {$contactIdField} = sg.contact_id");
$groupWhereCLause[] = " sg.group_id IN ( " . implode(', ', $smartGroupIDs) . " ) ";
}
if (!empty($regularGroupIDs)) {
$query->join('rg', " LEFT JOIN civicrm_group_contact rg ON {$contactIdField} = rg.contact_id AND rg.status = 'Added'");
$groupWhereCLause[] = " rg.group_id IN ( " . implode(', ', $regularGroupIDs) . " ) ";
}
return $query->where(implode(" OR ", $groupWhereCLause));
}
elseif (!empty($actionSchedule->recipient_manual)) {
$rList = \CRM_Utils_Type::escape($actionSchedule->recipient_manual, 'String');
Expand Down
88 changes: 88 additions & 0 deletions tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,94 @@ public function testMembershipJoinDateMatch() {
));
}


/**
* CRM-21675: Support parent and smart group in 'Limit to' field
*/
public function testScheduleReminderWithParentGroup() {
// Contact A with birth-date at '07-07-2005' and gender - Male, later got added in smart group
$contactID1 = $this->individualCreate(array('birth_date' => '20050707', 'gender_id' => 1, 'email' => '[email protected]'));
// Contact B with birth-date at '07-07-2005', later got added in regular group
$contactID2 = $this->individualCreate(array('birth_date' => '20050707', 'email' => '[email protected]'), 1);
// Contact C with birth-date at '07-07-2005', but not included in any group
$contactID3 = $this->individualCreate(array('birth_date' => '20050707', 'email' => '[email protected]'), 2);

// create regular group and add Contact B to it
$groupID = $this->groupCreate();
$this->callAPISuccess('GroupContact', 'Create', array(
'group_id' => $groupID,
'contact_id' => $contactID2,
));

// create smart group which will contain all Male contacts
$smartGroupParams = array('formValues' => array('gender_id' => 1));
$smartGroupID = $this->smartGroupCreate(
$smartGroupParams,
array(
'name' => 'new_smart_group',
'title' => 'New Smart Group',
'parents' => array($groupID => 1),
)
);

$actionScheduleParams = array(
'name' => 'sched_contact_bday_yesterday',
'title' => 'sched_contact_bday_yesterday',
'absolute_date' => '',
'body_html' => '<p>you look like you were born yesterday!</p>',
'body_text' => 'you look like you were born yesterday!',
'end_action' => '',
'end_date' => '',
'end_frequency_interval' => '',
'end_frequency_unit' => '',
'entity_status' => 1,
'entity_value' => 'birth_date',
'limit_to' => 1,
'group_id' => $groupID,
'is_active' => 1,
'is_repeat' => '0',
'mapping_id' => 6,
'msg_template_id' => '',
'recipient' => '2',
'recipient_listing' => '',
'recipient_manual' => '',
'record_activity' => 1,
'repetition_frequency_interval' => '',
'repetition_frequency_unit' => '',
'start_action_condition' => 'after',
'start_action_date' => 'date_field',
'start_action_offset' => '1',
'start_action_unit' => 'day',
'subject' => 'subject sched_contact_bday_yesterday',
);

// Create schedule reminder where parent group ($groupID) is selectd to limit recipients,
// which contain a individual contact - $contactID2 and is parent to smart group.
$actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionScheduleParams);
$this->assertTrue(is_numeric($actionScheduleDao->id));
$this->assertCronRuns(array(
array(
// On the birthday, no email.
'time' => '2005-07-07 01:00:00',
'recipients' => array(),
),
array(
// The next day, send an email.
'time' => '2005-07-08 20:00:00',
'recipients' => array(
array(
'[email protected]',
),
array(
'[email protected]',
),
),
),
));
$this->groupDelete($smartGroupID);
$this->groupDelete($groupID);
}

/**
* Test end date email sent.
*
Expand Down