From a788af7fd416e650353ebcc35551f55f588bb7c7 Mon Sep 17 00:00:00 2001 From: Pieter Cappelle Date: Mon, 9 Oct 2017 14:20:35 +0200 Subject: [PATCH 1/4] [2.2-Develop] Fix for issue #5726 Was fixed in 2.0.8 branch. Never got into 2.1-develop and 2.2-develop. Cherry-pick from 9fc059bc5b5432cadfd2f1cfa3cc2516e607e068 --- app/code/Magento/Checkout/Helper/Data.php | 2 ++ .../Customer/Model/AccountManagement.php | 1 + app/code/Magento/Customer/Model/Customer.php | 2 ++ .../Magento/Newsletter/Model/Subscriber.php | 15 ++++++++++++--- app/code/Magento/ProductAlert/Model/Email.php | 2 ++ .../Magento/Wishlist/Controller/Index/Send.php | 5 ++++- .../Mail/Template/TransportBuilder.php | 18 +++++++++++++++++- 7 files changed, 40 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/Checkout/Helper/Data.php b/app/code/Magento/Checkout/Helper/Data.php index b3c2e17e5d678..ba7179464641e 100644 --- a/app/code/Magento/Checkout/Helper/Data.php +++ b/app/code/Magento/Checkout/Helper/Data.php @@ -305,6 +305,8 @@ public function sendPaymentFailedEmail($checkout, $message, $checkoutType = 'one 'items' => nl2br($items), 'total' => $total, ] + )->setScopeId( + $checkout->getStoreId() )->setFrom( $this->scopeConfig->getValue( 'checkout/payment_failed/identity', diff --git a/app/code/Magento/Customer/Model/AccountManagement.php b/app/code/Magento/Customer/Model/AccountManagement.php index b7b099ec45232..86ae9fd819e12 100644 --- a/app/code/Magento/Customer/Model/AccountManagement.php +++ b/app/code/Magento/Customer/Model/AccountManagement.php @@ -1112,6 +1112,7 @@ protected function sendEmailTemplate( $transport = $this->transportBuilder->setTemplateIdentifier($templateId) ->setTemplateOptions(['area' => Area::AREA_FRONTEND, 'store' => $storeId]) ->setTemplateVars($templateParams) + ->setScopeId($storeId) ->setFrom($this->scopeConfig->getValue($sender, ScopeInterface::SCOPE_STORE, $storeId)) ->addTo($email, $this->customerViewHelper->getCustomerName($customer)) ->getTransport(); diff --git a/app/code/Magento/Customer/Model/Customer.php b/app/code/Magento/Customer/Model/Customer.php index 2e2260f16ff91..41ac820e218c3 100644 --- a/app/code/Magento/Customer/Model/Customer.php +++ b/app/code/Magento/Customer/Model/Customer.php @@ -831,6 +831,8 @@ protected function _sendEmailTemplate($template, $sender, $templateParams = [], ['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $storeId] )->setTemplateVars( $templateParams + )->setScopeId( + $storeId )->setFrom( $this->_scopeConfig->getValue($sender, ScopeInterface::SCOPE_STORE, $storeId) )->addTo( diff --git a/app/code/Magento/Newsletter/Model/Subscriber.php b/app/code/Magento/Newsletter/Model/Subscriber.php index 7847098083949..eeaed8ff51330 100644 --- a/app/code/Magento/Newsletter/Model/Subscriber.php +++ b/app/code/Magento/Newsletter/Model/Subscriber.php @@ -659,6 +659,7 @@ public function sendConfirmationRequestEmail() $this->inlineTranslation->suspend(); + $storeId = $this->_storeManager->getStore()->getId(); $this->_transportBuilder->setTemplateIdentifier( $this->_scopeConfig->getValue( self::XML_PATH_CONFIRM_EMAIL_TEMPLATE, @@ -667,10 +668,12 @@ public function sendConfirmationRequestEmail() )->setTemplateOptions( [ 'area' => \Magento\Framework\App\Area::AREA_FRONTEND, - 'store' => $this->_storeManager->getStore()->getId(), + 'store' => $storeId, ] )->setTemplateVars( ['subscriber' => $this, 'store' => $this->_storeManager->getStore()] + )->setScopeId( + $storeId )->setFrom( $this->_scopeConfig->getValue( self::XML_PATH_CONFIRM_EMAIL_IDENTITY, @@ -712,6 +715,7 @@ public function sendConfirmationSuccessEmail() $this->inlineTranslation->suspend(); + $storeId = $this->_storeManager->getStore()->getId(); $this->_transportBuilder->setTemplateIdentifier( $this->_scopeConfig->getValue( self::XML_PATH_SUCCESS_EMAIL_TEMPLATE, @@ -720,10 +724,12 @@ public function sendConfirmationSuccessEmail() )->setTemplateOptions( [ 'area' => \Magento\Framework\App\Area::AREA_FRONTEND, - 'store' => $this->_storeManager->getStore()->getId(), + 'store' => $storeId, ] )->setTemplateVars( ['subscriber' => $this] + )->setScopeId( + $storeId )->setFrom( $this->_scopeConfig->getValue( self::XML_PATH_SUCCESS_EMAIL_IDENTITY, @@ -764,6 +770,7 @@ public function sendUnsubscriptionEmail() $this->inlineTranslation->suspend(); + $storeId = $this->_storeManager->getStore()->getId(); $this->_transportBuilder->setTemplateIdentifier( $this->_scopeConfig->getValue( self::XML_PATH_UNSUBSCRIBE_EMAIL_TEMPLATE, @@ -772,10 +779,12 @@ public function sendUnsubscriptionEmail() )->setTemplateOptions( [ 'area' => \Magento\Framework\App\Area::AREA_FRONTEND, - 'store' => $this->_storeManager->getStore()->getId(), + 'store' => $storeId, ] )->setTemplateVars( ['subscriber' => $this] + )->setScopeId( + $storeId )->setFrom( $this->_scopeConfig->getValue( self::XML_PATH_UNSUBSCRIBE_EMAIL_IDENTITY, diff --git a/app/code/Magento/ProductAlert/Model/Email.php b/app/code/Magento/ProductAlert/Model/Email.php index 7bc4aba351546..1c5588655a12b 100644 --- a/app/code/Magento/ProductAlert/Model/Email.php +++ b/app/code/Magento/ProductAlert/Model/Email.php @@ -377,6 +377,8 @@ public function send() 'customerName' => $this->_customerHelper->getCustomerName($this->_customer), 'alertGrid' => $alertGrid, ] + )->setScopeId( + $storeId )->setFrom( $this->_scopeConfig->getValue( self::XML_PATH_EMAIL_IDENTITY, diff --git a/app/code/Magento/Wishlist/Controller/Index/Send.php b/app/code/Magento/Wishlist/Controller/Index/Send.php index c2389af6a2282..9c1a45e4faf9e 100644 --- a/app/code/Magento/Wishlist/Controller/Index/Send.php +++ b/app/code/Magento/Wishlist/Controller/Index/Send.php @@ -186,6 +186,7 @@ public function execute() try { foreach ($emails as $email) { + $storeId = $this->storeManager->getStore()->getStoreId(); $transport = $this->_transportBuilder->setTemplateIdentifier( $this->scopeConfig->getValue( 'wishlist/email/email_template', @@ -194,7 +195,7 @@ public function execute() )->setTemplateOptions( [ 'area' => \Magento\Framework\App\Area::AREA_FRONTEND, - 'store' => $this->storeManager->getStore()->getStoreId(), + 'store' => $storeId, ] )->setTemplateVars( [ @@ -206,6 +207,8 @@ public function execute() 'message' => $message, 'store' => $this->storeManager->getStore(), ] + )->setScopeId( + $storeId )->setFrom( $this->scopeConfig->getValue( 'wishlist/email/email_identity', diff --git a/lib/internal/Magento/Framework/Mail/Template/TransportBuilder.php b/lib/internal/Magento/Framework/Mail/Template/TransportBuilder.php index 18b241d77a426..2191e20e6f5d3 100644 --- a/lib/internal/Magento/Framework/Mail/Template/TransportBuilder.php +++ b/lib/internal/Magento/Framework/Mail/Template/TransportBuilder.php @@ -86,6 +86,9 @@ class TransportBuilder */ protected $mailTransportFactory; + /** @var int|null */ + private $scopeId; + /** * @param FactoryInterface $templateFactory * @param MessageInterface $message @@ -158,6 +161,18 @@ public function setReplyTo($email, $name = null) return $this; } + /** + * Set scope + * + * @param int $scopeId + * @return $this + */ + public function setScopeId($scopeId) + { + $this->scopeId = $scopeId; + return $this; + } + /** * Set mail from address * @@ -166,7 +181,7 @@ public function setReplyTo($email, $name = null) */ public function setFrom($from) { - $result = $this->_senderResolver->resolve($from); + $result = $this->_senderResolver->resolve($from, $this->scopeId); $this->message->setFrom($result['email'], $result['name']); return $this; } @@ -244,6 +259,7 @@ protected function reset() $this->templateIdentifier = null; $this->templateVars = null; $this->templateOptions = null; + $this->scopeId = null; return $this; } From 8ac74ae924aafa9a64c1f6de358be5269d14b911 Mon Sep 17 00:00:00 2001 From: Pieter Cappelle Date: Thu, 12 Oct 2017 09:23:07 +0200 Subject: [PATCH 2/4] Merge #11348 --- app/code/Magento/Sales/Model/Order/Email/SenderBuilder.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/code/Magento/Sales/Model/Order/Email/SenderBuilder.php b/app/code/Magento/Sales/Model/Order/Email/SenderBuilder.php index 93c6f19b08690..24c1c2f81f9ad 100644 --- a/app/code/Magento/Sales/Model/Order/Email/SenderBuilder.php +++ b/app/code/Magento/Sales/Model/Order/Email/SenderBuilder.php @@ -48,6 +48,8 @@ public function __construct( */ public function send() { + $this->transportBuilder->setScopeId($this->identityContainer->getStore()->getId()); + $this->configureEmailTemplate(); $this->transportBuilder->addTo( From 086ba735f47ad9ac888be01010682c0249dd1b0c Mon Sep 17 00:00:00 2001 From: Pieter Cappelle Date: Fri, 13 Oct 2017 16:17:00 +0200 Subject: [PATCH 3/4] Fix unittests --- .../Magento/Checkout/Test/Unit/Helper/DataTest.php | 10 ++++++++++ .../Customer/Test/Unit/Model/AccountManagementTest.php | 4 ++++ .../Magento/Customer/Test/Unit/Model/CustomerTest.php | 6 ++++++ .../Test/Unit/Model/Order/Email/SenderBuilderTest.php | 7 ++++++- 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Checkout/Test/Unit/Helper/DataTest.php b/app/code/Magento/Checkout/Test/Unit/Helper/DataTest.php index 31203b63f854a..674fa6d052d10 100644 --- a/app/code/Magento/Checkout/Test/Unit/Helper/DataTest.php +++ b/app/code/Magento/Checkout/Test/Unit/Helper/DataTest.php @@ -169,6 +169,16 @@ public function testSendPaymentFailedEmail() $this->returnSelf() ); + $this->_transportBuilder->expects( + $this->once() + )->method( + 'setScopeId' + )->with( + 1 + )->will( + $this->returnSelf() + ); + $this->_transportBuilder->expects( $this->once() )->method( diff --git a/app/code/Magento/Customer/Test/Unit/Model/AccountManagementTest.php b/app/code/Magento/Customer/Test/Unit/Model/AccountManagementTest.php index de9c0460ad29e..4c454022718e1 100644 --- a/app/code/Magento/Customer/Test/Unit/Model/AccountManagementTest.php +++ b/app/code/Magento/Customer/Test/Unit/Model/AccountManagementTest.php @@ -965,6 +965,10 @@ public function testSendPasswordReminderEmail() ->method('setTemplateVars') ->with(['customer' => $this->customerSecure, 'store' => $this->store]) ->willReturnSelf(); + $this->transportBuilder->expects($this->any()) + ->method('setScopeId') + ->with($customerStoreId) + ->willReturnSelf(); $this->transportBuilder->expects($this->once()) ->method('setFrom') ->with($sender) diff --git a/app/code/Magento/Customer/Test/Unit/Model/CustomerTest.php b/app/code/Magento/Customer/Test/Unit/Model/CustomerTest.php index 8b3f7875e3c97..c282b9ae1bd66 100644 --- a/app/code/Magento/Customer/Test/Unit/Model/CustomerTest.php +++ b/app/code/Magento/Customer/Test/Unit/Model/CustomerTest.php @@ -170,6 +170,12 @@ public function testSendNewAccountEmailWithoutStoreId() ->method($method) ->will($this->returnSelf()); } + + $this->transportBuilder->expects($this->any()) + ->method('setScopeId') + ->with(1) + ->willReturnSelf(); + $transportMock = $this->createMock(\Magento\Framework\Mail\TransportInterface::class); $transportMock->expects($this->once()) ->method('sendMessage') 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..8c74cc61daf05 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 @@ -52,10 +52,15 @@ protected function setUp() 'getCustomerName', 'getTemplateOptions', 'getEmailCopyTo', - 'getCopyMethod' + 'getCopyMethod', + 'getStore' ] ); + $this->identityContainerMock->expects($this->any()) + ->method('getStore') + ->willReturn($this->storeMock); + $this->transportBuilder = $this->createPartialMock(\Magento\Framework\Mail\Template\TransportBuilder::class, [ 'addTo', 'addBcc', 'getTransport', 'setTemplateIdentifier', 'setTemplateOptions', 'setTemplateVars', From 790f4bb015c5a989c8896c45c719595bf5d0e089 Mon Sep 17 00:00:00 2001 From: Pieter Cappelle Date: Sat, 14 Oct 2017 12:19:58 +0200 Subject: [PATCH 4/4] Fix unit tests. --- app/code/Magento/Checkout/Test/Unit/Helper/DataTest.php | 2 +- app/code/Magento/Customer/Test/Unit/Model/CustomerTest.php | 2 +- .../Magento/Wishlist/Test/Unit/Controller/Index/SendTest.php | 4 ++++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Checkout/Test/Unit/Helper/DataTest.php b/app/code/Magento/Checkout/Test/Unit/Helper/DataTest.php index 674fa6d052d10..33276d4a02221 100644 --- a/app/code/Magento/Checkout/Test/Unit/Helper/DataTest.php +++ b/app/code/Magento/Checkout/Test/Unit/Helper/DataTest.php @@ -174,7 +174,7 @@ public function testSendPaymentFailedEmail() )->method( 'setScopeId' )->with( - 1 + 8 )->will( $this->returnSelf() ); diff --git a/app/code/Magento/Customer/Test/Unit/Model/CustomerTest.php b/app/code/Magento/Customer/Test/Unit/Model/CustomerTest.php index c282b9ae1bd66..6218dceb654f0 100644 --- a/app/code/Magento/Customer/Test/Unit/Model/CustomerTest.php +++ b/app/code/Magento/Customer/Test/Unit/Model/CustomerTest.php @@ -171,7 +171,7 @@ public function testSendNewAccountEmailWithoutStoreId() ->will($this->returnSelf()); } - $this->transportBuilder->expects($this->any()) + $this->_transportBuilderMock->expects($this->any()) ->method('setScopeId') ->with(1) ->willReturnSelf(); diff --git a/app/code/Magento/Wishlist/Test/Unit/Controller/Index/SendTest.php b/app/code/Magento/Wishlist/Test/Unit/Controller/Index/SendTest.php index a8c0fbb951cce..17895dab0d86d 100644 --- a/app/code/Magento/Wishlist/Test/Unit/Controller/Index/SendTest.php +++ b/app/code/Magento/Wishlist/Test/Unit/Controller/Index/SendTest.php @@ -663,6 +663,10 @@ public function testExecute() 'store' => $this->store, ]) ->willReturnSelf(); + $this->transportBuilder->expects($this->any()) + ->method('setScopeId') + ->with($storeId) + ->willReturnSelf(); $this->transportBuilder->expects($this->once()) ->method('setFrom') ->with($from)