diff --git a/app/code/Magento/Sales/Model/Order/Email/SenderBuilder.php b/app/code/Magento/Sales/Model/Order/Email/SenderBuilder.php index 93c6f19b08690..7ec089b882972 100644 --- a/app/code/Magento/Sales/Model/Order/Email/SenderBuilder.php +++ b/app/code/Magento/Sales/Model/Order/Email/SenderBuilder.php @@ -5,7 +5,9 @@ */ namespace Magento\Sales\Model\Order\Email; +use Magento\Framework\App\ObjectManager; use Magento\Framework\Mail\Template\TransportBuilder; +use Magento\Framework\Mail\Template\TransportBuilderByStore; use Magento\Sales\Model\Order\Email\Container\IdentityInterface; use Magento\Sales\Model\Order\Email\Container\Template; @@ -26,19 +28,29 @@ class SenderBuilder */ protected $transportBuilder; + /** + * @var TransportBuilderByStore + */ + private $transportBuilderByStore; + /** * @param Template $templateContainer * @param IdentityInterface $identityContainer * @param TransportBuilder $transportBuilder + * @param TransportBuilderByStore $transportBuilderByStore */ public function __construct( Template $templateContainer, IdentityInterface $identityContainer, - TransportBuilder $transportBuilder + TransportBuilder $transportBuilder, + TransportBuilderByStore $transportBuilderByStore = null ) { $this->templateContainer = $templateContainer; $this->identityContainer = $identityContainer; $this->transportBuilder = $transportBuilder; + $this->transportBuilderByStore = $transportBuilderByStore ?: ObjectManager::getInstance()->get( + TransportBuilderByStore::class + ); } /** @@ -98,6 +110,9 @@ protected function configureEmailTemplate() $this->transportBuilder->setTemplateIdentifier($this->templateContainer->getTemplateId()); $this->transportBuilder->setTemplateOptions($this->templateContainer->getTemplateOptions()); $this->transportBuilder->setTemplateVars($this->templateContainer->getTemplateVars()); - $this->transportBuilder->setFrom($this->identityContainer->getEmailIdentity()); + $this->transportBuilderByStore->setFromByStore( + $this->identityContainer->getEmailIdentity(), + $this->identityContainer->getStore()->getId() + ); } } diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/Email/SenderBuilderTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/Email/SenderBuilderTest.php index 5319aa510bedf..38209bb22aef4 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/Email/SenderBuilderTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/Email/SenderBuilderTest.php @@ -3,8 +3,10 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + namespace Magento\Sales\Test\Unit\Model\Order\Email; +use Magento\Framework\Mail\Template\TransportBuilderByStore; use Magento\Sales\Model\Order\Email\SenderBuilder; class SenderBuilderTest extends \PHPUnit\Framework\TestCase @@ -29,6 +31,16 @@ class SenderBuilderTest extends \PHPUnit\Framework\TestCase */ protected $transportBuilder; + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + private $storeMock; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + private $transportBuilderByStore; + protected function setUp() { $templateId = 'test_template_id'; @@ -42,7 +54,11 @@ protected function setUp() ['getTemplateVars', 'getTemplateOptions', 'getTemplateId'] ); - $this->storeMock = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getStoreId', '__wakeup']); + $this->storeMock = $this->createPartialMock(\Magento\Store\Model\Store::class, [ + 'getStoreId', + '__wakeup', + 'getId', + ]); $this->identityContainerMock = $this->createPartialMock( \Magento\Sales\Model\Order\Email\Container\ShipmentIdentity::class, @@ -52,15 +68,24 @@ protected function setUp() 'getCustomerName', 'getTemplateOptions', 'getEmailCopyTo', - 'getCopyMethod' + 'getCopyMethod', + 'getStore', ] ); - $this->transportBuilder = $this->createPartialMock(\Magento\Framework\Mail\Template\TransportBuilder::class, [ - 'addTo', 'addBcc', 'getTransport', - 'setTemplateIdentifier', 'setTemplateOptions', 'setTemplateVars', - 'setFrom', - ]); + $this->transportBuilder = $this->createPartialMock( + \Magento\Framework\Mail\Template\TransportBuilder::class, + [ + 'addTo', + 'addBcc', + 'getTransport', + 'setTemplateIdentifier', + 'setTemplateOptions', + 'setTemplateVars', + ] + ); + + $this->transportBuilderByStore = $this->createMock(TransportBuilderByStore::class); $this->templateContainerMock->expects($this->once()) ->method('getTemplateId') @@ -84,8 +109,8 @@ protected function setUp() $this->identityContainerMock->expects($this->once()) ->method('getEmailIdentity') ->will($this->returnValue($emailIdentity)); - $this->transportBuilder->expects($this->once()) - ->method('setFrom') + $this->transportBuilderByStore->expects($this->once()) + ->method('setFromByStore') ->with($this->equalTo($emailIdentity)); $this->identityContainerMock->expects($this->once()) @@ -95,7 +120,8 @@ protected function setUp() $this->senderBuilder = new SenderBuilder( $this->templateContainerMock, $this->identityContainerMock, - $this->transportBuilder + $this->transportBuilder, + $this->transportBuilderByStore ); } @@ -119,6 +145,12 @@ public function testSend() $this->identityContainerMock->expects($this->once()) ->method('getCustomerName') ->will($this->returnValue($customerName)); + $this->identityContainerMock->expects($this->once()) + ->method('getStore') + ->willReturn($this->storeMock); + $this->storeMock->expects($this->once()) + ->method('getId') + ->willReturn(1); $this->transportBuilder->expects($this->once()) ->method('addTo') ->with($this->equalTo($customerEmail), $this->equalTo($customerName)); @@ -145,7 +177,12 @@ public function testSendCopyTo() $this->transportBuilder->expects($this->once()) ->method('addTo') ->with($this->equalTo('example@mail.com')); - + $this->identityContainerMock->expects($this->once()) + ->method('getStore') + ->willReturn($this->storeMock); + $this->storeMock->expects($this->once()) + ->method('getId') + ->willReturn(1); $this->transportBuilder->expects($this->once()) ->method('getTransport') ->will($this->returnValue($transportMock)); diff --git a/lib/internal/Magento/Framework/Mail/Template/TransportBuilderByStore.php b/lib/internal/Magento/Framework/Mail/Template/TransportBuilderByStore.php new file mode 100644 index 0000000000000..785c93824a57d --- /dev/null +++ b/lib/internal/Magento/Framework/Mail/Template/TransportBuilderByStore.php @@ -0,0 +1,54 @@ +message = $message; + $this->senderResolver = $senderResolver; + } + + /** + * Set mail from address by store. + * + * @param string|array $from + * @param string|int $store + * + * @return $this + */ + public function setFromByStore($from, $store) + { + $result = $this->senderResolver->resolve($from, $store); + $this->message->setFrom($result['email'], $result['name']); + + return $this; + } +} diff --git a/lib/internal/Magento/Framework/Mail/Test/Unit/Template/TransportBuilderByStoreTest.php b/lib/internal/Magento/Framework/Mail/Test/Unit/Template/TransportBuilderByStoreTest.php new file mode 100644 index 0000000000000..80df2887a3a93 --- /dev/null +++ b/lib/internal/Magento/Framework/Mail/Test/Unit/Template/TransportBuilderByStoreTest.php @@ -0,0 +1,64 @@ +messageMock = $this->createMock(\Magento\Framework\Mail\Message::class); + $this->senderResolverMock = $this->createMock(\Magento\Framework\Mail\Template\SenderResolverInterface::class); + + $this->model = $objectManagerHelper->getObject( + TransportBuilderByStore::class, + [ + 'message' => $this->messageMock, + 'senderResolver' => $this->senderResolverMock, + ] + ); + } + + /** + * @return void + */ + public function testSetFromByStore() + { + $sender = ['email' => 'from@example.com', 'name' => 'name']; + $store = 1; + $this->senderResolverMock->expects($this->once()) + ->method('resolve') + ->with($sender, $store) + ->willReturn($sender); + $this->messageMock->expects($this->once()) + ->method('setFrom') + ->with('from@example.com', 'name') + ->willReturnSelf(); + + $this->model->setFromByStore($sender, $store); + } +} diff --git a/lib/internal/Magento/Framework/Mail/Test/Unit/Template/TransportBuilderTest.php b/lib/internal/Magento/Framework/Mail/Test/Unit/Template/TransportBuilderTest.php index 696e0a9f310d8..927e17c824e33 100644 --- a/lib/internal/Magento/Framework/Mail/Test/Unit/Template/TransportBuilderTest.php +++ b/lib/internal/Magento/Framework/Mail/Test/Unit/Template/TransportBuilderTest.php @@ -3,6 +3,7 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + namespace Magento\Framework\Mail\Test\Unit\Template; use Magento\Framework\App\TemplateTypesInterface;