Skip to content

Commit

Permalink
⏫ Forwardport of #11992 to 2.3-develop branch
Browse files Browse the repository at this point in the history
Applied pull request patch https://github.com/magento/magento2/pull/11992.patch (created by @RomaKis) based on commit(s):
  1. 9193745
  2. ef03b52
  3. f2bfdd9
  4. b6764c8
  5. b322718

Fixed GitHub Issues in 2.3-develop branch:
  - #11740: Sending emails from Admin in Multi-Store Environment defaults to Primary Store (reported by @lee586)
  • Loading branch information
magento-engcom-team committed Jan 24, 2018
1 parent 8e77e2f commit cc2b11f
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 13 deletions.
19 changes: 17 additions & 2 deletions app/code/Magento/Sales/Model/Order/Email/SenderBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
);
}

/**
Expand Down Expand Up @@ -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()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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';
Expand All @@ -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,
Expand All @@ -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')
Expand All @@ -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())
Expand All @@ -95,7 +120,8 @@ protected function setUp()
$this->senderBuilder = new SenderBuilder(
$this->templateContainerMock,
$this->identityContainerMock,
$this->transportBuilder
$this->transportBuilder,
$this->transportBuilderByStore
);
}

Expand All @@ -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));
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Framework\Mail\Template;

use Magento\Framework\Mail\MessageInterface;

class TransportBuilderByStore
{
/**
* Message.
*
* @var \Magento\Framework\Mail\Message
*/
protected $message;

/**
* Sender resolver.
*
* @var \Magento\Framework\Mail\Template\SenderResolverInterface
*/
private $senderResolver;

/**
* @param MessageInterface $message
* @param SenderResolverInterface $senderResolver
*/
public function __construct(
MessageInterface $message,
SenderResolverInterface $senderResolver
) {
$this->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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Framework\Mail\Test\Unit\Template;

use Magento\Framework\Mail\Template\TransportBuilderByStore;

class TransportBuilderByStoreTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Framework\Mail\Template\TransportBuilderByStore
*/
protected $model;

/**
* @var \Magento\Framework\Mail\Message | \PHPUnit_Framework_MockObject_MockObject
*/
protected $messageMock;

/**
* @var \Magento\Framework\Mail\Template\SenderResolverInterface | \PHPUnit_Framework_MockObject_MockObject
*/
protected $senderResolverMock;

/**
* @return void
*/
protected function setUp()
{
$objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$this->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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit cc2b11f

Please sign in to comment.