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 2 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
5 changes: 4 additions & 1 deletion app/code/Magento/Sales/Model/Order/Email/SenderBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,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->transportBuilder->setFromByStore(
$this->identityContainer->getEmailIdentity(),
$this->identityContainer->getStore()->getId()
);
}
}
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\Sales\Test\Unit\Model\Order\Email;

use Magento\Sales\Model\Order\Email\SenderBuilder;
Expand All @@ -29,6 +30,11 @@ class SenderBuilderTest extends \PHPUnit\Framework\TestCase
*/
protected $transportBuilder;

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

protected function setUp()
{
$templateId = 'test_template_id';
Expand All @@ -42,7 +48,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 +62,23 @@ 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',
'setFromByStore',
]
);

$this->templateContainerMock->expects($this->once())
->method('getTemplateId')
Expand All @@ -85,7 +103,7 @@ protected function setUp()
->method('getEmailIdentity')
->will($this->returnValue($emailIdentity));
$this->transportBuilder->expects($this->once())
->method('setFrom')
->method('setFromByStore')
->with($this->equalTo($emailIdentity));

$this->identityContainerMock->expects($this->once())
Expand Down Expand Up @@ -119,6 +137,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 +169,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
14 changes: 14 additions & 0 deletions lib/internal/Magento/Framework/Mail/Template/TransportBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,20 @@ public function setFrom($from)
return $this;
}

/**
* 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.

It is not encouraged to add new public methods in classes marked as api

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, fixed. I created separate class. Notify me if you accept these changes, i'll squash commits.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is a hack. Surely the correct decision should have been to mark the old setFrom function as deprecated and introduce a new working function, setFromByStore in the API.

The resulting fix that was implemented was not good, involving making objects static that shouldnt have been static, to allow other classes access its protected members. This is far worse in my opinion and has already lead to two further issues being raised caused by this commit. Can this not be looked at again and fixed properly instead of the hack that is currently in place to avoid changing the transportBuilder class.

{
$result = $this->_senderResolver->resolve($from, $store);
$this->message->setFrom($result['email'], $result['name']);
return $this;
}

/**
* Set template identifier
*
Expand Down
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 Expand Up @@ -167,6 +168,25 @@ public function testSetFrom()
$this->builder->setFrom($sender);
}

/**
* @return void
*/
public function setFromByStore()
{
$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->builder->setFromByStore($sender);
}

/**
* @return void
*/
Expand Down