Skip to content

Commit 78ec76a

Browse files
committed
Merge remote-tracking branch 'origin/imported-magento-magento2-32730' into 2.4-develop-pr143
2 parents 9d1fe17 + 5d43aac commit 78ec76a

File tree

5 files changed

+141
-11
lines changed

5 files changed

+141
-11
lines changed

app/code/Magento/Email/Model/Transport.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
namespace Magento\Email\Model;
99

1010
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\App\ObjectManager;
1112
use Magento\Framework\Exception\MailException;
1213
use Magento\Framework\Mail\MessageInterface;
1314
use Magento\Framework\Mail\TransportInterface;
1415
use Magento\Framework\Phrase;
1516
use Magento\Store\Model\ScopeInterface;
1617
use Laminas\Mail\Message;
1718
use Laminas\Mail\Transport\Sendmail;
19+
use Psr\Log\LoggerInterface;
1820

1921
/**
2022
* Class that responsible for filling some message data before transporting it.
@@ -60,15 +62,22 @@ class Transport implements TransportInterface
6062
*/
6163
private $message;
6264

65+
/**
66+
* @var LoggerInterface|null
67+
*/
68+
private $logger;
69+
6370
/**
6471
* @param MessageInterface $message Email message object
6572
* @param ScopeConfigInterface $scopeConfig Core store config
6673
* @param null|string|array|\Traversable $parameters Config options for sendmail parameters
74+
* @param LoggerInterface|null $logger
6775
*/
6876
public function __construct(
6977
MessageInterface $message,
7078
ScopeConfigInterface $scopeConfig,
71-
$parameters = null
79+
$parameters = null,
80+
LoggerInterface $logger = null
7281
) {
7382
$this->isSetReturnPath = (int) $scopeConfig->getValue(
7483
self::XML_PATH_SENDING_SET_RETURN_PATH,
@@ -81,6 +90,7 @@ public function __construct(
8190

8291
$this->laminasTransport = new Sendmail($parameters);
8392
$this->message = $message;
93+
$this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
8494
}
8595

8696
/**
@@ -100,7 +110,8 @@ public function sendMessage()
100110

101111
$this->laminasTransport->send($laminasMessage);
102112
} catch (\Exception $e) {
103-
throw new MailException(new Phrase($e->getMessage()), $e);
113+
$this->logger->error($e);
114+
throw new MailException(new Phrase('Unable to send mail. Please try again later.'));
104115
}
105116
}
106117

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Email\Test\Unit\Model;
9+
10+
use Laminas\Mail\Transport\Exception\RuntimeException;
11+
use Magento\Email\Model\Transport;
12+
use Magento\Framework\App\Config\ScopeConfigInterface;
13+
use Magento\Framework\Mail\Message;
14+
use PHPUnit\Framework\MockObject\MockObject;
15+
use PHPUnit\Framework\TestCase;
16+
use Psr\Log\LoggerInterface;
17+
18+
/**
19+
* Tests for email transport functionality.
20+
*/
21+
class TransportTest extends TestCase
22+
{
23+
/**
24+
* @var MockObject|LoggerInterface
25+
*/
26+
private $loggerMock;
27+
28+
/**
29+
* @var Transport
30+
*/
31+
private $transport;
32+
33+
/**
34+
* @var ScopeConfigInterface|MockObject
35+
*/
36+
private $scopeConfigMock;
37+
38+
/**
39+
* @inheridoc
40+
*/
41+
protected function setUp(): void
42+
{
43+
$this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
44+
->disableOriginalConstructor()
45+
->onlyMethods(['error'])
46+
->getMockForAbstractClass();
47+
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
48+
->disableOriginalConstructor()
49+
->getMockForAbstractClass();
50+
$this->transport = new Transport(
51+
new Message(),
52+
$this->scopeConfigMock,
53+
null,
54+
$this->loggerMock
55+
);
56+
}
57+
58+
/**
59+
* Verify exception is properly handled in case one occurred when message sent.
60+
*
61+
* @return void
62+
*/
63+
public function testSendMessageBrokenMessage(): void
64+
{
65+
$exception = new RuntimeException('Invalid email; contains no at least one of "To", "Cc", and "Bcc" header');
66+
$this->loggerMock->expects(self::once())->method('error')->with($exception);
67+
$this->expectException('Magento\Framework\Exception\MailException');
68+
$this->expectExceptionMessage('Unable to send mail. Please try again later.');
69+
70+
$this->transport->sendMessage();
71+
}
72+
}

app/code/Magento/Email/i18n/en_US.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,4 @@ Action,Action
9898
"Email template chosen based on theme fallback, when the ""Default"" option is selected.","Email template chosen based on theme fallback, when the ""Default"" option is selected."
9999
"Header Template","Header Template"
100100
"Footer Template","Footer Template"
101+
"Unable to send mail. Please try again later.","Unable to send mail. Please try again later."

lib/internal/Magento/Framework/Mail/Test/Unit/TransportTest.php

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,57 @@
77

88
namespace Magento\Framework\Mail\Test\Unit;
99

10+
use Laminas\Mail\Transport\Exception\RuntimeException;
1011
use Magento\Framework\Mail\Message;
1112
use Magento\Framework\Mail\Transport;
13+
use PHPUnit\Framework\MockObject\MockObject;
1214
use PHPUnit\Framework\TestCase;
15+
use Psr\Log\LoggerInterface;
1316

17+
/**
18+
* Provides tests for framework email transport functionality.
19+
*/
1420
class TransportTest extends TestCase
1521
{
1622
/**
23+
* @var MockObject|LoggerInterface
24+
*/
25+
private $loggerMock;
26+
27+
/**
28+
* @var Transport
29+
*/
30+
private $transport;
31+
32+
/**
33+
* @inheridoc
34+
*/
35+
protected function setUp(): void
36+
{
37+
$this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
38+
->disableOriginalConstructor()
39+
->onlyMethods(['error'])
40+
->getMockForAbstractClass();
41+
$this->transport = new Transport(
42+
new Message(),
43+
null,
44+
$this->loggerMock
45+
);
46+
}
47+
48+
/**
49+
* Verify exception is properly handled in case one occurred when message sent.
50+
*
1751
* @covers \Magento\Framework\Mail\Transport::sendMessage
52+
* @return void
1853
*/
19-
public function testSendMessageBrokenMessage()
54+
public function testSendMessageBrokenMessage(): void
2055
{
56+
$exception = new RuntimeException('Invalid email; contains no at least one of "To", "Cc", and "Bcc" header');
57+
$this->loggerMock->expects(self::once())->method('error')->with($exception);
2158
$this->expectException('Magento\Framework\Exception\MailException');
22-
$this->expectExceptionMessage('Invalid email; contains no at least one of "To", "Cc", and "Bcc" header');
23-
$transport = new Transport(
24-
new Message()
25-
);
59+
$this->expectExceptionMessage('Unable to send mail. Please try again later.');
2660

27-
$transport->sendMessage();
61+
$this->transport->sendMessage();
2862
}
2963
}

lib/internal/Magento/Framework/Mail/Transport.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Framework\Mail;
79

10+
use Magento\Framework\App\ObjectManager;
811
use Magento\Framework\Exception\MailException;
912
use Magento\Framework\Phrase;
1013
use Laminas\Mail\Message as LaminasMessage;
1114
use Laminas\Mail\Transport\Sendmail;
15+
use Psr\Log\LoggerInterface;
1216

1317
/**
1418
* Mail transport
1519
*/
16-
class Transport implements \Magento\Framework\Mail\TransportInterface
20+
class Transport implements TransportInterface
1721
{
1822
/**
1923
* @var Sendmail
@@ -25,14 +29,21 @@ class Transport implements \Magento\Framework\Mail\TransportInterface
2529
*/
2630
private $message;
2731

32+
/**
33+
* @var LoggerInterface|null
34+
*/
35+
private $logger;
36+
2837
/**
2938
* @param MessageInterface $message
3039
* @param null|string|array|\Traversable $parameters
40+
* @param LoggerInterface|null $logger
3141
*/
32-
public function __construct(MessageInterface $message, $parameters = null)
42+
public function __construct(MessageInterface $message, $parameters = null, LoggerInterface $logger = null)
3343
{
3444
$this->laminasTransport = new Sendmail($parameters);
3545
$this->message = $message;
46+
$this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
3647
}
3748

3849
/**
@@ -45,7 +56,8 @@ public function sendMessage()
4556
LaminasMessage::fromString($this->message->getRawMessage())
4657
);
4758
} catch (\Exception $e) {
48-
throw new MailException(new Phrase($e->getMessage()), $e);
59+
$this->logger->error($e);
60+
throw new MailException(new Phrase('Unable to send mail. Please try again later.'));
4961
}
5062
}
5163

0 commit comments

Comments
 (0)