EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
Ref, fix test
Yurii09bcf41638a5
diff --git a/application/Espo/Core/Mail/Importer/DefaultImporter.php b/application/Espo/Core/Mail/Importer/DefaultImporter.phpindex 67c2ec3d0a..fb1cdda980 100644--- a/application/Espo/Core/Mail/Importer/DefaultImporter.php+++ b/application/Espo/Core/Mail/Importer/DefaultImporter.php@@ -66,13 +66,11 @@ use Espo\Tools\Stream\Jobs\ProcessNoteAcl;use DateTime;use Exception;-use PDOException;class DefaultImporter implements Importer{private const SUBJECT_MAX_LENGTH = 255;private const PROCESS_ACL_DELAY_PERIOD = '5 seconds';- private const int SAVE_RETRY_COUNT = 2;/** @var AssignmentNotificator<Email> */private AssignmentNotificator $notificator;@@ -88,6 +86,7 @@ class DefaultImporter implements Importerprivate JobSchedulerFactory $jobSchedulerFactory,private ParentFinder $parentFinder,private AutoReplyDetector $autoReplyDetector,+ private EmailSaver $emailSaver,) {$this->notificator = $notificatorFactory->createByClass(Email::class);$this->filtersMatcher = new FiltersMatcher();@@ -226,7 +225,8 @@ class DefaultImporter implements Importer$email->setStatus(Email::STATUS_ARCHIVED);- $this->processFinalTransactionalSave($email);+ $this->emailSaver->save($email);+$this->processAttachmentSave($inlineAttachmentList, $email);return $email;@@ -593,37 +593,6 @@ class DefaultImporter implements Importerreturn false;}- private function processFinalTransactionalSave(Email $email): void- {- for ($i = 0; $i < self::SAVE_RETRY_COUNT; $i ++) {- try {- $this->processFinalTransactionalSaveInternal($email);- } catch (PDOException $e) {- $code = (int) ($e->errorInfo[1] ?? '0');-- // Handles a snapshot isolation conflict.- if ($code === 1020) {- continue;- }-- throw $e;- }- }- }-- private function processFinalTransactionalSaveInternal(Email $email): void- {- $this->entityManager->getTransactionManager()->run(function () use ($email) {- $this->entityManager- ->getRDBRepositoryByClass(Email::class)- ->forUpdate()- ->select(Attribute::ID)- ->where([Attribute::ID => $email->getId()])- ->findOne();-- $this->entityManager->saveEntity($email, [Email::SAVE_OPTION_IS_BEING_IMPORTED => true]);- });- }/*** @param Attachment[] $inlineAttachmentListdiff --git a/application/Espo/Core/Mail/Importer/EmailSaver.php b/application/Espo/Core/Mail/Importer/EmailSaver.phpnew file mode 100644index 0000000000..465e23987c--- /dev/null+++ b/application/Espo/Core/Mail/Importer/EmailSaver.php@@ -0,0 +1,79 @@+<?php+/************************************************************************+ * This file is part of EspoCRM.+ *+ * EspoCRM – Open Source CRM application.+ * Copyright (C) 2014-2026 EspoCRM, Inc.+ * Website: https://www.espocrm.com+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as published by+ * the Free Software Foundation, either version 3 of the License, or+ * (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ * The interactive user interfaces in modified source and object code versions+ * of this program must display Appropriate Legal Notices, as required under+ * Section 5 of the GNU Affero General Public License version 3.+ *+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.+ ************************************************************************/++namespace Espo\Core\Mail\Importer;++use Espo\Entities\Email;+use Espo\ORM\EntityManager;+use Espo\ORM\Name\Attribute;+use PDOException;++/**+ * @internal+ */+class EmailSaver+{+ private const int SAVE_RETRY_COUNT = 2;++ public function __construct(+ private EntityManager $entityManager,+ ) {}++ public function save(Email $email): void+ {+ for ($i = 0; $i < self::SAVE_RETRY_COUNT; $i ++) {+ try {+ $this->saveInternal($email);+ } catch (PDOException $e) {+ $code = (int) ($e->errorInfo[1] ?? '0');++ // Handles a snapshot isolation conflict.+ if ($code === 1020) {+ continue;+ }++ throw $e;+ }+ }+ }++ private function saveInternal(Email $email): void+ {+ $this->entityManager->getTransactionManager()->run(function () use ($email) {+ $this->entityManager+ ->getRDBRepositoryByClass(Email::class)+ ->forUpdate()+ ->select(Attribute::ID)+ ->where([Attribute::ID => $email->getId()])+ ->findOne();++ $this->entityManager->saveEntity($email, [Email::SAVE_OPTION_IS_BEING_IMPORTED => true]);+ });+ }+}diff --git a/tests/unit/Espo/Core/Mail/ImporterTest.php b/tests/unit/Espo/Core/Mail/ImporterTest.phpindex 3fadeeb66d..1c83ea81a2 100644--- a/tests/unit/Espo/Core/Mail/ImporterTest.php+++ b/tests/unit/Espo/Core/Mail/ImporterTest.php@@ -168,7 +168,7 @@ class ImporterTest extends TestCase->willReturnMap($this->repositoryMap);$entityManager- ->expects($this->exactly(2))+ ->expects($this->exactly(1))->method('saveEntity')->with($this->isInstanceOf(Email::class))->willReturnCallback(function (Email $entity) {@@ -189,6 +189,13 @@ class ImporterTest extends TestCase]);+ $saver = $this->createMock(Importer\EmailSaver::class);++ $saver+ ->expects($this->exactly(1))+ ->method('save')+ ->with($this->isInstanceOf(Email::class));+$contents = file_get_contents('tests/unit/testData/Core/Mail/test_email_1.eml');$importer = new Importer\DefaultImporter(@@ -201,6 +208,7 @@ class ImporterTest extends TestCasejobSchedulerFactory: $this->jobSchedulerFactory,parentFinder: $this->parentFinder,autoReplyDetector: $this->createMock(Importer\AutoReplyDetector::class),+ emailSaver: $saver,);$message = new MessageWrapper(0, null, null, $contents);