From 00b6e743103b0754cf3a55a116dccd5158f71238 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 7 Sep 2023 14:17:05 +1200 Subject: [PATCH] Move from handling to processor, test --- CRM/Utils/Mail/Incoming.php | 24 ++++++------------- .../Utils/Mail/EmailProcessorInboundTest.php | 14 ++++++++++- .../Mail/data/inbound/test_broken_from.eml | 15 ++++++++++++ 3 files changed, 35 insertions(+), 18 deletions(-) create mode 100644 tests/phpunit/CRM/Utils/Mail/data/inbound/test_broken_from.eml diff --git a/CRM/Utils/Mail/Incoming.php b/CRM/Utils/Mail/Incoming.php index 11f0b23809..a6a70213a2 100644 --- a/CRM/Utils/Mail/Incoming.php +++ b/CRM/Utils/Mail/Incoming.php @@ -304,6 +304,7 @@ class CRM_Utils_Mail_Incoming { } } + $params = []; foreach ($emailFields as $field) { // to, bcc, cc are arrays of objects, but from is an object, so make it an array of one object so we can handle it the same if ($field === 'from') { @@ -312,7 +313,12 @@ class CRM_Utils_Mail_Incoming { else { $value = $mail->$field; } - self::parseAddresses($value, $field, $params, $mail, $createContact); + $params[$field] = []; + foreach ($value as $address) { + $subParam = []; + self::parseAddress($address, $subParam, $mail, $createContact); + $params[$field][] = $subParam; + } } // format and move attachments to the civicrm area if (!empty($attachments)) { @@ -363,22 +369,6 @@ class CRM_Utils_Mail_Incoming { $subParam['id'] = $contactID ?: NULL; } - /** - * @param ezcMailAddress[] $addresses - * @param $token - * @param array $params - * @param $mail - * @param $createContact - */ - private static function parseAddresses(&$addresses, $token, &$params, &$mail, $createContact = TRUE) { - $params[$token] = []; - foreach ($addresses as $address) { - $subParam = []; - self::parseAddress($address, $subParam, $mail, $createContact); - $params[$token][] = $subParam; - } - } - /** * Retrieve a contact ID and if not present. * diff --git a/tests/phpunit/CRM/Utils/Mail/EmailProcessorInboundTest.php b/tests/phpunit/CRM/Utils/Mail/EmailProcessorInboundTest.php index e05bd5b30a..a2bd614ab5 100644 --- a/tests/phpunit/CRM/Utils/Mail/EmailProcessorInboundTest.php +++ b/tests/phpunit/CRM/Utils/Mail/EmailProcessorInboundTest.php @@ -148,6 +148,18 @@ class CRM_Utils_Mail_EmailProcessorInboundTest extends CiviUnitTestCase { } } + /** + * Test messed up from. + * + * This ensures fix for https://issues.civicrm.org/jira/browse/CRM-19215. + */ + public function testBadFrom() :void { + $email = file_get_contents(__DIR__ . '/data/inbound/test_broken_from.eml'); + + copy(__DIR__ . '/data/inbound/test_broken_from.eml', __DIR__ . '/data/mail/test_broken_from.eml'); + $this->callAPISuccess('Job', 'fetch_activities', []); + } + /** * test hook_civicrm_emailProcessor */ @@ -189,7 +201,7 @@ class CRM_Utils_Mail_EmailProcessorInboundTest extends CiviUnitTestCase { * as a fallback if it doesn't match an individual first. */ public function hookImplForEmailProcessorContact($email, $contactID, &$result) { - list($mailName, $mailDomain) = CRM_Utils_System::explode('@', $email, 2); + [$mailName, $mailDomain] = CRM_Utils_System::explode('@', $email, 2); if (empty($mailDomain)) { return; } diff --git a/tests/phpunit/CRM/Utils/Mail/data/inbound/test_broken_from.eml b/tests/phpunit/CRM/Utils/Mail/data/inbound/test_broken_from.eml new file mode 100644 index 0000000000..00c4b74f5b --- /dev/null +++ b/tests/phpunit/CRM/Utils/Mail/data/inbound/test_broken_from.eml @@ -0,0 +1,15 @@ +Delivered-To: jjj@myorg.org +Received: by 10.2.13.84 with SMTP id 1234567890; + Tue, 08 Aug 2023 10:01:11 +0100 (CET) +Return-Path: <> +Message-ID: +Date: Tue, 08 Aug 2023 10:01:07 +0100 +MIME-Version: 1.0 +Content-Type: text/plain; charset="utf-8" +Content-Transfer-Encoding: 8bit +Content-Disposition: inline +From: foo@example.com (foo) +To: jjj@myorg.org +Subject: This is for hooks + +Sample body for hook test. -- 2.25.1