Skip to content

Commit d77e58a

Browse files
author
olysenko
committed
MAGETWO-88217: Guest users unable to sign up to newsletters between stores
1 parent bfa0c17 commit d77e58a

File tree

3 files changed

+64
-20
lines changed

3 files changed

+64
-20
lines changed

app/code/Magento/Newsletter/Controller/Subscriber/NewAction.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,8 @@ public function execute()
125125
$this->validateEmailAvailable($email);
126126

127127
$subscriber = $this->_subscriberFactory->create()->loadByEmail($email);
128-
$storeId = $this->_storeManager->getStore()->getId();
129128
if ($subscriber->getId()
130129
&& $subscriber->getSubscriberStatus() == \Magento\Newsletter\Model\Subscriber::STATUS_SUBSCRIBED
131-
&& $subscriber->getStoreId() === $storeId
132130
) {
133131
throw new \Magento\Framework\Exception\LocalizedException(
134132
__('This email address is already subscribed.')

app/code/Magento/Newsletter/Model/Subscriber.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,17 @@ public function isSubscribed()
364364
*/
365365
public function loadByEmail($subscriberEmail)
366366
{
367-
$this->addData($this->getResource()->loadByEmail($subscriberEmail));
367+
$storeId = $this->_storeManager->getStore()->getId();
368+
$customerData = ['store_id' => $storeId, 'email'=> $subscriberEmail];
369+
370+
/** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
371+
$customer = $this->customerFactory->create();
372+
$this->dataObjectHelper->populateWithArray(
373+
$customer,
374+
$customerData,
375+
\Magento\Customer\Api\Data\CustomerInterface::class
376+
);
377+
$this->addData($this->getResource()->loadByCustomerData($customer));
368378
return $this;
369379
}
370380

@@ -423,17 +433,7 @@ public function randomSequence($length = 32)
423433
*/
424434
public function subscribe($email)
425435
{
426-
$storeId = $this->_storeManager->getStore()->getId();
427-
$customerData = ['store_id' => $storeId, 'email'=> $email];
428-
429-
/** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
430-
$customer = $this->customerFactory->create();
431-
$this->dataObjectHelper->populateWithArray(
432-
$customer,
433-
$customerData,
434-
\Magento\Customer\Api\Data\CustomerInterface::class
435-
);
436-
$this->getResource()->loadByCustomerData($customer);
436+
$this->loadByEmail($email);
437437

438438
if ($this->getId() && $this->getStatus() == self::STATUS_SUBSCRIBED) {
439439
return $this->getStatus();

app/code/Magento/Newsletter/Test/Unit/Model/SubscriberTest.php

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ class SubscriberTest extends \PHPUnit\Framework\TestCase
6060
*/
6161
protected $objectManager;
6262

63+
/**
64+
* @var \Magento\Framework\Api\DataObjectHelper|\PHPUnit_Framework_MockObject_MockObject
65+
*/
66+
private $dataObjectHelper;
67+
68+
/**
69+
* @var \Magento\Customer\Api\Data\CustomerInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
70+
*/
71+
private $customerFactory;
72+
6373
/**
6474
* @var \Magento\Newsletter\Model\Subscriber
6575
*/
@@ -94,7 +104,13 @@ protected function setUp()
94104
'received'
95105
]);
96106
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
97-
107+
$this->customerFactory = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterfaceFactory::class)
108+
->setMethods(['create'])
109+
->disableOriginalConstructor()
110+
->getMock();
111+
$this->dataObjectHelper = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
112+
->disableOriginalConstructor()
113+
->getMock();
98114
$this->subscriber = $this->objectManager->getObject(
99115
\Magento\Newsletter\Model\Subscriber::class,
100116
[
@@ -106,15 +122,31 @@ protected function setUp()
106122
'customerRepository' => $this->customerRepository,
107123
'customerAccountManagement' => $this->customerAccountManagement,
108124
'inlineTranslation' => $this->inlineTranslation,
109-
'resource' => $this->resource
125+
'resource' => $this->resource,
126+
'customerFactory' => $this->customerFactory,
127+
'dataObjectHelper' => $this->dataObjectHelper
110128
]
111129
);
112130
}
113131

