From d15a97f4a19f528ba092c5cf019ac1045a50448d Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 15 Feb 2017 18:25:40 +1300 Subject: [PATCH] CRM-20016 fix mysql error on processing bounces --- CRM/Mailing/Event/BAO/Bounce.php | 80 +++++++++++-------- CRM/Utils/Mail/EmailProcessor.php | 8 +- .../CRM/Utils/Mail/EmailProcessorTest.php | 5 +- tests/phpunit/CiviTest/CiviUnitTestCase.php | 2 +- 4 files changed, 52 insertions(+), 43 deletions(-) diff --git a/CRM/Mailing/Event/BAO/Bounce.php b/CRM/Mailing/Event/BAO/Bounce.php index 8154cd8326..ae9944f0b9 100644 --- a/CRM/Mailing/Event/BAO/Bounce.php +++ b/CRM/Mailing/Event/BAO/Bounce.php @@ -79,45 +79,13 @@ class CRM_Mailing_Event_BAO_Bounce extends CRM_Mailing_Event_DAO_Bounce { $bounce->copyValues($params); $bounce->save(); - $success = TRUE; - $bounceTable = CRM_Mailing_Event_BAO_Bounce::getTableName(); - $bounceType = CRM_Mailing_DAO_BounceType::getTableName(); - $emailTable = CRM_Core_BAO_Email::getTableName(); - $queueTable = CRM_Mailing_Event_BAO_Queue::getTableName(); - - $bounce->reset(); - // might want to put distinct inside the count - $query = "SELECT count($bounceTable.id) as bounces, - $bounceType.hold_threshold as threshold - FROM $bounceTable - INNER JOIN $bounceType - ON $bounceTable.bounce_type_id = $bounceType.id - INNER JOIN $queueTable - ON $bounceTable.event_queue_id = $queueTable.id - INNER JOIN $emailTable - ON $queueTable.email_id = $emailTable.id - WHERE $emailTable.id = {$q->email_id} - AND ($emailTable.reset_date IS NULL - OR $bounceTable.time_stamp >= $emailTable.reset_date) - GROUP BY $bounceTable.bounce_type_id - ORDER BY threshold, bounces desc"; - - $bounce->query($query); - - while ($bounce->fetch()) { - if ($bounce->bounces >= $bounce->threshold) { - $email = new CRM_Core_BAO_Email(); - $email->id = $q->email_id; - $email->on_hold = TRUE; - $email->hold_date = date('YmdHis'); - $email->save(); - break; - } + if ($q->email_id) { + self::putEmailOnHold($q->email_id); } $transaction->commit(); - return $success; + return TRUE; } /** @@ -279,4 +247,46 @@ class CRM_Mailing_Event_BAO_Bounce extends CRM_Mailing_Event_DAO_Bounce { return $results; } + /** + * Put the email on hold if it has met the threshold. + * + * @param int $email_id + */ + protected static function putEmailOnHold($email_id) { + + $bounceTable = CRM_Mailing_Event_BAO_Bounce::getTableName(); + $bounceType = CRM_Mailing_DAO_BounceType::getTableName(); + $emailTable = CRM_Core_BAO_Email::getTableName(); + $queueTable = CRM_Mailing_Event_BAO_Queue::getTableName(); + + // might want to put distinct inside the count + $query = "SELECT count($bounceTable.id) as bounces, + $bounceType.hold_threshold as threshold + FROM $bounceTable + INNER JOIN $bounceType + ON $bounceTable.bounce_type_id = $bounceType.id + INNER JOIN $queueTable + ON $bounceTable.event_queue_id = $queueTable.id + INNER JOIN $emailTable + ON $queueTable.email_id = $emailTable.id + WHERE $emailTable.id = $email_id + AND ($emailTable.reset_date IS NULL + OR $bounceTable.time_stamp >= $emailTable.reset_date) + GROUP BY $bounceTable.bounce_type_id + ORDER BY threshold, bounces desc"; + + $dao = CRM_Core_DAO::executeQuery($query); + + while ($dao->fetch()) { + if ($dao->bounces >= $dao->threshold) { + $email = new CRM_Core_BAO_Email(); + $email->id = $email_id; + $email->on_hold = TRUE; + $email->hold_date = date('YmdHis'); + $email->save(); + break; + } + } + } + } diff --git a/CRM/Utils/Mail/EmailProcessor.php b/CRM/Utils/Mail/EmailProcessor.php index 336c820352..2e8c14a991 100644 --- a/CRM/Utils/Mail/EmailProcessor.php +++ b/CRM/Utils/Mail/EmailProcessor.php @@ -136,11 +136,9 @@ class CRM_Utils_Mail_EmailProcessor { $usedfor = $dao->is_default; $emailActivityTypeId - = (defined('EMAIL_ACTIVITY_TYPE_ID') && EMAIL_ACTIVITY_TYPE_ID) ? EMAIL_ACTIVITY_TYPE_ID : CRM_Core_OptionGroup::getValue( - 'activity_type', - 'Inbound Email', - 'name' - ); + = (defined('EMAIL_ACTIVITY_TYPE_ID') && EMAIL_ACTIVITY_TYPE_ID) + ? EMAIL_ACTIVITY_TYPE_ID + : CRM_Core_OptionGroup::getValue('activity_type', 'Inbound Email', 'name'); if (!$emailActivityTypeId) { CRM_Core_Error::fatal(ts('Could not find a valid Activity Type ID for Inbound Email')); diff --git a/tests/phpunit/CRM/Utils/Mail/EmailProcessorTest.php b/tests/phpunit/CRM/Utils/Mail/EmailProcessorTest.php index 45208024b1..0c9289cb2c 100644 --- a/tests/phpunit/CRM/Utils/Mail/EmailProcessorTest.php +++ b/tests/phpunit/CRM/Utils/Mail/EmailProcessorTest.php @@ -94,11 +94,12 @@ class CRM_Utils_EmailProcessorTest extends CiviUnitTestCase { */ public function setUpMailing() { $this->contactID = $this->individualCreate(array('email' => 'undeliverable@example.com')); - $groupID = $this->callAPISuccess('Group', 'create', array( + $groupID = $this->callAPISuccess('Group', 'create', array( 'title' => 'Mailing group', 'api.GroupContact.create' => array( 'contact_id' => $this->contactID, - ))); + ), + )); $this->createMailing(array('scheduled_date' => 'now', 'groups' => array('include' => array($groupID)))); $this->callAPISuccess('job', 'process_mailing', array()); $this->eventQueue = $this->callAPISuccess('MailingEventQueue', 'get', array('api.MailingEventQueue.create' => array('hash' => 'aaaaaaaaaaaaaaaa'))); diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index cfb4531416..a9204e7169 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -3204,7 +3204,7 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) * * @return int */ - public function createMailing($params) { + public function createMailing($params = array()) { $params = array_merge(array( 'subject' => 'maild' . rand(), 'body_text' => 'bdkfhdskfhduew{domain.address}{action.optOutUrl}', -- 2.25.1