From 5b4d40abc013b45bf9ca3882210ba752f6a52420 Mon Sep 17 00:00:00 2001 From: Jon Goldberg Date: Thu, 16 Jan 2020 19:50:24 -0500 Subject: [PATCH] Don't send scheduled reminders for event templates --- Civi/ActionSchedule/RecipientBuilder.php | 23 ++++++++++++++++- tests/phpunit/api/v3/JobTest.php | 32 ++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/Civi/ActionSchedule/RecipientBuilder.php b/Civi/ActionSchedule/RecipientBuilder.php index cb267eac2d..a54bca3b83 100644 --- a/Civi/ActionSchedule/RecipientBuilder.php +++ b/Civi/ActionSchedule/RecipientBuilder.php @@ -138,7 +138,7 @@ class RecipientBuilder { public function build() { $this->buildRelFirstPass(); - if ($this->prepareAddlFilter('c.id')) { + if ($this->prepareAddlFilter('c.id') && $this->notTemplate()) { $this->buildAddlFirstPass(); } @@ -603,4 +603,25 @@ reminder.action_schedule_id = {$this->actionSchedule->id}"; return $this->mapping->resetOnTriggerDateChange($this->actionSchedule); } + /** + * Confirm this object isn't attached to a template. + * Returns TRUE if this action schedule isn't attached to a template. + * Templates are (currently) unique to events, so we only evaluate those. + * + * @return bool; + */ + private function notTemplate() { + if ($this->mapping->getEntity() === 'civicrm_participant') { + $entityId = $this->actionSchedule->entity_value; + $query = new \CRM_Utils_SQL_Select('civicrm_event e'); + $sql = $query + ->select('is_template') + ->where("e.id = {$entityId}") + ->toSQL(); + $dao = \CRM_Core_DAO::executeQuery($sql); + return !(bool) $dao->fetchValue(); + } + return TRUE; + } + } diff --git a/tests/phpunit/api/v3/JobTest.php b/tests/phpunit/api/v3/JobTest.php index 54068b40c7..57b88cfa4a 100644 --- a/tests/phpunit/api/v3/JobTest.php +++ b/tests/phpunit/api/v3/JobTest.php @@ -325,6 +325,38 @@ class api_v3_JobTest extends CiviUnitTestCase { $this->contactDelete($orgID); } + /** + * Event templates should not send reminders to additional contacts. + */ + public function testTemplateRemindAddlContacts() { + $contactId = $this->individualCreate(); + $groupId = $this->groupCreate(['name' => 'Additional Contacts', 'title' => 'Additional Contacts']); + $this->callAPISuccess('GroupContact', 'create', [ + 'contact_id' => $contactId, + 'group_id' => $groupId, + ]); + $event = $this->eventCreate(['is_template' => 1, 'template_title' => "I'm a template", 'title' => NULL]); + $eventId = $event['id']; + + $actionSchedule = $this->callAPISuccess('action_schedule', 'create', [ + 'title' => "Do not send me", + 'subject' => "I am a reminder attached to a template.", + 'entity_value' => $eventId, + 'mapping_id' => 5, + 'start_action_date' => 'start_date', + 'start_action_offset' => 1, + 'start_action_condition' => 'before', + 'start_action_unit' => 'day', + 'group_id' => $groupId, + 'limit_to' => FALSE, + 'mode' => 'Email', + ]); + + $this->callAPISuccess('job', 'send_reminder', []); + $successfulCronCount = CRM_Core_DAO::singleValueQuery("SELECT count(*) FROM civicrm_action_log"); + $this->assertEquals(0, $successfulCronCount); + } + /** * Test scheduled reminders respect limit to (since above identified addition_to handling issue). * -- 2.25.1