Skip to content

Commit

Permalink
ENGCOM-3225: [2.3] Alternative fix for Multi Store Emails issue, Fix …
Browse files Browse the repository at this point in the history
…Async Emails issues, Fix Multiple Email issues #18471
  • Loading branch information
sidolov authored Jan 5, 2019
2 parents 48964bb + 0a3672f commit 47acae6
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 33 deletions.
16 changes: 6 additions & 10 deletions app/code/Magento/Sales/Model/Order/Email/SenderBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
*/
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;

/**
* Sender Builder
*/
class SenderBuilder
{
/**
Expand All @@ -29,11 +31,8 @@ class SenderBuilder
protected $transportBuilder;

/**
* @var TransportBuilderByStore
*/
private $transportBuilderByStore;

/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*
* @param Template $templateContainer
* @param IdentityInterface $identityContainer
* @param TransportBuilder $transportBuilder
Expand All @@ -48,9 +47,6 @@ public function __construct(
$this->templateContainer = $templateContainer;
$this->identityContainer = $identityContainer;
$this->transportBuilder = $transportBuilder;
$this->transportBuilderByStore = $transportBuilderByStore ?: ObjectManager::getInstance()->get(
TransportBuilderByStore::class
);
}

/**
Expand Down Expand Up @@ -110,7 +106,7 @@ protected function configureEmailTemplate()
$this->transportBuilder->setTemplateIdentifier($this->templateContainer->getTemplateId());
$this->transportBuilder->setTemplateOptions($this->templateContainer->getTemplateOptions());
$this->transportBuilder->setTemplateVars($this->templateContainer->getTemplateVars());
$this->transportBuilderByStore->setFromByStore(
$this->transportBuilder->setFromByStore(
$this->identityContainer->getEmailIdentity(),
$this->identityContainer->getStore()->getId()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

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 Down Expand Up @@ -36,11 +35,6 @@ class SenderBuilderTest extends \PHPUnit\Framework\TestCase
*/
private $storeMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $transportBuilderByStore;

protected function setUp()
{
$templateId = 'test_template_id';
Expand Down Expand Up @@ -82,11 +76,10 @@ protected function setUp()
'setTemplateIdentifier',
'setTemplateOptions',
'setTemplateVars',
'setFromByStore',
]
);

$this->transportBuilderByStore = $this->createMock(TransportBuilderByStore::class);

$this->templateContainerMock->expects($this->once())
->method('getTemplateId')
->will($this->returnValue($templateId));
Expand All @@ -109,9 +102,9 @@ protected function setUp()
$this->identityContainerMock->expects($this->once())
->method('getEmailIdentity')
->will($this->returnValue($emailIdentity));
$this->transportBuilderByStore->expects($this->once())
$this->transportBuilder->expects($this->once())
->method('setFromByStore')
->with($this->equalTo($emailIdentity));
->with($this->equalTo($emailIdentity), 1);

$this->identityContainerMock->expects($this->once())
->method('getEmailCopyTo')
Expand All @@ -120,15 +113,16 @@ protected function setUp()
$this->senderBuilder = new SenderBuilder(
$this->templateContainerMock,
$this->identityContainerMock,
$this->transportBuilder,
$this->transportBuilderByStore
$this->transportBuilder
);
}

