Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

11740: Sending emails from Admin in Multi-Store Environment defaults to Primary Store #11992

Merged
merged 5 commits into from
Nov 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason to introduce another class and method instead of just adding an optional parameter scopeId to \Magento\Framework\Mail\Template\TransportBuilder::setFrom?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ishakhsuvarov, is it allowed? According to the backward compatibility(http://devdocs.magento.com/guides/v2.2/contributor-guide/backward-compatible-development/index.html) seems it is prohibited.
May I add optional parameter scopeId?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. I overlooked that for this case

{
$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