Skip to content

Commit e7102aa

Browse files
committed
Merge branch 'main_2.2-develop' into MAGETWO-71289
2 parents 4b85f3a + fd81f51 commit e7102aa

File tree

6 files changed

+154
-48
lines changed

6 files changed

+154
-48
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Email\Model\Mail;
7+
8+
use Magento\Framework\App\Config\ScopeConfigInterface;
9+
use Magento\Framework\Exception\MailException;
10+
use Magento\Framework\Mail\TransportInterface;
11+
use Magento\Store\Model\ScopeInterface;
12+
13+
/**
14+
* Plugin over \Magento\Framework\Mail\TransportInterface
15+
*
16+
* It disables email sending depending on the system configuration settings
17+
*/
18+
class TransportInterfacePlugin
19+
{
20+
/**
21+
* @var ScopeConfigInterface
22+
*/
23+
private $scopeConfig;
24+
25+
/**
26+
* @param ScopeConfigInterface $scopeConfig
27+
*/
28+
public function __construct(
29+
ScopeConfigInterface $scopeConfig
30+
) {
31+
$this->scopeConfig = $scopeConfig;
32+
}
33+
34+
/**
35+
* Omit email sending depending on the system configuration setting
36+
*
37+
* @param TransportInterface $subject
38+
* @param \Closure $proceed
39+
* @return void
40+
* @throws MailException
41+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
42+
*/
43+
public function aroundSendMessage(
44+
TransportInterface $subject,
45+
\Closure $proceed
46+
) {
47+
if (!$this->scopeConfig->isSetFlag('system/smtp/disable', ScopeInterface::SCOPE_STORE)) {
48+
$proceed();
49+
}
50+
}
51+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ class Template extends AbstractTemplate implements \Magento\Framework\Mail\Templ
5757
/**
5858
* Config path to mail sending setting that shows if email communications are disabled
5959
* @deprecated
60-
* @see \Magento\Email\Model\Transport::XML_PATH_SYSTEM_SMTP_DISABLE
6160
*/
6261
const XML_PATH_SYSTEM_SMTP_DISABLE = 'system/smtp/disable';
6362

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

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,10 @@
1313

1414
/**
1515
* Class that responsible for filling some message data before transporting it.
16-
* This class checks whether this email should be sent in the first place, based on System Configurations.
1716
* @see Zend_Mail_Transport_Sendmail is used for transport
1817
*/
1918
class Transport implements TransportInterface
2019
{
21-
/**
22-
* Config path to mail sending setting that shows if email communications are disabled
23-
*/
24-
const XML_PATH_SYSTEM_SMTP_DISABLE = 'system/smtp/disable';
25-
2620
/**
2721
* Configuration path to source of Return-Path and whether it should be set at all
2822
* @see \Magento\Config\Model\Config\Source\Yesnocustom to possible values
@@ -85,29 +79,27 @@ public function __construct(
8579
public function sendMessage()
8680
{
8781
try {
88-
if (!$this->scopeConfig->isSetFlag(self::XML_PATH_SYSTEM_SMTP_DISABLE, ScopeInterface::SCOPE_STORE)) {
89-
/* configuration of whether return path should be set or no. Possible values are:
90-
* 0 - no
91-
* 1 - yes (set value as FROM address)
92-
* 2 - use custom value
93-
* @see Magento\Config\Model\Config\Source\Yesnocustom
94-
*/
95-
$isSetReturnPath = $this->scopeConfig->getValue(
96-
self::XML_PATH_SENDING_SET_RETURN_PATH,
97-
ScopeInterface::SCOPE_STORE
98-
);
99-
$returnPathValue = $this->scopeConfig->getValue(
100-
self::XML_PATH_SENDING_RETURN_PATH_EMAIL,
101-
ScopeInterface::SCOPE_STORE
102-
);
82+
/* configuration of whether return path should be set or no. Possible values are:
83+
* 0 - no
84+
* 1 - yes (set value as FROM address)
85+
* 2 - use custom value
86+
* @see Magento\Config\Model\Config\Source\Yesnocustom
87+
*/
88+
$isSetReturnPath = $this->scopeConfig->getValue(
89+
self::XML_PATH_SENDING_SET_RETURN_PATH,
90+
ScopeInterface::SCOPE_STORE
91+
);
92+
$returnPathValue = $this->scopeConfig->getValue(
93+
self::XML_PATH_SENDING_RETURN_PATH_EMAIL,
94+
ScopeInterface::SCOPE_STORE
95+
);
10396

104-
if ($isSetReturnPath == '1') {
105-
$this->message->setReturnPath($this->message->getFrom());
106-
} elseif ($isSetReturnPath == '2' && $returnPathValue !== null) {
107-
$this->message->setReturnPath($returnPathValue);
108-
}
109-
$this->transport->send($this->message);
97+
if ($isSetReturnPath == '1') {
98+
$this->message->setReturnPath($this->message->getFrom());
99+
} elseif ($isSetReturnPath == '2' && $returnPathValue !== null) {
100+
$this->message->setReturnPath($returnPathValue);
110101
}
102+
$this->transport->send($this->message);
111103
} catch (\Exception $e) {
112104
throw new MailException(__($e->getMessage()), $e);
113105
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Email\Test\Unit\Model\Mail;
7+
8+
use Magento\Email\Model\Mail\TransportInterfacePlugin;
9+
use Magento\Framework\App\Config\ScopeConfigInterface;
10+
use Magento\Framework\Mail\TransportInterface;
11+
use Magento\Store\Model\ScopeInterface;
12+
13+
/**
14+
* Covers \Magento\Email\Model\Transport
15+
*/
16+
class TransportInterfacePluginTest extends \PHPUnit\Framework\TestCase
17+
{
18+
/**
19+
* @var TransportInterface|\PHPUnit_Framework_MockObject_MockObject
20+
*/
21+
private $transportMock;
22+
23+
/**
24+
* @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
private $scopeConfigMock;
27+
28+
/**
29+
* @var \Callable|\PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
private $proceedMock;
32+
33+
/**
34+
* @var bool
35+
*/
36+
private $isProceedMockCalled = false;
37+
38+
/**
39+
* @var TransportInterfacePlugin
40+
*/
41+
private $model;
42+
43+
protected function setUp()
44+
{
45+
$this->transportMock = $this->createMock(TransportInterface::class);
46+
$this->scopeConfigMock = $this->getMockForAbstractClass(ScopeConfigInterface::class);
47+
$this->proceedMock = function () {
48+
$this->isProceedMockCalled = true;
49+
};
50+
51+
$this->model = new TransportInterfacePlugin($this->scopeConfigMock);
52+
}
53+
54+
/**
55+
* @dataProvider sendMessageDataProvider
56+
* @param bool $isDisabled
57+
* @param bool $shouldProceedRun
58+
*/
59+
public function testAroundSendMessage(bool $isDisabled, bool $shouldProceedRun)
60+
{
61+
$this->isProceedMockCalled = false;
62+
63+
$this->scopeConfigMock->expects($this->once())
64+
->method('isSetFlag')
65+
->with('system/smtp/disable', ScopeInterface::SCOPE_STORE)
66+
->willReturn($isDisabled);
67+
$this->model->aroundSendMessage($this->transportMock, $this->proceedMock);
68+
$this->assertEquals($shouldProceedRun, $this->isProceedMockCalled);
69+
}
70+
71+
/**
72+
* Data provider for testAroundSendMessage
73+
* @return array
74+
*/
75+
public function sendMessageDataProvider()
76+
{
77+
return [
78+
[false, true],
79+
[true, false],
80+
];
81+
}
82+
}

app/code/Magento/Email/Test/Unit/Model/TransportTest.php

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,11 @@ protected function setUp()
5454
public function testSendMessageException()
5555
{
5656
$this->scopeConfigMock->expects($this->once())
57-
->method('isSetFlag')
57+
->method('getValue')
5858
->willThrowException(new \Exception('some exception'));
5959
$this->model->sendMessage();
6060
}
6161

62-
/**
63-
* Tests that if sending emails is disabled in System Configuration, send nothing
64-
*/
65-
public function testSendMessageSmtpDisabled()
66-
{
67-
$this->scopeConfigMock->expects($this->once())
68-
->method('isSetFlag')
69-
->with(Transport::XML_PATH_SYSTEM_SMTP_DISABLE, ScopeInterface::SCOPE_STORE)
70-
->willReturn(true);
71-
$this->transportMock->expects($this->never())
72-
->method('send');
73-
$this->model->sendMessage();
74-
}
75-
7662
/**
7763
* Tests that if sending Return-Path was disabled or email was not provided, - this header won't be set
7864
*
@@ -154,11 +140,6 @@ public function testGetMessage()
154140
*/
155141
private function prepareSendingMessage($returnPathSet, $returnPathEmail)
156142
{
157-
$this->scopeConfigMock->expects($this->once())
158-
->method('isSetFlag')
159-
->with(Transport::XML_PATH_SYSTEM_SMTP_DISABLE, ScopeInterface::SCOPE_STORE)
160-
->willReturn(false);
161-
162143
$map = [
163144
[Transport::XML_PATH_SENDING_SET_RETURN_PATH, ScopeInterface::SCOPE_STORE, null, $returnPathSet],
164145
[Transport::XML_PATH_SENDING_RETURN_PATH_EMAIL, ScopeInterface::SCOPE_STORE, null, $returnPathEmail]

app/code/Magento/Email/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
</type>
6060
<type name="Magento\Framework\Mail\TransportInterface">
6161
<plugin name="WindowsSmtpConfig" type="Magento\Email\Model\Plugin\WindowsSmtpConfig" />
62+
<plugin name="EmailDisable" type="Magento\Email\Model\Mail\TransportInterfacePlugin" />
6263
</type>
6364
<type name="Magento\Config\Model\Config\TypePool">
6465
<arguments>

0 commit comments

Comments
 (0)