public function testSend()
{
$customerName = 'test_name';
$customerEmail = 'test_email';
$identity = 'email_identity_test';

$transportMock = $this->createMock(
\Magento\Sales\Test\Unit\Model\Order\Email\Stub\TransportInterfaceMock::class
);
Expand All @@ -151,6 +145,9 @@ public function testSend()
$this->storeMock->expects($this->once())
->method('getId')
->willReturn(1);
$this->transportBuilder->expects($this->once())
->method('setFromByStore')
->with($identity, 1);
$this->transportBuilder->expects($this->once())
->method('addTo')
->with($this->equalTo($customerEmail), $this->equalTo($customerName));
Expand All @@ -164,6 +161,7 @@ public function testSend()

public function testSendCopyTo()
{
$identity = 'email_identity_test';
$transportMock = $this->createMock(
\Magento\Sales\Test\Unit\Model\Order\Email\Stub\TransportInterfaceMock::class
);
Expand All @@ -177,6 +175,9 @@ public function testSendCopyTo()
$this->transportBuilder->expects($this->once())
->method('addTo')
->with($this->equalTo('example@mail.com'));
$this->transportBuilder->expects($this->once())
->method('setFromByStore')
->with($identity, 1);
$this->identityContainerMock->expects($this->once())
->method('getStore')
->willReturn($this->storeMock);
Expand Down
15 changes: 14 additions & 1 deletion lib/internal/Magento/Framework/Mail/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,23 @@ public function getBody()

/**
* {@inheritdoc}
*
* @deprecated This function is missing the from name. The
* setFromAddress() function sets both from address and from name.
* @see setFromAddress()
*/
public function setFrom($fromAddress)
{
$this->zendMessage->setFrom($fromAddress);
$this->setFromAddress($fromAddress, null);
return $this;
}

/**
* {@inheritdoc}
*/
public function setFromAddress($fromAddress, $fromName = null)
{
$this->zendMessage->setFrom($fromAddress, $fromName);
return $this;
}

Expand Down
23 changes: 21 additions & 2 deletions lib/internal/Magento/Framework/Mail/Template/TransportBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Magento\Framework\Phrase;

/**
* TransportBuilder
*
* @api
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
Expand Down Expand Up @@ -175,13 +177,30 @@ public function setReplyTo($email, $name = null)
/**
* Set mail from address
*
* @deprecated This function sets the from address for the first store only.
* new function setFromByStore introduced to allow setting of from address
* based on store.
* @see setFromByStore()
*
* @param string|array $from
* @return $this
*/
public function setFrom($from)
{
$result = $this->_senderResolver->resolve($from);
$this->message->setFrom($result['email'], $result['name']);
return $this->setFromByStore($from, null);
}

/**
* Set mail from address by store
*
* @param string|array $from
* @param string|int $store
* @return $this
*/
public function setFromByStore($from, $store = null)
{
$result = $this->_senderResolver->resolve($from, $store);
$this->message->setFromAddress($result['email'], $result['name']);
return $this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

use Magento\Framework\Mail\MessageInterface;

/**
* Class TransportBuilderByStore
*
* @deprecated The ability to set From address based on store is now available
* in the \Magento\Framework\Mail\Template\TransportBuilder class
* @see \Magento\Framework\Mail\Template\TransportBuilder::setFromByStore
*/
class TransportBuilderByStore
{
/**
Expand Down Expand Up @@ -47,7 +54,7 @@ public function __construct(
public function setFromByStore($from, $store)
{
$result = $this->senderResolver->resolve($from, $store);
$this->message->setFrom($result['email'], $result['name']);
$this->message->setFromAddress($result['email'], $result['name']);

return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public function testSetFromByStore()
->with($sender, $store)
->willReturn($sender);
$this->messageMock->expects($this->once())
->method('setFrom')
->with('from@example.com', 'name')
->method('setFromAddress')
->with($sender['email'], $sender['name'])
->willReturnSelf();

$this->model->setFromByStore($sender, $store);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,20 @@ public function getTransportDataProvider()
/**
* @return void
*/
public function testSetFrom()
public function testSetFromByStore()
{
$sender = ['email' => 'from@example.com', 'name' => 'name'];
$store = 1;
$this->senderResolverMock->expects($this->once())
->method('resolve')
->with($sender)
->with($sender, $store)
->willReturn($sender);
$this->messageMock->expects($this->once())
->method('setFrom')
->with('from@example.com', 'name')
->method('setFromAddress')
->with($sender['email'], $sender['name'])
->willReturnSelf();

$this->builder->setFrom($sender);
$this->builder->setFromByStore($sender, $store);
}

/**
Expand Down

0 comments on commit 47acae6

Please sign in to comment.