114132
public function testSubscribe()
115133
{
116134
$email = 'subscriber_email@magento.com';
117-
$this->resource->expects($this->any())->method('loadByEmail')->willReturn(
135+
$storeId = 1;
136+
$customerData = ['store_id' => $storeId, 'email' => $email];
137+
$storeModel = $this->getMockBuilder(\Magento\Store\Model\Store::class)
138+
->disableOriginalConstructor()
139+
->getMock();
140+
$this->storeManager->expects($this->any())->method('getStore')->willReturn($storeModel);
141+
$storeModel->expects($this->any())->method('getId')->willReturn($storeId);
142+
$customer = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
143+
$this->customerFactory->expects($this->once())->method('create')->willReturn($customer);
144+
$this->dataObjectHelper->expects($this->once())->method('populateWithArray')->with(
145+
$customer,
146+
$customerData,
147+
\Magento\Customer\Api\Data\CustomerInterface::class
148+
);
149+
$this->resource->expects($this->any())->method('loadByCustomerData')->with($customer)->willReturn(
118150
[
119151
'subscriber_status' => 3,
120152
'subscriber_email' => $email,
@@ -128,7 +160,7 @@ public function testSubscribe()
128160
$this->customerSession->expects($this->any())->method('getCustomerId')->willReturn(1);
129161
$customerDataModel->expects($this->any())->method('getEmail')->willReturn($email);
130162
$this->customerRepository->expects($this->any())->method('getById')->willReturn($customerDataModel);
131-
$customerDataModel->expects($this->any())->method('getStoreId')->willReturn(1);
163+
$customerDataModel->expects($this->any())->method('getStoreId')->willReturn($storeId);
132164
$customerDataModel->expects($this->any())->method('getId')->willReturn(1);
133165
$this->sendEmailCheck();
134166
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
@@ -139,7 +171,21 @@ public function testSubscribe()
139171
public function testSubscribeNotLoggedIn()
140172
{
141173
$email = 'subscriber_email@magento.com';
142-
$this->resource->expects($this->any())->method('loadByEmail')->willReturn(
174+
$storeId = 1;
175+
$customerData = ['store_id' => $storeId, 'email' => $email];
176+
$storeModel = $this->getMockBuilder(\Magento\Store\Model\Store::class)
177+
->disableOriginalConstructor()
178+
->getMock();
179+
$this->storeManager->expects($this->any())->method('getStore')->willReturn($storeModel);
180+
$storeModel->expects($this->any())->method('getId')->willReturn($storeId);
181+
$customer = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
182+
$this->customerFactory->expects($this->once())->method('create')->willReturn($customer);
183+
$this->dataObjectHelper->expects($this->once())->method('populateWithArray')->with(
184+
$customer,
185+
$customerData,
186+
\Magento\Customer\Api\Data\CustomerInterface::class
187+
);
188+
$this->resource->expects($this->any())->method('loadByCustomerData')->with($customer)->willReturn(
143189
[
144190
'subscriber_status' => 3,
145191
'subscriber_email' => $email,
@@ -153,7 +199,7 @@ public function testSubscribeNotLoggedIn()
153199
$this->customerSession->expects($this->any())->method('getCustomerId')->willReturn(1);
154200
$customerDataModel->expects($this->any())->method('getEmail')->willReturn($email);
155201
$this->customerRepository->expects($this->any())->method('getById')->willReturn($customerDataModel);
156-
$customerDataModel->expects($this->any())->method('getStoreId')->willReturn(1);
202+
$customerDataModel->expects($this->any())->method('getStoreId')->willReturn($storeId);
157203
$customerDataModel->expects($this->any())->method('getId')->willReturn(1);
158204
$this->sendEmailCheck();
159205
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();

0 commit comments

Comments
 (0)